Skip to content

Commit

Permalink
replace editoast train id by front train id for trainSchedules
Browse files Browse the repository at this point in the history
  • Loading branch information
SharglutDev committed Feb 7, 2025
1 parent 8560248 commit 797955e
Show file tree
Hide file tree
Showing 22 changed files with 243 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
SimulationSummaryResult,
TrainScheduleResult,
} from 'common/api/osrdEditoastApi';
import type { TrainScheduleId, TrainScheduleResultWithTrainId } from 'reducers/osrdconf/types';

export const pathLength = 4000;

Expand Down Expand Up @@ -351,8 +352,8 @@ export const electrificationRangesLarge: ElectrificationRange[] = [
},
];

export const trainScheduleTooFast: TrainScheduleResult = {
id: 98,
export const trainScheduleTooFast: TrainScheduleResultWithTrainId = {
id: 'train-98' as TrainScheduleId,
timetable_id: 10,
train_name: 'tooFast',
labels: [],
Expand Down Expand Up @@ -418,8 +419,8 @@ export const trainSummaryTooFast: Extract<SimulationSummaryResult, { status: 'su
path_item_times_base: [0, 1444453, 2491479],
};

export const trainScheduleNotHonored: TrainScheduleResult = {
id: 96,
export const trainScheduleNotHonored: TrainScheduleResultWithTrainId = {
id: 'train-96' as TrainScheduleId,
timetable_id: 10,
train_name: 'notHonored',
labels: [],
Expand Down Expand Up @@ -485,8 +486,8 @@ export const trainSummaryNotHonored: Extract<SimulationSummaryResult, { status:
path_item_times_base: [0, 1425534, 2186885],
};

export const trainScheduleHonored: TrainScheduleResult = {
id: 95,
export const trainScheduleHonored: TrainScheduleResultWithTrainId = {
id: 'train-95' as TrainScheduleId,
timetable_id: 10,
train_name: 'normal',
labels: [],
Expand Down Expand Up @@ -529,12 +530,12 @@ export const trainScheduleHonored: TrainScheduleResult = {
},
};

export const trainScheduleNoSchedule: TrainScheduleResult = {
export const trainScheduleNoSchedule: TrainScheduleResultWithTrainId = {
...trainScheduleHonored,
schedule: undefined,
};

export const trainScheduleNoMatch: TrainScheduleResult = {
export const trainScheduleNoMatch: TrainScheduleResultWithTrainId = {
...trainScheduleHonored,
schedule: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import {
osrdEditoastApi,
type SearchResultItemOperationalPoint,
type TrainScheduleBase,
type TrainScheduleResult,
} from 'common/api/osrdEditoastApi';
import type { TimetableItemId, TrainScheduleId } from 'reducers/osrdconf/types';
import type {
TimetableItemId,
TrainScheduleId,
TrainScheduleResultWithTrainId,
} from 'reducers/osrdconf/types';
import type { AppDispatch } from 'store';
import { formatToIsoDate } from 'utils/date';
import { Duration } from 'utils/duration';
Expand Down Expand Up @@ -277,7 +280,7 @@ const handleUpdateTrainSchedule = async ({
trainrun: TrainrunDto;
dispatch: AppDispatch;
infraId: number;
addUpsertedTrainSchedules: (trainSchedules: TrainScheduleResult[]) => void;
addUpsertedTrainSchedules: (trainSchedules: TrainScheduleResultWithTrainId[]) => void;
state: MacroEditorState;
}) => {
const { nodes, labels } = netzgrafikDto;
Expand Down Expand Up @@ -313,7 +316,11 @@ const handleUpdateTrainSchedule = async ({
},
})
).unwrap();
addUpsertedTrainSchedules([newTrainSchedule]);
const formattedNewTrainSchedule: TrainScheduleResultWithTrainId = {
...newTrainSchedule,
id: formatEditoastTrainIdToTrainScheduleId(newTrainSchedule.id),
};
addUpsertedTrainSchedules([formattedNewTrainSchedule]);
};

const handleTrainrunOperation = async ({
Expand All @@ -333,7 +340,7 @@ const handleTrainrunOperation = async ({
infraId: number;
timeTableId: number;
netzgrafikDto: NetzgrafikDto;
addUpsertedTrainSchedules: (trainSchedules: TrainScheduleResult[]) => void;
addUpsertedTrainSchedules: (trainSchedules: TrainScheduleResultWithTrainId[]) => void;
addDeletedTrainIds: (trainIds: TimetableItemId[]) => void;
state: MacroEditorState;
}) => {
Expand Down Expand Up @@ -366,9 +373,14 @@ const handleTrainrunOperation = async ({
})
).unwrap();
// TODO Paced train : Adapt to handle paced trains in issue https://github.com/OpenRailAssociation/osrd/issues/10612
const formattedTrainId = formatEditoastTrainIdToTrainScheduleId(newTrainSchedules[0].id);
state.trainScheduleIdByNgeId.set(trainrunId, formattedTrainId);
addUpsertedTrainSchedules(newTrainSchedules);
const formattedNewTrainSchedules: TrainScheduleResultWithTrainId[] = newTrainSchedules.map(
(trainSchedule) => ({
...trainSchedule,
id: formatEditoastTrainIdToTrainScheduleId(trainSchedule.id),
})
);
state.trainScheduleIdByNgeId.set(trainrunId, formattedNewTrainSchedules[0].id);
addUpsertedTrainSchedules(formattedNewTrainSchedules);
break;
}
case 'delete': {
Expand Down Expand Up @@ -501,7 +513,7 @@ const handleLabelOperation = async ({
netzgrafikDto: NetzgrafikDto;
dispatch: AppDispatch;
infraId: number;
addUpsertedTrainSchedules: (trainSchedules: TrainScheduleResult[]) => void;
addUpsertedTrainSchedules: (trainSchedules: TrainScheduleResultWithTrainId[]) => void;
state: MacroEditorState;
}) => {
const { trainruns } = netzgrafikDto;
Expand Down Expand Up @@ -544,7 +556,7 @@ const handleOperation = async ({
infraId: number;
timeTableId: number;
netzgrafikDto: NetzgrafikDto;
addUpsertedTrainSchedules: (trainSchedules: TrainScheduleResult[]) => void;
addUpsertedTrainSchedules: (trainSchedules: TrainScheduleResultWithTrainId[]) => void;
addDeletedTrainIds: (trainIds: TimetableItemId[]) => void;
}) => {
const { type } = event;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ import type {
InfraWithState,
ScenarioResponse,
TimetableDetailedResult,
TrainScheduleResult,
} from 'common/api/osrdEditoastApi';
import ScenarioLoaderMessage from 'modules/scenario/components/ScenarioLoaderMessage';
import TimetableManageTrainSchedule from 'modules/trainschedule/components/ManageTrainSchedule/TimetableManageTrainSchedule';
import Timetable from 'modules/trainschedule/components/Timetable/Timetable';
import type { TimetableItemId } from 'reducers/osrdconf/types';
import type { TimetableItemId, TrainScheduleResultWithTrainId } from 'reducers/osrdconf/types';
import { useAppDispatch } from 'store';

import ScenarioDescription from './ScenarioDescription';
Expand Down Expand Up @@ -116,7 +115,7 @@ const ScenarioContent = ({
infraId: infra.id,
timeTableId: scenario.timetable_id,
netzgrafikDto,
addUpsertedTrainSchedules: (upsertedTrainSchedules: TrainScheduleResult[]) => {
addUpsertedTrainSchedules: (upsertedTrainSchedules: TrainScheduleResultWithTrainId[]) => {
upsertTrainSchedules(upsertedTrainSchedules);
},
addDeletedTrainIds: (trainIds: TimetableItemId[]) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,29 @@ import { compact } from 'lodash';
import type {
LightRollingStockWithLiveries,
SimulationSummaryResult,
TrainScheduleResult,
} from 'common/api/osrdEditoastApi';
import type { TrainScheduleWithDetails } from 'modules/trainschedule/components/Timetable/types';
import type { TrainId, TrainScheduleId } from 'reducers/osrdconf/types';
import type {
TrainId,
TrainScheduleId,
TrainScheduleResultWithTrainId,
} from 'reducers/osrdconf/types';
import { Duration } from 'utils/duration';
import { jouleToKwh } from 'utils/physics';
import { formatKmValue } from 'utils/strings';
import {
formatEditoastTrainIdToTrainScheduleId,
formatTrainScheduleIdToEditoastTrainId,
} from 'utils/trainId';
import { mapBy } from 'utils/types';

import { isScheduledPointsNotHonored, isTooFast } from '../utils';

const formatTrainScheduleSummaries = (
trainIds: TrainId[],
rawSummaries: Record<string, SimulationSummaryResult>,
rawTrainSchedules: Map<number, TrainScheduleResult>,
rawSummaries: Record<TrainScheduleId, SimulationSummaryResult>,
rawTrainSchedules: Map<TrainScheduleId, TrainScheduleResultWithTrainId>,
rollingStocks: LightRollingStockWithLiveries[]
): Map<TrainId, TrainScheduleWithDetails> => {
const relevantTrainSchedules = compact(
// TODO Paced train : Adapt this for the add paced train issue : https://github.com/OpenRailAssociation/osrd/issues/10615
trainIds.map((trainId) => {
const editoastTrainId = formatTrainScheduleIdToEditoastTrainId(trainId as TrainScheduleId);

return rawTrainSchedules.get(editoastTrainId!);
})
trainIds.map((trainId) => rawTrainSchedules.get(trainId as TrainScheduleId))
);

const trainScheduleWithDetails = relevantTrainSchedules.map((trainSchedule) => {
Expand Down Expand Up @@ -78,7 +73,6 @@ const formatTrainScheduleSummaries = (

return {
...trainSchedule,
id: formatEditoastTrainIdToTrainScheduleId(trainSchedule.id),
trainName: trainSchedule.train_name,
startTime,
stopsCount:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
import type { TrainSpaceTimeData } from 'applications/operationalStudies/types';
import { type ProjectPathTrainResult, type TrainScheduleResult } from 'common/api/osrdEditoastApi';
import type { TrainId } from 'reducers/osrdconf/types';
import { formatEditoastTrainIdToTrainScheduleId } from 'utils/trainId';
import { type ProjectPathTrainResult } from 'common/api/osrdEditoastApi';
import type {
TrainId,
TrainScheduleId,
TrainScheduleResultWithTrainId,
} from 'reducers/osrdconf/types';

const upsertNewProjectedTrains = (
projectedTrains: Map<TrainId, TrainSpaceTimeData>,
projectedTrainsToUpsert: Record<string, ProjectPathTrainResult>,
trainSchedulesById: Map<number, TrainScheduleResult>
projectedTrainsToUpsert: Record<TrainScheduleId, ProjectPathTrainResult>,
trainSchedulesById: Map<TrainScheduleId, TrainScheduleResultWithTrainId>
) => {
const newProjectedTrains = new Map(projectedTrains);

// For each key (train id) in projectPathTrainResult, we either add it or update it in the state
Object.entries(projectedTrainsToUpsert).forEach(([trainIdKey, trainData]) => {
const editoastTrainId = Number(trainIdKey);
if (!trainData) {
console.error(`Train ${editoastTrainId} not found in the projectedTrainsToUpsert`);
console.error(`Train ${trainIdKey} not found in the projectedTrainsToUpsert`);
return;
}

// TODO : adapt this to handle paced train in space time chart in issue https://github.com/osrd-project/osrd-tests/issues/89
const trainId = formatEditoastTrainIdToTrainScheduleId(editoastTrainId);

const matchingTrain = trainSchedulesById.get(editoastTrainId);
// trainIdKey is in format TrainScheduleId but Object.entries tells typescript it's a string
const matchingTrain = trainSchedulesById.get(trainIdKey as TrainScheduleId);
const projectedTrain = {
id: trainId,
id: trainIdKey as TrainScheduleId,
name: matchingTrain?.train_name || 'Train name not found',
departureTime: new Date(trainData.departure_time),
spaceTimeCurves: trainData.space_time_curves,
signalUpdates: trainData.signal_updates,
};

newProjectedTrains.set(trainId, projectedTrain);
newProjectedTrains.set(trainIdKey as TrainScheduleId, projectedTrain);
});

return newProjectedTrains;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ import { useEffect, useState, type Dispatch, type SetStateAction, useMemo } from

import { useSelector } from 'react-redux';

import { osrdEditoastApi, type TrainScheduleResult } from 'common/api/osrdEditoastApi';
import { osrdEditoastApi, type SimulationSummaryResult } from 'common/api/osrdEditoastApi';
import { useOsrdConfSelectors } from 'common/osrdContext';
import type { TrainScheduleWithDetails } from 'modules/trainschedule/components/Timetable/types';
import type { TrainId, TrainScheduleId } from 'reducers/osrdconf/types';
import type {
TrainId,
TrainScheduleId,
TrainScheduleResultWithTrainId,
} from 'reducers/osrdconf/types';
import { getBatchPackage } from 'utils/batch';
import { formatTrainScheduleIdToEditoastTrainId } from 'utils/trainId';
import {
formatEditoastTrainIdToTrainScheduleId,
formatTrainScheduleIdToEditoastTrainId,
} from 'utils/trainId';
import { concatMap, mapBy } from 'utils/types';

import formatTrainScheduleSummaries from '../helpers/formatTrainScheduleSummaries';
Expand All @@ -18,7 +25,7 @@ const BATCH_SIZE = 10;
type UseLazyLoadTrainsProps = {
infraId?: number;
trainIdsToFetch?: TrainId[];
trainSchedules?: TrainScheduleResult[];
trainSchedules?: TrainScheduleResultWithTrainId[];
setTrainIdsToFetch?: Dispatch<SetStateAction<TrainId[] | undefined>>;
setTrainIdsToProject?: Dispatch<SetStateAction<Set<TrainId>>>;
};
Expand Down Expand Up @@ -73,6 +80,13 @@ const useLazyLoadTrains = ({
},
}).unwrap();

// TODO Paced train : Adapt this for the add paced train issue : https://github.com/OpenRailAssociation/osrd/issues/10615
const formattedRawSummaries: { [key: TrainScheduleId]: SimulationSummaryResult } = {};
for (const [editoastTrainId, trainSummary] of Object.entries(rawSummaries)) {
const trainId = formatEditoastTrainIdToTrainScheduleId(Number(editoastTrainId));
formattedRawSummaries[trainId] = trainSummary;
}

// the two rtk-query calls postTrainSchedule & postTrainScheduleSimulationSummary
// do not happen during the same react cycle.
// if we update a train, one is going to re-fetch first and the 2 are out of sync during a few cycles.
Expand All @@ -89,7 +103,7 @@ const useLazyLoadTrains = ({
// format the summaries to display them in the timetable
const newFormattedSummaries = formatTrainScheduleSummaries(
packageToFetch,
rawSummaries,
formattedRawSummaries,
trainSchedulesById,
rollingStocks!
);
Expand Down
Loading

0 comments on commit 797955e

Please sign in to comment.