1
- use crate :: core:: stdcm:: {
2
- STDCMCoreRequest , STDCMCoreResponse , STDCMCoreRouteOccupancy , STDCMCoreStep ,
3
- } ;
1
+ use crate :: core:: stdcm:: { STDCMCoreRequest , STDCMCoreResponse , STDCMCoreStep } ;
4
2
use crate :: core:: { AsCoreRequest , CoreClient } ;
5
- use crate :: diesel :: BelongingToDsl ;
3
+
6
4
use crate :: error:: Result ;
7
- use crate :: models:: train_schedule:: filter_invalid_trains;
8
5
pub use crate :: models:: train_schedule:: AllowanceValue ;
9
- use crate :: models:: { Create , SimulationOutput } ;
6
+ use crate :: models:: { Create , SpacingRequirement } ;
10
7
use crate :: models:: {
11
8
CurveGraph , Infra , PathWaypoint , Pathfinding , PathfindingChangeset , PathfindingPayload ,
12
9
Retrieve , SlopeGraph , TrainSchedule ,
@@ -16,14 +13,15 @@ use crate::DbPool;
16
13
use actix_web:: dev:: HttpServiceFactory ;
17
14
use actix_web:: web:: { self , Data , Json } ;
18
15
use actix_web:: { post, HttpResponse , Responder } ;
19
- use diesel_async :: RunQueryDsl ;
16
+
20
17
use editoast_derive:: EditoastError ;
21
18
use serde:: { Deserialize , Serialize } ;
22
19
use thiserror:: Error ;
23
20
24
21
use super :: pathfinding:: {
25
22
fetch_pathfinding_payload_track_map, parse_pathfinding_payload_waypoints, StepPayload ,
26
23
} ;
24
+ use super :: timetable:: get_simulated_schedules_from_timetable;
27
25
use super :: train_schedule:: process_simulation_response;
28
26
use super :: train_schedule:: projection:: Projection ;
29
27
use super :: train_schedule:: simulation_report:: { create_simulation_report, SimulationReport } ;
@@ -153,15 +151,14 @@ async fn call_core_stdcm(
153
151
let infra_version = infra. clone ( ) . version . unwrap ( ) ;
154
152
let rolling_stock = retrieve_existing_rolling_stock ( & db_pool, data. rolling_stock_id ) . await ?;
155
153
let steps = parse_stdcm_steps ( db_pool. clone ( ) , data, & infra) . await ?;
156
- let route_occupancies =
157
- make_route_occupancies ( infra_version. clone ( ) , db_pool, data. timetable_id ) . await ?;
154
+ let spacing_requirements = make_spacing_requirements ( db_pool, data. timetable_id ) . await ?;
158
155
STDCMCoreRequest {
159
156
infra : infra. id . unwrap ( ) . to_string ( ) ,
160
157
expected_version : infra_version,
161
158
rolling_stock,
162
159
comfort : data. comfort . clone ( ) ,
163
160
steps,
164
- route_occupancies ,
161
+ spacing_requirements ,
165
162
// end_time is not used by backend currently
166
163
// but at least one must be defined
167
164
start_time : data. start_time ,
@@ -207,47 +204,33 @@ async fn parse_stdcm_steps(
207
204
208
205
/// Create route occupancies, adjusted by simulation departure time.
209
206
/// uses base_simulation by default, or eco_simulation if given
210
- async fn make_route_occupancies (
211
- infra_version : String ,
207
+ async fn make_spacing_requirements (
212
208
db_pool : Data < DbPool > ,
213
209
timetable_id : i64 ,
214
- ) -> Result < Vec < STDCMCoreRouteOccupancy > > {
215
- let schedules = filter_invalid_trains ( db_pool. clone ( ) , timetable_id, infra_version) . await ?;
216
- let mut conn = db_pool. get ( ) . await ?;
217
- let ( simulations, schedules) : ( Vec < SimulationOutput > , Vec < TrainSchedule > ) = (
218
- SimulationOutput :: belonging_to ( & schedules)
219
- . load :: < SimulationOutput > ( & mut conn)
220
- . await ?,
221
- schedules,
222
- ) ;
223
- Ok ( simulations
224
- . iter ( )
225
- . filter_map ( |simulation| {
226
- if let Some ( schedule) = schedules
227
- . iter ( )
228
- . find ( |s| s. id == simulation. train_schedule_id )
229
- {
230
- let sim = simulation
231
- . eco_simulation
232
- . as_ref ( )
233
- . unwrap_or ( & simulation. base_simulation ) ;
234
- Some (
235
- sim. route_occupancies
236
- . iter ( )
237
- . map ( |( route_id, occupancy) | STDCMCoreRouteOccupancy {
238
- id : route_id. to_string ( ) ,
239
- start_occupancy_time : occupancy. time_head_occupy
240
- + schedule. departure_time ,
241
- end_occupancy_time : occupancy. time_tail_free + schedule. departure_time ,
242
- } )
243
- . collect :: < Vec < _ > > ( ) ,
244
- )
245
- } else {
246
- None
247
- }
210
+ ) -> Result < Vec < SpacingRequirement > > {
211
+ let ( schedules, simulations) =
212
+ get_simulated_schedules_from_timetable ( timetable_id, db_pool) . await ?;
213
+
214
+ let res = simulations
215
+ . into_iter ( )
216
+ . zip ( schedules)
217
+ . flat_map ( |( simulation, schedule) | {
218
+ let sim = simulation
219
+ . eco_simulation
220
+ . map ( |sim| sim. 0 )
221
+ . unwrap_or ( simulation. base_simulation . 0 ) ;
222
+
223
+ sim. spacing_requirements
224
+ . into_iter ( )
225
+ . map ( |req| SpacingRequirement {
226
+ zone : req. zone ,
227
+ begin_time : req. begin_time + schedule. departure_time ,
228
+ end_time : req. end_time + schedule. departure_time ,
229
+ } )
230
+ . collect :: < Vec < _ > > ( )
248
231
} )
249
- . flatten ( )
250
- . collect ( ) )
232
+ . collect ( ) ;
233
+ Ok ( res )
251
234
}
252
235
253
236
/// Creates a Pathfinding using the same function used with core /pathfinding response
0 commit comments