-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathuseOutputTableData.ts
129 lines (118 loc) · 4.92 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
import { useMemo } from 'react';
import { keyBy } from 'lodash';
import type {
PathPropertiesFormatted,
SimulationResponseSuccess,
} from 'applications/operationalStudies/types';
import type { PathfindingResultSuccess, TrainScheduleResult } from 'common/api/osrdEditoastApi';
import { formatSuggestedOperationalPoints } from 'modules/pathfinding/utils';
import type { OperationalPointWithTimeAndSpeed } from 'modules/trainschedule/components/DriverTrainScheduleV2/types';
import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSchedule/types';
import { secToHoursString } from 'utils/timeManipulation';
import { checkAndFormatCalculatedArrival } from '../helpers/arrivalTime';
import computeMargins from '../helpers/computeMargins';
import { computeScheduleData, formatScheduleData } from '../helpers/scheduleData';
import { findNextScheduledOpPoint } from '../helpers/utils';
import type { TrainScheduleBasePathWithUic, ScheduleEntry } from '../types';
function useOutputTableData(
simulatedTrain: SimulationResponseSuccess,
pathProperties: PathPropertiesFormatted,
operationalPoints: OperationalPointWithTimeAndSpeed[],
selectedTrainSchedule: TrainScheduleResult,
path?: PathfindingResultSuccess
) {
const scheduleByAt: Record<string, ScheduleEntry> = keyBy(selectedTrainSchedule.schedule, 'at');
const suggestedOperationalPoints: SuggestedOP[] = useMemo(
() =>
path?.length
? formatSuggestedOperationalPoints(
pathProperties.operationalPoints,
pathProperties.geometry,
path.length
)
: [],
[pathProperties.operationalPoints, pathProperties.geometry]
);
const pathStepsWithPositionOnPath = useMemo(() => {
if (!path || !selectedTrainSchedule) return [];
return selectedTrainSchedule.path.map((pathStep, index) => ({
...pathStep,
positionOnPath: path.path_item_positions[index],
}));
}, [path, selectedTrainSchedule]);
const pathStepsWithOpPointIndices = useMemo(
() =>
selectedTrainSchedule.path
.filter((pathStep): pathStep is TrainScheduleBasePathWithUic => 'uic' in pathStep)
.map((pathStep) => {
const correspondingOpPointIndex = suggestedOperationalPoints.findIndex(
(sugOpPoint) =>
'uic' in pathStep &&
sugOpPoint.uic === pathStep.uic &&
sugOpPoint.ch === pathStep.secondary_code
);
return {
...pathStep,
correspondingOpPointIndex,
};
}),
[selectedTrainSchedule, suggestedOperationalPoints]
);
const pathStepsByUic = keyBy(
pathStepsWithOpPointIndices,
(pathStep) => `${pathStep.uic}-${pathStep.secondary_code}`
);
const outputTableData = useMemo(() => {
const pathStepRows = pathStepsWithPositionOnPath.map((pathStep) => {
const schedule = scheduleByAt[pathStep.id];
const scheduleData = computeScheduleData(schedule, selectedTrainSchedule.start_time);
return { ...pathStep, ...formatScheduleData(scheduleData) };
});
const suggestedOpRows = suggestedOperationalPoints.map((sugOpPoint, sugOpIndex) => {
const opPoint = operationalPoints.find((op) => op.id === sugOpPoint.opId);
if (!opPoint) {
return sugOpPoint;
}
const nextOpPoint = findNextScheduledOpPoint(
operationalPoints,
pathStepsWithOpPointIndices,
sugOpIndex
);
const pathStepKey = `${sugOpPoint.uic}-${sugOpPoint.ch}`;
if (pathStepKey in pathStepsByUic) {
const pathStepId = pathStepsByUic[pathStepKey].id || '';
const schedule = scheduleByAt[pathStepId];
const scheduleData = computeScheduleData(schedule, selectedTrainSchedule.start_time);
const formattedScheduleData = formatScheduleData(scheduleData);
const marginsData = nextOpPoint
? computeMargins(simulatedTrain, opPoint, nextOpPoint, selectedTrainSchedule, pathStepId)
: null;
const calculatedArrival = checkAndFormatCalculatedArrival(scheduleData, opPoint.time);
return {
...sugOpPoint,
...formattedScheduleData,
onStopSignal: schedule?.on_stop_signal || '',
calculatedArrival,
calculatedDeparture:
opPoint.duration > 0 ? secToHoursString(opPoint.time + opPoint.duration, true) : '',
...marginsData,
} as SuggestedOP;
}
return { ...sugOpPoint, calculatedArrival: secToHoursString(opPoint.time, true) };
});
return suggestedOpRows.map((sugOpPoint) => {
const matchingPathStep = pathStepRows.find(
(pathStepRow) => sugOpPoint.positionOnPath === pathStepRow.positionOnPath
);
if (matchingPathStep) {
return {
...sugOpPoint,
...matchingPathStep,
};
}
return sugOpPoint;
});
}, [simulatedTrain, pathProperties, operationalPoints, selectedTrainSchedule, path]);
return outputTableData;
}
export default useOutputTableData;