Skip to content

Commit

Permalink
front: Refactor useStdcmResults to directly return zones from pathPro…
Browse files Browse the repository at this point in the history
…perties

Signed-off-by: Achraf Mohyeddine <[email protected]>
  • Loading branch information
achrafmohye committed Oct 30, 2024
1 parent 392c603 commit bb21d2e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 28 deletions.
2 changes: 2 additions & 0 deletions front/src/applications/operationalStudies/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ export type ManageTrainSchedulePathProperties = {
length: number;
trackSectionRanges: NonNullable<PathfindingResultSuccess['track_section_ranges']>;
incompatibleConstraints?: IncompatibleConstraints;
zones: PathProperties['zones'];
operationalPoints: SuggestedOP[];
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { PDFDownloadLink } from '@react-pdf/renderer';
import { useTranslation, Trans } from 'react-i18next';
import { useSelector } from 'react-redux';

import type { ManageTrainSchedulePathProperties } from 'applications/operationalStudies/types';
import { STDCM_TRAIN_ID } from 'applications/stdcm/consts';
import useProjectedTrainsForStdcm from 'applications/stdcm/hooks/useProjectedTrainsForStdcm';
import type { StdcmSimulation } from 'applications/stdcm/types';
Expand All @@ -14,15 +15,9 @@ import {
getOperationalPointsWithTimes,
} from 'applications/stdcm/utils/formatSimulationReportSheet';
import conflictData from 'applications/stdcmV2/components/StdcmResults/conflicts.json';
import {
osrdEditoastApi,
type Conflict,
type PathfindingResultSuccess,
type TrackRange,
} from 'common/api/osrdEditoastApi';
import { useOsrdConfSelectors, useInfraID } from 'common/osrdContext';
import { osrdEditoastApi, type Conflict, type TrackRange } from 'common/api/osrdEditoastApi';
import { useOsrdConfSelectors } from 'common/osrdContext';
import i18n from 'i18n';
import usePathProperties from 'modules/pathfinding/hooks/usePathProperties';
import ManchetteWithSpaceTimeChartWrapper from 'modules/simulationResult/components/ManchetteWithSpaceTimeChart/ManchetteWithSpaceTimeChart';
import SpeedSpaceChartContainer from 'modules/simulationResult/components/SpeedSpaceChart/SpeedSpaceChartContainer';
import { Map } from 'modules/trainschedule/components/ManageTrainSchedule';
Expand All @@ -46,6 +41,7 @@ type StcdmResultsV2Props = {
showStatusBanner: boolean;
simulationsList: StdcmSimulation[];
pathTrackRanges?: TrackRange[];
pathProperties?: ManageTrainSchedulePathProperties;
};

const StcdmResults = ({
Expand All @@ -59,6 +55,7 @@ const StcdmResults = ({
showStatusBanner,
simulationsList,
pathTrackRanges,
pathProperties,
}: StcdmResultsV2Props) => {
const { getWorkScheduleGroupId } = useOsrdConfSelectors() as StdcmConfSelectors;
const workScheduleGroupId = useSelector(getWorkScheduleGroupId);
Expand Down Expand Up @@ -100,13 +97,6 @@ const StcdmResults = ({
);
}, [selectedSimulation]);

const infraId = useInfraID();
const pathProperties = usePathProperties(
infraId,
conflictData.pathfinding_result as PathfindingResultSuccess,
['operational_points', 'zones', 'geometry']
);

useEffect(() => {
if (!pathProperties) return;
const generateConflictMessages = () => {
Expand Down
6 changes: 4 additions & 2 deletions front/src/applications/stdcm/hooks/useStdcmResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ const useStdcmResults = (
const getPathProperties = async (_infraId: number, path: PathfindingResultSuccess) => {
const pathPropertiesParams: PostInfraByInfraIdPathPropertiesApiArg = {
infraId: _infraId,
props: ['electrifications', 'geometry', 'operational_points'],
props: ['electrifications', 'geometry', 'operational_points', 'zones'],
pathPropertiesInput: {
track_section_ranges: path.track_section_ranges,
},
};
const { geometry, operational_points, electrifications } =
const { geometry, operational_points, electrifications, zones } =
await postPathProperties(pathPropertiesParams).unwrap();

if (geometry && operational_points && electrifications) {
Expand Down Expand Up @@ -97,6 +97,8 @@ const useStdcmResults = (
allWaypoints: updatedSuggestedOPs,
length: path.length,
trackSectionRanges: path.track_section_ranges,
operationalPoints: suggestedOperationalPoints,
zones,
});
}
};
Expand Down
26 changes: 15 additions & 11 deletions front/src/applications/stdcm/utils/fomatConflicts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import dayjs from 'dayjs';

import type { Conflict, PathProperties } from 'common/api/osrdEditoastApi';
import type { ManageTrainSchedulePathProperties } from 'applications/operationalStudies/types';
import type { Conflict } from 'common/api/osrdEditoastApi';
import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSchedule/types';

interface ConflictWithInterval {
trainIds?: number[];
Expand All @@ -15,27 +17,29 @@ interface ConflictWithInterval {

// Helper function to find the closest operational point before or after a given position
const findClosestOperationalPoint = (
operationalPoints: PathProperties['operational_points'],
operationalPoints: SuggestedOP[],
position: number,
direction: 'before' | 'after'
) => {
if (direction === 'before') {
const pointsBefore = operationalPoints?.filter((point) => point?.position <= position);
const pointsBefore = operationalPoints?.filter((point) => point?.positionOnPath <= position);
return pointsBefore
? pointsBefore?.reduce((prev, curr) => (curr.position > prev.position ? curr : prev))
? pointsBefore?.reduce((prev, curr) =>
curr.positionOnPath > prev.positionOnPath ? curr : prev
)
: null;
}
const pointsAfter = operationalPoints?.filter((point) => point.position >= position);
const pointsAfter = operationalPoints?.filter((point) => point.positionOnPath >= position);
return pointsAfter
? pointsAfter?.reduce((prev, curr) => (curr.position < prev.position ? curr : prev))
? pointsAfter?.reduce((prev, curr) => (curr.positionOnPath < prev.positionOnPath ? curr : prev))
: null;
};

// Function to get the start and end positions of each conflict
// eslint-disable-next-line import/prefer-default-export
export const processConflicts = (
conflicts: Conflict[],
pathProperties: PathProperties
pathProperties: ManageTrainSchedulePathProperties
): ConflictWithInterval[] =>
conflicts.reduce<ConflictWithInterval[]>((acc, conflict) => {
// Extract zones from conflict requirements
Expand All @@ -57,11 +61,11 @@ export const processConflicts = (

// Find the closest operational point before and after the conflict
const waypointBefore =
findClosestOperationalPoint(pathProperties.operational_points, start_position, 'before')
?.extensions?.identifier?.name ?? null; // Extract only the ID of the operational point before
findClosestOperationalPoint(pathProperties.operationalPoints, start_position, 'before')
?.name ?? null; // Extract only the ID of the operational point before
const waypointAfter =
findClosestOperationalPoint(pathProperties.operational_points, end_position, 'after')
?.extensions?.identifier?.name ?? null; // Extract only the ID of the operational point after
findClosestOperationalPoint(pathProperties.operationalPoints, end_position, 'after')?.name ??
null; // Extract only the ID of the operational point after

acc.push({
trainIds: conflict.train_ids,
Expand Down
1 change: 1 addition & 0 deletions front/src/applications/stdcm/views/StdcmView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ const StdcmView = () => {
showStatusBanner={showStatusBanner}
simulationsList={simulationsList}
pathTrackRanges={stdcmResults?.stdcmResponse.path.track_section_ranges}
pathProperties={pathProperties}
/>
)}
</div>
Expand Down

0 comments on commit bb21d2e

Please sign in to comment.