Skip to content

Commit 06cfde4

Browse files
committed
editoast: timetable and trainschedule CRUD
1 parent 9358bbd commit 06cfde4

File tree

13 files changed

+463
-48
lines changed

13 files changed

+463
-48
lines changed

editoast/Cargo.lock

+14-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

editoast/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ sha1 = "0.10"
6363
strum = "0.25.0"
6464
strum_macros = "0.25.3"
6565
thiserror = "1.0.56"
66+
time = {version = "0.3.34", features = [ "serde" ]}
6667
enum-map = "2.7.3"
6768
editoast_derive = { path = "./editoast_derive" }
6869
osrd_containers = { path = "./osrd_containers" }

editoast/migrations/2024-02-01-002011_create_v2_scenario_trainschedule_timetable/up.sql

+9-9
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ CREATE TABLE scenariov2 (
1616
CREATE TABLE trainschedulev2 (
1717
id int8 PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
1818
train_name varchar(128) NOT NULL,
19-
labels jsonb NOT NULL,
20-
rolling_stock_id varchar(128) NOT NULL,
19+
labels text [] NOT NULL,
20+
rolling_stock_name varchar(128) NOT NULL,
2121
timetable_id int8 NOT NULL UNIQUE REFERENCES timetablev2(id) DEFERRABLE INITIALLY DEFERRED,
22-
departure_time timestamptz NOT NULL,
23-
scheduled_points jsonb NOT NULL,
24-
allowances jsonb NOT NULL,
22+
start_time timestamptz NOT NULL,
23+
schedule jsonb NOT NULL,
24+
margins jsonb NOT NULL,
2525
initial_speed float8 NOT NULL,
2626
comfort varchar(8) NOT NULL,
2727
path jsonb NOT NULL,
28-
tag_constrains jsonb NOT NULL,
29-
speed_limit_tags varchar(128) NULL,
30-
power_restriction_ranges jsonb NULL,
31-
options jsonb NULL
28+
constraint_distribution varchar(8) NOT NULL,
29+
speed_limit_tags text [] NOT NULL,
30+
power_restrictions jsonb NOT NULL,
31+
train_schedule_options jsonb NOT NULL
3232
);
+16-11
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
1+
use crate::schema::v2::trainschedulev2::{
2+
Margins, PathItem, PowerRestrictions, ScheduleItem, TrainScheduleOptions,
3+
};
14
use crate::DieselJson;
25
use chrono::NaiveDateTime;
36
use diesel_async::RunQueryDsl;
47
use editoast_derive::ModelV2;
8+
use serde::{Deserialize, Serialize};
59

6-
#[derive(Debug, Default, Clone, ModelV2)]
10+
#[derive(Debug, Default, Clone, ModelV2, Serialize, Deserialize)]
711
#[model(table = crate::tables::trainschedulev2)]
12+
#[model(changeset(public))]
813
pub struct TrainScheduleV2 {
914
pub id: i64,
1015
pub train_name: String,
11-
pub labels: DieselJson<Vec<String>>,
12-
pub rolling_stock_id: String,
16+
pub labels: Vec<Option<String>>,
17+
pub rolling_stock_name: String,
1318
pub timetable_id: i64,
14-
pub departure_time: NaiveDateTime,
15-
pub scheduled_points: DieselJson<Vec<String>>,
16-
pub allowances: DieselJson<Vec<String>>,
19+
pub start_time: NaiveDateTime,
20+
pub schedule: DieselJson<ScheduleItem>,
21+
pub margins: DieselJson<Margins>,
1722
pub initial_speed: f64,
1823
pub comfort: String,
19-
pub path: DieselJson<Vec<String>>,
20-
pub tag_constrains: DieselJson<Vec<String>>,
21-
pub speed_limit_tags: Option<String>,
22-
pub power_restriction_ranges: Option<DieselJson<Vec<String>>>,
23-
pub options: Option<DieselJson<Vec<String>>>,
24+
pub path: DieselJson<Vec<PathItem>>,
25+
pub constraint_distribution: String,
26+
pub speed_limit_tags: Vec<Option<String>>,
27+
pub power_restrictions: DieselJson<PowerRestrictions>,
28+
pub train_schedule_options: DieselJson<TrainScheduleOptions>,
2429
}

editoast/src/schema/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ mod switch;
1717
mod switch_type;
1818
pub mod track_section;
1919
pub mod utils;
20+
pub mod v2;
2021

2122
pub use buffer_stop::{BufferStop, BufferStopCache};
2223
pub use detector::{Detector, DetectorCache};

editoast/src/schema/v2/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod trainschedulev2;
+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
use serde::{Deserialize, Serialize};
2+
use std::str::FromStr;
3+
use time::Duration;
4+
5+
use crate::schema::utils::NonBlankString;
6+
7+
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
8+
pub struct PowerRestrictions {
9+
from: NonBlankString,
10+
to: NonBlankString,
11+
value: String,
12+
}
13+
14+
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
15+
pub struct TrainScheduleOptions {
16+
use_electrical_profiles: bool,
17+
}
18+
19+
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
20+
pub struct ScheduleItem {
21+
at: NonBlankString,
22+
departure: Option<Duration>,
23+
stop_for: Option<Duration>,
24+
locked: bool,
25+
}
26+
27+
#[derive(Debug, Clone, Serialize, Deserialize)]
28+
#[serde(untagged)]
29+
enum PathItemLocation {
30+
TrackOffset {
31+
track: NonBlankString,
32+
offset: f64,
33+
},
34+
OperationalPointId {
35+
operational_point: NonBlankString,
36+
},
37+
OperationalPointDescription {
38+
trigram: NonBlankString,
39+
secondary_code: Option<String>,
40+
},
41+
OperationalPointUic {
42+
uic: NonBlankString,
43+
secondary_code: Option<String>,
44+
},
45+
}
46+
47+
#[derive(Debug, Clone, Serialize, Deserialize)]
48+
pub struct PathItem {
49+
id: NonBlankString,
50+
#[serde(flatten)]
51+
location: PathItemLocation,
52+
deleted: bool,
53+
}
54+
55+
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
56+
pub struct Margins {
57+
boudaries: Vec<NonBlankString>,
58+
values: Vec<MarginValue>,
59+
}
60+
61+
#[derive(Debug, Copy, Clone, Default)]
62+
enum MarginValue {
63+
#[default]
64+
Zero,
65+
Percentage(f64),
66+
MinPerKm(f64),
67+
}
68+
69+
impl<'de> Deserialize<'de> for MarginValue {
70+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
71+
where
72+
D: serde::Deserializer<'de>,
73+
{
74+
let value = String::deserialize(deserializer)?;
75+
if value == "0" {
76+
return Ok(Self::Zero);
77+
}
78+
if value.ends_with('%') {
79+
let float_value: f64 = f64::from_str(&value[0..value.len() - 1]).map_err(|_| {
80+
serde::de::Error::invalid_value(
81+
serde::de::Unexpected::Str(&value),
82+
&"a valid float",
83+
)
84+
})?;
85+
return Ok(Self::Percentage(float_value));
86+
}
87+
return Err(serde::de::Error::custom("Invalid margin value"));
88+
}
89+
}
90+
91+
impl Serialize for MarginValue {
92+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
93+
where
94+
S: serde::Serializer,
95+
{
96+
match self {
97+
MarginValue::Zero => serializer.serialize_str("0"),
98+
MarginValue::Percentage(value) => serializer.serialize_str(&format!("{}%", value)),
99+
MarginValue::MinPerKm(value) => serializer.serialize_str(&value.to_string()),
100+
}
101+
}
102+
}

editoast/src/tables.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -679,22 +679,22 @@ diesel::table! {
679679
id -> Int8,
680680
#[max_length = 128]
681681
train_name -> Varchar,
682-
labels -> Jsonb,
682+
labels -> Array<Nullable<Text>>,
683683
#[max_length = 128]
684-
rolling_stock_id -> Varchar,
684+
rolling_stock_name -> Varchar,
685685
timetable_id -> Int8,
686-
departure_time -> Timestamptz,
687-
scheduled_points -> Jsonb,
688-
allowances -> Jsonb,
686+
start_time -> Timestamptz,
687+
schedule -> Jsonb,
688+
margins -> Jsonb,
689689
initial_speed -> Float8,
690690
#[max_length = 8]
691691
comfort -> Varchar,
692692
path -> Jsonb,
693-
tag_constrains -> Jsonb,
694-
#[max_length = 128]
695-
speed_limit_tags -> Nullable<Varchar>,
696-
power_restriction_ranges -> Nullable<Jsonb>,
697-
options -> Nullable<Jsonb>,
693+
#[max_length = 8]
694+
constraint_distribution -> Varchar,
695+
speed_limit_tags -> Array<Nullable<Text>>,
696+
power_restrictions -> Jsonb,
697+
train_schedule_options -> Jsonb,
698698
}
699699
}
700700

editoast/src/views/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@ pub mod pathfinding;
1010
pub mod projects;
1111
pub mod rolling_stocks;
1212
pub mod scenario;
13-
pub mod scenariov2;
1413
pub mod search;
1514
mod single_simulation;
1615
pub mod sprites;
1716
pub mod stdcm;
1817
pub mod study;
1918
pub mod timetable;
20-
pub mod timetablev2;
2119
pub mod train_schedule;
20+
pub mod v2;
2221

2322
use self::openapi::{merge_path_items, remove_discriminator, OpenApiMerger, Routes};
2423
use crate::client::get_app_version;

editoast/src/views/v2/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod scenariov2;
2+
pub mod timetablev2;
3+
pub mod trainschedulev2;

editoast/src/views/scenariov2.rs editoast/src/views/v2/scenariov2.rs

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ use super::projects::QueryParams;
3333
crate::routes! {
3434
"/scenarios" => {
3535
create,
36-
list,
3736
"/{scenario_id}" => {
3837
get,
3938
delete,

0 commit comments

Comments
 (0)