Skip to content

Commit 78a0f76

Browse files
committed
fixup! editoast: refacto path item location to add track and label information
Signed-off-by: Youness CHRIFI ALAOUI <youness.chrifi@gmail.com>
1 parent 604294d commit 78a0f76

File tree

7 files changed

+101
-29
lines changed

7 files changed

+101
-29
lines changed

editoast/editoast_schemas/src/train_schedule.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub use path_item::OperationalPointIdentifier;
1111
pub use path_item::OperationalPointReference;
1212
pub use path_item::PathItem;
1313
pub use path_item::PathItemLocation;
14+
pub use path_item::TrackReference;
1415

1516
mod train_schedule_options;
1617
pub use train_schedule_options::TrainScheduleOptions;

editoast/editoast_schemas/src/train_schedule/path_item.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ editoast_common::schemas! {
1010
PathItem,
1111
PathItemLocation,
1212
OperationalPointReference,
13+
TrackReference,
1314
}
1415

1516
/// A location on the path of a train
@@ -41,11 +42,20 @@ pub struct OperationalPointReference {
4142
#[serde(flatten)]
4243
#[schema(inline)]
4344
pub reference: OperationalPointIdentifier,
44-
#[schema(inline)]
45-
#[serde(default)]
46-
pub track_id: Option<Identifier>,
4745
#[serde(default)]
48-
pub track_label: Option<String>,
46+
pub track_reference: Option<TrackReference>,
47+
}
48+
49+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema, Hash)]
50+
#[serde(untagged, deny_unknown_fields)]
51+
pub enum TrackReference {
52+
Id {
53+
#[schema(inline)]
54+
track_id: Identifier,
55+
},
56+
Label {
57+
track_label: NonBlankString,
58+
},
4959
}
5060

5161
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema, Hash)]

editoast/editoast_schemas/src/train_schedule/train_schedule_base.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,7 @@ mod tests {
186186
reference: OperationalPointId {
187187
operational_point: "op".into(),
188188
},
189-
track_id: None,
190-
track_label: None,
189+
track_reference: None,
191190
});
192191
let path_item = PathItem {
193192
id: "a".into(),
@@ -245,8 +244,7 @@ mod tests {
245244
reference: OperationalPointId {
246245
operational_point: "op".into(),
247246
},
248-
track_id: None,
249-
track_label: None,
247+
track_reference: None,
250248
});
251249
let path_item = PathItem {
252250
id: "a".into(),
@@ -284,8 +282,7 @@ mod tests {
284282
reference: OperationalPointId {
285283
operational_point: "op".into(),
286284
},
287-
track_id: None,
288-
track_label: None,
285+
track_reference: None,
289286
});
290287
let path_item = PathItem {
291288
id: "a".into(),

editoast/openapi.yaml

+18-7
Original file line numberDiff line numberDiff line change
@@ -7110,14 +7110,9 @@ components:
71107110
minimum: 0
71117111
- type: object
71127112
properties:
7113-
track_id:
7113+
track_reference:
71147114
allOf:
7115-
- type: string
7116-
maxLength: 255
7117-
minLength: 1
7118-
nullable: true
7119-
track_label:
7120-
type: string
7115+
- $ref: '#/components/schemas/TrackReference'
71217116
nullable: true
71227117
Ordering:
71237118
type: string
@@ -10493,6 +10488,22 @@ components:
1049310488
type: string
1049410489
maxLength: 255
1049510490
minLength: 1
10491+
TrackReference:
10492+
oneOf:
10493+
- type: object
10494+
required:
10495+
- track_id
10496+
properties:
10497+
track_id:
10498+
type: string
10499+
maxLength: 255
10500+
minLength: 1
10501+
- type: object
10502+
required:
10503+
- track_label
10504+
properties:
10505+
track_label:
10506+
type: string
1049610507
TrackSection:
1049710508
type: object
1049810509
required:

editoast/src/views/path/path_item_cache.rs

+56-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use crate::error::Result;
44
use crate::models::TrackSectionModel;
55
use crate::RetrieveBatchUnchecked;
66
use editoast_schemas::infra::TrackOffset;
7+
use editoast_schemas::primitives::NonBlankString;
78
use editoast_schemas::train_schedule::OperationalPointReference;
9+
use editoast_schemas::train_schedule::TrackReference;
810
use std::collections::HashMap;
911
use std::collections::HashSet;
1012

@@ -25,6 +27,7 @@ pub struct PathItemCache {
2527
trigram_to_ops: HashMap<String, Vec<OperationalPointModel>>,
2628
ids_to_ops: HashMap<String, OperationalPointModel>,
2729
existing_track_ids: HashSet<String>,
30+
existing_track_labels: HashMap<String, NonBlankString>,
2831
}
2932

3033
impl PathItemCache {
@@ -53,17 +56,30 @@ impl PathItemCache {
5356
.map(|part| (infra_id, part.track.0.clone()))
5457
.chain(tracks);
5558
let existing_track_ids =
56-
TrackSectionModel::retrieve_batch_unchecked::<_, Vec<_>>(conn, tracks)
59+
TrackSectionModel::retrieve_batch_unchecked::<_, Vec<_>>(conn, tracks.clone())
5760
.await?
5861
.into_iter()
5962
.map(|track| track.obj_id)
6063
.collect();
64+
let existing_track_labels =
65+
TrackSectionModel::retrieve_batch_unchecked::<_, Vec<_>>(conn, tracks)
66+
.await?
67+
.into_iter()
68+
.filter_map(|track| {
69+
track
70+
.extensions
71+
.sncf
72+
.as_ref()
73+
.map(|extension| (track.obj_id.clone(), extension.line_name.clone()))
74+
})
75+
.collect();
6176

6277
Ok(PathItemCache {
6378
uic_to_ops,
6479
trigram_to_ops,
6580
ids_to_ops,
6681
existing_track_ids,
82+
existing_track_labels,
6783
})
6884
}
6985

@@ -95,16 +111,18 @@ impl PathItemCache {
95111
let mut result: Vec<Vec<_>> = Vec::default();
96112
let mut invalid_path_items = Vec::new();
97113
for (index, &path_item) in path_items.iter().enumerate() {
98-
dbg!(&path_item);
99114
let track_offsets = match path_item {
100115
PathItemLocation::TrackOffset(track_offset) => {
101116
vec![track_offset.clone()]
102117
}
103118
PathItemLocation::OperationalPointReference(OperationalPointReference {
104119
reference: OperationalPointIdentifier::OperationalPointId { operational_point },
105-
..
120+
track_reference,
106121
}) => match self.get_from_id(&operational_point.0) {
107-
Some(op) => op.track_offset(),
122+
Some(op) => {
123+
let track_offsets = op.track_offset();
124+
self.track_reference_filter(track_offsets, track_reference)
125+
}
108126
None => {
109127
invalid_path_items.push(InvalidPathItem {
110128
index,
@@ -119,7 +137,7 @@ impl PathItemCache {
119137
trigram,
120138
secondary_code,
121139
},
122-
..
140+
track_reference,
123141
}) => {
124142
let ops = self
125143
.get_from_trigram(&trigram.0)
@@ -133,15 +151,16 @@ impl PathItemCache {
133151
});
134152
continue;
135153
}
136-
track_offsets_from_ops(&ops)
154+
let track_offsets = track_offsets_from_ops(&ops);
155+
self.track_reference_filter(track_offsets, track_reference)
137156
}
138157
PathItemLocation::OperationalPointReference(OperationalPointReference {
139158
reference:
140159
OperationalPointIdentifier::OperationalPointUic {
141160
uic,
142161
secondary_code,
143162
},
144-
..
163+
track_reference,
145164
}) => {
146165
let ops = self
147166
.get_from_uic(i64::from(*uic))
@@ -155,7 +174,8 @@ impl PathItemCache {
155174
});
156175
continue;
157176
}
158-
track_offsets_from_ops(&ops)
177+
let track_offsets = track_offsets_from_ops(&ops);
178+
self.track_reference_filter(track_offsets, track_reference)
159179
}
160180
};
161181

@@ -185,6 +205,34 @@ impl PathItemCache {
185205

186206
Ok(result)
187207
}
208+
209+
/// Filter operational points parts by a track label or a track id
210+
/// If neither a track label or id is provided, the original list is returned
211+
fn track_reference_filter(
212+
&self,
213+
track_offsets: Vec<TrackOffset>,
214+
track_reference: &Option<TrackReference>,
215+
) -> Vec<TrackOffset> {
216+
if let Some(track_reference) = track_reference {
217+
match track_reference {
218+
TrackReference::Id { track_id } => track_offsets
219+
.into_iter()
220+
.filter(|track_offset| &track_offset.track == track_id)
221+
.collect(),
222+
TrackReference::Label { track_label } => track_offsets
223+
.into_iter()
224+
.filter(|track_offset| {
225+
self.existing_track_labels
226+
.get::<String>(&track_offset.track)
227+
.unwrap()
228+
== track_label
229+
})
230+
.collect(),
231+
}
232+
} else {
233+
track_offsets
234+
}
235+
}
188236
}
189237

190238
/// Collect the ids of the operational points from the path items

editoast/src/views/path/pathfinding.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,7 @@ pub mod tests {
484484
trigram: "NO_TRIGRAM".into(),
485485
secondary_code: None
486486
},
487-
track_id: None,
488-
track_label: None
487+
track_reference: None,
489488
}
490489
)
491490
}]

front/src/common/api/generatedEditoastApi.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -2421,6 +2421,13 @@ export type TrackOffset = {
24212421
offset: number;
24222422
track: string;
24232423
};
2424+
export type TrackReference =
2425+
| {
2426+
track_id: string;
2427+
}
2428+
| {
2429+
track_label: string;
2430+
};
24242431
export type OperationalPointReference = (
24252432
| {
24262433
operational_point: string;
@@ -2437,8 +2444,7 @@ export type OperationalPointReference = (
24372444
uic: number;
24382445
}
24392446
) & {
2440-
track_id?: string | null;
2441-
track_label?: string | null;
2447+
track_reference?: TrackReference | null;
24422448
};
24432449
export type PathItemLocation = TrackOffset | OperationalPointReference;
24442450
export type PathfindingInputError =

0 commit comments

Comments
 (0)