Skip to content

Commit

Permalink
editoast: add work schedules to core input request
Browse files Browse the repository at this point in the history
  • Loading branch information
eckter committed May 14, 2024
1 parent 44dbc94 commit a7a4aff
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
25 changes: 25 additions & 0 deletions editoast/src/core/v2/stdcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ pub struct STDCMRequest {
pub time_gap_after: u64,
/// Margin to apply to the whole train
pub margin: Option<MarginValue>,
/// List of planned work schedules
pub work_schedules: Vec<SimpleWorkSchedule>,
}

#[derive(Debug, Serialize)]
Expand All @@ -64,6 +66,29 @@ pub struct STDCMPathItem {
pub stop_duration: Option<u64>,
}

/// Lighter description of a work schedule
#[derive(Debug, Serialize)]
pub struct SimpleWorkSchedule {
/// Start time as a time delta from the stdcm start time in ms
pub start_time: u64,
/// End time as a time delta from the stdcm start time in ms
pub end_time: u64,
/// List of unavailable track ranges
pub track_ranges: Vec<UndirectedTrackRange>,
}

/// A range on a track section.
/// `begin` is always less than `end`.
#[derive(Serialize, Deserialize, Clone, Debug, ToSchema, Hash, PartialEq, Eq)]
pub struct UndirectedTrackRange {
/// The track section identifier.
pub track_section: String,
/// The beginning of the range in mm.
pub begin: u64,
/// The end of the range in mm.
pub end: u64,
}

#[derive(Debug, Serialize)]
pub struct TrainRequirement {
/// The start datetime of the train
Expand Down
45 changes: 40 additions & 5 deletions editoast/src/views/v2/timetable/stdcm.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::cmp::max;
use actix_web::post;
use actix_web::web::Data;
use actix_web::web::Json;
use actix_web::web::Path;
use actix_web::web::Query;
use chrono::DateTime;
use chrono::{DateTime, NaiveDateTime, TimeZone};
use chrono::Utc;
use editoast_derive::EditoastError;
use editoast_schemas::train_schedule::Comfort;
Expand All @@ -18,7 +19,7 @@ use utoipa::IntoParams;
use utoipa::ToSchema;

use crate::core::v2::simulation::SimulationResponse;
use crate::core::v2::stdcm::STDCMPathItem;
use crate::core::v2::stdcm::{SimpleWorkSchedule, STDCMPathItem, UndirectedTrackRange};
use crate::core::v2::stdcm::STDCMRequest;
use crate::core::v2::stdcm::STDCMResponse;
use crate::core::v2::stdcm::TrainRequirement;
Expand All @@ -27,12 +28,13 @@ use crate::core::CoreClient;
use crate::error::Result;
use crate::modelsv2::timetable::TimetableWithTrains;
use crate::modelsv2::train_schedule::TrainSchedule;
use crate::modelsv2::Infra;
use crate::modelsv2::{DbConnection, Infra, List};
use crate::modelsv2::RollingStockModel;
use crate::views::v2::path::pathfinding::extract_location_from_path_items;
use crate::views::v2::path::pathfinding::TrackOffsetExtractionError;
use crate::views::v2::train_schedule::train_simulation_batch;
use crate::DbConnectionPool;
use crate::modelsv2::work_schedules::WorkSchedule;
use crate::RedisClient;
use crate::Retrieve;
use crate::RetrieveBatch;
Expand Down Expand Up @@ -215,13 +217,46 @@ async fn stdcm(
time_gap_after: data.time_gap_after,
margin: data.margin,
time_step: Some(2000),
work_schedules: build_work_schedules(conn, data.start_time).await?,
}
.fetch(core_client.as_ref())
.await?;
.fetch(core_client.as_ref())
.await?;

Ok(Json(stdcm_response))
}

async fn build_work_schedules(
conn: &mut DbConnection,
time: DateTime<Utc>,
) -> Result<Vec<SimpleWorkSchedule>> {
let res = Ok(WorkSchedule::list(conn, Default::default())
.await?
.iter()
.map(|schedule|
{
let schedule1 = SimpleWorkSchedule {
start_time: delta_as_ms(&schedule.start_date_time, &time),
end_time: delta_as_ms(&schedule.end_date_time, &time),
track_ranges: schedule.track_ranges
.iter()
.map(|track| UndirectedTrackRange {
track_section: track.track.to_string(),
begin: (track.begin * 1000.0) as u64,
end: (track.end * 1000.0) as u64,
})
.collect(),
};
schedule1
})
.collect()
);
return res;
}

fn delta_as_ms(time: &NaiveDateTime, zero: &DateTime<Utc>) -> u64 {
max(0, (Utc.from_utc_datetime(&time) - zero).num_milliseconds()) as u64
}

/// Create steps from track_map and waypoints
async fn parse_stdcm_steps(
db_pool: Arc<DbConnectionPool>,
Expand Down

0 comments on commit a7a4aff

Please sign in to comment.