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 Aug 22, 2024
1 parent 90e655f commit 47e5a35
Showing 1 changed file with 69 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 './types';
import type { NetzgrafikDto, NGEEvent, TrainrunSection, Node, TimeLock, Trainrun } from './types';

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

Expand All @@ -16,14 +18,9 @@ const getTrainrunSectionsByTrainrunId = (trainrunSections: TrainrunSection[], tr
const getNodeById = (nodes: Node[], nodeId: number | string) =>
nodes.find((node) => node.id === nodeId);

// 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 @@ -35,10 +32,24 @@ 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
) => {
// TODO: check that the trainrunSections format is still compatible
const path = trainrunSections.flatMap((section, index) => {
Expand All @@ -47,15 +58,52 @@ const createTrainSchedulePayload = (
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!;

let stopFor = null;
if (departure) {
stopFor = formatDateDifference(departure, arrival);
} else if (!nextSection) {
// Arrival needs to explicitly stop the train
stopFor = formatDurationAsISO8601(0);
}

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

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

Expand Down Expand Up @@ -83,13 +131,19 @@ const handleTrainrunOperation = async ({
);
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 @@ -116,14 +170,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 47e5a35

Please sign in to comment.