-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathgetSimulationResults.ts
146 lines (134 loc) · 4.8 KB
/
getSimulationResults.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
import {
updateAllowancesSettings,
updateIsUpdating,
updateSelectedProjection,
updateSelectedTrainId,
updateSimulation,
} from 'reducers/osrdsimulation/actions';
import { setFailure } from 'reducers/main';
import { store } from 'Store';
import i18n from 'i18next';
import {
SimulationReport,
TimetableWithSchedulesDetails,
TrainScheduleSummary,
osrdEditoastApi,
} from 'common/api/osrdEditoastApi';
import { extractMessageFromError } from 'utils/error';
import { AllowancesSettings, Projection } from 'reducers/osrdsimulation/types';
export function selectProjection(
trainSchedules: TrainScheduleSummary[],
currentProjection?: Projection,
selectedTrainId?: number
) {
if (trainSchedules.length === 0) return;
if (selectedTrainId && !currentProjection) {
// if a selected train is given, we use it for the projection
const selectedTrain = trainSchedules.find((train) => train.id === selectedTrainId);
if (selectedTrain) {
store.dispatch(
updateSelectedProjection({
id: selectedTrainId,
path: selectedTrain.path_id,
})
);
return;
}
}
// if there is already a projection
if (currentProjection) {
const trainSchedulesIds = trainSchedules.map((train) => train.id);
// if the projected train still exists, keep it
if (trainSchedulesIds.includes(currentProjection.id)) {
if (!selectedTrainId) store.dispatch(updateSelectedTrainId(trainSchedules[0].id));
return;
}
// if the projected train has been deleted but an other train has the same path,
// keep the path and select this train
const newProjectedTrain = trainSchedules.find(
(train) => train.path_id === currentProjection.path
);
if (newProjectedTrain) {
store.dispatch(
updateSelectedProjection({
id: newProjectedTrain.id,
path: currentProjection.path,
})
);
store.dispatch(updateSelectedTrainId(newProjectedTrain.id));
return;
}
}
// by default, use the first train
const sortedTrainSchedules = [...trainSchedules].sort(
(trainA, trainB) => trainA.departure_time - trainB.departure_time
);
store.dispatch(
updateSelectedProjection({
id: sortedTrainSchedules[0].id,
path: sortedTrainSchedules[0].path_id,
})
);
store.dispatch(updateSelectedTrainId(sortedTrainSchedules[0].id));
}
/**
* Recover the time table for all the trains
*/
export default async function getSimulationResults(
timetable: TimetableWithSchedulesDetails,
selectedProjection: Projection,
allowancesSettings?: AllowancesSettings
) {
store.dispatch(updateIsUpdating(true));
const trainSchedulesIDs = timetable.train_schedule_summaries.map((train) => train.id);
if (trainSchedulesIDs.length > 0) {
// We use this syntax first because of the .initiate, to be able to unsubscribe from the results later
const results = store.dispatch(
osrdEditoastApi.endpoints.getTrainScheduleResults.initiate({
timetableId: timetable.id,
pathId: selectedProjection.path,
})
);
const {
data: simulationLocal,
isError: isGetTrainScheduleResultsError,
error: getTrainScheduleResultsError,
} = await results;
if (simulationLocal) {
const sortedSimulationLocal = [...simulationLocal].sort(
(a: SimulationReport, b: SimulationReport) => a.base.stops[0].time - b.base.stops[0].time
);
store.dispatch(updateSimulation({ trains: sortedSimulationLocal }));
// Create margins settings for each train if not set
const newAllowancesSettings = { ...(allowancesSettings || {}) };
sortedSimulationLocal.forEach((train) => {
if (!newAllowancesSettings[train.id]) {
newAllowancesSettings[train.id] = {
base: true,
baseBlocks: true,
eco: true,
ecoBlocks: false,
};
}
});
store.dispatch(updateAllowancesSettings(newAllowancesSettings));
store.dispatch(updateIsUpdating(false));
} else if (isGetTrainScheduleResultsError && getTrainScheduleResultsError) {
store.dispatch(
setFailure({
name: i18n.t('simulation:errorMessages.unableToRetrieveTrainSchedule'),
message: extractMessageFromError(getTrainScheduleResultsError),
})
);
store.dispatch(updateIsUpdating(false));
console.error(getTrainScheduleResultsError);
}
// Manually dispatching an RTK request with .initiate will create a subscription entry, but we need to unsubscribe from that data also manually - otherwise the data stays in the cache permanently.
results.unsubscribe();
} else {
store.dispatch(updateSimulation({ trains: [] }));
store.dispatch(updateIsUpdating(false));
store.dispatch(updateSelectedTrainId(undefined));
store.dispatch(updateSelectedProjection(undefined));
}
}