Skip to content

Commit

Permalink
use number of seconds since departure in store
Browse files Browse the repository at this point in the history
  • Loading branch information
anisometropie committed Aug 30, 2024
1 parent 802ee9a commit 0fa1f84
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,12 @@ import {
import { useOsrdConfActions, useOsrdConfSelectors } from 'common/osrdContext';
import { formatSuggestedOperationalPoints, upsertPathStepsInOPs } from 'modules/pathfinding/utils';
import { getSupportedElectrification, isThermal } from 'modules/rollingStock/helpers/electric';
import { updateDaySinceDeparture } from 'modules/timesStops/helpers/utils';
import { adjustConfWithTrainToModifyV2 } from 'modules/trainschedule/components/ManageTrainSchedule/helpers/adjustConfWithTrainToModify';
import type {
SuggestedOP,
TimeExtraDays,
} from 'modules/trainschedule/components/ManageTrainSchedule/types';
import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSchedule/types';
import { setFailure } from 'reducers/main';
import type { OperationalStudiesConfSliceActions } from 'reducers/osrdconf/operationalStudiesConf';
import type { PathStep } from 'reducers/osrdconf/types';
import { useAppDispatch } from 'store';
import { addDurationToIsoDate, extractHHMMSS } from 'utils/date';
import { castErrorToFailure } from 'utils/error';
import { getPointCoordinates } from 'utils/geometry';
import { mmToM } from 'utils/physics';
Expand All @@ -42,8 +37,8 @@ type ItineraryForTrainUpdate = {
/**
* create pathSteps in the case pathfinding fails or the train is imported from NGE
*/
const computeBasePathSteps = (trainSchedule: TrainScheduleResult) => {
const basePathSteps = trainSchedule.path.map((step) => {
const computeBasePathSteps = (trainSchedule: TrainScheduleResult) =>
trainSchedule.path.map((step) => {
const correspondingSchedule = trainSchedule.schedule?.find(
(schedule) => schedule.at === step.id
);
Expand Down Expand Up @@ -71,26 +66,18 @@ const computeBasePathSteps = (trainSchedule: TrainScheduleResult) => {
name = step.operational_point;
}

const fomattedArrival: TimeExtraDays | undefined = arrival
? { time: extractHHMMSS(addDurationToIsoDate(trainSchedule.start_time, arrival)) }
: undefined;
// NOTE: arrival was the time "HH:MM:SS"
// addDurationToIsoDate(trainSchedule.start_time, arrival).substring(11, 19)
return {
...stepWithoutSecondaryCode,
ch: 'secondary_code' in step ? step.secondary_code : undefined,
name,
arrival: fomattedArrival,
arrival,
stopFor: stopFor ? ISO8601Duration2sec(stopFor).toString() : stopFor,
locked,
onStopSignal,
} as PathStep;
});
// console.log(
// 'computeBasePathSteps',
// basePathSteps,
// updateDaySinceDeparture(basePathSteps, trainSchedule.start_time)
// );
return updateDaySinceDeparture(basePathSteps, trainSchedule.start_time);
};

export function updatePathStepsFromOperationalPoints(
pathSteps: PathStep[],
Expand Down Expand Up @@ -271,7 +258,6 @@ const useSetupItineraryForTrainUpdate = (
dispatch(setFailure(castErrorToFailure(e)));
}
}

adjustConfWithTrainToModifyV2(
trainSchedule,
pathSteps || computeBasePathSteps(trainSchedule),
Expand Down
6 changes: 6 additions & 0 deletions front/src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,9 @@ export type RangedValue = {
end: number;
value: string;
};

/**
* A ISO 8601 duration string
* @example "PT3600S"
*/
export type IsoDurationString = string;
2 changes: 1 addition & 1 deletion front/src/modules/timesStops/ReadOnlyTime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';

import type { CellProps } from 'react-datasheet-grid/dist/types';

import type { TimeExtraDays } from 'modules/trainschedule/components/ManageTrainSchedule/types';
import type { TimeExtraDays } from './types';

type ReadOnlyTimeProps = CellProps<TimeExtraDays | null | undefined, string>;

Expand Down
2 changes: 1 addition & 1 deletion front/src/modules/timesStops/TimeInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useRef, useState, useEffect } from 'react';

import type { CellProps } from 'react-datasheet-grid/dist/types';

import type { TimeExtraDays } from 'modules/trainschedule/components/ManageTrainSchedule/types';
import type { TimeExtraDays } from './types';

type TimeInputProps = CellProps<TimeExtraDays | null | undefined, string>;

Expand Down
8 changes: 4 additions & 4 deletions front/src/modules/timesStops/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export function updateRowTimesAndMargin(
* This function goes through the whole array of path waypoints
* and updates the number of days since departure.
*/
export function updateDaySinceDeparture<T extends Pick<SuggestedOP, 'arrival' | 'stopFor'>>(
export function updateDaySinceDeparture<T extends Pick<PathWaypointRow, 'arrival' | 'stopFor'>>(
pathWaypointRows: T[],
startTime?: IsoDateTimeString,
keepFirstIndexArrival?: boolean,
Expand All @@ -194,7 +194,7 @@ export function updateDaySinceDeparture<T extends Pick<SuggestedOP, 'arrival' |
const { arrival, stopFor } = pathWaypoint;

const arrivalInSeconds = arrival?.time ? time2sec(arrival.time) : null;
let formattedArrival: TimeExtraDays | null = null;
let formattedArrival: TimeExtraDays | undefined;
if (arrivalInSeconds) {
if (arrivalInSeconds < previousTime) {
currentDaySinceDeparture += 1;
Expand All @@ -212,7 +212,7 @@ export function updateDaySinceDeparture<T extends Pick<SuggestedOP, 'arrival' |
previousTime = arrivalInSeconds;
}

let formattedDeparture: TimeExtraDays | null = null;
let formattedDeparture: TimeExtraDays | undefined;
if (withDeparture && stopFor && arrivalInSeconds) {
const departureInSeconds = (arrivalInSeconds + Number(stopFor)) % SECONDS_IN_A_DAY;
const isAfterMidnight = departureInSeconds < previousTime;
Expand All @@ -234,7 +234,7 @@ export function updateDaySinceDeparture<T extends Pick<SuggestedOP, 'arrival' |

return {
...pathWaypoint,
arrival: keepFirstIndexArrival || index > 0 ? formattedArrival : null,
arrival: keepFirstIndexArrival || index > 0 ? formattedArrival : undefined,
...(withDeparture && { departure: formattedDeparture }),
};
});
Expand Down
5 changes: 4 additions & 1 deletion front/src/modules/timesStops/hooks/useOutputTableData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ function useOutputTableData(
} as SuggestedOP;
}

return { ...sugOpPoint, calculatedArrival: secToHoursString(opPoint.time, true) };
return {
...sugOpPoint,
calculatedArrival: secToHoursString(opPoint.time, true),
};
}),
[simulatedTrain, pathProperties, operationalPoints, selectedTrainSchedule, pathLength]
);
Expand Down
7 changes: 2 additions & 5 deletions front/src/modules/timesStops/hooks/useTimeStopsColumns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ import { keyColumn, type Column, checkboxColumn, createTextColumn } from 'react-
import type { CellComponent } from 'react-datasheet-grid/dist/types';
import { useTranslation } from 'react-i18next';

import type {
SuggestedOP,
TimeExtraDays,
} from 'modules/trainschedule/components/ManageTrainSchedule/types';
import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSchedule/types';

import { marginRegExValidation } from '../consts';
import { disabledTextColumn } from '../helpers/utils';
import ReadOnlyTime from '../ReadOnlyTime';
import TimeInput from '../TimeInput';
import { TableType, type PathWaypointRow } from '../types';
import { TableType, type PathWaypointRow, type TimeExtraDays } from '../types';

const timeColumn = (isOutputTable: boolean) =>
({
Expand Down
10 changes: 9 additions & 1 deletion front/src/modules/timesStops/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@ import type { TrainScheduleBase, TrainScheduleResult } from 'common/api/osrdEdit
import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSchedule/types';
import type { ArrayElement } from 'utils/types';

export type PathWaypointRow = SuggestedOP & {
export type TimeExtraDays = {
time: string | null | undefined;
daySinceDeparture?: number;
dayDisplayed?: boolean;
};

export type PathWaypointRow = Omit<SuggestedOP, 'arrival' | 'departure'> & {
isMarginValid: boolean;
arrival?: TimeExtraDays | null; // value asked by user
departure?: TimeExtraDays | null; // value asked by user
};

export enum TableType {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import type { Position } from 'geojson';

import type { TrainScheduleBase } from 'common/api/osrdEditoastApi';
import type { IsoDurationString } from 'common/types';

export type TimePlusDay = {
time: string;
day: string | undefined;
};

export type TimeExtraDays = {
time: string | null | undefined;
daySinceDeparture?: number;
dayDisplayed?: boolean;
};

export type SuggestedOP = {
opId: string;
name?: string;
Expand All @@ -32,8 +27,8 @@ export type SuggestedOP = {
It's useful for soft deleting the point (waiting to fix / remove all references)
If true, the train schedule is consider as invalid and must be edited */
deleted?: boolean;
arrival?: TimeExtraDays | null; // value asked by user
departure?: TimeExtraDays | null; // value asked by user
arrival?: IsoDurationString | null; // value asked by user
departure?: IsoDurationString | null; // value asked by user
locked?: boolean;
stopFor?: string | null; // value asked by user
theoreticalMargin?: string; // value asked by user
Expand Down
4 changes: 2 additions & 2 deletions front/src/reducers/osrdconf/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { PowerRestrictionV2 } from 'applications/operationalStudies/types';
import type { AllowanceValue } from 'applications/stdcm/types';
import type { ArrivalTimeTypes } from 'applications/stdcmV2/types';
import type { Comfort, Distribution, PathItemLocation } from 'common/api/osrdEditoastApi';
import type { TimeExtraDays } from 'modules/trainschedule/components/ManageTrainSchedule/types';
import type { IsoDurationString } from 'common/types';
import type { InfraState } from 'reducers/infra';

export interface OsrdConfState extends InfraState {
Expand Down Expand Up @@ -56,7 +56,7 @@ export type PathStep = PathItemLocation & {
It's useful for soft deleting the point (waiting to fix / remove all references)
If true, the train schedule is consider as invalid and must be edited */
deleted?: boolean;
arrival?: TimeExtraDays | null;
arrival?: IsoDurationString | null;
arrivalType?: ArrivalTimeTypes;
arrivalToleranceBefore?: number;
arrivalToleranceAfter?: number;
Expand Down

0 comments on commit 0fa1f84

Please sign in to comment.