From 35cdfea772092987ccb2ea259d1516bd54c20f70 Mon Sep 17 00:00:00 2001 From: Ian Reynolds Date: Mon, 17 Jun 2024 21:59:36 -0400 Subject: [PATCH 1/3] align service graphs --- .../TimeSeriesChart/TimeSeriesChart.tsx | 2 ++ common/utils/array.ts | 10 +++++++ modules/service/ServiceGraph.tsx | 30 +++++++++++++++---- 3 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 common/utils/array.ts diff --git a/common/components/charts/TimeSeriesChart/TimeSeriesChart.tsx b/common/components/charts/TimeSeriesChart/TimeSeriesChart.tsx index 20fb51fe1..b0dabee3b 100644 --- a/common/components/charts/TimeSeriesChart/TimeSeriesChart.tsx +++ b/common/components/charts/TimeSeriesChart/TimeSeriesChart.tsx @@ -150,6 +150,8 @@ export const TimeSeriesChart = (props: Props) => { }, }; + console.log({ unit, time }); + return { x: { min: timeAxis.from, diff --git a/common/utils/array.ts b/common/utils/array.ts new file mode 100644 index 000000000..368c849b7 --- /dev/null +++ b/common/utils/array.ts @@ -0,0 +1,10 @@ +export const indexByProperty = ( + array: T[], + property: keyof T +) => { + const res: Record = {}; + array.forEach((el) => { + res[el[property]] = el; + }); + return res; +}; diff --git a/modules/service/ServiceGraph.tsx b/modules/service/ServiceGraph.tsx index cfe123db6..b012f7911 100644 --- a/modules/service/ServiceGraph.tsx +++ b/modules/service/ServiceGraph.tsx @@ -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'; @@ -36,11 +37,27 @@ export const ServiceGraph: React.FC = (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'); + + console.log(allDates); + 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: { @@ -51,14 +68,15 @@ export const ServiceGraph: React.FC = (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) => { From ad666fd215d68bcfabb965039427ff14f7a6a602 Mon Sep 17 00:00:00 2001 From: Ian Reynolds Date: Mon, 17 Jun 2024 22:07:02 -0400 Subject: [PATCH 2/3] I am the console.log fiend --- common/components/charts/TimeSeriesChart/TimeSeriesChart.tsx | 2 -- modules/service/ServiceGraph.tsx | 2 -- 2 files changed, 4 deletions(-) diff --git a/common/components/charts/TimeSeriesChart/TimeSeriesChart.tsx b/common/components/charts/TimeSeriesChart/TimeSeriesChart.tsx index b0dabee3b..20fb51fe1 100644 --- a/common/components/charts/TimeSeriesChart/TimeSeriesChart.tsx +++ b/common/components/charts/TimeSeriesChart/TimeSeriesChart.tsx @@ -150,8 +150,6 @@ export const TimeSeriesChart = (props: Props) => { }, }; - console.log({ unit, time }); - return { x: { min: timeAxis.from, diff --git a/modules/service/ServiceGraph.tsx b/modules/service/ServiceGraph.tsx index b012f7911..ca8c0f2af 100644 --- a/modules/service/ServiceGraph.tsx +++ b/modules/service/ServiceGraph.tsx @@ -47,8 +47,6 @@ export const ServiceGraph: React.FC = (props: ServiceGraphPro const scheduledDataByDate = indexByProperty(predictedData.counts, 'date'); const deliveredDataByDate = indexByProperty(data, 'date'); - console.log(allDates); - const scheduled = useMemo(() => { return { label: 'Scheduled round trips', From 216d05ac19177b415f0ee180dac4395cad4bf408 Mon Sep 17 00:00:00 2001 From: Ian Reynolds Date: Tue, 18 Jun 2024 06:55:18 -0400 Subject: [PATCH 3/3] lint --- common/utils/array.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/common/utils/array.ts b/common/utils/array.ts index 368c849b7..37e884054 100644 --- a/common/utils/array.ts +++ b/common/utils/array.ts @@ -1,10 +1,10 @@ export const indexByProperty = ( - array: T[], - property: keyof T + array: T[], + property: keyof T ) => { - const res: Record = {}; - array.forEach((el) => { - res[el[property]] = el; - }); - return res; + const res: Record = {}; + array.forEach((el) => { + res[el[property]] = el; + }); + return res; };