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 9882eef
Show file tree
Hide file tree
Showing 8 changed files with 307 additions and 294 deletions.
204 changes: 102 additions & 102 deletions editoast/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8218,6 +8218,108 @@ components:
type: integer
format: int64
minimum: 0
Request:
type: object
description: An STDCM request
required:
- steps
- rolling_stock_id
- comfort
properties:
comfort:
$ref: '#/components/schemas/Comfort'
electrical_profile_set_id:
type: integer
format: int64
nullable: true
loading_gauge_type:
allOf:
- $ref: '#/components/schemas/LoadingGaugeType'
nullable: true
margin:
type: string
description: Can be a percentage `X%`, a time in minutes per 100 kilometer `Xmin/100km`
example:
- 5%
- 2min/100km
nullable: true
max_speed:
type: number
format: double
description: Maximum speed of the consist in km/h
nullable: true
maximum_departure_delay:
type: integer
format: int64
description: |-
By how long we can shift the departure time in milliseconds
Deprecated, first step data should be used instead
nullable: true
minimum: 0
maximum_run_time:
type: integer
format: int64
description: |-
Specifies how long the total run time can be in milliseconds
Deprecated, first step data should be used instead
nullable: true
minimum: 0
rolling_stock_id:
type: integer
format: int64
speed_limit_tags:
type: string
description: Train categories for speed limits
nullable: true
start_time:
type: string
format: date-time
description: Deprecated, first step arrival time should be used instead
nullable: true
steps:
type: array
items:
$ref: '#/components/schemas/PathfindingItem'
temporary_speed_limit_group_id:
type: integer
format: int64
nullable: true
time_gap_after:
type: integer
format: int64
description: |-
Margin after the train passage in milliseconds
Enforces that the path used by the train should be free and
available at least that many milliseconds after its passage.
minimum: 0
time_gap_before:
type: integer
format: int64
description: |-
Margin before the train passage in seconds
Enforces that the path used by the train should be free and
available at least that many milliseconds before its passage.
minimum: 0
total_length:
type: number
format: double
description: Total length of the consist in meters
nullable: true
total_mass:
type: number
format: double
description: Total mass of the consist in kg
nullable: true
towed_rolling_stock_id:
type: integer
format: int64
nullable: true
work_schedule_group_id:
type: integer
format: int64
nullable: true
RjsPowerRestrictionRange:
type: object
description: A range along the train path where a power restriction is applied.
Expand Down Expand Up @@ -8726,108 +8828,6 @@ components:
type: string
zone:
type: string
STDCMRequestPayload:
type: object
description: An STDCM request
required:
- steps
- rolling_stock_id
- comfort
properties:
comfort:
$ref: '#/components/schemas/Comfort'
electrical_profile_set_id:
type: integer
format: int64
nullable: true
loading_gauge_type:
allOf:
- $ref: '#/components/schemas/LoadingGaugeType'
nullable: true
margin:
type: string
description: Can be a percentage `X%`, a time in minutes per 100 kilometer `Xmin/100km`
example:
- 5%
- 2min/100km
nullable: true
max_speed:
type: number
format: double
description: Maximum speed of the consist in km/h
nullable: true
maximum_departure_delay:
type: integer
format: int64
description: |-
By how long we can shift the departure time in milliseconds
Deprecated, first step data should be used instead
nullable: true
minimum: 0
maximum_run_time:
type: integer
format: int64
description: |-
Specifies how long the total run time can be in milliseconds
Deprecated, first step data should be used instead
nullable: true
minimum: 0
rolling_stock_id:
type: integer
format: int64
speed_limit_tags:
type: string
description: Train categories for speed limits
nullable: true
start_time:
type: string
format: date-time
description: Deprecated, first step arrival time should be used instead
nullable: true
steps:
type: array
items:
$ref: '#/components/schemas/PathfindingItem'
temporary_speed_limit_group_id:
type: integer
format: int64
nullable: true
time_gap_after:
type: integer
format: int64
description: |-
Margin after the train passage in milliseconds
Enforces that the path used by the train should be free and
available at least that many milliseconds after its passage.
minimum: 0
time_gap_before:
type: integer
format: int64
description: |-
Margin before the train passage in seconds
Enforces that the path used by the train should be free and
available at least that many milliseconds before its passage.
minimum: 0
total_length:
type: number
format: double
description: Total length of the consist in meters
nullable: true
total_mass:
type: number
format: double
description: Total mass of the consist in kg
nullable: true
towed_rolling_stock_id:
type: integer
format: int64
nullable: true
work_schedule_group_id:
type: integer
format: int64
nullable: true
Scenario:
type: object
required:
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
Loading

0 comments on commit 9882eef

Please sign in to comment.