-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathelectrifications.rs
349 lines (320 loc) · 12.1 KB
/
electrifications.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
use std::collections::HashMap;
use std::collections::HashSet;
use std::fmt::Debug;
use actix_web::get;
use actix_web::web::Data;
use actix_web::web::Json;
use actix_web::web::Path;
use chashmap::CHashMap;
use editoast_common::rangemap_utils::RangedValue;
use rangemap::RangeMap;
use serde::Deserialize;
use serde::Serialize;
use utoipa::ToSchema;
use crate::error::InternalError;
use crate::error::Result;
use crate::infra_cache::InfraCache;
use crate::models::pathfinding::Pathfinding;
use crate::models::Retrieve;
use crate::modelsv2::Infra;
use crate::modelsv2::Retrieve as RetrieveV2;
use crate::schema::ObjectType;
use crate::views::pathfinding::path_rangemap::make_path_range_map;
use crate::views::pathfinding::path_rangemap::TrackMap;
use crate::views::pathfinding::PathfindingError;
use crate::views::pathfinding::PathfindingIdParam;
use crate::DbPool;
crate::routes! {
electrifications_on_path,
}
crate::schemas! {
ElectrificationsOnPathResponse,
&editoast_common::rangemap_utils::RangedValue,
}
/// Build a rangemap for each track section, giving the voltage for each range
fn map_electrification_modes(
infra_cache: &InfraCache,
track_ids: HashSet<String>,
) -> (TrackMap<String>, Vec<InternalError>) {
let mut warnings = Vec::new();
let unique_electrification_ids = track_ids
.iter()
.flat_map(|track_id| {
infra_cache
.get_track_refs_type(track_id, ObjectType::Electrification)
.into_iter()
})
.map(|electrification_ref| &electrification_ref.obj_id)
.collect::<HashSet<_>>();
let mut res = HashMap::new();
for electrification_id in unique_electrification_ids {
let electrification = infra_cache
.electrifications()
.get(electrification_id)
.expect("electrification not found")
.unwrap_electrification();
let mut overlapping_ranges = Vec::new();
for track_range in &electrification.track_ranges {
if track_ids.contains(track_range.track.as_str()) {
let res_entry = res
.entry(track_range.track.to_string())
.or_insert_with(RangeMap::new);
let range = track_range.begin.into()..track_range.end.into();
if res_entry.overlaps(&range) {
overlapping_ranges.push(track_range.clone());
}
res_entry.insert(range, electrification.voltage.0.clone());
}
}
if !overlapping_ranges.is_empty() {
warnings.push(
PathfindingError::ElectrificationOverlap {
electrification_id: electrification_id.to_string(),
overlapping_ranges,
}
.into(),
);
}
}
(res, warnings)
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
/// A list of ranges associated to electrification modes. When a profile overlapping another is found,
/// a warning is added to the list
struct ElectrificationsOnPathResponse {
electrification_ranges: Vec<RangedValue>,
warnings: Vec<InternalError>,
}
#[utoipa::path(
tag = "infra",
params(PathfindingIdParam),
responses(
(status = 200, body = ElectrificationsOnPathResponse),
)
)]
#[get("/electrifications")]
/// Retrieve the electrification modes along a path, as seen by the rolling stock specified
async fn electrifications_on_path(
params: Path<PathfindingIdParam>,
db_pool: Data<DbPool>,
infra_caches: Data<CHashMap<i64, InfraCache>>,
) -> Result<Json<ElectrificationsOnPathResponse>> {
let mut conn = db_pool.get().await?;
let pathfinding_id = params.pathfinding_id;
let pathfinding = match Pathfinding::retrieve_conn(&mut conn, pathfinding_id).await? {
Some(pf) => pf,
None => return Err(PathfindingError::NotFound { pathfinding_id }.into()),
};
let infra = <Infra as RetrieveV2<_>>::retrieve(&mut conn, pathfinding.infra_id)
.await?
.expect("Foreign key constraint not respected");
let track_section_ids = pathfinding.track_section_ids();
let infra_cache = InfraCache::get_or_load(&mut conn, &infra_caches, &infra).await?;
let (electrification_mode_map, warnings) =
map_electrification_modes(&infra_cache, track_section_ids);
let res = make_path_range_map(&electrification_mode_map, &pathfinding);
Ok(Json(ElectrificationsOnPathResponse {
electrification_ranges: RangedValue::list_from_range_map(&res),
warnings,
}))
}
#[cfg(test)]
pub mod tests {
use actix_http::StatusCode;
use actix_web::test::call_service;
use actix_web::test::read_body_json;
use actix_web::test::TestRequest;
use editoast_common::range_map;
use rstest::*;
use serde_json::from_value;
use ApplicableDirections::*;
use super::*;
use crate::fixtures::tests::db_pool;
use crate::fixtures::tests::empty_infra;
use crate::fixtures::tests::TestFixture;
use crate::models::pathfinding::tests::simple_pathfinding_fixture;
use crate::modelsv2::prelude::*;
use crate::modelsv2::ElectrificationModel;
use crate::schema::ApplicableDirections;
use crate::schema::ApplicableDirectionsTrackRange;
use crate::schema::Electrification as ElectrificationSchema;
use crate::views::tests::create_test_service;
#[fixture]
fn simple_mode_map() -> TrackMap<String> {
// The mode map associated to the following electrifications
let mut mode_map = [
("track_1", "25kV"),
("track_2", "25kV"),
("track_5", "1.5kV"),
]
.iter()
.map(|(track, voltage)| (track.to_string(), range_map!(0.0, 10.0 => *voltage)))
.collect::<HashMap<_, _>>();
mode_map.insert(
"track_3".to_string(),
range_map!(0.0, 5.0 => "25kV", 5.0, 10.0 => "1.5kV"),
);
mode_map
}
#[fixture]
async fn infra_with_electrifications(
db_pool: Data<DbPool>,
#[future] empty_infra: TestFixture<Infra>,
) -> TestFixture<Infra> {
let infra = empty_infra.await;
// See the diagram in `models::pathfinding::tests::simple_path` to see how the track sections are connected.
let electrification_schemas = vec![
ElectrificationSchema {
track_ranges: vec![
ApplicableDirectionsTrackRange::new("track_1", 0.0, 10.0, Both),
ApplicableDirectionsTrackRange::new("track_2", 5.0, 10.0, Both),
],
voltage: "25kV".into(),
id: "electrification_1".into(),
},
ElectrificationSchema {
track_ranges: vec![
ApplicableDirectionsTrackRange::new("track_2", 0.0, 5.0, Both),
ApplicableDirectionsTrackRange::new("track_3", 0.0, 5.0, Both),
],
voltage: "25kV".into(),
..Default::default()
},
ElectrificationSchema {
track_ranges: vec![
ApplicableDirectionsTrackRange::new("track_3", 5.0, 10.0, Both),
ApplicableDirectionsTrackRange::new("track_5", 0.0, 10.0, Both),
],
voltage: "1.5kV".into(),
..Default::default()
},
];
ElectrificationModel::create_batch::<_, Vec<_>>(
&mut db_pool.get().await.unwrap(),
ElectrificationModel::from_infra_schemas(infra.id(), electrification_schemas),
)
.await
.expect("Could not create electrifications");
infra
}
#[rstest]
async fn test_map_electrification_modes(
db_pool: Data<DbPool>,
#[future] infra_with_electrifications: TestFixture<Infra>,
simple_mode_map: TrackMap<String>,
) {
let mut conn = db_pool.get().await.unwrap();
let infra_with_electrifications = infra_with_electrifications.await;
let infra_cache = InfraCache::load(&mut conn, &infra_with_electrifications.model)
.await
.expect("Could not load infra_cache");
let track_sections: HashSet<_> =
vec!["track_1", "track_2", "track_3", "track_4", "track_5"]
.into_iter()
.map(|s| s.to_string())
.collect();
let (mode_map, warnings) = map_electrification_modes(&infra_cache, track_sections);
assert_eq!(mode_map, simple_mode_map);
assert!(warnings.is_empty());
}
#[rstest]
async fn test_map_electrification_modes_with_warnings(
db_pool: Data<DbPool>,
#[future] infra_with_electrifications: TestFixture<Infra>,
) {
let mut conn = db_pool.get().await.unwrap();
let infra_with_electrifications = infra_with_electrifications.await;
let mut infra_cache = InfraCache::load(&mut conn, &infra_with_electrifications.model)
.await
.expect("Could not load infra_cache");
infra_cache
.add(ElectrificationSchema {
track_ranges: vec![ApplicableDirectionsTrackRange::new(
"track_1", 0.0, 10.0, Both,
)],
voltage: "25kV".into(),
id: "electrification_that_overlaps".into(),
})
.unwrap();
let track_sections: HashSet<_> =
vec!["track_1", "track_2", "track_3", "track_4", "track_5"]
.into_iter()
.map(|s| s.to_string())
.collect();
let (_, warnings) = map_electrification_modes(&infra_cache, track_sections);
assert_eq!(warnings.len(), 1);
let warning = &warnings[0];
assert!(warning.get_context().contains_key("electrification_id"));
let electrification_id: String = from_value(
warning
.get_context()
.get("electrification_id")
.unwrap()
.clone(),
)
.unwrap();
assert!(
electrification_id == "electrification_that_overlaps"
|| electrification_id == "electrification_1"
);
assert!(warning.get_context().contains_key("overlapping_ranges"));
let overlapping_ranges: Vec<ApplicableDirectionsTrackRange> = from_value(
warning
.get_context()
.get("overlapping_ranges")
.unwrap()
.clone(),
)
.unwrap();
assert_eq!(overlapping_ranges.len(), 1);
assert_eq!(
overlapping_ranges[0],
ApplicableDirectionsTrackRange::new("track_1", 0.0, 10.0, Both)
);
}
#[rstest]
async fn test_view_electrifications_on_path(
db_pool: Data<DbPool>,
#[future] infra_with_electrifications: TestFixture<Infra>,
) {
let infra_with_electrifications = infra_with_electrifications.await;
let pathfinding =
simple_pathfinding_fixture(infra_with_electrifications.id(), db_pool.clone()).await;
let app = create_test_service().await;
let req = TestRequest::get()
.uri(&format!(
"/pathfinding/{}/electrifications/",
pathfinding.id()
))
.to_request();
let response = call_service(&app, req).await;
assert_eq!(response.status(), StatusCode::OK);
let response: ElectrificationsOnPathResponse = read_body_json(response).await;
assert!(response.warnings.is_empty());
assert_eq!(response.electrification_ranges.len(), 3);
assert_eq!(
response.electrification_ranges[0],
RangedValue {
begin: 0.0,
end: 25.0,
value: "25kV".into(),
}
);
assert_eq!(
response.electrification_ranges[1],
RangedValue {
begin: 25.0,
end: 30.0,
value: "1.5kV".into(),
}
);
assert_eq!(
response.electrification_ranges[2],
RangedValue {
begin: 40.0,
end: 48.0,
value: "1.5kV".into(),
}
);
}
}