Skip to content

Commit

Permalink
editoast: move 'simulation_response' one module up
Browse files Browse the repository at this point in the history
Signed-off-by: Jean SIMARD <[email protected]>
Signed-off-by: Youness CHRIFI ALAOUI <[email protected]>
  • Loading branch information
woshilapin authored and younesschrifi committed Mar 7, 2025
1 parent 3624d5e commit 579cabf
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 45 deletions.
43 changes: 42 additions & 1 deletion editoast/src/views/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ use crate::client::get_app_version;
use crate::core::mq_client;
use crate::core::pathfinding::PathfindingInputError;
use crate::core::pathfinding::PathfindingNotFound;
use crate::core::simulation::SimulationResponse;
use crate::core::version::CoreVersionRequest;
use crate::core::AsCoreRequest;
use crate::core::CoreClient;
Expand All @@ -93,6 +94,7 @@ use crate::map::MapLayers;
use crate::models;
use crate::models::auth::PgAuthDriver;
use crate::valkey_utils::ValkeyConfig;
use crate::views::path::pathfinding::PathfindingFailure;
use crate::ValkeyClient;

crate::routes! {
Expand Down Expand Up @@ -172,7 +174,7 @@ pub struct InfraIdQueryParam {

#[derive(Debug, PartialEq, Serialize, Deserialize, ToSchema)]
#[serde(tag = "status", rename_all = "snake_case")]
pub enum SimulationSummaryResult {
enum SimulationSummaryResult {
/// Minimal information on a simulation's result
Success {
/// Length of a path in mm
Expand Down Expand Up @@ -201,6 +203,45 @@ pub enum SimulationSummaryResult {
PathfindingInputError(PathfindingInputError),
}

fn simulation_response(sim: SimulationResponse) -> SimulationSummaryResult {
match sim {
SimulationResponse::Success {
final_output,
provisional,
base,
..
} => {
let report = final_output.report_train;
SimulationSummaryResult::Success {
length: *report.positions.last().unwrap(),
time: *report.times.last().unwrap(),
energy_consumption: report.energy_consumption,
path_item_times_final: report.path_item_times.clone(),
path_item_times_provisional: provisional.path_item_times.clone(),
path_item_times_base: base.path_item_times.clone(),
}
}
SimulationResponse::PathfindingFailed { pathfinding_failed } => match pathfinding_failed {
PathfindingFailure::InternalError { core_error } => {
SimulationSummaryResult::PathfindingFailure { core_error }
}

PathfindingFailure::PathfindingInputError(input_error) => {
SimulationSummaryResult::PathfindingInputError(input_error)
}

PathfindingFailure::PathfindingNotFound(not_found) => {
SimulationSummaryResult::PathfindingNotFound(not_found)
}
},
SimulationResponse::SimulationFailed { core_error } => {
SimulationSummaryResult::SimulationFailed {
error_type: core_error.get_type().into(),
}
}
}
}

/// Represents the bundle of information about the issuer of a request
/// that can be extracted form recognized headers.
#[derive(Debug, Clone)]
Expand Down
39 changes: 35 additions & 4 deletions editoast/src/views/paced_train.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use utoipa::ToSchema;

use super::path::pathfinding::PathfindingResult;
use super::projection::ProjectPathForm;
use super::train_schedule::simulation_response;
use super::simulation_response;
use super::train_schedule::train_simulation_batch;
use super::AppState;
use super::AuthenticationExt;
Expand Down Expand Up @@ -560,15 +560,15 @@ mod tests {
},
provisional: ReportTrain {
positions: vec![],
times: vec![0, 10],
times: vec![],
speeds: vec![],
energy_consumption: 0.0,
path_item_times: vec![0, 1000, 2000, 3000]
},
final_output: CompleteReportTrain {
report_train: ReportTrain {
positions: vec![],
times: vec![],
positions: vec![0],
times: vec![0],
speeds: vec![],
energy_consumption: 0.0,
path_item_times: vec![0, 1000, 2000, 3000]
Expand All @@ -590,6 +590,21 @@ mod tests {
);
}

#[rstest]
async fn paced_train_simulation_not_found() {
let (app, infra_id, _paced_train_id) =
app_infra_id_paced_train_id_for_simulation_tests().await;
let request =
app.get(format!("/paced_train/{}/simulation/?infra_id={}", 0, infra_id).as_str());

let response: InternalError = app
.fetch(request)
.assert_status(StatusCode::NOT_FOUND)
.json_into();

assert_eq!(&response.error_type, "editoast:paced_train:NotFound")
}

#[rstest]
async fn paced_train_simulation_summary() {
let (app, infra_id, paced_train_id) =
Expand All @@ -613,4 +628,20 @@ mod tests {
}
);
}

#[rstest]
async fn paced_train_simulation_summary_not_found() {
let (app, infra_id, _paced_train_id) =
app_infra_id_paced_train_id_for_simulation_tests().await;
let request = app.post("/paced_train/simulation_summary").json(&json!({
"infra_id": infra_id,
"ids": vec![0],
}));
let response: InternalError = app
.fetch(request)
.assert_status(StatusCode::NOT_FOUND)
.json_into();

assert_eq!(&response.error_type, "editoast:paced_train:BatchNotFound")
}
}
41 changes: 1 addition & 40 deletions editoast/src/views/train_schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ use crate::models::prelude::*;
use crate::models::train_schedule::TrainSchedule;
use crate::models::train_schedule::TrainScheduleChangeset;
use crate::views::path::pathfinding::pathfinding_from_train;
use crate::views::path::pathfinding::PathfindingFailure;
use crate::views::path::pathfinding::PathfindingResult;
use crate::views::path::pathfinding_from_train_batch;
use crate::views::path::projection::PathProjection;
Expand All @@ -70,6 +69,7 @@ use crate::RollingStockModel;
use crate::ValkeyClient;

use super::projection::ProjectPathForm;
use super::simulation_response;
use super::SimulationSummaryResult;

crate::routes! {
Expand Down Expand Up @@ -636,45 +636,6 @@ struct SimulationBatchForm {
ids: HashSet<i64>,
}

pub fn simulation_response(sim: SimulationResponse) -> SimulationSummaryResult {
match sim {
SimulationResponse::Success {
final_output,
provisional,
base,
..
} => {
let report = final_output.report_train;
SimulationSummaryResult::Success {
length: *report.positions.last().unwrap(),
time: *report.times.last().unwrap(),
energy_consumption: report.energy_consumption,
path_item_times_final: report.path_item_times.clone(),
path_item_times_provisional: provisional.path_item_times.clone(),
path_item_times_base: base.path_item_times.clone(),
}
}
SimulationResponse::PathfindingFailed { pathfinding_failed } => match pathfinding_failed {
PathfindingFailure::InternalError { core_error } => {
SimulationSummaryResult::PathfindingFailure { core_error }
}

PathfindingFailure::PathfindingInputError(input_error) => {
SimulationSummaryResult::PathfindingInputError(input_error)
}

PathfindingFailure::PathfindingNotFound(not_found) => {
SimulationSummaryResult::PathfindingNotFound(not_found)
}
},
SimulationResponse::SimulationFailed { core_error } => {
SimulationSummaryResult::SimulationFailed {
error_type: core_error.get_type().into(),
}
}
}
}

/// Associate each train id with its simulation summary response
/// If the simulation fails, it associates the reason: pathfinding failed or running time failed
#[utoipa::path(
Expand Down

0 comments on commit 579cabf

Please sign in to comment.