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: display only trains around stdcm result #10432

Merged
merged 3 commits into from
Jan 23, 2025
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 @@ -3,13 +3,8 @@ import { useEffect, useState, type Dispatch, type SetStateAction, useMemo } from

import { useSelector } from 'react-redux';

import {
osrdEditoastApi,
type PathfindingResultSuccess,
type TrainScheduleResult,
} from 'common/api/osrdEditoastApi';
import { osrdEditoastApi, type TrainScheduleResult } from 'common/api/osrdEditoastApi';
import { useOsrdConfSelectors } from 'common/osrdContext';
import useLazyProjectTrains from 'modules/simulationResult/components/SpaceTimeChart/useLazyProjectTrains';
import type { TrainScheduleWithDetails } from 'modules/trainschedule/components/Timetable/types';
import { getBatchPackage } from 'utils/batch';
import { concatMap, mapBy } from 'utils/types';
Expand All @@ -21,9 +16,9 @@ const BATCH_SIZE = 10;
type UseLazyLoadTrainsProps = {
infraId?: number;
trainIdsToFetch?: number[];
path?: PathfindingResultSuccess;
trainSchedules?: TrainScheduleResult[];
setTrainIdsToFetch: Dispatch<SetStateAction<number[] | undefined>>;
setTrainIdsToFetch?: Dispatch<SetStateAction<number[] | undefined>>;
setTrainIdsToProject?: Dispatch<SetStateAction<Set<number>>>;
};

/**
Expand All @@ -36,17 +31,15 @@ type UseLazyLoadTrainsProps = {
const useLazyLoadTrains = ({
infraId,
trainIdsToFetch,
path,
trainSchedules,
setTrainIdsToFetch,
setTrainIdsToProject,
}: UseLazyLoadTrainsProps) => {
const { getElectricalProfileSetId } = useOsrdConfSelectors();
const electricalProfileSetId = useSelector(getElectricalProfileSetId);

const [trainScheduleSummariesById, setTrainScheduleSummariesById] = useState<
Map<number, TrainScheduleWithDetails>
>(new Map());
const [trainIdsToProject, setTrainIdsToProject] = useState<Set<number>>(new Set());
const [allTrainsLoaded, setAllTrainsLoaded] = useState(false);

const [postTrainScheduleSimulationSummary] =
Expand All @@ -57,17 +50,6 @@ const useLazyLoadTrains = ({

const trainSchedulesById = useMemo(() => mapBy(trainSchedules, 'id'), [trainSchedules]);

const allTrainsProjected = useMemo(() => trainIdsToProject.size === 0, [trainIdsToProject]);

const { projectedTrainsById, setProjectedTrainsById } = useLazyProjectTrains({
infraId,
trainIdsToProject,
path,
trainSchedules,
moreTrainsToCome: !allTrainsLoaded,
setTrainIdsToProject,
});

// gradually fetch the simulation of the trains
useEffect(() => {
const getTrainScheduleSummaries = async (_infraId: number, _trainToFetchIds: number[]) => {
Expand Down Expand Up @@ -97,9 +79,6 @@ const useLazyLoadTrains = ({
});

if (!outOfSync) {
// launch the projection of the trains
setTrainIdsToProject((prev) => new Set([...prev, ...packageToFetch]));

// format the summaries to display them in the timetable
const newFormattedSummaries = formatTrainScheduleSummaries(
packageToFetch,
Expand All @@ -108,12 +87,14 @@ const useLazyLoadTrains = ({
rollingStocks!
);

// launch the projection of the trains if needed
setTrainIdsToProject?.((prev) => new Set([...prev, ...packageToFetch]));

// as formattedSummaries is a dictionary, we replace the previous values with the new ones
setTrainScheduleSummariesById((prev) => concatMap(prev, newFormattedSummaries));
}
}

setTrainIdsToFetch([]);
setAllTrainsLoaded(true);
};

Expand All @@ -124,10 +105,7 @@ const useLazyLoadTrains = ({

return {
trainScheduleSummariesById,
projectedTrainsById,
setTrainScheduleSummariesById,
setProjectedTrainsById,
allTrainsProjected,
allTrainsLoaded,
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
type TrainScheduleResult,
} from 'common/api/osrdEditoastApi';
import { useOsrdConfSelectors } from 'common/osrdContext';
import useLazyProjectTrains from 'modules/simulationResult/components/SpaceTimeChart/useLazyProjectTrains';
import { getTrainIdUsedForProjection } from 'reducers/simulationResults/selectors';
import { mapBy } from 'utils/types';

Expand All @@ -30,6 +31,7 @@ const useScenarioData = (

const [trainSchedules, setTrainSchedules] = useState<TrainScheduleResult[]>();
const [trainIdsToFetch, setTrainIdsToFetch] = useState<number[]>();
const [trainIdsToProject, setTrainIdsToProject] = useState<Set<number>>(new Set());

const [fetchTrainSchedules] = osrdEditoastApi.endpoints.postTrainSchedule.useLazyQuery();
const [putTrainScheduleById] = osrdEditoastApi.endpoints.putTrainScheduleById.useMutation();
Expand All @@ -40,21 +42,36 @@ const useScenarioData = (

const projectionPath = usePathProjection(infra);

const {
trainScheduleSummariesById,
projectedTrainsById,
setTrainScheduleSummariesById,
setProjectedTrainsById,
allTrainsProjected,
allTrainsLoaded,
} = useLazyLoadTrains({
const { trainScheduleSummariesById, setTrainScheduleSummariesById, allTrainsLoaded } =
useLazyLoadTrains({
infraId: scenario.infra_id,
trainIdsToFetch,
trainSchedules,
setTrainIdsToProject,
});

useEffect(() => {
if (allTrainsLoaded) {
setTrainIdsToFetch([]);
}
}, [allTrainsLoaded]);

const { projectedTrainsById, allTrainsProjected, setProjectedTrainsById } = useLazyProjectTrains({
infraId: scenario.infra_id,
trainIdsToFetch,
setTrainIdsToFetch,
trainIdsToProject,
path: projectionPath?.path,
trainSchedules,
moreTrainsToCome: !allTrainsLoaded,
setTrainIdsToProject,
});

useEffect(() => {
if (trainSchedules && projectionPath?.path && allTrainsLoaded) {
const trainIds = trainSchedules.map((trainSchedule) => trainSchedule.id);
setTrainIdsToProject(new Set(trainIds));
}
}, [projectionPath?.path]);

const { data: conflicts, refetch: refetchConflicts } =
osrdEditoastApi.endpoints.getTimetableByIdConflicts.useQuery(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { useSelector } from 'react-redux';

import { STDCM_TRAIN_ID } from 'applications/stdcm/consts';
import useProjectedTrainsForStdcm from 'applications/stdcm/hooks/useProjectedTrainsForStdcm';
import type { StdcmResultsOutput } from 'applications/stdcm/types';
import { osrdEditoastApi, type TrackRange } from 'common/api/osrdEditoastApi';
import type { StdcmSimulationOutputs } from 'applications/stdcm/types';
import { hasResults } from 'applications/stdcm/utils/simulationOutputUtils';
import { osrdEditoastApi } from 'common/api/osrdEditoastApi';
import { useOsrdConfSelectors } from 'common/osrdContext';
import ResizableSection from 'common/ResizableSection';
import i18n from 'i18n';
Expand All @@ -20,16 +21,13 @@ const HANDLE_TAB_RESIZE_HEIGHT = 20;
const MANCHETTE_HEIGHT_DIFF = 100;

type StdcmDebugResultsProps = {
pathTrackRanges: TrackRange[];
simulationOutputs: StdcmResultsOutput;
simulationOutputs?: StdcmSimulationOutputs;
};

const StdcmDebugResults = ({
pathTrackRanges,
simulationOutputs: { pathProperties, results, speedSpaceChartData },
}: StdcmDebugResultsProps) => {
const StdcmDebugResults = ({ simulationOutputs }: StdcmDebugResultsProps) => {
const { getWorkScheduleGroupId } = useOsrdConfSelectors() as StdcmConfSelectors;
const workScheduleGroupId = useSelector(getWorkScheduleGroupId);
const successfulSimulation = hasResults(simulationOutputs) ? simulationOutputs : undefined;

const [speedSpaceChartContainerHeight, setSpeedSpaceChartContainerHeight] =
useState(SPEED_SPACE_CHART_HEIGHT);
Expand All @@ -39,46 +37,53 @@ const StdcmDebugResults = ({
MANCHETTE_WITH_SPACE_TIME_CHART_DEFAULT_HEIGHT
);

const projectedData = useProjectedTrainsForStdcm(results);
const projectedData = useProjectedTrainsForStdcm(successfulSimulation?.results);

const { data: workSchedules } = osrdEditoastApi.endpoints.postWorkSchedulesProjectPath.useQuery(
{
body: {
path_track_ranges: pathTrackRanges,
path_track_ranges: successfulSimulation?.results.path.track_section_ranges || [],
work_schedule_group_id: workScheduleGroupId!,
},
},
{ skip: !workScheduleGroupId }
{ skip: !workScheduleGroupId || !successfulSimulation }
);

if (!successfulSimulation) return null;
const { pathProperties, results, speedSpaceChartData } = successfulSimulation;

return (
<>
{projectedData && pathProperties.manchetteOperationalPoints && (
<ResizableSection
height={manchetteWithSpaceTimeChartHeight}
setHeight={setManchetteWithSpaceTimeChartHeight}
minHeight={MANCHETTE_WITH_SPACE_TIME_CHART_DEFAULT_HEIGHT}
>
<div
className="osrd-simulation-container mb-2"
style={{
height: manchetteWithSpaceTimeChartHeight,
}}
{projectedData &&
projectedData?.spaceTimeData.length > 0 &&
pathProperties.manchetteOperationalPoints && (
<ResizableSection
height={manchetteWithSpaceTimeChartHeight}
setHeight={setManchetteWithSpaceTimeChartHeight}
minHeight={MANCHETTE_WITH_SPACE_TIME_CHART_DEFAULT_HEIGHT}
>
<p className="mt-2 mb-3 ml-4 font-weight-bold">{tWithoutPrefix('spaceTimeGraphic')}</p>
<div className="chart-container mt-2">
<ManchetteWithSpaceTimeChartWrapper
operationalPoints={pathProperties.manchetteOperationalPoints}
projectPathTrainResult={projectedData.spaceTimeData}
selectedTrainScheduleId={STDCM_TRAIN_ID}
workSchedules={workSchedules}
projectionLoaderData={projectedData.projectionLoaderData}
height={manchetteWithSpaceTimeChartHeight - MANCHETTE_HEIGHT_DIFF}
/>
<div
className="osrd-simulation-container mb-2"
style={{
height: manchetteWithSpaceTimeChartHeight,
}}
>
<p className="mt-2 mb-3 ml-4 font-weight-bold">
{tWithoutPrefix('spaceTimeGraphic')}
</p>
<div className="chart-container mt-2">
<ManchetteWithSpaceTimeChartWrapper
operationalPoints={pathProperties.manchetteOperationalPoints}
projectPathTrainResult={projectedData.spaceTimeData}
selectedTrainScheduleId={STDCM_TRAIN_ID}
workSchedules={workSchedules}
projectionLoaderData={projectedData.projectionLoaderData}
height={manchetteWithSpaceTimeChartHeight - MANCHETTE_HEIGHT_DIFF}
/>
</div>
</div>
</div>
</ResizableSection>
)}
</ResizableSection>
)}

<div className="osrd-simulation-container my-2 speedspacechart-container">
<div
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
getOperationalPointsWithTimes,
} from 'applications/stdcm/utils/formatSimulationReportSheet';
import { hasConflicts, hasResults } from 'applications/stdcm/utils/simulationOutputUtils';
import { type TrackRange } from 'common/api/osrdEditoastApi';
import NewMap from 'modules/trainschedule/components/ManageTrainSchedule/NewMap';
import useDeploymentSettings from 'utils/hooks/useDeploymentSettings';

Expand All @@ -33,7 +32,6 @@ type StcdmResultsProps = {
selectedSimulationIndex: number;
showStatusBanner: boolean;
simulationsList: StdcmSimulation[];
pathTrackRanges?: TrackRange[];
};

const StcdmResults = ({
Expand All @@ -48,7 +46,6 @@ const StcdmResults = ({
selectedSimulationIndex,
showStatusBanner,
simulationsList,
pathTrackRanges,
}: StcdmResultsProps) => {
const { t } = useTranslation('stdcm', { keyPrefix: 'simulation.results' });
const { stdcmName } = useDeploymentSettings();
Expand Down Expand Up @@ -188,9 +185,7 @@ const StcdmResults = ({
/>
</div>
</div>
{isDebugMode && pathTrackRanges && hasSimulationResults && (
<StdcmDebugResults pathTrackRanges={pathTrackRanges} simulationOutputs={outputs} />
)}
{isDebugMode && <StdcmDebugResults simulationOutputs={outputs} />}
</>
);
};
Expand Down
Loading
Loading