Skip to content

Commit 6604760

Browse files
committed
fixup! front: refacto useOutputData
1 parent d47c3e1 commit 6604760

File tree

10 files changed

+105
-206
lines changed

10 files changed

+105
-206
lines changed

front/src/applications/operationalStudies/views/SimulationResults.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,12 @@ const SimulationResults = ({
209209
</div>
210210

211211
{/* TIME STOPS TABLE */}
212-
213212
<div className="time-stop-outputs">
214213
<p className="mt-2 mb-3 ml-3 font-weight-bold">{t('timetableOutput')}</p>
215214
<TimesStopsOutput
216215
simulatedTrain={trainSimulation}
217216
trainSummary={selectedTrainSummary}
218-
pathProperties={pathProperties}
217+
operationalPoints={pathProperties?.operationalPoints}
219218
selectedTrainSchedule={selectedTrainSchedule}
220219
path={path}
221220
dataIsLoading={formattedOpPointsLoading}

front/src/modules/timesStops/TimesStopsOutput.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { TableType, type TimeStopsRow } from './types';
1616
type TimesStopsOutputProps = {
1717
simulatedTrain: SimulationResponseSuccess;
1818
trainSummary?: TrainScheduleWithDetails;
19-
pathProperties?: PathPropertiesFormatted;
19+
operationalPoints?: PathPropertiesFormatted['operationalPoints'];
2020
selectedTrainSchedule?: TrainScheduleResult;
2121
path?: PathfindingResultSuccess;
2222
dataIsLoading: boolean;
@@ -25,20 +25,20 @@ type TimesStopsOutputProps = {
2525
const TimesStopsOutput = ({
2626
simulatedTrain,
2727
trainSummary,
28-
pathProperties,
28+
operationalPoints,
2929
selectedTrainSchedule,
3030
path,
3131
dataIsLoading,
3232
}: TimesStopsOutputProps) => {
3333
const enrichedOperationalPoints = useOutputTableData(
3434
simulatedTrain,
3535
trainSummary,
36-
pathProperties,
36+
operationalPoints,
3737
selectedTrainSchedule,
3838
path
3939
);
4040

41-
if (dataIsLoading || !trainSummary || !pathProperties || !selectedTrainSchedule) {
41+
if (dataIsLoading || !trainSummary || !operationalPoints || !selectedTrainSchedule) {
4242
return (
4343
<div style={{ height: '600px' }}>
4444
<Loader />

front/src/modules/timesStops/helpers/__tests__/computeMargins.spec.ts

-124
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { describe, it, expect } from 'vitest';
33
import type { TrainScheduleResult } from 'common/api/osrdEditoastApi';
44

55
import computeMargins from '../computeMargins';
6-
import { findNextScheduledOpPoint } from '../utils';
76

87
describe('computeMargins', () => {
98
const path = [
@@ -49,126 +48,3 @@ describe('computeMargins', () => {
4948
});
5049
});
5150
});
52-
53-
describe('findNextScheduledOpPoint', () => {
54-
const operationalPoints = [
55-
{
56-
id: 'd9c92cb4-tsrn-x',
57-
name: 'Gondor',
58-
time: 1715646000,
59-
speed: 0,
60-
duration: 0,
61-
position: 0,
62-
line_code: 78945612,
63-
track_number: 9213,
64-
line_name: 'line α',
65-
track_name: 'A',
66-
ch: 'hl',
67-
},
68-
{
69-
id: 'd9b38600-tsrn-x',
70-
name: 'Gondor',
71-
time: 1715646048.531712,
72-
speed: 12.867353322128086,
73-
duration: 0,
74-
position: 438,
75-
line_code: 78945612,
76-
track_number: 9288,
77-
line_name: 'line α',
78-
track_name: 'J1300',
79-
ch: 't',
80-
},
81-
{
82-
id: 'd94a2af4-tsrn-x',
83-
name: 'Gondor',
84-
time: 1715646059.5107863,
85-
speed: 13.801572793437755,
86-
duration: 0,
87-
position: 586,
88-
line_code: 78945612,
89-
track_number: 7815,
90-
line_name: 'line α',
91-
track_name: 'track_1',
92-
ch: 'P2',
93-
},
94-
{
95-
id: 'd9cdd03e-tsrn-x',
96-
name: 'Gondor',
97-
time: 1715646219.668,
98-
speed: 18.64034770551918,
99-
duration: 0,
100-
position: 2791,
101-
line_code: 78945612,
102-
track_number: 940,
103-
line_name: 'line α',
104-
track_name: 'a',
105-
ch: 'PB',
106-
},
107-
{
108-
id: '36239402-c97c-11e7-y',
109-
name: 'Gondor',
110-
time: 1715646254.8250234,
111-
speed: 14.091773148623844,
112-
duration: 0,
113-
position: 3308,
114-
line_code: 78945612,
115-
track_number: 940,
116-
line_name: 'line α',
117-
track_name: 'a',
118-
ch: '3M',
119-
},
120-
{
121-
id: 'd9c4227a-tsrn-x',
122-
name: 'Gondor',
123-
time: 1715646279.9558847,
124-
speed: 13.990647751771307,
125-
duration: 0,
126-
position: 3663,
127-
line_code: 78945612,
128-
track_number: 7815,
129-
line_name: 'line α',
130-
track_name: 'track_1',
131-
ch: 'P1',
132-
},
133-
{
134-
id: 'd982df3e-tsrn-x',
135-
name: 'Mordor',
136-
time: 1715646528.955,
137-
speed: 0,
138-
duration: 0,
139-
position: 6288,
140-
line_code: 78945612,
141-
track_number: 940,
142-
line_name: 'line α',
143-
track_name: 'a',
144-
ch: 'hl',
145-
},
146-
];
147-
const pathStepsWithOpPointIndex = [
148-
{
149-
id: 'id1087',
150-
deleted: false,
151-
uic: 78,
152-
secondary_code: 'hl',
153-
correspondingOpPointIndex: 0,
154-
},
155-
{
156-
id: 'id461',
157-
deleted: false,
158-
uic: 78,
159-
secondary_code: 'PB',
160-
correspondingOpPointIndex: 3,
161-
},
162-
{
163-
id: 'id1088',
164-
deleted: false,
165-
uic: 456,
166-
secondary_code: 'hl',
167-
correspondingOpPointIndex: 6,
168-
},
169-
];
170-
it('should find the next scheduled pathstep', () => {
171-
const result = findNextScheduledOpPoint(operationalPoints, pathStepsWithOpPointIndex, 3);
172-
expect(result).toBe(operationalPoints[6]);
173-
});
174-
});
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable import/prefer-default-export */
22
import { dateToHHMM } from 'utils/date';
3-
import { ISO8601Duration2sec } from 'utils/timeManipulation';
3+
import { ISO8601Duration2sec, calculateTimeDifferenceInDays } from 'utils/timeManipulation';
44

55
import type { ScheduleEntry, TimeExtraDays } from '../types';
66

@@ -15,8 +15,7 @@ const computeDayTimeFromStartTime = (
1515

1616
const timeExtraDay = {
1717
time: dateToHHMM(arrivalDatetime, { withSeconds: true }),
18-
daySinceDeparture:
19-
Math.abs(arrivalDatetime.getTime() - startDatetime.getTime()) / (1000 * 60 * 60 * 24),
18+
daySinceDeparture: calculateTimeDifferenceInDays(startDatetime, arrivalDatetime),
2019
dayDisplayed: isAfterMidnight,
2120
};
2221

@@ -25,34 +24,40 @@ const computeDayTimeFromStartTime = (
2524

2625
export const computeInputDatetimes = (
2726
startDatetime: Date,
28-
previousDatetime: Date,
29-
schedule: ScheduleEntry,
27+
lastReferenceDate: Date,
28+
schedule: ScheduleEntry | undefined,
3029
{ isDeparture }: { isDeparture: boolean }
3130
) => {
31+
let theoriticalArrival: Date | undefined;
3232
let arrival: TimeExtraDays | undefined;
3333
let departure: TimeExtraDays | undefined;
34-
let prev = previousDatetime;
34+
let refDate = lastReferenceDate;
35+
36+
let arrivalInSeconds;
37+
// if is departure, use the startDatetime
38+
if (isDeparture) {
39+
arrivalInSeconds = 0;
40+
} else if (schedule?.arrival) {
41+
arrivalInSeconds = ISO8601Duration2sec(schedule.arrival); // duration from startTime
42+
}
3543

36-
if (schedule && schedule.arrival) {
37-
const arrivalInSeconds = ISO8601Duration2sec(schedule.arrival); // duration from startTime
38-
const result = computeDayTimeFromStartTime(startDatetime, arrivalInSeconds, prev);
44+
if (arrivalInSeconds !== undefined) {
45+
theoriticalArrival = new Date(startDatetime.getTime() + arrivalInSeconds * 1000);
46+
const result = computeDayTimeFromStartTime(startDatetime, arrivalInSeconds, refDate);
3947
arrival = result.timeExtraDay;
40-
prev = result.previousTime;
41-
42-
if (schedule.stop_for) {
43-
const departureInSeconds =
44-
ISO8601Duration2sec(schedule.arrival) + ISO8601Duration2sec(schedule.stop_for);
45-
const resultDeparture = computeDayTimeFromStartTime(startDatetime, departureInSeconds, prev);
48+
refDate = result.previousTime;
49+
50+
if (schedule?.stop_for) {
51+
const departureInSeconds = arrivalInSeconds + ISO8601Duration2sec(schedule.stop_for);
52+
const resultDeparture = computeDayTimeFromStartTime(
53+
startDatetime,
54+
departureInSeconds,
55+
refDate
56+
);
4657
departure = resultDeparture.timeExtraDay;
47-
prev = resultDeparture.previousTime;
58+
refDate = resultDeparture.previousTime;
4859
}
4960
}
5061

51-
// if is departure and no arrival is defined, use the startDatetime
52-
if (isDeparture && !arrival) {
53-
const result = computeDayTimeFromStartTime(startDatetime, 0, prev);
54-
arrival = result.timeExtraDay;
55-
}
56-
57-
return { arrival, departure, prev };
62+
return { theoriticalArrival, arrival, departure, refDate };
5863
};

front/src/modules/timesStops/helpers/computeMargins.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,16 @@ function computeMargins(
5050

5151
// find the previous pathStep where margin was defined
5252
let prevIndex = 0;
53-
// eslint-disable-next-line no-plusplus
54-
for (let index = 1; index < pathStepIndex; index++) {
53+
for (let index = 1; index < pathStepIndex; index += 1) {
5554
if (margins.boundaries.includes(path[index].id)) {
5655
prevIndex = index;
5756
}
5857
}
5958

60-
// durations to go from the previous pathStep with margin to current one.
59+
// durations to go from the last pathStep with theorical margin to the next pathStep
6160
// base = no margin
6261
// provisional = margins
63-
// final = margin + requested arrival times
62+
// final = margins + requested arrival times
6463
const { base, provisional, final } = pathItemTimes;
6564
const baseDuration = ms2sec(base[pathStepIndex + 1] - base[prevIndex]);
6665
const provisionalDuration = ms2sec(provisional[pathStepIndex + 1] - provisional[prevIndex]);

front/src/modules/timesStops/helpers/utils.ts

-17
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import type { TFunction } from 'i18next';
44
import { round, isEqual, isNil } from 'lodash';
55
import { keyColumn, createTextColumn } from 'react-datasheet-grid';
66

7-
import type { OperationalPointWithTimeAndSpeed } from 'applications/operationalStudies/types';
87
import type { ReceptionSignal } from 'common/api/osrdEditoastApi';
98
import type { IsoDateTimeString, IsoDurationString, TimeString } from 'common/types';
109
import { matchPathStepAndOp } from 'modules/pathfinding/utils';
@@ -26,7 +25,6 @@ import { marginRegExValidation, MarginUnit } from '../consts';
2625
import {
2726
TableType,
2827
type TimeExtraDays,
29-
type PathStepOpPointCorrespondance,
3028
type TimesStopsInputRow,
3129
} from '../types';
3230

@@ -116,21 +114,6 @@ export const formatSuggestedViasToRowVias = (
116114
});
117115
};
118116

119-
export function findNextScheduledOpPoint(
120-
operationalPoints: OperationalPointWithTimeAndSpeed[],
121-
pathStepsWithOpPointIndex: PathStepOpPointCorrespondance[],
122-
sugOpIndex: number
123-
) {
124-
const pathStepIndex = pathStepsWithOpPointIndex.findIndex(
125-
(pathStep) => pathStep.correspondingOpPointIndex === sugOpIndex
126-
);
127-
if (pathStepIndex >= 0) {
128-
const nextPathStep = pathStepsWithOpPointIndex[pathStepIndex + 1];
129-
return operationalPoints[nextPathStep?.correspondingOpPointIndex];
130-
}
131-
return null;
132-
}
133-
134117
const getDigits = (unit: string | undefined) =>
135118
unit === MarginUnit.second || unit === MarginUnit.percent ? 0 : 1;
136119

0 commit comments

Comments
 (0)