Skip to content

Commit

Permalink
Merge branch 'main' into mattapan-trolley
Browse files Browse the repository at this point in the history
  • Loading branch information
devinmatte authored Jun 21, 2024
2 parents f4794d8 + 01d398b commit 30ff9c8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 12 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
21 changes: 16 additions & 5 deletions server/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MIT"
[tool.poetry.dependencies]
python = "~3.12"
json-api-doc = "0.15.0"
requests = "2.31.0"
requests = "2.32.0"
pytz = "2024.1"
boto3 = "1.34.84"
botocore = "1.34.84"
Expand Down

0 comments on commit 30ff9c8

Please sign in to comment.