Skip to content

Commit

Permalink
front: convert times from NGE to OSRD
Browse files Browse the repository at this point in the history
Closes: #8389
  • Loading branch information
emersion committed Sep 3, 2024
1 parent 1c230a1 commit a7828f4
Showing 1 changed file with 61 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import {
type TrainScheduleResult,
} from 'common/api/osrdEditoastApi';
import type { AppDispatch } from 'store';
import { formatToIsoDate } from 'utils/date';
import { calculateTimeDifferenceInSeconds, formatDurationAsISO8601 } from 'utils/timeManipulation';

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

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

Expand Down Expand Up @@ -87,14 +89,9 @@ const getTrainrunSectionsByTrainrunId = (netzgrafikDto: NetzgrafikDto, trainrunI
return orderedSections;
};

// TODO: add a dynamic values when available
const DEFAULT_PAYLOAD: Pick<
TrainScheduleBase,
'constraint_distribution' | 'rolling_stock_name' | 'start_time'
> = {
const DEFAULT_PAYLOAD: Pick<TrainScheduleBase, 'constraint_distribution' | 'rolling_stock_name'> = {
constraint_distribution: 'STANDARD',
rolling_stock_name: '',
start_time: '2024-07-15T08:00:00+02:00',
};

const createPathItemFromNode = (node: Node, index: number) => {
Expand All @@ -106,26 +103,69 @@ const createPathItemFromNode = (node: Node, index: number) => {
};
};

const getTimeLockDate = (
timeLock: TimeLock,
startTimeLock: TimeLock,
startDate: Date
): Date | null => {
if (timeLock.time === null) return null;
const offset = timeLock.consecutiveTime! - startTimeLock.consecutiveTime!;
return new Date(startDate.getTime() + offset * 60 * 1000);
};

const formatDateDifference = (start: Date, stop: Date) =>
formatDurationAsISO8601(calculateTimeDifferenceInSeconds(start, stop));

const createTrainSchedulePayload = (
trainrunSections: TrainrunSection[],
nodes: Node[],
trainrun: Trainrun
trainrun: Trainrun,
oldStartDate: Date
) => {
const path = trainrunSections.flatMap((section, index) => {
const sourceNode = getNodeById(nodes, section.sourceNodeId);
const targetNode = getNodeById(nodes, section.targetNodeId);
if (!sourceNode || !targetNode) return [];
const originPathItem = createPathItemFromNode(sourceNode, index);
if (index === trainrunSections.length - 1) {
const destinationPathItem = createPathItemFromNode(targetNode, index);
const destinationPathItem = createPathItemFromNode(targetNode, index + 1);
return [originPathItem, destinationPathItem];
}
return [originPathItem];
});

// The departure time of the first section is guaranteed to be non-null
const startTimeLock = trainrunSections[0].sourceDeparture;
const startDate = new Date(oldStartDate);
startDate.setMinutes(startTimeLock.time!, 0, 0);

const schedule = trainrunSections.flatMap((section, index) => {
const nextSection = trainrunSections[index + 1];

// TODO: extract isNonStopTransit from transitions
let arrival = getTimeLockDate(section.targetArrival, startTimeLock, startDate);
const departure = nextSection
? getTimeLockDate(nextSection.sourceDeparture, startTimeLock, startDate)
: null;
if (!arrival && !departure) {
return [];
}

// If missing arrival time, default to a zero stop duration
arrival = arrival || departure!;

return {
at: `${section.targetNodeId}-${index + 1}`,
arrival: formatDateDifference(arrival, startDate),
stop_for: departure ? formatDateDifference(departure, arrival) : null,
};
});

return {
path,
train_name: trainrun.name,
start_time: formatToIsoDate(startDate),
path,
schedule,
};
};

Expand All @@ -150,13 +190,19 @@ const handleTrainrunOperation = async ({
const trainrunSectionsByTrainrunId = getTrainrunSectionsByTrainrunId(netzgrafikDto, trainrun.id);
switch (type) {
case 'create': {
const startDate = new Date();
const newTrainSchedules = await dispatch(
osrdEditoastApi.endpoints.postV2TimetableByIdTrainSchedule.initiate({
id: timeTableId,
body: [
{
...DEFAULT_PAYLOAD,
...createTrainSchedulePayload(trainrunSectionsByTrainrunId, nodes, trainrun),
...createTrainSchedulePayload(
trainrunSectionsByTrainrunId,
nodes,
trainrun,
startDate
),
},
],
})
Expand All @@ -183,14 +229,15 @@ const handleTrainrunOperation = async ({
id: trainrunIdToUpdate,
})
).unwrap();
const startDate = new Date(trainSchedule.start_time);
const newTrainSchedule = await dispatch(
osrdEditoastApi.endpoints.putV2TrainScheduleById.initiate({
id: trainrunIdToUpdate,
trainScheduleForm: {
...trainSchedule,
...createTrainSchedulePayload(trainrunSectionsByTrainrunId, nodes, trainrun),
// TODO: convert NGE times to OSRD schedule
schedule: [],
...createTrainSchedulePayload(trainrunSectionsByTrainrunId, nodes, trainrun, startDate),
// Reset margins because they contain references to path items
margins: undefined,
},
})
).unwrap();
Expand Down

0 comments on commit a7828f4

Please sign in to comment.