Skip to content

Commit

Permalink
front: refacto create consts file + remove regex for util function
Browse files Browse the repository at this point in the history
Signed-off-by: Louis Greiner <[email protected]>
  • Loading branch information
louisgreiner committed Feb 4, 2025
1 parent ad63e98 commit c9c33f8
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 120 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import type {
LabelGroupDto,
NetzgrafikDto,
TimeLockDto,
TrainrunCategory,
TrainrunFrequency,
TrainrunTimeCategory,
} from '../NGE/types';

export const TRAINRUN_CATEGORY_HALTEZEITEN = {
HaltezeitIPV: { haltezeit: 0, no_halt: false },
HaltezeitA: { haltezeit: 0, no_halt: false },
HaltezeitB: { haltezeit: 0, no_halt: false },
HaltezeitC: { haltezeit: 0, no_halt: false },
HaltezeitD: { haltezeit: 0, no_halt: false },
HaltezeitUncategorized: { haltezeit: 0, no_halt: false },
};

export const TRAINRUN_LABEL_GROUP: LabelGroupDto = {
id: 1,
name: 'Default',
labelRef: 'Trainrun',
};
export const NODE_LABEL_GROUP: LabelGroupDto = {
id: 2,
name: 'Node',
labelRef: 'Node',
};

export const DEFAULT_TRAINRUN_CATEGORY: TrainrunCategory = {
id: 1, // In NGE, Trainrun.DEFAULT_TRAINRUN_CATEGORY
order: 0,
name: 'Default',
shortName: '', // TODO: find a better way to hide this in the graph
fachCategory: 'HaltezeitUncategorized',
colorRef: 'EC',
minimalTurnaroundTime: 0,
nodeHeadwayStop: 0,
nodeHeadwayNonStop: 0,
sectionHeadway: 0,
};

export const DEFAULT_TRAINRUN_FREQUENCIES: TrainrunFrequency[] = [
{
id: 2,
order: 0,
frequency: 30,
offset: 0,
name: 'Half-hourly',
shortName: '30',
linePatternRef: '30',
},
{
id: 3, // default NGE frequency takes id 3
order: 1,
frequency: 60,
offset: 0,
name: 'Hourly',
/** Short name, needs to be unique */
shortName: '60',
linePatternRef: '60',
},
{
id: 4,
order: 2,
frequency: 120,
offset: 0,
name: 'Two-hourly',
shortName: '120',
linePatternRef: '120',
},
];

export const DEFAULT_TRAINRUN_FREQUENCY: TrainrunFrequency = DEFAULT_TRAINRUN_FREQUENCIES[1];

export const DEFAULT_TRAINRUN_TIME_CATEGORY: TrainrunTimeCategory = {
id: 0, // In NGE, Trainrun.DEFAULT_TRAINRUN_TIME_CATEGORY
order: 0,
name: 'Default',
shortName: '7/24',
dayTimeInterval: [],
weekday: [1, 2, 3, 4, 5, 6, 7],
linePatternRef: '7/24',
};

export const DEFAULT_DTO: NetzgrafikDto = {
resources: [],
nodes: [],
trainruns: [],
trainrunSections: [],
metadata: {
netzgrafikColors: [],
trainrunCategories: [DEFAULT_TRAINRUN_CATEGORY],
trainrunFrequencies: [...DEFAULT_TRAINRUN_FREQUENCIES],
trainrunTimeCategories: [DEFAULT_TRAINRUN_TIME_CATEGORY],
},
freeFloatingTexts: [],
labels: [],
labelGroups: [],
filterData: {
filterSettings: [],
},
};

export const DEFAULT_TIME_LOCK: TimeLockDto = {
time: null,
consecutiveTime: null,
lock: false,
warning: null,
timeFormatter: null,
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ import type { AppDispatch } from 'store';
import { formatToIsoDate } from 'utils/date';
import { Duration } from 'utils/duration';

import { DEFAULT_TRAINRUN_FREQUENCIES, DEFAULT_TRAINRUN_FREQUENCY } from './consts';
import type MacroEditorState from './MacroEditorState';
import type { NodeIndexed } from './MacroEditorState';
import { DEFAULT_TRAINRUN_FREQUENCIES, DEFAULT_TRAINRUN_FREQUENCY } from './osrdToNge';
import { createMacroNode, deleteMacroNodeByNgeId, updateMacroNode } from './utils';
import {
createMacroNode,
deleteMacroNodeByNgeId,
trainrunFrequencyFromLabel,
updateMacroNode,
} from './utils';
import type {
NetzgrafikDto,
NGEEvent,
Expand Down Expand Up @@ -209,7 +214,7 @@ const createTrainSchedulePayload = async ({
}

const trainScheduleLabels =
trainSchedule?.labels?.filter((label) => label.match(/^frequency::(?!30$|60$|120$)\d+$/)) || [];
trainSchedule?.labels?.filter((label) => trainrunFrequencyFromLabel(label) !== null) || [];

trainrunLabels = uniq([...trainrunLabels, ...trainScheduleLabels]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,124 +6,29 @@ import buildOpSearchQuery from 'modules/operationalPoint/helpers/buildOpSearchQu
import type { AppDispatch } from 'store';
import { Duration, addDurationToDate } from 'utils/duration';

import {
TRAINRUN_CATEGORY_HALTEZEITEN,
NODE_LABEL_GROUP,
DEFAULT_TRAINRUN_FREQUENCIES,
DEFAULT_TRAINRUN_CATEGORY,
DEFAULT_TRAINRUN_FREQUENCY,
DEFAULT_TRAINRUN_TIME_CATEGORY,
TRAINRUN_LABEL_GROUP,
DEFAULT_TIME_LOCK,
DEFAULT_DTO,
} from './consts';
import MacroEditorState, { type NodeIndexed } from './MacroEditorState';
import { deleteMacroNodeByDbId, getSavedMacroNodes } from './utils';
import { deleteMacroNodeByDbId, getSavedMacroNodes, trainrunFrequencyFromLabel } from './utils';
import {
type PortDto,
type TimeLockDto,
type TrainrunSectionDto,
type TrainrunCategory,
type TrainrunTimeCategory,
type TrainrunFrequency,
type NetzgrafikDto,
type LabelGroupDto,
PortAlignment,
type LabelDto,
} from '../NGE/types';

const TRAINRUN_CATEGORY_HALTEZEITEN = {
HaltezeitIPV: { haltezeit: 0, no_halt: false },
HaltezeitA: { haltezeit: 0, no_halt: false },
HaltezeitB: { haltezeit: 0, no_halt: false },
HaltezeitC: { haltezeit: 0, no_halt: false },
HaltezeitD: { haltezeit: 0, no_halt: false },
HaltezeitUncategorized: { haltezeit: 0, no_halt: false },
};

const TRAINRUN_LABEL_GROUP: LabelGroupDto = {
id: 1,
name: 'Default',
labelRef: 'Trainrun',
};
const NODE_LABEL_GROUP: LabelGroupDto = {
id: 2,
name: 'Node',
labelRef: 'Node',
};

const DEFAULT_TRAINRUN_CATEGORY: TrainrunCategory = {
id: 1, // In NGE, Trainrun.DEFAULT_TRAINRUN_CATEGORY
order: 0,
name: 'Default',
shortName: '', // TODO: find a better way to hide this in the graph
fachCategory: 'HaltezeitUncategorized',
colorRef: 'EC',
minimalTurnaroundTime: 0,
nodeHeadwayStop: 0,
nodeHeadwayNonStop: 0,
sectionHeadway: 0,
};

export const DEFAULT_TRAINRUN_FREQUENCIES: TrainrunFrequency[] = [
{
id: 2,
order: 0,
frequency: 30,
offset: 0,
name: 'Half-hourly',
shortName: '30',
linePatternRef: '30',
},
{
id: 3, // default NGE frequency takes id 3
order: 1,
frequency: 60,
offset: 0,
name: 'Hourly',
/** Short name, needs to be unique */
shortName: '60',
linePatternRef: '60',
},
{
id: 4,
order: 2,
frequency: 120,
offset: 0,
name: 'Two-hourly',
shortName: '120',
linePatternRef: '120',
},
];

export const DEFAULT_TRAINRUN_FREQUENCY: TrainrunFrequency = DEFAULT_TRAINRUN_FREQUENCIES[1];

const DEFAULT_TRAINRUN_TIME_CATEGORY: TrainrunTimeCategory = {
id: 0, // In NGE, Trainrun.DEFAULT_TRAINRUN_TIME_CATEGORY
order: 0,
name: 'Default',
shortName: '7/24',
dayTimeInterval: [],
weekday: [1, 2, 3, 4, 5, 6, 7],
linePatternRef: '7/24',
};

const DEFAULT_DTO: NetzgrafikDto = {
resources: [],
nodes: [],
trainruns: [],
trainrunSections: [],
metadata: {
netzgrafikColors: [],
trainrunCategories: [DEFAULT_TRAINRUN_CATEGORY],
trainrunFrequencies: [...DEFAULT_TRAINRUN_FREQUENCIES],
trainrunTimeCategories: [DEFAULT_TRAINRUN_TIME_CATEGORY],
},
freeFloatingTexts: [],
labels: [],
labelGroups: [],
filterData: {
filterSettings: [],
},
};

const DEFAULT_TIME_LOCK: TimeLockDto = {
time: null,
consecutiveTime: null,
lock: false,
warning: null,
timeFormatter: null,
};

/**
* Execute the search payload and collect all result pages.
*/
Expand Down Expand Up @@ -225,16 +130,6 @@ const castNodeToNge = (
),
});

/**
* Match a frequency label to a NGE TrainrunFrequency, or `null` if not handled.
*/
const trainrunFrequencyFromLabel = (label: string) => {
if (!label.startsWith('frequency::')) return null;
const n = parseInt(label.split('::', 2)[1], 10);
const frequency = DEFAULT_TRAINRUN_FREQUENCIES.find((freq) => freq.frequency === n);
return frequency ?? null;
};

/**
* NGE trainrun frequency is stored as OSRD labels (`"frequency::30"` or `"frequency::120"`).
* Update the current frequency if the new frequency is smaller.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from 'common/api/osrdEditoastApi';
import type { AppDispatch } from 'store';

import { DEFAULT_TRAINRUN_FREQUENCIES } from './consts';
import type MacroEditorState from './MacroEditorState';
import type { NodeIndexed } from './MacroEditorState';

Expand Down Expand Up @@ -161,3 +162,13 @@ export const getSavedMacroNodes = async (
}
return result;
};

/**
* Match a frequency label to a NGE TrainrunFrequency, or `null` if not handled.
*/
export const trainrunFrequencyFromLabel = (label: string) => {
if (!label.startsWith('frequency::')) return null;
const n = parseInt(label.split('::', 2)[1], 10);
const frequency = DEFAULT_TRAINRUN_FREQUENCIES.find((freq) => freq.frequency === n);
return frequency ?? null;
};

0 comments on commit c9c33f8

Please sign in to comment.