Skip to content

Commit

Permalink
editoast: refactor module visibility and naming for improved organiza…
Browse files Browse the repository at this point in the history
…tion

Signed-off-by: hamz2a <[email protected]>
  • Loading branch information
hamz2a committed Nov 15, 2024
1 parent 0a4465c commit 1d42ba4
Show file tree
Hide file tree
Showing 10 changed files with 454 additions and 432 deletions.
334 changes: 167 additions & 167 deletions editoast/openapi.yaml

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions editoast/src/core/conflict_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,31 @@ pub struct WorkSchedulesRequest {
pub start_time: DateTime<Utc>,
pub work_schedule_requirements: HashMap<i64, WorkSchedule>,
}
impl WorkSchedulesRequest {
pub fn new(
work_schedules: Vec<crate::models::work_schedules::WorkSchedule>,
earliest_departure_time: DateTime<Utc>,
latest_simulation_end: DateTime<Utc>,
) -> Option<Self> {
if work_schedules.is_empty() {
return None;
}
// Filter the provided work schedules to find those that conflict with the given parameters
// This identifies any work schedules that may overlap with the earliest departure time and latest simulation end.
let work_schedule_requirements = work_schedules
.into_iter()
.filter_map(|ws| {
ws.as_core_work_schedule(earliest_departure_time, latest_simulation_end)
.map(|core_ws| (ws.id, core_ws))
})
.collect();

Some(Self {
start_time: earliest_departure_time,
work_schedule_requirements,
})
}
}

#[derive(Debug, Deserialize, ToSchema)]
pub struct ConflictDetectionResponse {
Expand Down
8 changes: 5 additions & 3 deletions editoast/src/core/simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,11 +477,13 @@ impl AsCoreRequest<Json<SimulationResponse>> for SimulationRequest {
}

impl SimulationResponse {
// Boxing the error to avoid large enum size on the stack
pub fn simulation_run_time(self) -> Result<u64, Box<Self>> {
match self {
SimulationResponse::Success { provisional, .. } => {
Ok(*provisional.times.last().expect("empty simulation result"))
}
SimulationResponse::Success { provisional, .. } => Ok(*provisional
.times
.last()
.expect("core error: empty simulation result")),
err => Err(Box::from(err)),
}
}
Expand Down
4 changes: 2 additions & 2 deletions editoast/src/core/stdcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub struct UndirectedTrackRange {
// We accepted the difference of memory size taken by variants
// Since there is only on success and others are error cases
#[allow(clippy::large_enum_variant)]
pub enum STDCMCoreResponse {
pub enum Response {
Success {
simulation: SimulationResponse,
path: PathfindingResultSuccess,
Expand All @@ -131,7 +131,7 @@ pub enum STDCMCoreResponse {
},
}

impl AsCoreRequest<Json<STDCMCoreResponse>> for STDCMRequest {
impl AsCoreRequest<Json<Response>> for STDCMRequest {
const METHOD: reqwest::Method = reqwest::Method::POST;
const URL_PATH: &'static str = "/v2/stdcm";

Expand Down
46 changes: 20 additions & 26 deletions editoast/src/models/work_schedules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,24 @@ pub struct WorkSchedule {
}

impl WorkSchedule {
fn map_to_core_work_schedule(
pub fn as_core_work_schedule(
&self,
start_time: DateTime<Utc>,
) -> crate::core::stdcm::WorkSchedule {
crate::core::stdcm::WorkSchedule {
start_time: elapsed_since_time_ms(&self.start_date_time, &start_time),
end_time: elapsed_since_time_ms(&self.end_date_time, &start_time),
earliest_departure_time: DateTime<Utc>,
latest_simulation_end: DateTime<Utc>,
) -> Option<crate::core::stdcm::WorkSchedule> {
let search_window_duration =
(latest_simulation_end - earliest_departure_time).num_milliseconds() as u64;

let start_time = elapsed_time_since_ms(&self.start_date_time, &earliest_departure_time);
let end_time = elapsed_time_since_ms(&self.end_date_time, &earliest_departure_time);

if end_time == 0 || start_time >= search_window_duration {
return None;
}

Some(crate::core::stdcm::WorkSchedule {
start_time,
end_time,
track_ranges: self
.track_ranges
.iter()
Expand All @@ -63,27 +74,10 @@ impl WorkSchedule {
end: (track.end * 1000.0) as u64,
})
.collect(),
}
}

pub fn get_id_and_core_work_schedule(
&self,
start_time: DateTime<Utc>,
latest_simulation_end: DateTime<Utc>,
) -> Option<(i64, crate::core::stdcm::WorkSchedule)> {
let search_window_duration = (latest_simulation_end - start_time).num_milliseconds() as u64;

let ws = self.map_to_core_work_schedule(start_time);
if ws.end_time > 0 && ws.start_time < search_window_duration {
return Some((self.id, ws));
}
None
})
}
}

fn elapsed_since_time_ms(time: &NaiveDateTime, start_time: &DateTime<Utc>) -> u64 {
max(
0,
(Utc.from_utc_datetime(time) - start_time).num_milliseconds(),
) as u64
fn elapsed_time_since_ms(time: &NaiveDateTime, since: &DateTime<Utc>) -> u64 {
max(0, (Utc.from_utc_datetime(time) - since).num_milliseconds()) as u64
}
4 changes: 1 addition & 3 deletions editoast/src/views/timetable.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
pub mod path_not_found_handler;
pub mod stdcm;
pub mod stdcm_request_payload;

use std::collections::HashMap;

Expand Down Expand Up @@ -59,7 +57,7 @@ crate::routes! {
editoast_common::schemas! {
TimetableResult,
TimetableDetailedResult,
stdcm_request_payload::schemas(),
stdcm::schemas(),
}

#[derive(Debug, Error, EditoastError)]
Expand Down
112 changes: 0 additions & 112 deletions editoast/src/views/timetable/path_not_found_handler.rs

This file was deleted.

Loading

0 comments on commit 1d42ba4

Please sign in to comment.