Skip to content

Commit

Permalink
fixup! editoast: refacto path item location to add track and label in…
Browse files Browse the repository at this point in the history
…formation

Signed-off-by: Youness CHRIFI ALAOUI <[email protected]>
  • Loading branch information
younesschrifi committed Nov 26, 2024
1 parent 604294d commit 78a0f76
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 29 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 @@ -11,6 +11,7 @@ pub use path_item::OperationalPointIdentifier;
pub use path_item::OperationalPointReference;
pub use path_item::PathItem;
pub use path_item::PathItemLocation;
pub use path_item::TrackReference;

mod train_schedule_options;
pub use train_schedule_options::TrainScheduleOptions;
Expand Down
18 changes: 14 additions & 4 deletions editoast/editoast_schemas/src/train_schedule/path_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ editoast_common::schemas! {
PathItem,
PathItemLocation,
OperationalPointReference,
TrackReference,
}

/// A location on the path of a train
Expand Down Expand Up @@ -41,11 +42,20 @@ pub struct OperationalPointReference {
#[serde(flatten)]
#[schema(inline)]
pub reference: OperationalPointIdentifier,
#[schema(inline)]
#[serde(default)]
pub track_id: Option<Identifier>,
#[serde(default)]
pub track_label: Option<String>,
pub track_reference: Option<TrackReference>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema, Hash)]
#[serde(untagged, deny_unknown_fields)]
pub enum TrackReference {
Id {
#[schema(inline)]
track_id: Identifier,
},
Label {
track_label: NonBlankString,
},
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema, Hash)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ mod tests {
reference: OperationalPointId {
operational_point: "op".into(),
},
track_id: None,
track_label: None,
track_reference: None,
});
let path_item = PathItem {
id: "a".into(),
Expand Down Expand Up @@ -245,8 +244,7 @@ mod tests {
reference: OperationalPointId {
operational_point: "op".into(),
},
track_id: None,
track_label: None,
track_reference: None,
});
let path_item = PathItem {
id: "a".into(),
Expand Down Expand Up @@ -284,8 +282,7 @@ mod tests {
reference: OperationalPointId {
operational_point: "op".into(),
},
track_id: None,
track_label: None,
track_reference: None,
});
let path_item = PathItem {
id: "a".into(),
Expand Down
25 changes: 18 additions & 7 deletions editoast/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7110,14 +7110,9 @@ components:
minimum: 0
- type: object
properties:
track_id:
track_reference:
allOf:
- type: string
maxLength: 255
minLength: 1
nullable: true
track_label:
type: string
- $ref: '#/components/schemas/TrackReference'
nullable: true
Ordering:
type: string
Expand Down Expand Up @@ -10493,6 +10488,22 @@ components:
type: string
maxLength: 255
minLength: 1
TrackReference:
oneOf:
- type: object
required:
- track_id
properties:
track_id:
type: string
maxLength: 255
minLength: 1
- type: object
required:
- track_label
properties:
track_label:
type: string
TrackSection:
type: object
required:
Expand Down
64 changes: 56 additions & 8 deletions editoast/src/views/path/path_item_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use crate::error::Result;
use crate::models::TrackSectionModel;
use crate::RetrieveBatchUnchecked;
use editoast_schemas::infra::TrackOffset;
use editoast_schemas::primitives::NonBlankString;
use editoast_schemas::train_schedule::OperationalPointReference;
use editoast_schemas::train_schedule::TrackReference;
use std::collections::HashMap;
use std::collections::HashSet;

Expand All @@ -25,6 +27,7 @@ pub struct PathItemCache {
trigram_to_ops: HashMap<String, Vec<OperationalPointModel>>,
ids_to_ops: HashMap<String, OperationalPointModel>,
existing_track_ids: HashSet<String>,
existing_track_labels: HashMap<String, NonBlankString>,
}

impl PathItemCache {
Expand Down Expand Up @@ -53,17 +56,30 @@ impl PathItemCache {
.map(|part| (infra_id, part.track.0.clone()))
.chain(tracks);
let existing_track_ids =
TrackSectionModel::retrieve_batch_unchecked::<_, Vec<_>>(conn, tracks)
TrackSectionModel::retrieve_batch_unchecked::<_, Vec<_>>(conn, tracks.clone())
.await?
.into_iter()
.map(|track| track.obj_id)
.collect();
let existing_track_labels =
TrackSectionModel::retrieve_batch_unchecked::<_, Vec<_>>(conn, tracks)
.await?
.into_iter()
.filter_map(|track| {
track
.extensions
.sncf
.as_ref()
.map(|extension| (track.obj_id.clone(), extension.line_name.clone()))
})
.collect();

Ok(PathItemCache {
uic_to_ops,
trigram_to_ops,
ids_to_ops,
existing_track_ids,
existing_track_labels,
})
}

Expand Down Expand Up @@ -95,16 +111,18 @@ impl PathItemCache {
let mut result: Vec<Vec<_>> = Vec::default();
let mut invalid_path_items = Vec::new();
for (index, &path_item) in path_items.iter().enumerate() {
dbg!(&path_item);
let track_offsets = match path_item {
PathItemLocation::TrackOffset(track_offset) => {
vec![track_offset.clone()]
}
PathItemLocation::OperationalPointReference(OperationalPointReference {
reference: OperationalPointIdentifier::OperationalPointId { operational_point },
..
track_reference,
}) => match self.get_from_id(&operational_point.0) {
Some(op) => op.track_offset(),
Some(op) => {
let track_offsets = op.track_offset();
self.track_reference_filter(track_offsets, track_reference)
}
None => {
invalid_path_items.push(InvalidPathItem {
index,
Expand All @@ -119,7 +137,7 @@ impl PathItemCache {
trigram,
secondary_code,
},
..
track_reference,
}) => {
let ops = self
.get_from_trigram(&trigram.0)
Expand All @@ -133,15 +151,16 @@ impl PathItemCache {
});
continue;
}
track_offsets_from_ops(&ops)
let track_offsets = track_offsets_from_ops(&ops);
self.track_reference_filter(track_offsets, track_reference)
}
PathItemLocation::OperationalPointReference(OperationalPointReference {
reference:
OperationalPointIdentifier::OperationalPointUic {
uic,
secondary_code,
},
..
track_reference,
}) => {
let ops = self
.get_from_uic(i64::from(*uic))
Expand All @@ -155,7 +174,8 @@ impl PathItemCache {
});
continue;
}
track_offsets_from_ops(&ops)
let track_offsets = track_offsets_from_ops(&ops);
self.track_reference_filter(track_offsets, track_reference)
}
};

Expand Down Expand Up @@ -185,6 +205,34 @@ impl PathItemCache {

Ok(result)
}

/// Filter operational points parts by a track label or a track id
/// If neither a track label or id is provided, the original list is returned
fn track_reference_filter(
&self,
track_offsets: Vec<TrackOffset>,
track_reference: &Option<TrackReference>,
) -> Vec<TrackOffset> {
if let Some(track_reference) = track_reference {
match track_reference {
TrackReference::Id { track_id } => track_offsets
.into_iter()
.filter(|track_offset| &track_offset.track == track_id)
.collect(),
TrackReference::Label { track_label } => track_offsets
.into_iter()
.filter(|track_offset| {
self.existing_track_labels
.get::<String>(&track_offset.track)
.unwrap()
== track_label
})
.collect(),
}
} else {
track_offsets
}
}
}

/// Collect the ids of the operational points from the path items
Expand Down
3 changes: 1 addition & 2 deletions editoast/src/views/path/pathfinding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,7 @@ pub mod tests {
trigram: "NO_TRIGRAM".into(),
secondary_code: None
},
track_id: None,
track_label: None
track_reference: None,
}
)
}]
Expand Down
10 changes: 8 additions & 2 deletions front/src/common/api/generatedEditoastApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2421,6 +2421,13 @@ export type TrackOffset = {
offset: number;
track: string;
};
export type TrackReference =
| {
track_id: string;
}
| {
track_label: string;
};
export type OperationalPointReference = (
| {
operational_point: string;
Expand All @@ -2437,8 +2444,7 @@ export type OperationalPointReference = (
uic: number;
}
) & {
track_id?: string | null;
track_label?: string | null;
track_reference?: TrackReference | null;
};
export type PathItemLocation = TrackOffset | OperationalPointReference;
export type PathfindingInputError =
Expand Down

0 comments on commit 78a0f76

Please sign in to comment.