Skip to content

Commit 965f5d0

Browse files
committed
front: convert times from NGE to OSRD
Closes: #8389
1 parent 151709d commit 965f5d0

File tree

1 file changed

+60
-14
lines changed
  • front/src/applications/operationalStudies/components/MacroEditor

1 file changed

+60
-14
lines changed

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

+60-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
import { osrdEditoastApi, type TrainScheduleBase } from 'common/api/osrdEditoastApi';
22
import type { AppDispatch } from 'store';
3+
import { formatToIsoDate } from 'utils/date';
4+
import { calculateTimeDifferenceInSeconds, formatDurationAsISO8601 } from 'utils/timeManipulation';
35

4-
import type { NetzgrafikDto, NGETrainrunEvent, TrainrunSection, Node, Trainrun } from './types';
6+
import type {
7+
NetzgrafikDto,
8+
NGETrainrunEvent,
9+
TrainrunSection,
10+
Node,
11+
TimeLock,
12+
Trainrun,
13+
} from './types';
514

615
const createdTrainrun = new Map<number, number>();
716

@@ -11,14 +20,9 @@ const getTrainrunSectionsByTrainrunId = (trainrunSections: TrainrunSection[], tr
1120
const getNodeById = (nodes: Node[], nodeId: number | string) =>
1221
nodes.find((node) => node.id === nodeId);
1322

14-
// TODO: add a dynamic values when available
15-
const DEFAULT_PAYLOAD: Pick<
16-
TrainScheduleBase,
17-
'constraint_distribution' | 'rolling_stock_name' | 'start_time'
18-
> = {
23+
const DEFAULT_PAYLOAD: Pick<TrainScheduleBase, 'constraint_distribution' | 'rolling_stock_name'> = {
1924
constraint_distribution: 'STANDARD',
2025
rolling_stock_name: '',
21-
start_time: '2024-07-15T08:00:00+02:00',
2226
};
2327

2428
const createPathItemFromNode = (node: Node, index: number) => {
@@ -30,10 +34,19 @@ const createPathItemFromNode = (node: Node, index: number) => {
3034
};
3135
};
3236

37+
const getTimeLockDate = (timeLock: TimeLock, baseDate: Date): Date | null => {
38+
if (timeLock.time === null) return null;
39+
return new Date(baseDate.getTime() + timeLock.consecutiveTime! * 60 * 1000);
40+
};
41+
42+
const formatDateDifference = (start: Date, stop: Date) =>
43+
formatDurationAsISO8601(calculateTimeDifferenceInSeconds(start, stop));
44+
3345
const createTrainSchedulePayload = (
3446
trainrunSections: TrainrunSection[],
3547
nodes: Node[],
36-
trainrun: Trainrun
48+
trainrun: Trainrun,
49+
startDate: Date
3750
) => {
3851
// TODO: check that the trainrunSections format is still compatible
3952
const path = trainrunSections.flatMap((section, index) => {
@@ -42,15 +55,43 @@ const createTrainSchedulePayload = (
4255
if (!sourceNode || !targetNode) return [];
4356
const originPathItem = createPathItemFromNode(sourceNode, index);
4457
if (index === trainrunSections.length - 1) {
45-
const destinationPathItem = createPathItemFromNode(targetNode, index);
58+
const destinationPathItem = createPathItemFromNode(targetNode, index + 1);
4659
return [originPathItem, destinationPathItem];
4760
}
4861
return [originPathItem];
4962
});
5063

64+
const baseDate = new Date(startDate);
65+
baseDate.setMinutes(0, 0, 0);
66+
67+
// The departure time of the first section is guaranteed to be non-null
68+
const startTime = getTimeLockDate(trainrunSections[0].sourceDeparture, baseDate)!;
69+
70+
const schedule = trainrunSections.flatMap((section, index) => {
71+
const nextSection = trainrunSections[index + 1];
72+
73+
// TODO: extract isNonStopTransit from transitions
74+
let arrival = getTimeLockDate(section.targetArrival, baseDate);
75+
const departure = nextSection ? getTimeLockDate(nextSection.sourceDeparture, baseDate) : null;
76+
if (!arrival && !departure) {
77+
return [];
78+
}
79+
80+
// If missing arrival time, default to a zero stop duration
81+
arrival = arrival || departure!;
82+
83+
return {
84+
at: `${section.targetNodeId}-${index + 1}`,
85+
arrival: formatDateDifference(startTime, arrival),
86+
stop_for: departure ? formatDateDifference(arrival, departure) : null,
87+
};
88+
});
89+
5190
return {
52-
path,
5391
train_name: trainrun.name,
92+
start_time: formatToIsoDate(startTime),
93+
path,
94+
schedule,
5495
};
5596
};
5697

@@ -74,13 +115,19 @@ const handleOperation = async ({
74115
);
75116
switch (type) {
76117
case 'create': {
118+
const startDate = new Date();
77119
const newTrainSchedules = await dispatch(
78120
osrdEditoastApi.endpoints.postV2TimetableByIdTrainSchedule.initiate({
79121
id: timeTableId,
80122
body: [
81123
{
82124
...DEFAULT_PAYLOAD,
83-
...createTrainSchedulePayload(trainrunSectionsByTrainrunId, nodes, trainrun),
125+
...createTrainSchedulePayload(
126+
trainrunSectionsByTrainrunId,
127+
nodes,
128+
trainrun,
129+
startDate
130+
),
84131
},
85132
],
86133
})
@@ -105,14 +152,13 @@ const handleOperation = async ({
105152
id: trainrunIdToUpdate,
106153
})
107154
).unwrap();
155+
const startDate = new Date(trainSchedule.start_time);
108156
await dispatch(
109157
osrdEditoastApi.endpoints.putV2TrainScheduleById.initiate({
110158
id: trainrunIdToUpdate,
111159
trainScheduleForm: {
112160
...trainSchedule,
113-
...createTrainSchedulePayload(trainrunSectionsByTrainrunId, nodes, trainrun),
114-
// TODO: convert NGE times to OSRD schedule
115-
schedule: [],
161+
...createTrainSchedulePayload(trainrunSectionsByTrainrunId, nodes, trainrun, startDate),
116162
},
117163
})
118164
).unwrap();

0 commit comments

Comments
 (0)