-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a delays page for alert analysis (#997)
- Loading branch information
1 parent
109e923
commit 31f41d1
Showing
31 changed files
with
5,462 additions
and
4,872 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { FetchAlertDelaysByLineParams, type FetchAlertDelaysByLineOptions } from '../types/api'; | ||
import type { LineDelays } from '../types/delays'; | ||
import { apiFetch } from './utils/fetch'; | ||
|
||
export const fetchLineDelaysByLine = async ( | ||
options: FetchAlertDelaysByLineOptions | ||
): Promise<LineDelays[]> => { | ||
if (!options[FetchAlertDelaysByLineParams.line]) return []; | ||
|
||
return await apiFetch({ | ||
path: '/api/linedelays', | ||
options, | ||
errorMessage: 'Failed to fetch delay metrics', | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { ONE_HOUR } from '../../constants/time'; | ||
import { fetchLineDelaysByLine } from '../delays'; | ||
import type { FetchAlertDelaysByLineOptions } from '../../types/api'; | ||
|
||
export const useAlertDelays = (options: FetchAlertDelaysByLineOptions, enabled?: boolean) => { | ||
return useQuery({ | ||
queryKey: ['lineDelays', options], | ||
queryFn: () => fetchLineDelaysByLine(options), | ||
enabled: enabled, | ||
staleTime: ONE_HOUR, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React from 'react'; | ||
import { ButtonGroup } from '../general/ButtonGroup'; | ||
import type { LineRouteId } from '../../types/lines'; | ||
|
||
interface BranchSelectorProps { | ||
routeId: LineRouteId; | ||
setRouteId: (routeId: LineRouteId) => void; | ||
} | ||
|
||
enum GreenLineBranchOptions { | ||
'Green-B' = 'B Branch', | ||
'Green-C' = 'C Branch', | ||
'Green-D' = 'D Branch', | ||
'Green-E' = 'E Branch', | ||
} | ||
|
||
export const BranchSelector: React.FunctionComponent<BranchSelectorProps> = ({ | ||
routeId, | ||
setRouteId, | ||
}) => { | ||
const selectedIndex = Object.keys(GreenLineBranchOptions).findIndex((route) => route === routeId); | ||
|
||
return ( | ||
<div className={'flex w-full justify-center pt-2'}> | ||
<ButtonGroup | ||
selectedIndex={selectedIndex} | ||
pressFunction={setRouteId} | ||
options={Object.entries(GreenLineBranchOptions)} | ||
additionalDivClass="md:w-auto" | ||
additionalButtonClass="md:w-fit" | ||
/> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { Line } from './lines'; | ||
|
||
export interface LineDelays { | ||
date: string; | ||
disabled_vehicle: number; | ||
door_problem: number; | ||
flooding: number; | ||
fire: number; | ||
line: Line; | ||
medical_emergency: number; | ||
other: number; | ||
police_activity: number; | ||
power_problem: number; | ||
signal_problem: number; | ||
mechanical_problem: number; | ||
brake_problem: number; | ||
switch_problem: number; | ||
total_delay_time: number; | ||
track_issue: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
'use client'; | ||
|
||
import React from 'react'; | ||
import dayjs from 'dayjs'; | ||
import utc from 'dayjs/plugin/utc'; | ||
import { useDelimitatedRoute } from '../../common/utils/router'; | ||
import { ChartPlaceHolder } from '../../common/components/graphics/ChartPlaceHolder'; | ||
import { Layout } from '../../common/layouts/layoutTypes'; | ||
import { PageWrapper } from '../../common/layouts/PageWrapper'; | ||
import { ChartPageDiv } from '../../common/components/charts/ChartPageDiv'; | ||
import { useAlertDelays } from '../../common/api/hooks/delays'; | ||
import { Widget, WidgetDiv } from '../../common/components/widgets'; | ||
import { BranchSelector } from '../../common/components/inputs/BranchSelector'; | ||
import { lineToDefaultRouteId } from '../predictions/utils/utils'; | ||
import type { LineRouteId } from '../../common/types/lines'; | ||
import { Accordion } from '../../common/components/accordion/Accordion'; | ||
import { DelayByCategoryGraph } from './charts/DelayByCategoryGraph'; | ||
import { DelayBreakdownGraph } from './charts/DelayBreakdownGraph'; | ||
import { TotalDelayGraph } from './charts/TotalDelayGraph'; | ||
|
||
dayjs.extend(utc); | ||
|
||
export function DelaysDetails() { | ||
const { | ||
line, | ||
query: { startDate, endDate }, | ||
} = useDelimitatedRoute(); | ||
|
||
const [routeId, setRouteId] = React.useState<LineRouteId>(lineToDefaultRouteId(line)); | ||
const greenBranchToggle = React.useMemo(() => { | ||
return line === 'line-green' && <BranchSelector routeId={routeId} setRouteId={setRouteId} />; | ||
}, [line, routeId]); | ||
|
||
React.useEffect(() => { | ||
setRouteId(lineToDefaultRouteId(line)); | ||
}, [line]); | ||
|
||
const enabled = Boolean(startDate && endDate && line); | ||
const alertDelays = useAlertDelays( | ||
{ | ||
start_date: startDate, | ||
end_date: endDate, | ||
line: routeId, | ||
}, | ||
enabled | ||
); | ||
const delaysReady = alertDelays && line && !alertDelays.isError && alertDelays.data; | ||
if (!startDate || !endDate) { | ||
return <p>Select a date range to load graphs.</p>; | ||
} | ||
|
||
return ( | ||
<PageWrapper pageTitle={'Delays'}> | ||
<ChartPageDiv> | ||
<Widget title="Total Time Delayed" ready={[alertDelays]}> | ||
{delaysReady ? ( | ||
<TotalDelayGraph data={alertDelays.data} startDate={startDate} endDate={endDate} /> | ||
) : ( | ||
<div className="relative flex h-full"> | ||
<ChartPlaceHolder query={alertDelays} /> | ||
</div> | ||
)} | ||
{greenBranchToggle} | ||
</Widget> | ||
<Widget title="Delay Time by Reason" ready={[alertDelays]}> | ||
{delaysReady ? ( | ||
<DelayBreakdownGraph data={alertDelays.data} startDate={startDate} endDate={endDate} /> | ||
) : ( | ||
<div className="relative flex h-full"> | ||
<ChartPlaceHolder query={alertDelays} /> | ||
</div> | ||
)} | ||
{greenBranchToggle} | ||
</Widget> | ||
<Widget title="Delay Time by Reason" ready={[alertDelays]}> | ||
{delaysReady ? ( | ||
<DelayByCategoryGraph data={alertDelays.data} /> | ||
) : ( | ||
<div className="relative flex h-full"> | ||
<ChartPlaceHolder query={alertDelays} /> | ||
</div> | ||
)} | ||
{greenBranchToggle} | ||
</Widget> | ||
<WidgetDiv> | ||
<Accordion | ||
contentList={[ | ||
{ | ||
title: 'About this data', | ||
content: ( | ||
<div> | ||
<p> | ||
When there's a delay on the T, the MBTA sends out an alert to riders. These | ||
alerts almost always include a reason for the delay, and an estimate of how | ||
long trains may be delayed. We collect these alerts and group them by general | ||
matching categories, and add up the total delay time to riders in a week. | ||
</p> | ||
<h4 className="mt-2 font-semibold">Example Alerts</h4> | ||
<blockquote className="my-2 border-s-4 border-gray-300 bg-gray-50 p-4 dark:border-gray-500 dark:bg-gray-800"> | ||
<span className="animate-text bg-clip-text text-mbta-blue shadow-none transition-shadow duration-300"> | ||
Blue Line | ||
</span> | ||
: Delays of about{' '} | ||
<span className="animate-text bg-gradient-to-r from-red-500 to-blue-500 bg-clip-text font-semibold text-transparent shadow-none transition-shadow duration-300"> | ||
20 minutes | ||
</span>{' '} | ||
due to a{' '} | ||
<span className="animate-text bg-yellow-400 bg-clip-text font-semibold text-transparent shadow-none transition-shadow duration-300"> | ||
power issue | ||
</span>{' '} | ||
near wood island. Some trains may hold at stations. | ||
</blockquote> | ||
<blockquote className="my-2 border-s-4 border-gray-300 bg-gray-50 p-4 dark:border-gray-500 dark:bg-gray-800"> | ||
<span className="animate-text bg-clip-text text-mbta-orange shadow-none transition-shadow duration-300"> | ||
Orange Line | ||
</span> | ||
: Delays of about{' '} | ||
<span className="animate-text bg-gradient-to-r from-red-500 to-blue-500 bg-clip-text font-semibold text-transparent shadow-none transition-shadow duration-300"> | ||
10 minutes | ||
</span>{' '} | ||
due to a{' '} | ||
<span className="animate-text bg-red-500 bg-clip-text font-semibold text-transparent shadow-none transition-shadow duration-300"> | ||
disabled train | ||
</span>{' '} | ||
at Northeastern. | ||
</blockquote> | ||
<blockquote className="my-2 border-s-4 border-gray-300 bg-gray-50 p-4 dark:border-gray-500 dark:bg-gray-800"> | ||
<span className="animate-text bg-clip-text text-mbta-green shadow-none transition-shadow duration-300"> | ||
Green Line D Branch | ||
</span> | ||
: Delays of about{' '} | ||
<span className="animate-text bg-gradient-to-r from-red-500 to-blue-500 bg-clip-text font-semibold text-transparent shadow-none transition-shadow duration-300"> | ||
10 minutes | ||
</span>{' '} | ||
eastbound due to a maintenance train{' '} | ||
<span className="animate-text bg-yellow-400 bg-clip-text font-semibold text-transparent shadow-none transition-shadow duration-300"> | ||
inspecting the overhead | ||
</span>{' '} | ||
between riverside and kenmore. | ||
</blockquote> | ||
</div> | ||
), | ||
}, | ||
]} | ||
size={'lg'} | ||
/> | ||
</WidgetDiv> | ||
</ChartPageDiv> | ||
</PageWrapper> | ||
); | ||
} | ||
|
||
DelaysDetails.Layout = Layout.Dashboard; |
Oops, something went wrong.