Skip to content

Commit

Permalink
front: fix pr comments
Browse files Browse the repository at this point in the history
Signed-off-by: Egor Berezovskiy <[email protected]>
  • Loading branch information
Wadjetz committed Nov 22, 2024
1 parent 8bec4b7 commit 12ba25e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 97 deletions.
5 changes: 0 additions & 5 deletions editoast/src/core/simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,6 @@ pub struct PhysicsConsistParameters {
}

impl PhysicsConsistParameters {
pub fn set_traction_engine(mut self, traction_engine: RollingStock) -> Self {
self.traction_engine = traction_engine;
self
}

pub fn from_traction_engine(traction_engine: RollingStock) -> Self {
PhysicsConsistParameters {
max_speed: None,
Expand Down
25 changes: 20 additions & 5 deletions editoast/src/views/timetable/stdcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,17 @@ async fn stdcm(
conn,
valkey_client.clone(),
core_client.clone(),
&train_schedules,
&infra,
&train_schedules,
vec![rolling_stock.clone()]
.into_iter()
.map(|rs| (rs.name.clone(), rs.clone()))
.collect(),
vec![physics_consist_parameters.clone()]
.into_iter()
.map(|consist| (consist.traction_engine.name.clone(), consist))
.collect(),
stdcm_request.electrical_profile_set_id,
physics_consist_parameters.clone(),
)
.await?
.into_iter()
Expand All @@ -226,8 +233,9 @@ async fn stdcm(
let stdcm_request = crate::core::stdcm::Request {
infra: infra.id,
expected_version: infra.version.clone(),
rolling_stock_loading_gauge: rolling_stock.loading_gauge,
rolling_stock_loading_gauge: rolling_stock.clone().loading_gauge,
rolling_stock_supported_signaling_systems: rolling_stock
.clone()
.supported_signaling_systems
.clone(),
rolling_stock: physics_consist_parameters.into(),
Expand Down Expand Up @@ -424,10 +432,17 @@ impl VirtualTrainRun {
&mut db_pool.get().await?,
valkey_client,
core_client,
&[train_schedule.clone()],
infra,
&[train_schedule.clone()],
vec![rolling_stock]
.into_iter()
.map(|rs| (rs.name.clone(), rs.clone()))
.collect(),
vec![consist_parameters.clone()]
.into_iter()
.map(|consist| (consist.traction_engine.name.clone(), consist))
.collect(),
None,
consist_parameters.clone(),
)
.await?
.pop()
Expand Down
135 changes: 48 additions & 87 deletions editoast/src/views/train_schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ use crate::views::AuthorizationError;
use crate::AppState;
use crate::RollingStockModel;
use crate::ValkeyClient;
use crate::ValkeyConnection;
use editoast_derive::EditoastError;
use editoast_models::DbConnection;
use editoast_models::DbConnectionPoolV2;
Expand Down Expand Up @@ -375,75 +374,84 @@ async fn simulation(
Ok(Json(simulation))
}

/// Compute in batch the simulation of a list of train schedule and consist parameters
async fn retrieve_rolling_stocks_from_train_schedules(
conn: &mut DbConnection,
train_schedules: &[TrainSchedule],
) -> Result<HashMap<String, RollingStockModel>> {
let rolling_stocks_ids = train_schedules
.iter()
.map::<String, _>(|t| t.rolling_stock_name.clone());
let (rolling_stocks, _): (Vec<_>, _) =
RollingStockModel::retrieve_batch(conn, rolling_stocks_ids).await?;

let rolling_stocks: HashMap<_, _> = rolling_stocks
.into_iter()
.map(|rs| (rs.name.clone(), rs))
.collect();

Ok(rolling_stocks)
}

/// Compute in batch the simulation of a list of train schedule
///
/// Note: The order of the returned simulations is the same as the order of the train schedules.
pub async fn consist_train_simulation_batch(
pub async fn train_simulation_batch(
conn: &mut DbConnection,
valkey_client: Arc<ValkeyClient>,
core: Arc<CoreClient>,
train_schedules: &[TrainSchedule],
infra: &Infra,
electrical_profile_set_id: Option<i64>,
physics_consist_parameters: PhysicsConsistParameters,
) -> Result<Vec<(SimulationResponse, PathfindingResult)>> {
// Compute path
let rolling_stocks =
retrieve_rolling_stocks_from_train_schedules(conn, train_schedules).await?;

let mut valkey_conn = valkey_client.get_connection().await?;
let consists: HashMap<String, PhysicsConsistParameters> = rolling_stocks
.iter()
.map(|(key, rs)| {
(
key.clone(),
PhysicsConsistParameters::from_traction_engine(rs.clone().into()),
)
})
.collect();

let pathfinding_results = pathfinding_from_train_batch(
consist_train_simulation_batch(
conn,
&mut valkey_conn,
core.clone(),
infra,
train_schedules,
&rolling_stocks,
)
.await?;

get_train_batch_simulation(
&mut valkey_conn,
valkey_client,
core.clone(),
infra,
train_schedules,
rolling_stocks,
pathfinding_results,
consists,
electrical_profile_set_id,
Some(physics_consist_parameters),
)
.await
}

async fn retrieve_rolling_stocks_from_train_schedules(
pub async fn consist_train_simulation_batch(
conn: &mut DbConnection,
train_schedules: &[TrainSchedule],
) -> Result<HashMap<String, RollingStockModel>> {
let rolling_stocks_ids = train_schedules
.iter()
.map::<String, _>(|t| t.rolling_stock_name.clone());
let (rolling_stocks, _): (Vec<_>, _) =
RollingStockModel::retrieve_batch(conn, rolling_stocks_ids).await?;

let rolling_stocks: HashMap<_, _> = rolling_stocks
.into_iter()
.map(|rs| (rs.name.clone(), rs))
.collect();

Ok(rolling_stocks)
}

async fn get_train_batch_simulation(
valkey_conn: &mut ValkeyConnection,
valkey_client: Arc<ValkeyClient>,
core: Arc<CoreClient>,
infra: &Infra,
train_schedules: &[TrainSchedule],
rolling_stocks: HashMap<String, RollingStockModel>,
pathfinding_results: Vec<PathfindingResult>,
consists: HashMap<String, PhysicsConsistParameters>,
electrical_profile_set_id: Option<i64>,
physics_consist_parameters: Option<PhysicsConsistParameters>,
) -> Result<Vec<(SimulationResponse, PathfindingResult)>> {
let mut valkey_conn = valkey_client.get_connection().await?;

let pathfinding_results = pathfinding_from_train_batch(
conn,
&mut valkey_conn,
core.clone(),
infra,
train_schedules,
&rolling_stocks,
)
.await?;

let mut simulation_results = vec![SimulationResponse::default(); train_schedules.len()];
let mut to_sim = Vec::with_capacity(train_schedules.len());
for (index, (pathfinding, train_schedule)) in
Expand Down Expand Up @@ -474,14 +482,7 @@ async fn get_train_batch_simulation(
};

// Build simulation request
let rolling_stock = rolling_stocks[&train_schedule.rolling_stock_name].clone();

let physics_consist_parameters = physics_consist_parameters
.clone()
.map(|params| params.set_traction_engine(rolling_stock.clone().into()))
.unwrap_or_else(|| {
PhysicsConsistParameters::from_traction_engine(rolling_stock.into())
});
let physics_consist_parameters = consists[&train_schedule.rolling_stock_name].clone();

let simulation_request = build_simulation_request(
infra,
Expand Down Expand Up @@ -548,46 +549,6 @@ async fn get_train_batch_simulation(
.collect())
}

/// Compute in batch the simulation of a list of train schedule
///
/// Note: The order of the returned simulations is the same as the order of the train schedules.
pub async fn train_simulation_batch(
conn: &mut DbConnection,
valkey_client: Arc<ValkeyClient>,
core: Arc<CoreClient>,
train_schedules: &[TrainSchedule],
infra: &Infra,
electrical_profile_set_id: Option<i64>,
) -> Result<Vec<(SimulationResponse, PathfindingResult)>> {
// Compute path
let rolling_stocks =
retrieve_rolling_stocks_from_train_schedules(conn, train_schedules).await?;

let mut valkey_conn = valkey_client.get_connection().await?;

let pathfinding_results = pathfinding_from_train_batch(
conn,
&mut valkey_conn,
core.clone(),
infra,
train_schedules,
&rolling_stocks,
)
.await?;

get_train_batch_simulation(
&mut valkey_conn,
core.clone(),
infra,
train_schedules,
rolling_stocks,
pathfinding_results,
electrical_profile_set_id,
None,
)
.await
}

fn build_simulation_request(
infra: &Infra,
train_schedule: &TrainSchedule,
Expand Down

0 comments on commit 12ba25e

Please sign in to comment.