Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

front: fix train edition setup #9826

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const formatTrainScheduleSummaries = (
};

return {
id: trainSchedule.id,
...trainSchedule,
trainName: trainSchedule.train_name,
startTime,
stopsCount:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,13 @@ import {
import { useOsrdConfActions, useOsrdConfSelectors } from 'common/osrdContext';
import { formatSuggestedOperationalPoints, upsertPathStepsInOPs } from 'modules/pathfinding/utils';
import { getSupportedElectrification, isThermal } from 'modules/rollingStock/helpers/electric';
import { adjustConfWithTrainToModify } from 'modules/trainschedule/components/ManageTrainSchedule/helpers/adjustConfWithTrainToModify';
import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSchedule/types';
import computeBasePathSteps from 'modules/trainschedule/helpers/computeBasePathSteps';
import { setFailure } from 'reducers/main';
import type { OperationalStudiesConfSliceActions } from 'reducers/osrdconf/operationalStudiesConf';
import type { PathStep } from 'reducers/osrdconf/types';
import { useAppDispatch } from 'store';
import { castErrorToFailure } from 'utils/error';
import { getPointCoordinates } from 'utils/geometry';
import { mmToM } from 'utils/physics';
import { ISO8601Duration2sec } from 'utils/timeManipulation';

import type { ManageTrainSchedulePathProperties } from '../types';

Expand All @@ -34,48 +31,6 @@ type ItineraryForTrainUpdate = {
pathProperties: ManageTrainSchedulePathProperties;
};

/**
* create pathSteps in the case pathfinding fails or the train is imported from NGE
*/
const computeBasePathSteps = (trainSchedule: TrainScheduleResult) =>
trainSchedule.path.map((step) => {
const correspondingSchedule = trainSchedule.schedule?.find(
(schedule) => schedule.at === step.id
);

const {
arrival,
stop_for: stopFor,
locked,
reception_signal: receptionSignal,
} = correspondingSchedule || {};

const stepWithoutSecondaryCode = omit(step, ['secondary_code']);

if ('track' in stepWithoutSecondaryCode) {
stepWithoutSecondaryCode.offset = mmToM(stepWithoutSecondaryCode.offset!);
}

let name;
if ('trigram' in step) {
name = step.trigram + (step.secondary_code ? `/${step.secondary_code}` : '');
} else if ('uic' in step) {
name = step.uic.toString();
} else if ('operational_point' in step) {
name = step.operational_point;
}

return {
...stepWithoutSecondaryCode,
ch: 'secondary_code' in step ? step.secondary_code : undefined,
name,
arrival, // ISODurationString
stopFor: stopFor ? ISO8601Duration2sec(stopFor).toString() : stopFor,
locked,
receptionSignal,
} as PathStep;
});

export function updatePathStepsFromOperationalPoints(
pathSteps: PathStep[],
suggestedOperationalPoints: SuggestedOP[],
Expand Down Expand Up @@ -122,11 +77,12 @@ const useSetupItineraryForTrainUpdate = (
setPathProperties: (pathProperties: ManageTrainSchedulePathProperties) => void,
trainIdToEdit: number
) => {
const { getInfraID, getUsingElectricalProfiles } = useOsrdConfSelectors();
const { getInfraID } = useOsrdConfSelectors();
const infraId = useSelector(getInfraID);
const usingElectricalProfiles = useSelector(getUsingElectricalProfiles);
const dispatch = useAppDispatch();
const osrdActions = useOsrdConfActions() as OperationalStudiesConfSliceActions;

const { updatePathSteps } = useOsrdConfActions();

const [getTrainScheduleById] = osrdEditoastApi.endpoints.getTrainScheduleById.useLazyQuery({});
const [getRollingStockByName] =
osrdEditoastApi.endpoints.getRollingStockNameByRollingStockName.useLazyQuery();
Expand Down Expand Up @@ -237,15 +193,18 @@ const useSetupItineraryForTrainUpdate = (
const trainSchedule = await getTrainScheduleById({ id: trainIdToEdit }).unwrap();

let rollingStock: RollingStockWithLiveries | null = null;
let pathSteps: (PathStep | null)[] | undefined;

if (trainSchedule.rolling_stock_name) {
try {
rollingStock = await getRollingStockByName({
rollingStockName: trainSchedule.rolling_stock_name,
}).unwrap();
const itinerary = await computeItineraryForTrainUpdate(trainSchedule, rollingStock);
pathSteps = itinerary?.pathSteps;
const pathSteps = itinerary?.pathSteps;

if (pathSteps) {
dispatch(updatePathSteps({ pathSteps }));
}

if (itinerary?.pathProperties) {
setPathProperties(itinerary.pathProperties);
Expand All @@ -254,15 +213,6 @@ const useSetupItineraryForTrainUpdate = (
dispatch(setFailure(castErrorToFailure(e)));
}
}

adjustConfWithTrainToModify(
trainSchedule,
pathSteps || computeBasePathSteps(trainSchedule),
rollingStock?.id,
dispatch,
usingElectricalProfiles,
osrdActions
);
};

setupItineraryForTrainUpdate();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import { GiPathDistance } from 'react-icons/gi';
import { MANAGE_TRAIN_SCHEDULE_TYPES } from 'applications/operationalStudies/consts';
import { osrdEditoastApi } from 'common/api/osrdEditoastApi';
import type { TrainScheduleBase, TrainScheduleResult } from 'common/api/osrdEditoastApi';
import { useOsrdConfActions } from 'common/osrdContext';
import RollingStock2Img from 'modules/rollingStock/components/RollingStock2Img';
import trainNameWithNum from 'modules/trainschedule/components/ManageTrainSchedule/helpers/trainNameHelper';
import { setFailure, setSuccess } from 'reducers/main';
import type { OperationalStudiesConfSliceActions } from 'reducers/osrdconf/operationalStudiesConf';
import { updateTrainIdUsedForProjection, updateSelectedTrainId } from 'reducers/simulationResults';
import { useAppDispatch } from 'store';
import { formatToIsoDate, isoDateToMs } from 'utils/date';
Expand Down Expand Up @@ -51,6 +53,8 @@ const TimetableTrainCard = ({
}: TimetableTrainCardProps) => {
const { t } = useTranslation(['operationalStudies/scenario']);
const dispatch = useAppDispatch();
const { selectTrainToEdit } = useOsrdConfActions() as OperationalStudiesConfSliceActions;

const [postTrainSchedule] =
osrdEditoastApi.endpoints.postTimetableByIdTrainSchedule.useMutation();
const [getTrainSchedule] = osrdEditoastApi.endpoints.postTrainSchedule.useLazyQuery();
Expand All @@ -61,6 +65,7 @@ const TimetableTrainCard = ({
};

const editTrainSchedule = () => {
dispatch(selectTrainToEdit(train));
setTrainIdToEdit(train.id);
setDisplayTrainScheduleManagement(MANAGE_TRAIN_SCHEDULE_TYPES.edit);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ import type {
PathfindingInputError,
PathfindingNotFound,
SimulationSummaryResult,
TrainScheduleResult,
} from 'common/api/osrdEditoastApi';

export type ValidityFilter = 'both' | 'valid' | 'invalid';

export type ScheduledPointsHonoredFilter = 'both' | 'honored' | 'notHonored';

export type TrainScheduleWithDetails = {
id: number;
export type TrainScheduleWithDetails = Omit<
TrainScheduleResult,
'train_name' | 'rolling_stock_name' | 'timetable_id'
> & {
trainName: string;
startTime: Date;
arrivalTime: Date | null;
Expand Down
69 changes: 69 additions & 0 deletions front/src/modules/trainschedule/helpers/computeBasePathSteps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { omit } from 'lodash';

import type { TrainScheduleResult } from 'common/api/osrdEditoastApi';
import type { PathStep } from 'reducers/osrdconf/types';
import { mmToM } from 'utils/physics';
import { ISO8601Duration2sec } from 'utils/timeManipulation';

const findCorrespondingMargin = (
stepId: string,
stepIndex: number,
margins: { boundaries: string[]; values: string[] }
) => {
// The first pathStep will never have its id in boundaries
if (stepIndex === 0) return margins.values[0] === 'none' ? undefined : margins.values[0];

const marginIndex = margins.boundaries.findIndex((boundaryId) => boundaryId === stepId);

return marginIndex !== -1 ? margins.values[marginIndex + 1] : undefined;
};

/** Given a trainScheduleResult, extract its pathSteps */
const computeBasePathSteps = (
trainSchedule: Pick<TrainScheduleResult, 'path' | 'schedule' | 'margins'>
) =>
trainSchedule.path.map((step, index) => {
const correspondingSchedule = trainSchedule.schedule?.find(
(schedule) => schedule.at === step.id
);

const {
arrival,
stop_for: stopFor,
locked,
reception_signal: receptionSignal,
} = correspondingSchedule || {};

const stepWithoutSecondaryCode = omit(step, ['secondary_code']);

if ('track' in stepWithoutSecondaryCode) {
stepWithoutSecondaryCode.offset = mmToM(stepWithoutSecondaryCode.offset!);
}

let name;
if ('trigram' in step) {
name = step.trigram + (step.secondary_code ? `/${step.secondary_code}` : '');
} else if ('uic' in step) {
name = step.uic.toString();
} else if ('operational_point' in step) {
name = step.operational_point;
}

let theoreticalMargin;
if (trainSchedule.margins && index !== trainSchedule.path.length - 1) {
theoreticalMargin = findCorrespondingMargin(step.id, index, trainSchedule.margins);
}

return {
...stepWithoutSecondaryCode,
ch: 'secondary_code' in step ? step.secondary_code : undefined,
name,
arrival, // ISODurationString
stopFor: stopFor ? ISO8601Duration2sec(stopFor).toString() : stopFor,
locked,
receptionSignal,
theoreticalMargin,
} as PathStep;
});

export default computeBasePathSteps;
33 changes: 32 additions & 1 deletion front/src/reducers/osrdconf/operationalStudiesConf/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { createSlice } from '@reduxjs/toolkit';
import { createSlice, type Draft, type PayloadAction } from '@reduxjs/toolkit';

import type { TrainScheduleWithDetails } from 'modules/trainschedule/components/Timetable/types';
import computeBasePathSteps from 'modules/trainschedule/helpers/computeBasePathSteps';
import { defaultCommonConf, buildCommonConfReducers } from 'reducers/osrdconf/osrdConfCommon';
import type { OsrdConfState } from 'reducers/osrdconf/types';
import { convertIsoUtcToLocalTime } from 'utils/date';
import { msToKmh } from 'utils/physics';

import { builPowerRestrictionReducer } from './powerRestrictionReducer';

Expand All @@ -13,6 +17,33 @@ export const operationalStudiesConfSlice = createSlice({
reducers: {
...buildCommonConfReducers<OperationalStudiesConfState>(),
...builPowerRestrictionReducer<OperationalStudiesConfState>(),
selectTrainToEdit(
state: Draft<OperationalStudiesConfState>,
action: PayloadAction<TrainScheduleWithDetails>
) {
const {
rollingStock,
trainName,
initial_speed,
start_time,
options,
speedLimitTag,
labels,
power_restrictions,
} = action.payload;

state.rollingStockID = rollingStock?.id;
state.pathSteps = computeBasePathSteps(action.payload);
state.startTime = convertIsoUtcToLocalTime(start_time);

state.name = trainName;
state.initialSpeed = initial_speed ? Math.floor(msToKmh(initial_speed) * 10) / 10 : 0;

state.usingElectricalProfiles = options?.use_electrical_profiles ?? true;
state.labels = labels;
state.speedLimitByTag = speedLimitTag || undefined;
state.powerRestriction = power_restrictions || [];
},
},
});

Expand Down
Loading
Loading