Skip to content

Commit

Permalink
editoast: refacto path item location to add track and label information
Browse files Browse the repository at this point in the history
Signed-off-by: Youness CHRIFI ALAOUI <youness.chrifi@gmail.com>
  • Loading branch information
younesschrifi committed Nov 21, 2024
1 parent 57c96f7 commit 9ba7f2c
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 30 deletions.
1 change: 1 addition & 0 deletions editoast/editoast_schemas/src/train_schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub use schedule_item::ReceptionSignal;
pub use schedule_item::ScheduleItem;

mod path_item;
pub use path_item::OperationalPointIdentifier;
pub use path_item::PathItem;
pub use path_item::PathItemLocation;

Expand Down
10 changes: 10 additions & 0 deletions editoast/editoast_schemas/src/train_schedule/path_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ pub struct PathItem {
#[serde(untagged, deny_unknown_fields)]
pub enum PathItemLocation {
TrackOffset(#[schema(inline)] TrackOffset),
OperationalPointReference {
reference: OperationalPointIdentifier,
track: Option<Identifier>,
track_label: Option<String>,
},
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema, Hash)]
#[serde(untagged, deny_unknown_fields)]
pub enum OperationalPointIdentifier {
OperationalPointId {
/// The object id of an operational point
#[schema(inline)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ mod tests {
use serde_json::from_str;
use serde_json::to_string;

use crate::train_schedule::path_item::OperationalPointIdentifier::OperationalPointId;
use crate::train_schedule::schedule_item::ReceptionSignal;
use crate::train_schedule::Margins;
use crate::train_schedule::PathItemLocation;
Expand All @@ -180,8 +181,12 @@ mod tests {
/// Test deserialize an invalid train schedule
#[test]
fn deserialize_duplicate_path_id_train_schedule() {
let location = PathItemLocation::OperationalPointId {
operational_point: "op".into(),
let location = PathItemLocation::OperationalPointReference {
reference: OperationalPointId {
operational_point: "op".into(),
},
track: None,
track_label: None,
};
let path_item = PathItem {
id: "a".into(),
Expand Down Expand Up @@ -235,8 +240,12 @@ mod tests {
/// Test deserialize an invalid train schedule
#[test]
fn deserialize_duplicate_schedule_points_train_schedule() {
let location = PathItemLocation::OperationalPointId {
operational_point: "op".into(),
let location = PathItemLocation::OperationalPointReference {
reference: OperationalPointId {
operational_point: "op".into(),
},
track: None,
track_label: None,
};
let path_item = PathItem {
id: "a".into(),
Expand Down Expand Up @@ -270,8 +279,12 @@ mod tests {
/// Test deserialize an invalid train schedule
#[test]
fn deserialize_arrival_time_first_waypoint_schedule_train_schedule() {
let location = PathItemLocation::OperationalPointId {
operational_point: "op".into(),
let location = PathItemLocation::OperationalPointReference {
reference: OperationalPointId {
operational_point: "op".into(),
},
track: None,
track_label: None,
};
let path_item = PathItem {
id: "a".into(),
Expand Down
62 changes: 41 additions & 21 deletions editoast/src/views/path/path_item_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::collections::HashMap;
use std::collections::HashSet;

use editoast_models::DbConnection;
use editoast_schemas::train_schedule::OperationalPointIdentifier;
use editoast_schemas::train_schedule::PathItemLocation;

use crate::models::OperationalPointModel;
Expand Down Expand Up @@ -97,21 +98,26 @@ impl PathItemCache {
PathItemLocation::TrackOffset(track_offset) => {
vec![track_offset.clone()]
}
PathItemLocation::OperationalPointId { operational_point } => {
match self.get_from_id(&operational_point.0) {
Some(op) => op.track_offset(),
None => {
invalid_path_items.push(InvalidPathItem {
index,
path_item: path_item.clone(),
});
continue;
}
PathItemLocation::OperationalPointReference {
reference: OperationalPointIdentifier::OperationalPointId { operational_point },
..
} => match self.get_from_id(&operational_point.0) {
Some(op) => op.track_offset(),
None => {
invalid_path_items.push(InvalidPathItem {
index,
path_item: path_item.clone(),
});
continue;
}
}
PathItemLocation::OperationalPointDescription {
trigram,
secondary_code,
},
PathItemLocation::OperationalPointReference {
reference:
OperationalPointIdentifier::OperationalPointDescription {
trigram,
secondary_code,
},
..
} => {
let ops = self
.get_from_trigram(&trigram.0)
Expand All @@ -127,9 +133,13 @@ impl PathItemCache {
}
track_offsets_from_ops(&ops)
}
PathItemLocation::OperationalPointUic {
uic,
secondary_code,
PathItemLocation::OperationalPointReference {
reference:
OperationalPointIdentifier::OperationalPointUic {
uic,
secondary_code,
},
..
} => {
let ops = self
.get_from_uic(i64::from(*uic))
Expand Down Expand Up @@ -183,14 +193,24 @@ fn collect_path_item_ids(path_items: &[&PathItemLocation]) -> (Vec<String>, Vec<

for item in path_items {
match item {
PathItemLocation::OperationalPointDescription { trigram, .. } => {
PathItemLocation::OperationalPointReference {
reference: OperationalPointIdentifier::OperationalPointDescription { trigram, .. },
..
} => {
trigrams.push(trigram.clone().0);
}
PathItemLocation::OperationalPointUic { uic, .. } => {
PathItemLocation::OperationalPointReference {
reference: OperationalPointIdentifier::OperationalPointUic { uic, .. },
..
} => {
ops_uic.push(i64::from(*uic));
}
PathItemLocation::OperationalPointId {
operational_point, ..
PathItemLocation::OperationalPointReference {
reference:
OperationalPointIdentifier::OperationalPointId {
operational_point, ..
},
..
} => {
ops_id.push(operational_point.clone().0);
}
Expand Down
11 changes: 8 additions & 3 deletions editoast/src/views/path/pathfinding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ fn path_input_hash(infra: i64, infra_version: &String, path_input: &PathfindingI
pub mod tests {
use axum::http::StatusCode;
use editoast_models::DbConnectionPoolV2;
use editoast_schemas::train_schedule::OperationalPointIdentifier;
use editoast_schemas::train_schedule::PathItemLocation;
use pretty_assertions::assert_eq;
use rstest::rstest;
Expand Down Expand Up @@ -475,9 +476,13 @@ pub mod tests {
PathfindingInputError::InvalidPathItems {
items: vec![InvalidPathItem {
index: 1,
path_item: PathItemLocation::OperationalPointDescription {
trigram: "NO_TRIGRAM".into(),
secondary_code: None
path_item: PathItemLocation::OperationalPointReference {
reference: OperationalPointIdentifier::OperationalPointDescription {
trigram: "NO_TRIGRAM".into(),
secondary_code: None
},
track: None,
track_label: None
}
}]
}
Expand Down

0 comments on commit 9ba7f2c

Please sign in to comment.