Skip to content

Commit

Permalink
front: convert frequency between nge and osrd
Browse files Browse the repository at this point in the history
  - convert tags osrd to frequency in nge

Signed-off-by: Uriel Sautron <[email protected]>

  - convert frequency to label in osrd

Signed-off-by: Uriel Sautron <[email protected]>
  • Loading branch information
Uriel-Sautron committed Sep 24, 2024
1 parent a7da946 commit f4ddc9d
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { compact } from 'lodash';
import { compact, uniq } from 'lodash';

import {
osrdEditoastApi,
Expand All @@ -11,14 +11,15 @@ import { formatToIsoDate } from 'utils/date';
import { calculateTimeDifferenceInSeconds, formatDurationAsISO8601 } from 'utils/timeManipulation';

import nodeStore from './nodeStore';
import type {
NetzgrafikDto,
NGEEvent,
TrainrunSection,
Node,
TimeLock,
Trainrun,
Label,
import { DEFAULT_TRAINRUN_FREQUENCY } from './osrdToNge';
import {
type NetzgrafikDto,
type NGEEvent,
type TrainrunSection,
type Node,
type TimeLock,
type Trainrun,
type Label,
} from '../NGE/types';

const createdTrainrun = new Map<number, number>();
Expand Down Expand Up @@ -157,15 +158,25 @@ const getTimeLockDate = (
const formatDateDifference = (start: Date, stop: Date) =>
formatDurationAsISO8601(calculateTimeDifferenceInSeconds(start, stop));

const createTrainSchedulePayload = async (
trainrunSections: TrainrunSection[],
nodes: Node[],
trainrun: Trainrun,
infraId: number,
dispatch: AppDispatch,
labels: Label[],
oldStartDate: Date
) => {
const createTrainSchedulePayload = async ({
trainrunSections,
nodes,
trainrun,
infraId,
dispatch,
labels,
oldStartDate,
trainSchedule,
}: {
trainrunSections: TrainrunSection[];
nodes: Node[];
trainrun: Trainrun;
infraId: number;
dispatch: AppDispatch;
labels: Label[];
oldStartDate: Date;
trainSchedule?: TrainScheduleBase;
}) => {
// TODO: check that the trainrunSections format is still compatible
const pathPromise = trainrunSections.map(async (section, index) => {
const sourceNode = getNodeById(nodes, section.sourceNodeId);
Expand All @@ -186,10 +197,19 @@ const createTrainSchedulePayload = async (

const path = await Promise.all(pathPromise);

const trainrunLabels = trainrun.labelIds.map(
let trainrunLabels = trainrun.labelIds.map(
(labelId) => labels.find((label) => label.id === labelId)?.label
);

if (trainrun.trainrunFrequency.id !== DEFAULT_TRAINRUN_FREQUENCY.id) {
trainrunLabels.push(`frequency::${trainrun.trainrunFrequency.frequency}`);
}

const trainScheduleLabels =
trainSchedule?.labels?.filter((label) => label.match(/^frequency::(?!30$|60$|120$)\d+$/)) || [];

trainrunLabels = uniq([...trainrunLabels, ...trainScheduleLabels]);

// The departure time of the first section is guaranteed to be non-null
const startTimeLock = trainrunSections[0].sourceDeparture;
const startDate = new Date(oldStartDate);
Expand Down Expand Up @@ -259,15 +279,15 @@ const handleTrainrunOperation = async ({
body: [
{
...DEFAULT_PAYLOAD,
...(await createTrainSchedulePayload(
trainrunSectionsByTrainrunId,
...(await createTrainSchedulePayload({
trainrunSections: trainrunSectionsByTrainrunId,
nodes,
trainrun,
infraId,
dispatch,
labels,
startDate
)),
oldStartDate: startDate,
})),
},
],
})
Expand Down Expand Up @@ -304,15 +324,16 @@ const handleTrainrunOperation = async ({
id: trainrunIdToUpdate,
trainScheduleForm: {
...trainSchedule,
...(await createTrainSchedulePayload(
trainrunSectionsByTrainrunId,
...(await createTrainSchedulePayload({
trainrunSections: trainrunSectionsByTrainrunId,
nodes,
trainrun,
infraId,
dispatch,
labels,
startDate
)),
oldStartDate: startDate,
trainSchedule,
})),
// Reset margins because they contain references to path items
margins: undefined,
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { compact } from 'lodash';

import { osrdEditoastApi } from 'common/api/osrdEditoastApi';
import type {
SearchResultItemOperationalPoint,
Expand Down Expand Up @@ -53,16 +55,38 @@ const DEFAULT_TRAINRUN_CATEGORY: TrainrunCategory = {
sectionHeadway: 0,
};

const DEFAULT_TRAINRUN_FREQUENCY: TrainrunFrequency = {
id: 3, // In NGE, Trainrun.DEFAULT_TRAINRUN_FREQUENCY
order: 0,
frequency: 60,
offset: 0,
name: 'Default',
/** Short name, needs to be unique */
shortName: 'D',
linePatternRef: '60',
};
const DEFAULT_TRAINRUN_FREQUENCIES: TrainrunFrequency[] = [
{
id: 2,
order: 0,
frequency: 30,
offset: 0,
name: 'Half-hourly',
shortName: '30',
linePatternRef: '30',
},
{
id: 3, // default NGE frequency takes id 3
order: 1,
frequency: 60,
offset: 0,
name: 'Hourly',
/** Short name, needs to be unique */
shortName: '60',
linePatternRef: '60',
},
{
id: 4,
order: 2,
frequency: 120,
offset: 0,
name: 'Two-hourly',
shortName: '120',
linePatternRef: '120',
},
];

export const DEFAULT_TRAINRUN_FREQUENCY: TrainrunFrequency = DEFAULT_TRAINRUN_FREQUENCIES[1];

const DEFAULT_TRAINRUN_TIME_CATEGORY: TrainrunTimeCategory = {
id: 0, // In NGE, Trainrun.DEFAULT_TRAINRUN_TIME_CATEGORY
Expand All @@ -82,7 +106,7 @@ const DEFAULT_DTO: NetzgrafikDto = {
metadata: {
netzgrafikColors: [],
trainrunCategories: [DEFAULT_TRAINRUN_CATEGORY],
trainrunFrequencies: [DEFAULT_TRAINRUN_FREQUENCY],
trainrunFrequencies: [...DEFAULT_TRAINRUN_FREQUENCIES],
trainrunTimeCategories: [DEFAULT_TRAINRUN_TIME_CATEGORY],
},
freeFloatingTexts: [],
Expand Down Expand Up @@ -293,31 +317,46 @@ const importTimetable = async (
// Create one NGE train run per OSRD train schedule
let labelId = 0;
const trainruns: Trainrun[] = trainSchedules.map((trainSchedule) => {
let formatedLabels: Label[] = [];
const formatedLabels: (Label | undefined)[] = [];
let trainrunFrequency: TrainrunFrequency | undefined;
if (trainSchedule.labels) {
formatedLabels = trainSchedule.labels.map((label) => {
trainSchedule.labels.forEach((label) => {
// Frenquency labels management from OSRD (labels for moment manage 'frequency::30' and 'frequency::120')
if (label.includes('frequency')) {
const frequency = parseInt(label.split('::')[1], 10);
if (!trainrunFrequency || trainrunFrequency.frequency > frequency) {
const trainrunFrequencyFind = DEFAULT_TRAINRUN_FREQUENCIES.find(
(freq) => freq.frequency === frequency
);
trainrunFrequency = trainrunFrequencyFind || trainrunFrequency;
}
return;
}
const DTOLabel = DTOLabels.find((DTOlabel) => DTOlabel.label === label);
if (DTOLabel) {
return DTOLabel;
formatedLabels.push(DTOLabel);
} else {
const newDTOLabel: Label = {
id: labelId,
label,
labelGroupId: DEFAULT_LABEL_GROUP.id,
labelRef: 'Trainrun',
};
DTOLabels.push(newDTOLabel);
labelId += 1;
formatedLabels.push(newDTOLabel);
}
const newDTOLabel: Label = {
id: labelId,
label,
labelGroupId: DEFAULT_LABEL_GROUP.id,
labelRef: 'Trainrun',
};
DTOLabels.push(newDTOLabel);
labelId += 1;
return newDTOLabel;
});
}

return {
id: trainSchedule.id,
name: trainSchedule.train_name,
categoryId: DEFAULT_TRAINRUN_CATEGORY.id,
frequencyId: DEFAULT_TRAINRUN_FREQUENCY.id,
frequencyId: trainrunFrequency?.id || DEFAULT_TRAINRUN_FREQUENCY.id,
trainrunTimeCategoryId: DEFAULT_TRAINRUN_TIME_CATEGORY.id,
labelIds: formatedLabels.map((label) => label.id),
labelIds: compact(formatedLabels).map((label) => label.id),
trainrunFrequency: trainrunFrequency || DEFAULT_TRAINRUN_FREQUENCY,
};
});

Expand Down Expand Up @@ -477,7 +516,7 @@ const importTimetable = async (
metadata: {
netzgrafikColors: [],
trainrunCategories: [DEFAULT_TRAINRUN_CATEGORY],
trainrunFrequencies: [DEFAULT_TRAINRUN_FREQUENCY],
trainrunFrequencies: DEFAULT_TRAINRUN_FREQUENCIES,
trainrunTimeCategories: [DEFAULT_TRAINRUN_TIME_CATEGORY],
},
nodes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export type Trainrun = {
frequencyId: number;
trainrunTimeCategoryId: number;
labelIds: (number | string)[];
trainrunFrequency: TrainrunFrequency;
};

export type TimeLock = {
Expand Down

0 comments on commit f4ddc9d

Please sign in to comment.