Skip to content

Commit

Permalink
fixup! editoast: improve performance of batch_pathfinding function
Browse files Browse the repository at this point in the history
  • Loading branch information
flomonster committed Jul 12, 2024
1 parent f8e1168 commit 366cc35
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 42 deletions.
36 changes: 15 additions & 21 deletions editoast/src/views/v2/path/path_item_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use editoast_schemas::train_schedule::PathItemLocation;

use crate::modelsv2::OperationalPointModel;

type TrackOffsetResult = std::result::Result<Vec<Vec<TrackOffset>>, TrackOffsetExtractionError>;
type TrackOffsetResult = std::result::Result<Vec<Vec<TrackOffset>>, PathfindingResult>;

/// Gather information about several path items, factorizing db calls.
pub struct PathItemCache {
Expand Down Expand Up @@ -96,7 +96,7 @@ impl PathItemCache {
match self.get_from_id(&operational_point.0) {
Some(op) => op.track_offset(),
None => {
return Err(TrackOffsetExtractionError {
return Err(PathfindingResult::InvalidPathItem {
index,
path_item: path_item.clone(),
});
Expand All @@ -113,7 +113,7 @@ impl PathItemCache {
.unwrap_or_default();
let ops = secondary_code_filter(secondary_code, ops);
if ops.is_empty() {
return Err(TrackOffsetExtractionError {
return Err(PathfindingResult::InvalidPathItem {
index,
path_item: path_item.clone(),
});
Expand All @@ -130,14 +130,25 @@ impl PathItemCache {
.unwrap_or_default();
let ops = secondary_code_filter(secondary_code, ops);
if ops.is_empty() {
return Err(TrackOffsetExtractionError {
return Err(PathfindingResult::InvalidPathItem {
index,
path_item: path_item.clone(),
});
}
track_offsets_from_ops(&ops)
}
};

// Check if tracks exist
for track_offset in &track_offsets {
if !self.track_exists(&track_offset.track.0) {
return Err(PathfindingResult::InvalidPathItem {
index,
path_item: path_item.clone(),
});
}
}

result.push(track_offsets);
}
Ok(result)
Expand Down Expand Up @@ -243,20 +254,3 @@ fn secondary_code_filter(
ops
}
}

/// Error when extracting track offsets from path items
pub struct TrackOffsetExtractionError {
/// The index of the path item that caused the error
pub index: usize,
/// The path item that caused the error
pub path_item: PathItemLocation,
}

impl From<TrackOffsetExtractionError> for PathfindingResult {
fn from(error: TrackOffsetExtractionError) -> Self {
PathfindingResult::InvalidPathItem {
index: error.index,
path_item: error.path_item,
}
}
}
9 changes: 0 additions & 9 deletions editoast/src/views/v2/path/pathfinding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,6 @@ fn build_pathfinding_request(
}
let track_offsets = path_item_cache.extract_location_from_path_items(&path_items)?;

// Check if tracks exist
for tracks in &track_offsets {
for track_offset in tracks {
if !path_item_cache.track_exists(&track_offset.track.0) {
return Err(PathfindingResult::NotFoundInTracks);
}
}
}

// Create the pathfinding request
Ok(PathfindingRequest {
infra: infra.id,
Expand Down
21 changes: 9 additions & 12 deletions editoast/src/views/v2/timetable/stdcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use thiserror::Error;
use utoipa::IntoParams;
use utoipa::ToSchema;

use crate::core::v2::pathfinding::PathfindingResult;
use crate::core::v2::simulation::SimulationResponse;
use crate::core::v2::stdcm::STDCMResponse;
use crate::core::v2::stdcm::TrainRequirement;
Expand All @@ -33,7 +34,6 @@ use crate::modelsv2::work_schedules::WorkSchedule;
use crate::modelsv2::RollingStockModel;
use crate::modelsv2::{Infra, List};
use crate::views::v2::path::path_item_cache::PathItemCache;
use crate::views::v2::path::path_item_cache::TrackOffsetExtractionError;
use crate::views::v2::train_schedule::train_simulation;
use crate::views::v2::train_schedule::train_simulation_batch;
use crate::RedisClient;
Expand Down Expand Up @@ -437,8 +437,14 @@ async fn parse_stdcm_steps(
let locations: Vec<_> = data.steps.iter().map(|item| &item.location).collect();

let path_item_cache = PathItemCache::load(conn, infra.id, &locations).await?;
let track_offsets = path_item_cache.extract_location_from_path_items(&locations);
let track_offsets = track_offsets.map_err::<STDCMError, _>(|err| err.into())?;
let track_offsets = path_item_cache
.extract_location_from_path_items(&locations)
.map_err(|path_res| match path_res {
PathfindingResult::InvalidPathItem { index, path_item } => {
STDCMError::InvalidPathItem { index, path_item }
}
_ => panic!("Unexpected pathfinding result"),
})?;

Ok(track_offsets
.iter()
Expand All @@ -457,15 +463,6 @@ async fn parse_stdcm_steps(
.collect())
}

impl From<TrackOffsetExtractionError> for STDCMError {
fn from(error: TrackOffsetExtractionError) -> Self {
STDCMError::InvalidPathItem {
index: error.index,
path_item: error.path_item,
}
}
}

enum MaxRunningTimeResult {
MaxRunningTime { value: u64 },
Error { error: Box<SimulationResponse> },
Expand Down

0 comments on commit 366cc35

Please sign in to comment.