Skip to content

Commit e710fdd

Browse files
committed
front: fix osrdToNge trainrunFrequency
Signed-off-by: Louis Greiner <[email protected]>
1 parent 895a071 commit e710fdd

File tree

1 file changed

+38
-6
lines changed
  • front/src/applications/operationalStudies/components/MacroEditor

1 file changed

+38
-6
lines changed

front/src/applications/operationalStudies/components/MacroEditor/osrdToNge.ts

+38-6
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,34 @@ const castNodeToNge = (
225225
),
226226
});
227227

228+
/**
229+
* Match a frequency label to a NGE TrainrunFrequency, or `null` if not handled.
230+
*/
231+
const trainrunFrequencyFromLabel = (label: string) => {
232+
if (!label.startsWith('frequency::')) return null;
233+
const n = parseInt(label.split('::', 2)[1], 10);
234+
const frequency = DEFAULT_TRAINRUN_FREQUENCIES.find((freq) => freq.frequency === n);
235+
return frequency ?? null;
236+
};
237+
238+
/**
239+
* NGE trainrun frequency is stored as OSRD labels (`"frequency::30"` or `"frequency::120"`).
240+
* Update the current frequency if the new frequency is smaller.
241+
*/
242+
const getFrequencyFromLabels = (labels: string[]): TrainrunFrequency | null => {
243+
let currentFrequency: TrainrunFrequency | null = null;
244+
labels.forEach((label) => {
245+
const newFrequency = trainrunFrequencyFromLabel(label);
246+
if (
247+
newFrequency &&
248+
(!currentFrequency || newFrequency.frequency < currentFrequency.frequency)
249+
) {
250+
currentFrequency = newFrequency;
251+
}
252+
});
253+
return currentFrequency;
254+
};
255+
228256
/**
229257
* Load & index the data of the train schedule for the given scenario
230258
*/
@@ -298,7 +326,7 @@ export const loadAndIndexNge = async (
298326
};
299327

300328
/**
301-
* Translate the train schedule in NGE "trainruns".
329+
* Translate the train schedule in NGE "trainrun".
302330
*/
303331
const getNgeTrainruns = (state: MacroEditorState, labels: LabelDto[]) =>
304332
state.trainSchedules
@@ -307,11 +335,15 @@ const getNgeTrainruns = (state: MacroEditorState, labels: LabelDto[]) =>
307335
id: trainSchedule.id,
308336
name: trainSchedule.train_name,
309337
categoryId: DEFAULT_TRAINRUN_CATEGORY.id,
310-
frequencyId: DEFAULT_TRAINRUN_FREQUENCY.id,
338+
frequencyId:
339+
getFrequencyFromLabels(trainSchedule.labels || [])?.id ?? DEFAULT_TRAINRUN_FREQUENCY.id,
311340
trainrunTimeCategoryId: DEFAULT_TRAINRUN_TIME_CATEGORY.id,
312-
labelIds: (trainSchedule.labels || []).map((l) =>
313-
labels.findIndex((e) => e.label === l && e.labelGroupId === TRAINRUN_LABEL_GROUP.id)
314-
),
341+
labelIds: (trainSchedule.labels || [])
342+
// we keep only not handled frequencies as labels to be not redundant
343+
.filter((l) => trainrunFrequencyFromLabel(l) === null)
344+
.map((l) =>
345+
labels.findIndex((e) => e.label === l && e.labelGroupId === TRAINRUN_LABEL_GROUP.id)
346+
),
315347
}));
316348

317349
/**
@@ -497,7 +529,7 @@ export const getNgeDto = (state: MacroEditorState): NetzgrafikDto => {
497529
metadata: {
498530
netzgrafikColors: [],
499531
trainrunCategories: [DEFAULT_TRAINRUN_CATEGORY],
500-
trainrunFrequencies: [DEFAULT_TRAINRUN_FREQUENCY],
532+
trainrunFrequencies: DEFAULT_TRAINRUN_FREQUENCIES,
501533
trainrunTimeCategories: [DEFAULT_TRAINRUN_TIME_CATEGORY],
502534
},
503535
trainruns: getNgeTrainruns(state, labels),

0 commit comments

Comments
 (0)