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: add default CH code when not provided during open data import #10236

Closed
Closed
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
@@ -1,18 +1,20 @@
import { useMemo } from 'react';
import { useCallback, useMemo } from 'react';

import { Rocket } from '@osrd-project/ui-icons';
import type { TFunction } from 'i18next';
import { keyBy } from 'lodash';
import { useTranslation } from 'react-i18next';

import type { ImportedTrainSchedule } from 'applications/operationalStudies/types';
import type { ImportedTrainSchedule, Step } from 'applications/operationalStudies/types';
import {
osrdEditoastApi,
type LightRollingStockWithLiveries,
type SearchResultItemOperationalPoint,
type TrainScheduleBase,
type TrainScheduleResult,
} from 'common/api/osrdEditoastApi';
import { Loader } from 'common/Loaders';
import { MAIN_OP_CH_CODES } from 'common/Map/Search/useSearchOperationalPoint';
import { ImportTrainScheduleTrainDetail } from 'modules/trainschedule/components/ImportTrainSchedule';
import rollingstockOpenData2OSRD from 'modules/trainschedule/components/ImportTrainSchedule/rollingstock_opendata2osrd.json';
import { setFailure, setSuccess } from 'reducers/main';
Expand Down Expand Up @@ -50,6 +52,12 @@ const ImportTrainScheduleTrainsList = ({
upsertTrainSchedules,
}: ImportTrainScheduleTrainsListProps) => {
const { t } = useTranslation(['operationalStudies/importTrainSchedule']);
const dispatch = useAppDispatch();

const [postSearch] = osrdEditoastApi.endpoints.postSearch.useMutation();
const [postTrainSchedule] =
osrdEditoastApi.endpoints.postTimetableByIdTrainSchedule.useMutation();

const rollingStockDict = useMemo(
() => keyBy(rollingStocks, (rollingStock) => rollingStock.name),
[rollingStocks]
Expand All @@ -72,21 +80,50 @@ const ImportTrainScheduleTrainsList = ({
[trainsList]
);

const [postTrainSchedule] =
osrdEditoastApi.endpoints.postTimetableByIdTrainSchedule.useMutation();

const dispatch = useAppDispatch();
const getStepsMainChCode = useCallback(
async (steps: Step[]) => {
const stepsQuery = ['or', ...steps.map((step) => ['=', ['uic'], Number(step.uic)])];
const mainChCodeConstraint = [
'or',
...MAIN_OP_CH_CODES.map((chCode) => ['=', ['ch'], chCode]),
];

try {
const payloadSteps = {
object: 'operationalpoint',
query: ['and', stepsQuery, mainChCodeConstraint],
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we also need to filter by infra ID here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we retrieve information from the OP with its UIC code, which is normally unique regardless of the infra (thanks to the "country code"), i don't think it's necessary here ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A test infra created by a random person might have a BV CH which doesn't really exist in the real infra the user is interested in.

const stepsDetails = (await postSearch({
searchPayload: payloadSteps,
pageSize: 25,
}).unwrap()) as SearchResultItemOperationalPoint[];

const stepsChCodeByUIC = stepsDetails.reduce<{ [key: number]: string }>((acc, step) => {
acc[step.uic] = step.ch;
return acc;
}, {});

return stepsChCodeByUIC;
} catch (error) {
console.error('Failed to fetch operational points:', error);
return undefined;
}
},
[postSearch]
);

async function generateTrainSchedules() {
try {
let payloads;

if (trainsXmlData.length > 0) {
payloads = generateTrainSchedulesPayloads(trainsXmlData, true);
const uicToMainChCodes = await getStepsMainChCode(trainsXmlData[0].steps);
payloads = generateTrainSchedulesPayloads(trainsXmlData, uicToMainChCodes);
} else if (trainsJsonData.length > 0) {
payloads = trainsJsonData;
} else {
payloads = generateTrainSchedulesPayloads(formattedTrainsList, false);
const uicToMainChCodes = await getStepsMainChCode(formattedTrainsList[0].steps);
payloads = generateTrainSchedulesPayloads(formattedTrainsList, uicToMainChCodes, false);
}

const trainSchedules = await postTrainSchedule({ id: timetableId, body: payloads }).unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { calculateTimeDifferenceInSeconds, formatDurationAsISO8601 } from 'utils

export function generateTrainSchedulesPayloads(
trains: ImportedTrainSchedule[],
uicToMainChCodes?: {
[key: number]: string;
},
checkChAndUIC: boolean = true
): TrainScheduleBase[] {
return trains.reduce((payloads, train) => {
Expand All @@ -33,7 +36,11 @@ export function generateTrainSchedulesPayloads(
return acc; // Skip invalid step
}

acc.path.push({ id: stepId, uic: Number(step.uic), secondary_code: step.chCode });
acc.path.push({
id: stepId,
uic: Number(step.uic),
secondary_code: step.chCode || uicToMainChCodes?.[step.uic],
});

// Skip first step, handle time differences
if (index !== 0) {
Expand Down
Loading