Skip to content

Commit 62e717c

Browse files
committed
editoast: refactor move errors and delete models
1 parent 583b3f6 commit 62e717c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+323
-340
lines changed
File renamed without changes.

editoast/src/client/postgres_config.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use clap::Args;
22
use derivative::Derivative;
3+
use diesel::{Connection, PgConnection};
34

45
#[derive(Args, Debug, Derivative)]
56
#[derivative(Default)]
@@ -32,4 +33,8 @@ impl PostgresConfig {
3233
self.psql_database
3334
)
3435
}
36+
37+
pub fn make_connection(&self) -> PgConnection {
38+
PgConnection::establish(&self.url()).expect("Can't connect to database")
39+
}
3540
}

editoast/src/models/mod.rs editoast/src/db_connection.rs

-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
pub mod errors;
2-
pub mod infra;
3-
pub mod infra_errors;
4-
5-
pub use infra::{CreateInfra, Infra, InfraError};
6-
71
use rocket_contrib::databases::diesel;
82

93
#[database("postgres")]

editoast/src/models/errors/buffer_stops.rs editoast/src/errors/buffer_stops.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use diesel::sql_types::{Array, Integer, Json, Text};
22
use diesel::{sql_query, PgConnection, RunQueryDsl};
33

4-
use super::InfraError;
5-
use crate::objects::ObjectType;
6-
use crate::{infra_cache::InfraCache, objects::ObjectRef};
4+
use crate::infra_cache::InfraCache;
5+
use crate::schema::{InfraError, ObjectRef, ObjectType};
76
use diesel::result::Error as DieselError;
87
use serde_json::to_value;
98

@@ -18,7 +17,7 @@ pub fn insert_errors(
1817
let mut errors = vec![];
1918

2019
for error in infra_errors {
21-
buffer_stop_ids.push(error.obj_id.clone());
20+
buffer_stop_ids.push(error.get_id().clone());
2221
errors.push(to_value(error).unwrap());
2322
}
2423

@@ -73,10 +72,8 @@ pub fn generate_errors(infra_cache: &InfraCache) -> Vec<InfraError> {
7372
mod tests {
7473
use super::generate_errors;
7574
use super::InfraError;
76-
use crate::{
77-
infra_cache::tests::{create_buffer_stop_cache, create_small_infra_cache},
78-
objects::{ObjectRef, ObjectType},
79-
};
75+
use crate::infra_cache::tests::{create_buffer_stop_cache, create_small_infra_cache};
76+
use crate::schema::{ObjectRef, ObjectType};
8077

8178
#[test]
8279
fn invalid_ref() {

editoast/src/models/errors/detectors.rs editoast/src/errors/detectors.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use diesel::sql_types::{Array, Integer, Json, Text};
22
use diesel::{sql_query, PgConnection, RunQueryDsl};
33

4-
use super::InfraError;
5-
use crate::objects::ObjectType;
6-
use crate::{infra_cache::InfraCache, objects::ObjectRef};
4+
use crate::infra_cache::InfraCache;
5+
use crate::schema::{InfraError, ObjectRef, ObjectType};
76
use diesel::result::Error as DieselError;
87
use serde_json::to_value;
98

@@ -18,7 +17,7 @@ pub fn insert_errors(
1817
let mut errors = vec![];
1918

2019
for error in infra_errors {
21-
detector_ids.push(error.obj_id.clone());
20+
detector_ids.push(error.get_id().clone());
2221
errors.push(to_value(error).unwrap());
2322
}
2423

@@ -68,10 +67,8 @@ pub fn generate_errors(infra_cache: &InfraCache) -> Vec<InfraError> {
6867

6968
#[cfg(test)]
7069
mod tests {
71-
use crate::{
72-
infra_cache::tests::{create_detector_cache, create_small_infra_cache},
73-
objects::{ObjectRef, ObjectType},
74-
};
70+
use crate::infra_cache::tests::{create_detector_cache, create_small_infra_cache};
71+
use crate::schema::{ObjectRef, ObjectType};
7572

7673
use super::generate_errors;
7774
use super::InfraError;

editoast/src/models/errors/graph.rs editoast/src/errors/graph.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::{HashMap, HashSet};
22

33
use crate::infra_cache::InfraCache;
4-
use crate::objects::{ApplicableDirections, TrackEndpoint};
4+
use crate::schema::{ApplicableDirections, TrackEndpoint};
55

66
#[derive(Default, Clone, Debug)]
77
pub struct Graph<'a> {
@@ -72,11 +72,11 @@ impl<'a> Graph<'a> {
7272
mod tests {
7373
use std::collections::{HashMap, HashSet};
7474

75-
use crate::{
76-
infra_cache::tests::{create_small_infra_cache, create_track_endpoint},
77-
infra_cache::InfraCache,
78-
objects::Endpoint,
75+
use crate::infra_cache::{
76+
tests::{create_small_infra_cache, create_track_endpoint},
77+
InfraCache,
7978
};
79+
use crate::schema::Endpoint;
8080

8181
use super::Graph;
8282

editoast/src/errors/mod.rs

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
pub mod buffer_stops;
2+
pub mod detectors;
3+
pub mod graph;
4+
pub mod operational_points;
5+
pub mod routes;
6+
pub mod signals;
7+
pub mod speed_sections;
8+
pub mod switch_types;
9+
pub mod switches;
10+
pub mod track_section_links;
11+
pub mod track_sections;
12+
13+
use diesel::result::Error as DieselError;
14+
use diesel::sql_types::{BigInt, Integer, Json, Text};
15+
use diesel::{sql_query, PgConnection, RunQueryDsl};
16+
use serde::Serialize;
17+
use serde_json::Value;
18+
19+
use crate::api_error::ApiError;
20+
use crate::client::ChartosConfig;
21+
use crate::infra_cache::InfraCache;
22+
use crate::layer::invalidate_chartos_layer;
23+
use crate::views::pagination::{paginate, PaginationError};
24+
25+
use graph::Graph;
26+
27+
/// This function regenerate the errors and warnings of the infra
28+
pub fn generate_errors(
29+
conn: &PgConnection,
30+
infra: i32,
31+
infra_cache: &InfraCache,
32+
chartos_config: &ChartosConfig,
33+
) -> Result<(), DieselError> {
34+
// Clear the whole layer
35+
sql_query("DELETE FROM osrd_infra_errorlayer WHERE infra_id = $1")
36+
.bind::<Integer, _>(infra)
37+
.execute(conn)?;
38+
39+
// Create a graph for topological errors
40+
let graph = Graph::load(infra_cache);
41+
42+
// Generate the errors
43+
track_sections::insert_errors(conn, infra, infra_cache, &graph)?;
44+
signals::insert_errors(conn, infra, infra_cache)?;
45+
speed_sections::insert_errors(conn, infra, infra_cache)?;
46+
track_section_links::insert_errors(conn, infra, infra_cache)?;
47+
switch_types::insert_errors(conn, infra, infra_cache)?;
48+
switches::insert_errors(conn, infra, infra_cache)?;
49+
detectors::insert_errors(conn, infra, infra_cache)?;
50+
buffer_stops::insert_errors(conn, infra, infra_cache)?;
51+
routes::insert_errors(conn, infra, infra_cache, &graph)?;
52+
operational_points::insert_errors(conn, infra, infra_cache)?;
53+
54+
// Invalidate chartos cache
55+
invalidate_chartos_layer(infra, "errors", chartos_config);
56+
Ok(())
57+
}
58+
59+
#[derive(QueryableByName, Debug, Clone)]
60+
struct InfraErrorQueryable {
61+
#[sql_type = "BigInt"]
62+
pub count: i64,
63+
#[sql_type = "Text"]
64+
pub obj_id: String,
65+
#[sql_type = "Text"]
66+
pub obj_type: String,
67+
#[sql_type = "Json"]
68+
pub information: Value,
69+
}
70+
71+
#[derive(Debug, Clone, Serialize)]
72+
#[serde(deny_unknown_fields)]
73+
pub struct InfraErrorModel {
74+
pub obj_id: String,
75+
pub obj_type: String,
76+
pub information: Value,
77+
}
78+
79+
impl From<InfraErrorQueryable> for InfraErrorModel {
80+
fn from(error: InfraErrorQueryable) -> Self {
81+
Self {
82+
obj_id: error.obj_id,
83+
obj_type: error.obj_type,
84+
information: error.information,
85+
}
86+
}
87+
}
88+
89+
pub fn get_paginated_infra_errors(
90+
conn: &PgConnection,
91+
infra: i32,
92+
page: i64,
93+
per_page: i64,
94+
exclude_warnings: bool,
95+
) -> Result<(Vec<InfraErrorModel>, i64), Box<dyn ApiError>> {
96+
let mut query = String::from(
97+
"SELECT obj_id, obj_type, information::text FROM osrd_infra_errorlayer WHERE infra_id = $1",
98+
);
99+
if exclude_warnings {
100+
query += " AND information->>'is_warning' = 'false'"
101+
}
102+
let infra_errors = paginate(query, page, per_page)
103+
.bind::<Integer, _>(infra)
104+
.load::<InfraErrorQueryable>(conn)?;
105+
let count = infra_errors.first().map(|e| e.count).unwrap_or_default();
106+
let infra_errors: Vec<InfraErrorModel> = infra_errors.into_iter().map(|e| e.into()).collect();
107+
if infra_errors.is_empty() && page > 1 {
108+
return Err(Box::new(PaginationError));
109+
}
110+
Ok((infra_errors, count))
111+
}

editoast/src/models/errors/operational_points.rs editoast/src/errors/operational_points.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use diesel::sql_types::{Array, Integer, Json, Text};
22
use diesel::{sql_query, PgConnection, RunQueryDsl};
33

4-
use super::InfraError;
5-
use crate::objects::ObjectType;
6-
use crate::{infra_cache::InfraCache, objects::ObjectRef};
4+
use crate::infra_cache::InfraCache;
5+
use crate::schema::{InfraError, ObjectRef, ObjectType};
76
use diesel::result::Error as DieselError;
87
use serde_json::to_value;
98

@@ -18,7 +17,7 @@ pub fn insert_errors(
1817
let mut errors = vec![];
1918

2019
for error in infra_errors {
21-
op_ids.push(error.obj_id.clone());
20+
op_ids.push(error.get_id().clone());
2221
errors.push(to_value(error).unwrap());
2322
}
2423

@@ -79,10 +78,8 @@ pub fn generate_errors(infra_cache: &InfraCache) -> Vec<InfraError> {
7978

8079
#[cfg(test)]
8180
mod tests {
82-
use crate::{
83-
infra_cache::tests::{create_operational_point_cache, create_small_infra_cache},
84-
objects::{ObjectRef, ObjectType},
85-
};
81+
use crate::infra_cache::tests::{create_operational_point_cache, create_small_infra_cache};
82+
use crate::schema::{ObjectRef, ObjectType};
8683

8784
use super::generate_errors;
8885
use super::InfraError;

editoast/src/models/errors/routes.rs editoast/src/errors/routes.rs

+12-58
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,13 @@
1-
use std::fmt;
2-
3-
use diesel::sql_types::{Array, Integer, Json, Text};
4-
use diesel::{sql_query, PgConnection, RunQueryDsl};
5-
use serde::{Deserialize, Serialize};
6-
use strum::IntoEnumIterator;
7-
use strum_macros::EnumIter;
8-
91
use super::graph::Graph;
10-
use super::InfraError;
11-
use crate::objects::{Direction, DirectionalTrackRange, ObjectType, Route};
12-
use crate::{infra_cache::InfraCache, objects::ObjectRef};
2+
use crate::infra_cache::InfraCache;
3+
use crate::schema::{
4+
Direction, DirectionalTrackRange, InfraError, ObjectRef, ObjectType, PathEndpointField,
5+
};
136
use diesel::result::Error as DieselError;
7+
use diesel::sql_types::{Array, Integer, Json, Text};
8+
use diesel::{sql_query, PgConnection, RunQueryDsl};
149
use serde_json::to_value;
15-
16-
/// Represent the entry or exit point of a path
17-
#[derive(Serialize, Deserialize, Debug, Clone, Copy, EnumIter, PartialEq, Eq)]
18-
#[serde(deny_unknown_fields)]
19-
pub enum PathEndpointField {
20-
#[serde(rename = "entry_point")]
21-
EntryPoint,
22-
#[serde(rename = "exit_point")]
23-
ExitPoint,
24-
}
25-
26-
impl PathEndpointField {
27-
/// Given a path, retrieve track id and position offset of the path endpoint location
28-
pub fn get_path_location(&self, path: &[DirectionalTrackRange]) -> (String, f64) {
29-
let track_range = match self {
30-
PathEndpointField::EntryPoint => path.first().unwrap(),
31-
PathEndpointField::ExitPoint => path.last().unwrap(),
32-
};
33-
34-
let pos = match (self, &track_range.direction) {
35-
(PathEndpointField::EntryPoint, Direction::StartToStop) => track_range.begin,
36-
(PathEndpointField::EntryPoint, Direction::StopToStart) => track_range.end,
37-
(PathEndpointField::ExitPoint, Direction::StartToStop) => track_range.end,
38-
(PathEndpointField::ExitPoint, Direction::StopToStart) => track_range.begin,
39-
};
40-
41-
(track_range.track.obj_id.clone(), pos)
42-
}
43-
44-
pub fn get_route_endpoint<'a>(&self, route: &'a Route) -> &'a ObjectRef {
45-
match self {
46-
PathEndpointField::EntryPoint => &route.entry_point,
47-
PathEndpointField::ExitPoint => &route.exit_point,
48-
}
49-
}
50-
}
51-
52-
impl fmt::Display for PathEndpointField {
53-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54-
write!(f, "{}", serde_json::to_string(self).unwrap())
55-
}
56-
}
10+
use strum::IntoEnumIterator;
5711

5812
/// Given an ObjectRef, retrieve the id and position of its track section
5913
/// If the object type is not a detector or a buffer stop, return `None`
@@ -111,7 +65,7 @@ pub fn insert_errors(
11165
let mut errors = vec![];
11266

11367
for error in infra_errors {
114-
route_ids.push(error.obj_id.clone());
68+
route_ids.push(error.get_id().clone());
11569
errors.push(to_value(error).unwrap());
11670
}
11771

@@ -302,11 +256,11 @@ pub fn generate_errors(infra_cache: &InfraCache, graph: &Graph) -> Vec<InfraErro
302256

303257
#[cfg(test)]
304258
mod tests {
305-
use crate::{
306-
infra_cache::tests::{create_detector_cache, create_route_cache, create_small_infra_cache},
307-
models::errors::{graph::Graph, routes::PathEndpointField},
308-
objects::{Direction, ObjectRef, ObjectType},
259+
use crate::errors::graph::Graph;
260+
use crate::infra_cache::tests::{
261+
create_detector_cache, create_route_cache, create_small_infra_cache,
309262
};
263+
use crate::schema::{Direction, ObjectRef, ObjectType, PathEndpointField};
310264

311265
use super::generate_errors;
312266
use super::InfraError;

editoast/src/models/errors/signals.rs editoast/src/errors/signals.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use diesel::sql_types::{Array, Integer, Json, Text};
22
use diesel::{sql_query, PgConnection, RunQueryDsl};
33

4-
use super::InfraError;
5-
use crate::objects::ObjectType;
6-
use crate::{infra_cache::InfraCache, objects::ObjectRef};
4+
use crate::infra_cache::InfraCache;
5+
use crate::schema::{InfraError, ObjectRef, ObjectType};
76
use diesel::result::Error as DieselError;
87
use serde_json::to_value;
98

@@ -18,7 +17,7 @@ pub fn insert_errors(
1817
let mut errors = vec![];
1918

2019
for error in infra_errors {
21-
signal_ids.push(error.obj_id.clone());
20+
signal_ids.push(error.get_id().clone());
2221
errors.push(to_value(error).unwrap());
2322
}
2423

@@ -68,10 +67,8 @@ pub fn generate_errors(infra_cache: &InfraCache) -> Vec<InfraError> {
6867

6968
#[cfg(test)]
7069
mod tests {
71-
use crate::{
72-
infra_cache::tests::{create_signal_cache, create_small_infra_cache},
73-
objects::{ObjectRef, ObjectType},
74-
};
70+
use crate::infra_cache::tests::{create_signal_cache, create_small_infra_cache};
71+
use crate::schema::{ObjectRef, ObjectType};
7572

7673
use super::generate_errors;
7774
use super::InfraError;

0 commit comments

Comments
 (0)