Skip to content

Commit

Permalink
create strict typing for csv util
Browse files Browse the repository at this point in the history
  • Loading branch information
cdwhitt committed Jan 15, 2024
1 parent 4b4defe commit f9fae7e
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 47 deletions.
13 changes: 10 additions & 3 deletions common/components/buttons/DownloadButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ interface DownloadButtonProps {
datasetName: string;
data: Record<string, any>[];
startDate: string;
bothStops: boolean;
includeBothStopsForLocation?: boolean;
location?: Location;
endDate?: string;
}

export const DownloadButton: React.FC<DownloadButtonProps> = ({
datasetName,
data,
bothStops,
includeBothStopsForLocation,
startDate,
location,
endDate,
Expand All @@ -32,7 +32,14 @@ export const DownloadButton: React.FC<DownloadButtonProps> = ({
className={'csv-link'}
data={data}
title={'Download data as CSV'}
filename={getCsvFilename(datasetName, bothStops, startDate, line, location, endDate)}
filename={getCsvFilename({
datasetName,
includeBothStopsForLocation,
startDate,
line,
location,
endDate,
})}
>
<FontAwesomeIcon
icon={faFileArrowDown}
Expand Down
4 changes: 2 additions & 2 deletions common/components/charts/AggregateLineChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const AggregateLineChart: React.FC<AggregateLineProps> = ({
data,
location,
pointField,
bothStops = false,
includeBothStopsForLocation = false,
fname,
timeUnit,
timeFormat,
Expand Down Expand Up @@ -175,7 +175,7 @@ export const AggregateLineChart: React.FC<AggregateLineProps> = ({
data={data}
datasetName={fname}
location={location}
bothStops={bothStops}
includeBothStopsForLocation={includeBothStopsForLocation}
startDate={startDate}
/>
)}
Expand Down
4 changes: 2 additions & 2 deletions common/components/charts/SingleDayLineChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const SingleDayLineChart: React.FC<SingleDayLineProps> = ({
pointField,
benchmarkField,
fname,
bothStops = false,
includeBothStopsForLocation = false,
location,
showLegend = true,
}) => {
Expand Down Expand Up @@ -222,7 +222,7 @@ export const SingleDayLineChart: React.FC<SingleDayLineProps> = ({
data={data}
datasetName={fname}
location={location}
bothStops={bothStops}
includeBothStopsForLocation={includeBothStopsForLocation}
startDate={date}
/>
)}
Expand Down
4 changes: 2 additions & 2 deletions common/types/charts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export interface LineProps {
chartId: string;
location: Location;
pointField: PointField; // X value
bothStops?: boolean;
includeBothStopsForLocation?: boolean;
fname: DataName;
showLegend?: boolean;
}
Expand Down Expand Up @@ -105,7 +105,7 @@ export interface HeadwayHistogramProps {
date: string | undefined;
location: Location;
isLoading: boolean;
bothStops?: boolean;
includeBothStopsForLocation?: boolean;
fname: DataName;
showLegend?: boolean;
metricField: MetricField;
Expand Down
42 changes: 35 additions & 7 deletions common/utils/csv.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { flatten } from 'lodash';
import type { Location } from '../types/charts';
import { DeliveredTripMetrics, TimePredictionWeek } from '../types/dataPoints';

const directionAbbrs = {
northbound: 'NB',
Expand All @@ -9,14 +11,19 @@ const directionAbbrs = {
outbound: 'OB',
};

type GetCsvFilenameOptions = {
datasetName: string;
startDate: string;
endDate?: string;
line?: string;
location?: Location;
includeBothStopsForLocation?: boolean | undefined;
}

export function getCsvFilename(
datasetName: string,
bothStops: boolean,
startDate: string,
line?: string,
location?: Location,
endDate?: string
options: GetCsvFilenameOptions
) {
const {datasetName, startDate, endDate, line, location, includeBothStopsForLocation} = options
// CharlesMGH-SB_dwells_20210315.csv
// CentralSquareCambridge-MelneaCassWashington_traveltimesByHour-weekday_20200101-20201231.csv
// BostonUniversityWest-EB_headways_20161226-20170328.csv
Expand All @@ -25,11 +32,32 @@ export function getCsvFilename(
const dir = location && directionAbbrs[location.direction];

//Location does not exist on all widgets - in that case, 'where' will just be the name of the line
const where = location ? `${fromStop}-${bothStops ? toStop : dir}` : line;
const where = location ? `${fromStop}-${includeBothStopsForLocation ? toStop : dir}` : line;
const what = datasetName;
const date1 = startDate.replaceAll('-', '');
const date2 = endDate ? `-${endDate.replaceAll('-', '')}` : '';
const when = `${date1}${date2}`;

return `${where}_${what}_${when}.csv`;
}

export const addAccuracyPercentageToData = (data: TimePredictionWeek[]) => {
const predictionsList = flatten(data.map(({ prediction }) => prediction));

const newData = predictionsList.map((item) => {
const accuracyPercentage = (item?.num_accurate_predictions / item?.num_predictions) * 100;
return { ...item, accuracy_percentage: accuracyPercentage.toFixed(1) };
});

return newData;
};

export const addMPHToSpeedData = (data: DeliveredTripMetrics[]) => {
const newData = data.map((item) => {
const hours = item.total_time / 3600;
const mph = item.miles_covered / hours;
return { ...item, miles_per_hour: mph.toFixed(1) };
});

return newData;
};
2 changes: 1 addition & 1 deletion modules/dwells/charts/DwellsAggregateChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const DwellsAggregateChart: React.FC<DwellsAggregateChartProps> = ({
endDate={endDate}
fillColor={CHART_COLORS.FILL}
location={getLocationDetails(fromStation, toStation)}
bothStops={false}
includeBothStopsForLocation={false}
fname="dwells"
/>
);
Expand Down
2 changes: 1 addition & 1 deletion modules/headways/charts/HeadwaysAggregateChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const HeadwaysAggregateChart: React.FC<HeadwaysAggregateChartProps> = ({
endDate={endDate}
fillColor={CHART_COLORS.FILL}
location={getLocationDetails(fromStation, toStation)}
bothStops={false}
includeBothStopsForLocation={false}
fname="headways"
/>
);
Expand Down
4 changes: 2 additions & 2 deletions modules/predictions/charts/PredictionsGraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { PEAK_SPEED } from '../../../common/constants/baselines';
import { getRemainingBlockAnnotation } from '../../service/utils/graphUtils';
import { DATE_FORMAT, TODAY } from '../../../common/constants/dates';
import { DownloadButton } from '../../../common/components/buttons/DownloadButton';
import { addAccuracyPercentageToData } from '../utils/utils';
import { addAccuracyPercentageToData } from '../../../common/utils/csv';

interface PredictionsGraphProps {
data: TimePredictionWeek[];
Expand Down Expand Up @@ -216,7 +216,7 @@ export const PredictionsGraph: React.FC<PredictionsGraphProps> = ({
<DownloadButton
data={dataWithPercentage}
datasetName="ridership predictions"
bothStops={false}
includeBothStopsForLocation={false}
startDate={startDate}
endDate={endDate}
/>
Expand Down
11 changes: 0 additions & 11 deletions modules/predictions/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,3 @@ const calcValues = (predictions: TimePredictionWeek[]) => {
export const getDetailsPredictiondWidgetValues = (datapoints: TimePredictionWeek[]) => {
return calcValues(datapoints);
};

export const addAccuracyPercentageToData = (data: TimePredictionWeek[]) => {
const predictionsList = flatten(data.map(({ prediction }) => prediction));

const newData = predictionsList.map((item) => {
const accuracyPercentage = (item?.num_accurate_predictions / item?.num_predictions) * 100;
return { ...item, accuracy_percentage: accuracyPercentage.toFixed(1) };
});

return newData;
};
2 changes: 1 addition & 1 deletion modules/ridership/RidershipGraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export const RidershipGraph: React.FC<RidershipGraphProps> = ({
<DownloadButton
data={data}
datasetName="ridership"
bothStops={false}
includeBothStopsForLocation={false}
startDate={startDate}
endDate={endDate}
/>
Expand Down
2 changes: 1 addition & 1 deletion modules/service/ServiceGraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const ServiceGraph: React.FC<ServiceGraphProps> = (props: ServiceGraphPro
<DownloadButton
data={data as unknown as (DataPoint | AggregateDataPoint)[]}
datasetName="service"
bothStops={false}
includeBothStopsForLocation={false}
startDate={startDate}
endDate={endDate}
/>
Expand Down
4 changes: 2 additions & 2 deletions modules/speed/SpeedGraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { PEAK_SPEED } from '../../common/constants/baselines';
import { getShuttlingBlockAnnotations } from '../service/utils/graphUtils';
import { DownloadButton } from '../../common/components/buttons/DownloadButton';
import type { ParamsType } from './constants/speeds';
import { addMPHToSpeedData } from './utils/utils';
import { addMPHToSpeedData } from '../../common/utils/csv';

interface SpeedGraphProps {
data: DeliveredTripMetrics[];
Expand Down Expand Up @@ -200,7 +200,7 @@ export const SpeedGraph: React.FC<SpeedGraphProps> = ({
<DownloadButton
data={dataWithMPH}
datasetName="speed"
bothStops={false}
includeBothStopsForLocation={false}
startDate={startDate}
endDate={endDate}
/>
Expand Down
10 changes: 0 additions & 10 deletions modules/speed/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,3 @@ const calcValues = (speeds: DeliveredTripMetrics[], isOverview = false) => {
export const getDetailsSpeedWidgetValues = (datapoints: DeliveredTripMetrics[]) => {
return calcValues(datapoints);
};

export const addMPHToSpeedData = (data: DeliveredTripMetrics[]) => {
const newData = data.map((item) => {
const hours = item.total_time / 3600;
const mph = item.miles_covered / hours;
return { ...item, miles_per_hour: mph.toFixed(1) };
});

return newData;
};
2 changes: 1 addition & 1 deletion modules/traveltimes/charts/TravelTimesAggregateChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const TravelTimesAggregateChart: React.FC<TravelTimesAggregateChartProps>
endDate={endDate}
fillColor={timeUnitByDate ? CHART_COLORS.FILL : CHART_COLORS.FILL_HOURLY}
location={getLocationDetails(fromStation, toStation)}
bothStops={true}
includeBothStopsForLocation={true}
fname="traveltimes"
/>
);
Expand Down
2 changes: 1 addition & 1 deletion modules/traveltimes/charts/TravelTimesSingleChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const TravelTimesSingleChart: React.FC<TravelTimesSingleChartProps> = ({
metricField={MetricFieldKeys.travelTimeSec}
pointField={PointFieldKeys.depDt}
benchmarkField={BenchmarkFieldKeys.benchmarkTravelTimeSec}
bothStops={true}
includeBothStopsForLocation={true}
location={getLocationDetails(fromStation, toStation)}
fname={'traveltimes'}
showLegend={showLegend && anyTravelBenchmarks}
Expand Down

0 comments on commit f9fae7e

Please sign in to comment.