-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathuseOutputTableData.ts
163 lines (137 loc) · 5.52 KB
/
useOutputTableData.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { useEffect, useMemo, useState } from 'react';
import { keyBy } from 'lodash';
import { useTranslation } from 'react-i18next';
import { useScenarioContext } from 'applications/operationalStudies/hooks/useScenarioContext';
import type {
PathPropertiesFormatted,
SimulationResponseSuccess,
} from 'applications/operationalStudies/types';
import type { PathfindingResultSuccess, TrainScheduleResult } from 'common/api/osrdEditoastApi';
import { interpolateValue } from 'modules/simulationResult/SimulationResultExport/utils';
import type { TrainScheduleWithDetails } from 'modules/trainschedule/components/Timetable/types';
import { dateToHHMMSS } from 'utils/date';
import { calculateTimeDifferenceInSeconds } from 'utils/timeManipulation';
import { ARRIVAL_TIME_ACCEPTABLE_ERROR_MS } from '../consts';
import { computeInputDatetimes } from '../helpers/arrivalTime';
import computeMargins, { getTheoreticalMargins } from '../helpers/computeMargins';
import { formatSchedule } from '../helpers/scheduleData';
import { type ScheduleEntry, type TimesStopsRow } from '../types';
const useOutputTableData = (
{ final_output: simulatedTrain }: SimulationResponseSuccess,
trainSummary?: TrainScheduleWithDetails,
operationalPoints?: PathPropertiesFormatted['operationalPoints'],
selectedTrainSchedule?: TrainScheduleResult,
path?: PathfindingResultSuccess
): TimesStopsRow[] => {
const { t } = useTranslation('timesStops');
const { getTrackSectionsByIds } = useScenarioContext();
const [rows, setRows] = useState<TimesStopsRow[]>([]);
const scheduleByAt: Record<string, ScheduleEntry> = keyBy(selectedTrainSchedule?.schedule, 'at');
const theoreticalMargins = selectedTrainSchedule && getTheoreticalMargins(selectedTrainSchedule);
const startDatetime = selectedTrainSchedule
? new Date(selectedTrainSchedule.start_time)
: undefined;
const pathStepRows = useMemo(() => {
const pathItemTimes = trainSummary?.pathItemTimes;
if (!path || !selectedTrainSchedule || !pathItemTimes || !startDatetime) return [];
let lastReferenceDate = startDatetime;
return selectedTrainSchedule.path.map((pathStep, index) => {
const schedule: ScheduleEntry | undefined = scheduleByAt[pathStep.id];
const computedArrival = new Date(startDatetime.getTime() + pathItemTimes.final[index]);
const { stopFor, shortSlipDistance, onStopSignal, calculatedDeparture } = formatSchedule(
computedArrival,
schedule
);
const {
theoreticalMargin,
isTheoreticalMarginBoundary,
theoreticalMarginSeconds,
calculatedMargin,
diffMargins,
} = computeMargins(
theoreticalMargins,
selectedTrainSchedule,
scheduleByAt,
index,
pathItemTimes
);
const { theoreticalArrival, arrival, departure, refDate } = computeInputDatetimes(
startDatetime,
lastReferenceDate,
schedule,
{
isDeparture: index === 0,
}
);
lastReferenceDate = refDate;
const isOnTime = theoreticalArrival
? calculateTimeDifferenceInSeconds(theoreticalArrival, computedArrival) <=
ARRIVAL_TIME_ACCEPTABLE_ERROR_MS / 1000
: false;
return {
pathStepId: pathStep.id,
name: t('waypoint', { id: pathStep.id }),
ch: undefined,
isWaypoint: true,
arrival,
departure,
stopFor,
onStopSignal,
shortSlipDistance,
theoreticalMargin,
isTheoreticalMarginBoundary,
theoreticalMarginSeconds,
calculatedMargin,
diffMargins,
calculatedArrival: dateToHHMMSS(isOnTime ? theoreticalArrival! : computedArrival),
calculatedDeparture,
positionOnPath: path.path_item_positions[index],
};
});
}, [selectedTrainSchedule, path, trainSummary?.pathItemTimes]);
useEffect(() => {
const formatRows = async () => {
if (!operationalPoints || !startDatetime) {
setRows([]);
return;
}
const trackIds = operationalPoints.map((op) => op.part.track);
const trackSections = await getTrackSectionsByIds(trackIds);
const formattedRows = operationalPoints.map((op) => {
const matchingPathStep = pathStepRows.find(
(pathStepRow) => op.position === pathStepRow.positionOnPath
);
if (matchingPathStep) {
return {
...matchingPathStep,
opId: op.id,
name: op.extensions?.identifier?.name,
ch: op.extensions?.sncf?.ch,
trackName: trackSections[op.part.track]?.extensions?.sncf?.track_name,
};
}
// compute arrival time
const matchingReportTrainIndex = simulatedTrain.positions.findIndex(
(position) => position === op.position
);
const time =
matchingReportTrainIndex === -1
? interpolateValue(simulatedTrain, op.position, 'times')
: simulatedTrain.times[matchingReportTrainIndex];
const calculatedArrival = new Date(startDatetime.getTime() + time);
return {
isWaypoint: false,
opId: op.id,
name: op.extensions?.identifier?.name,
ch: op.extensions?.sncf?.ch,
calculatedArrival: dateToHHMMSS(calculatedArrival),
trackName: trackSections[op.part.track]?.extensions?.sncf?.track_name,
};
});
setRows(formattedRows);
};
formatRows();
}, [operationalPoints, pathStepRows, simulatedTrain, getTrackSectionsByIds]);
return rows;
};
export default useOutputTableData;