Skip to content

Commit

Permalink
fix metrics values summing
Browse files Browse the repository at this point in the history
The goal here is to sum values if they are numbers (as with distance, duration, count) and sum objects' properties if values are objects.
But I forgot to consider the case where val is null or undefined, in which case isNaN(val) would be true.
Better to check if value is a number by !isNaN(val), then check if it's an object.
Otherwise do nothing because it's null or undefined.
  • Loading branch information
JGreenlee committed May 23, 2024
1 parent 0642f06 commit e4ba6d0
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions www/js/metrics/MetricsCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,18 @@ const MetricsCard = ({
uniqueLabels.forEach((label) => {
const sum: any = metricDataDays.reduce((acc, day) => {
const val = valueForFieldOnDay(day, groupingFields[0], label);
// if val is object, add its values to the accumulator's values
if (isNaN(val)) {
// if val is number, add it to the accumulator
if (!isNaN(val)) {
return acc + val;
} else if (val && typeof val == 'object') {
// if val is object, add its values to the accumulator's values
const newAcc = {};
for (let key in val) {
newAcc[key] = (acc[key] || 0) + val[key];
}
return newAcc;
} else {
// if val is number, add it to the accumulator
if (typeof val == 'number') return acc + val;
}
return acc;
}, 0);
vals[label] = unitFormatFn ? unitFormatFn(sum) : sum;
});
Expand Down

0 comments on commit e4ba6d0

Please sign in to comment.