-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathuseSimulationResults.ts
More file actions
188 lines (165 loc) · 5.7 KB
/
useSimulationResults.ts
File metadata and controls
188 lines (165 loc) · 5.7 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { useMemo } from 'react';
import { skipToken } from '@reduxjs/toolkit/query';
import { useTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';
import { osrdEditoastApi } from 'common/api/osrdEditoastApi';
import formatPowerRestrictionRangesWithHandled from 'modules/powerRestriction/helpers/formatPowerRestrictionRangesWithHandled';
import {
extractOccurrenceDetailsFromPacedTrain,
findExceptionWithOccurrenceId,
computeIndexedOccurrenceStartTime,
getOccurrenceTrainName,
} from 'modules/timetableItem/helpers/pacedTrain';
import useSelectedTimetableItem from 'modules/timetableItem/hooks/useSelectedTimetableItem';
import type { Train } from 'reducers/osrdconf/types';
import { getSelectedTrainId } from 'reducers/simulationResults/selectors';
import { Duration } from 'utils/duration';
import {
extractOccurrenceIndexFromOccurrenceId,
formatEditoastIdToPacedTrainId,
isOccurrenceId,
} from 'utils/trainId';
import type { SimulationResults } from '../types';
import { preparePathPropertiesData } from '../utils';
import { useScenarioContext } from './useScenarioContext';
/**
* Prepare data to be used in simulation results
*/
const useSimulationResults = (): {
results: SimulationResults | undefined;
isSimulationDataLoading: boolean;
} => {
const { t } = useTranslation('operational-studies');
const { infraId, electricalProfileSetId } = useScenarioContext();
const selectedTrainId = useSelector(getSelectedTrainId);
const timetableItem = useSelectedTimetableItem();
const train: Train | undefined = useMemo(() => {
if (!selectedTrainId || !timetableItem) return undefined;
if (!isOccurrenceId(selectedTrainId) || !timetableItem.paced) {
return { ...timetableItem, id: formatEditoastIdToPacedTrainId(timetableItem.id) };
}
const exception = findExceptionWithOccurrenceId(
timetableItem.paced.exceptions,
selectedTrainId
);
let startTime: string;
if (exception?.start_time) {
startTime = exception.start_time.value;
} else {
const selectedOccurrenceIndex = extractOccurrenceIndexFromOccurrenceId(selectedTrainId);
startTime = computeIndexedOccurrenceStartTime(
new Date(timetableItem.start_time),
Duration.parse(timetableItem.paced.interval),
selectedOccurrenceIndex
).toISOString();
}
return {
...timetableItem,
...(exception ? extractOccurrenceDetailsFromPacedTrain(timetableItem, exception) : {}),
// overwrite start_time from extractOccurrenceDetailsFromPacedTrain
start_time: startTime,
// overwrite train_name to reflect the occurrence name
train_name: getOccurrenceTrainName(
{ ...timetableItem, paced: timetableItem.paced },
selectedTrainId
),
id: selectedTrainId,
};
}, [selectedTrainId, timetableItem]);
const exception = useMemo(() => {
if (!selectedTrainId || !isOccurrenceId(selectedTrainId) || !timetableItem?.paced)
return undefined;
return findExceptionWithOccurrenceId(timetableItem.paced.exceptions, selectedTrainId);
}, [selectedTrainId, timetableItem]);
const { currentData: pathfinding, isFetching: isPathfindingFetching } =
osrdEditoastApi.endpoints.getTrainPath.useQuery(
selectedTrainId
? {
id: selectedTrainId,
infraId,
exceptionKey: exception?.key,
}
: skipToken
);
const { currentData: simulation, isFetching: isSimulationFetching } =
osrdEditoastApi.endpoints.getTrainSimulation.useQuery(
selectedTrainId
? {
id: selectedTrainId,
infraId,
electricalProfileSetId,
exceptionKey: exception?.key,
}
: skipToken
);
// TODO: replace this API call by extracting the rolling stock from the rolling
// stocks list
const { currentData: rollingStock } =
osrdEditoastApi.endpoints.getRollingStockNameByRollingStockName.useQuery(
train?.rolling_stock_name
? {
rollingStockName: train.rolling_stock_name,
}
: skipToken
);
const { currentData: rawPathProperties, isFetching: isPathPropertiesFetching } =
osrdEditoastApi.endpoints.postInfraByInfraIdPathProperties.useQuery(
pathfinding?.status === 'success'
? {
infraId,
pathPropertiesInput: {
track_section_ranges: pathfinding.path.track_section_ranges,
},
}
: skipToken
);
const isSimulationDataLoading =
isPathfindingFetching || isSimulationFetching || isPathPropertiesFetching;
if (!train || exception?.disabled) {
return { results: undefined, isSimulationDataLoading };
}
if (pathfinding?.status !== 'success' || !rawPathProperties || !rollingStock) {
return {
results: { isValid: false, train, rollingStock },
isSimulationDataLoading,
};
}
const pathProperties = preparePathPropertiesData(
simulation?.status === 'success' ? simulation.electrical_profiles : undefined,
rawPathProperties,
pathfinding,
train.path,
t
);
if (simulation?.status !== 'success') {
return {
results: {
isValid: false,
train,
rollingStock,
pathProperties,
},
isSimulationDataLoading,
};
}
const powerRestrictions =
formatPowerRestrictionRangesWithHandled({
selectedTimetableItem: train,
selectedTrainRollingStock: rollingStock,
pathfindingResult: pathfinding,
pathProperties,
}) ?? [];
return {
results: {
isValid: true,
train,
rollingStock,
simulation,
path: pathfinding,
pathProperties,
powerRestrictions,
},
isSimulationDataLoading,
};
};
export default useSimulationResults;