Skip to content

Commit

Permalink
editoast: timetable and trainschedule CRUD
Browse files Browse the repository at this point in the history
  • Loading branch information
younesschrifi committed Feb 8, 2024
1 parent 9358bbd commit 06cfde4
Show file tree
Hide file tree
Showing 13 changed files with 463 additions and 48 deletions.
18 changes: 14 additions & 4 deletions editoast/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions editoast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ sha1 = "0.10"
strum = "0.25.0"
strum_macros = "0.25.3"
thiserror = "1.0.56"
time = {version = "0.3.34", features = [ "serde" ]}
enum-map = "2.7.3"
editoast_derive = { path = "./editoast_derive" }
osrd_containers = { path = "./osrd_containers" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ CREATE TABLE scenariov2 (
CREATE TABLE trainschedulev2 (
id int8 PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
train_name varchar(128) NOT NULL,
labels jsonb NOT NULL,
rolling_stock_id varchar(128) NOT NULL,
labels text [] NOT NULL,
rolling_stock_name varchar(128) NOT NULL,
timetable_id int8 NOT NULL UNIQUE REFERENCES timetablev2(id) DEFERRABLE INITIALLY DEFERRED,
departure_time timestamptz NOT NULL,
scheduled_points jsonb NOT NULL,
allowances jsonb NOT NULL,
start_time timestamptz NOT NULL,
schedule jsonb NOT NULL,
margins jsonb NOT NULL,
initial_speed float8 NOT NULL,
comfort varchar(8) NOT NULL,
path jsonb NOT NULL,
tag_constrains jsonb NOT NULL,
speed_limit_tags varchar(128) NULL,
power_restriction_ranges jsonb NULL,
options jsonb NULL
constraint_distribution varchar(8) NOT NULL,
speed_limit_tags text [] NOT NULL,
power_restrictions jsonb NOT NULL,
train_schedule_options jsonb NOT NULL
);
27 changes: 16 additions & 11 deletions editoast/src/modelsv2/trainschedule.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
use crate::schema::v2::trainschedulev2::{
Margins, PathItem, PowerRestrictions, ScheduleItem, TrainScheduleOptions,
};
use crate::DieselJson;
use chrono::NaiveDateTime;
use diesel_async::RunQueryDsl;
use editoast_derive::ModelV2;
use serde::{Deserialize, Serialize};

#[derive(Debug, Default, Clone, ModelV2)]
#[derive(Debug, Default, Clone, ModelV2, Serialize, Deserialize)]
#[model(table = crate::tables::trainschedulev2)]
#[model(changeset(public))]
pub struct TrainScheduleV2 {
pub id: i64,
pub train_name: String,
pub labels: DieselJson<Vec<String>>,
pub rolling_stock_id: String,
pub labels: Vec<Option<String>>,
pub rolling_stock_name: String,
pub timetable_id: i64,
pub departure_time: NaiveDateTime,
pub scheduled_points: DieselJson<Vec<String>>,
pub allowances: DieselJson<Vec<String>>,
pub start_time: NaiveDateTime,
pub schedule: DieselJson<ScheduleItem>,
pub margins: DieselJson<Margins>,
pub initial_speed: f64,
pub comfort: String,
pub path: DieselJson<Vec<String>>,
pub tag_constrains: DieselJson<Vec<String>>,
pub speed_limit_tags: Option<String>,
pub power_restriction_ranges: Option<DieselJson<Vec<String>>>,
pub options: Option<DieselJson<Vec<String>>>,
pub path: DieselJson<Vec<PathItem>>,
pub constraint_distribution: String,
pub speed_limit_tags: Vec<Option<String>>,
pub power_restrictions: DieselJson<PowerRestrictions>,
pub train_schedule_options: DieselJson<TrainScheduleOptions>,
}
1 change: 1 addition & 0 deletions editoast/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod switch;
mod switch_type;
pub mod track_section;
pub mod utils;
pub mod v2;

pub use buffer_stop::{BufferStop, BufferStopCache};
pub use detector::{Detector, DetectorCache};
Expand Down
1 change: 1 addition & 0 deletions editoast/src/schema/v2/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod trainschedulev2;
102 changes: 102 additions & 0 deletions editoast/src/schema/v2/trainschedulev2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use serde::{Deserialize, Serialize};
use std::str::FromStr;
use time::Duration;

use crate::schema::utils::NonBlankString;

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct PowerRestrictions {
from: NonBlankString,
to: NonBlankString,
value: String,
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct TrainScheduleOptions {
use_electrical_profiles: bool,
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct ScheduleItem {
at: NonBlankString,
departure: Option<Duration>,
stop_for: Option<Duration>,
locked: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
enum PathItemLocation {
TrackOffset {
track: NonBlankString,
offset: f64,
},
OperationalPointId {
operational_point: NonBlankString,
},
OperationalPointDescription {
trigram: NonBlankString,
secondary_code: Option<String>,
},
OperationalPointUic {
uic: NonBlankString,
secondary_code: Option<String>,
},
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PathItem {
id: NonBlankString,
#[serde(flatten)]
location: PathItemLocation,
deleted: bool,
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct Margins {
boudaries: Vec<NonBlankString>,
values: Vec<MarginValue>,
}

#[derive(Debug, Copy, Clone, Default)]
enum MarginValue {
#[default]
Zero,
Percentage(f64),
MinPerKm(f64),
}

impl<'de> Deserialize<'de> for MarginValue {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let value = String::deserialize(deserializer)?;
if value == "0" {
return Ok(Self::Zero);
}
if value.ends_with('%') {
let float_value: f64 = f64::from_str(&value[0..value.len() - 1]).map_err(|_| {
serde::de::Error::invalid_value(
serde::de::Unexpected::Str(&value),
&"a valid float",
)
})?;
return Ok(Self::Percentage(float_value));
}
return Err(serde::de::Error::custom("Invalid margin value"));
}
}

impl Serialize for MarginValue {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match self {
MarginValue::Zero => serializer.serialize_str("0"),
MarginValue::Percentage(value) => serializer.serialize_str(&format!("{}%", value)),
MarginValue::MinPerKm(value) => serializer.serialize_str(&value.to_string()),
}
}
}
20 changes: 10 additions & 10 deletions editoast/src/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,22 +679,22 @@ diesel::table! {
id -> Int8,
#[max_length = 128]
train_name -> Varchar,
labels -> Jsonb,
labels -> Array<Nullable<Text>>,
#[max_length = 128]
rolling_stock_id -> Varchar,
rolling_stock_name -> Varchar,
timetable_id -> Int8,
departure_time -> Timestamptz,
scheduled_points -> Jsonb,
allowances -> Jsonb,
start_time -> Timestamptz,
schedule -> Jsonb,
margins -> Jsonb,
initial_speed -> Float8,
#[max_length = 8]
comfort -> Varchar,
path -> Jsonb,
tag_constrains -> Jsonb,
#[max_length = 128]
speed_limit_tags -> Nullable<Varchar>,
power_restriction_ranges -> Nullable<Jsonb>,
options -> Nullable<Jsonb>,
#[max_length = 8]
constraint_distribution -> Varchar,
speed_limit_tags -> Array<Nullable<Text>>,
power_restrictions -> Jsonb,
train_schedule_options -> Jsonb,
}
}

Expand Down
3 changes: 1 addition & 2 deletions editoast/src/views/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ pub mod pathfinding;
pub mod projects;
pub mod rolling_stocks;
pub mod scenario;
pub mod scenariov2;
pub mod search;
mod single_simulation;
pub mod sprites;
pub mod stdcm;
pub mod study;
pub mod timetable;
pub mod timetablev2;
pub mod train_schedule;
pub mod v2;

use self::openapi::{merge_path_items, remove_discriminator, OpenApiMerger, Routes};
use crate::client::get_app_version;
Expand Down
3 changes: 3 additions & 0 deletions editoast/src/views/v2/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod scenariov2;
pub mod timetablev2;
pub mod trainschedulev2;
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ use super::projects::QueryParams;
crate::routes! {
"/scenarios" => {
create,
list,
"/{scenario_id}" => {
get,
delete,
Expand Down
Loading

0 comments on commit 06cfde4

Please sign in to comment.