Skip to content

Commit

Permalink
wip start refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Khoyo committed Dec 19, 2023
1 parent 12108d7 commit 7660a6a
Showing 1 changed file with 31 additions and 20 deletions.
51 changes: 31 additions & 20 deletions editoast/src/views/timetable/import.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::schema::OperationalPointPart;
use crate::views::timetable::TimetableError;
use crate::{core::CoreClient, views::timetable::Path, DbPool};
use actix_web::{post, web::Data};
Expand Down Expand Up @@ -114,17 +115,18 @@ pub async fn post_timetable(
return Err(TimetableError::InfraNotLoaded { infra_id }.into());
}

let pathfinding_request = PathfindingRequest::new(infra_id, infra_version.clone());
for import_item in data.into_inner() {
// PATHFINDING
let mut pf_request = pathfinding_request.clone();
let mut pf_request = PathfindingRequest::new(infra_id, infra_version.clone());
let Some(rolling_stock_model) =
RollingStockModel::retrieve_by_name(&mut conn, import_item.rolling_stock.clone())
.await?
else {
continue;
};
let rolling_stock: RollingStock = rolling_stock_model.clone().into();
let rolling_stock_id = rolling_stock_model.id.unwrap();
let rollingstock_version = rolling_stock_model.version;
let rolling_stock: RollingStock = rolling_stock_model.into();
pf_request.with_rolling_stocks(&mut vec![rolling_stock.clone()]);
// List operational points uic needed for this import
let mut ops_uic = import_item
Expand Down Expand Up @@ -153,22 +155,9 @@ pub async fn post_timetable(
continue;
}
// Create waypoints
let mut waypoints = PathfindingWaypoints::new();
for step in import_item.path.iter() {
waypoints.push(match &step.location {
TimetableImportPathLocation::TrackOffsetLocation {
track_section,
offset,
} => Vec::from(Waypoint::bidirectional(track_section, *offset)),
TimetableImportPathLocation::OperationalPointLocation { uic } => op_id_to_parts
.get(uic)
.unwrap()
.iter()
.flat_map(|op_part| Waypoint::bidirectional(&op_part.track, op_part.position))
.collect(),
});
}
let mut waypoints = waypoints_from_steps(&import_item.path, &op_id_to_parts);
pf_request.with_waypoints(&mut waypoints);

// Run pathfinding
// TODO: Stops duration should be associated to trains not path
let steps_duration = vec![0.; pf_request.nb_waypoints()];
Expand Down Expand Up @@ -280,10 +269,10 @@ pub async fn post_timetable(
departure_time: import_train.departure_time.num_seconds_from_midnight() as f64,
scheduled_points: Default::default(), // TODO change
path_id,
rolling_stock_id: rolling_stock_model.id.unwrap(),
rolling_stock_id,
timetable_id,
infra_version: Some(infra_version.clone()),
rollingstock_version: rolling_stock_model.version,
rollingstock_version,
..Default::default()
}
.create_conn(&mut conn)
Expand All @@ -297,3 +286,25 @@ pub async fn post_timetable(

Ok(HttpResponse::NoContent().finish())
}

fn waypoints_from_steps(
path: &Vec<TimetableImportPathStep>,
op_id_to_parts: &HashMap<i64, Vec<OperationalPointPart>>,
) -> Vec<Vec<Waypoint>> {
let mut res = PathfindingWaypoints::new();
for step in path {
res.push(match &step.location {
TimetableImportPathLocation::TrackOffsetLocation {
track_section,
offset,
} => Vec::from(Waypoint::bidirectional(track_section, *offset)),
TimetableImportPathLocation::OperationalPointLocation { uic } => op_id_to_parts
.get(uic)
.unwrap()
.iter()
.flat_map(|op_part| Waypoint::bidirectional(&op_part.track, op_part.position))
.collect(),
});
}
res
}

0 comments on commit 7660a6a

Please sign in to comment.