Skip to content

Commit

Permalink
front: refacto osrdToNge trainrunFrequency
Browse files Browse the repository at this point in the history
Signed-off-by: Louis Greiner <[email protected]>
  • Loading branch information
louisgreiner committed Jan 28, 2025
1 parent 2c1f9f5 commit d08cdd0
Showing 1 changed file with 43 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,34 @@ const castNodeToNge = (
),
});

/**
* Match a frequency label to a NGE TrainrunFrequency, or `null` if not handled.
*/
const trainrunFrequencyFromLabel = (label: string) => {
if (!label.startsWith('frequency::')) return null;
const n = parseInt(label.split('::', 2)[1], 10);
const frequency = DEFAULT_TRAINRUN_FREQUENCIES.find((freq) => freq.frequency === n);
return frequency ?? null;
};

/**
* NGE trainrun frequency is stored as OSRD labels (`"frequency::30"` or `"frequency::120"`).
* Update the current frequency if the new frequency is smaller.
*/
const getFrequencyFromLabels = (labels: string[]): TrainrunFrequency | null => {
let currentFrequency: TrainrunFrequency | null = null;
labels.forEach((label) => {
const newFrequency = trainrunFrequencyFromLabel(label);
if (
newFrequency &&
(!currentFrequency || newFrequency.frequency < currentFrequency.frequency)
) {
currentFrequency = newFrequency;
}
});
return currentFrequency;
};

/**
* Load & index the data of the train schedule for the given scenario
*/
Expand Down Expand Up @@ -298,44 +326,25 @@ export const loadAndIndexNge = async (
};

/**
* Translate the train schedule in NGE "trainruns".
* Translate the train schedule in NGE "trainrun".
*/
const getNgeTrainruns = (state: MacroEditorState, labels: LabelDto[]) =>
state.trainSchedules
.filter((trainSchedule) => trainSchedule.path.length >= 2)
.map((trainSchedule) => {
// NGE trainrun frequency is stored as OSRD labels ('frequency::30' or 'frequency::120')
let trainrunFrequency: TrainrunFrequency | undefined;
if (trainSchedule.labels) {
trainSchedule.labels.forEach((label) => {
if (!label.match(/^frequency::(?!30$|60$|120$)\d+$/)) {
const frequency = parseInt(label.split('::')[1], 10);
const trainrunFrequencyFound = DEFAULT_TRAINRUN_FREQUENCIES.find(
(freq) => freq.frequency === frequency
);
if (
trainrunFrequencyFound &&
(!trainrunFrequency || trainrunFrequencyFound.frequency < trainrunFrequency.frequency)
) {
trainrunFrequency = trainrunFrequencyFound;
}
}
});
}

return {
id: trainSchedule.id,
name: trainSchedule.train_name,
categoryId: DEFAULT_TRAINRUN_CATEGORY.id,
frequencyId: trainrunFrequency?.id || DEFAULT_TRAINRUN_FREQUENCY.id,
trainrunTimeCategoryId: DEFAULT_TRAINRUN_TIME_CATEGORY.id,
labelIds: (trainSchedule.labels || [])
.filter((l) => l.match(/^frequency::(?!30$|60$|120$)\d+$/))
.map((l) =>
labels.findIndex((e) => e.label === l && e.labelGroupId === TRAINRUN_LABEL_GROUP.id)
),
};
});
.map((trainSchedule) => ({
id: trainSchedule.id,
name: trainSchedule.train_name,
categoryId: DEFAULT_TRAINRUN_CATEGORY.id,
frequencyId:
getFrequencyFromLabels(trainSchedule.labels || [])?.id ?? DEFAULT_TRAINRUN_FREQUENCY.id,
trainrunTimeCategoryId: DEFAULT_TRAINRUN_TIME_CATEGORY.id,
labelIds: (trainSchedule.labels || [])
// we keep only not handled frequencies as labels to be not redundant
.filter((l) => trainrunFrequencyFromLabel(l) === null)
.map((l) =>
labels.findIndex((e) => e.label === l && e.labelGroupId === TRAINRUN_LABEL_GROUP.id)
),
}));

/**
* Translate the train schedule in NGE "trainrunSection" & "nodes".
Expand Down

0 comments on commit d08cdd0

Please sign in to comment.