Skip to content

Commit

Permalink
Fix: Align scheduled/delivered trip counts (#993)
Browse files Browse the repository at this point in the history
  • Loading branch information
idreyn authored Jun 19, 2024
1 parent 32d79d0 commit 01d398b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
10 changes: 10 additions & 0 deletions common/utils/array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const indexByProperty = <T extends { [key: string]: any }>(
array: T[],
property: keyof T
) => {
const res: Record<string, T> = {};
array.forEach((el) => {
res[el[property]] = el;
});
return res;
};
28 changes: 22 additions & 6 deletions modules/service/ServiceGraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { DeliveredTripMetrics, ScheduledService } from '../../common/types/
import type { ParamsType } from '../speed/constants/speeds';
import { ChartBorder } from '../../common/components/charts/ChartBorder';
import { DownloadButton } from '../../common/components/buttons/DownloadButton';
import { indexByProperty } from '../../common/utils/array';
import { ScheduledAndDeliveredGraph } from './ScheduledAndDeliveredGraph';
import { getShuttlingBlockAnnotations } from './utils/graphUtils';

Expand Down Expand Up @@ -36,11 +37,25 @@ export const ServiceGraph: React.FC<ServiceGraphProps> = (props: ServiceGraphPro
}));
}, [data]);

const allDates = [
...new Set([
...predictedData.counts.map((point) => point.date),
...data.map((point) => point.date),
]),
].sort((a, b) => (a > b ? 1 : -1));

const scheduledDataByDate = indexByProperty(predictedData.counts, 'date');
const deliveredDataByDate = indexByProperty(data, 'date');

const scheduled = useMemo(() => {
return {
label: 'Scheduled round trips',
data: predictedData.counts.map(({ date, count }, index) => {
const value = data[index]?.miles_covered > 0 && count ? count / 2 : 0;
data: allDates.map((date) => {
const scheduledToday = scheduledDataByDate[date];
const deliveredToday = deliveredDataByDate[date];
const anyDeliveredToday = deliveredToday?.miles_covered > 0;
const value =
scheduledToday.count && anyDeliveredToday ? Math.round(scheduledToday.count) / 2 : 0;
return { date, value };
}),
style: {
Expand All @@ -51,14 +66,15 @@ export const ServiceGraph: React.FC<ServiceGraphProps> = (props: ServiceGraphPro
},
},
};
}, [data, predictedData, peak]);
}, [allDates, deliveredDataByDate, peak, scheduledDataByDate]);

const delivered = useMemo(() => {
return {
label: 'Daily round trips',
data: data.map((datapoint) => {
const value = datapoint.miles_covered ? Math.round(datapoint.count) : 0;
return { date: datapoint.date, value };
data: allDates.map((date) => {
const deliveredToday = deliveredDataByDate[date];
const value = deliveredToday?.miles_covered ? Math.round(deliveredToday.count) : 0;
return { date, value };
}),
style: {
tooltipLabel: (point) => {
Expand Down

0 comments on commit 01d398b

Please sign in to comment.