Skip to content

Commit

Permalink
editoast: add STDCMCoreResponse
Browse files Browse the repository at this point in the history
Signed-off-by: hamz2a <atrari.hamza@gmail.com>
  • Loading branch information
hamz2a committed Oct 25, 2024
1 parent d26633e commit 217365c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 46 deletions.
8 changes: 0 additions & 8 deletions editoast/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2620,14 +2620,6 @@ paths:
type: string
enum:
- success
- type: object
required:
- status
properties:
status:
type: string
enum:
- path_not_found
- type: object
required:
- pathfinding_result
Expand Down
13 changes: 3 additions & 10 deletions editoast/src/core/stdcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,12 @@ use serde::Deserialize;
use serde::Serialize;
use utoipa::ToSchema;

use super::conflict_detection::Conflict;
use super::conflict_detection::TrainRequirements;
use super::pathfinding::PathfindingResult;
use super::pathfinding::PathfindingResultSuccess;
use super::pathfinding::TrackRange;
use super::simulation::PhysicsRollingStock;
use super::simulation::SimulationResponse;
use crate::core::{AsCoreRequest, Json};
use crate::views::path::pathfinding::PathfindingResult;

#[derive(Debug, Serialize)]
pub struct STDCMRequest {
Expand Down Expand Up @@ -116,28 +113,24 @@ pub struct UndirectedTrackRange {
pub end: u64,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, ToSchema)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(tag = "status", rename_all = "snake_case")]
// We accepted the difference of memory size taken by variants
// Since there is only on success and others are error cases
#[allow(clippy::large_enum_variant)]
pub enum STDCMResponse {
pub enum STDCMCoreResponse {
Success {
simulation: SimulationResponse,
path: PathfindingResultSuccess,
departure_time: DateTime<Utc>,
},
PathNotFound,
Conflicts {
pathfinding_result: PathfindingResult,
conflicts: Vec<Conflict>,
},
PreprocessingSimulationError {
error: SimulationResponse,
},
}

impl AsCoreRequest<Json<STDCMResponse>> for STDCMRequest {
impl AsCoreRequest<Json<STDCMCoreResponse>> for STDCMRequest {
const METHOD: reqwest::Method = reqwest::Method::POST;
const URL_PATH: &'static str = "/v2/stdcm";

Expand Down
4 changes: 2 additions & 2 deletions editoast/src/views/timetable/path_not_found_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use chrono::Utc;

use crate::core::conflict_detection::ConflictDetectionRequest;
use crate::core::conflict_detection::WorkSchedulesRequest;
use crate::core::pathfinding::PathfindingResult;
use crate::core::simulation::SimulationResponse;
use crate::core::stdcm::STDCMResponse;
use crate::core::AsCoreRequest;
use crate::core::CoreClient;
use crate::error::Result;
use crate::models::train_schedule::TrainSchedule;
use crate::models::work_schedules::WorkSchedule;
use crate::views::path::pathfinding::PathfindingResult;
use crate::views::timetable::stdcm::STDCMResponse;

use super::filter_core_work_schedule;
use super::stdcm::build_train_requirements;
Expand Down
78 changes: 55 additions & 23 deletions editoast/src/views/timetable/stdcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ use super::path_not_found_handler::PathNotFoundHandler;
use super::stdcm_request_payload::convert_steps;
use super::stdcm_request_payload::STDCMRequestPayload;
use super::SelectionSettings;
use crate::core::conflict_detection::Conflict;
use crate::core::conflict_detection::TrainRequirements;
use crate::core::pathfinding::InvalidPathItem;
use crate::core::pathfinding::PathfindingInputError;
use crate::core::pathfinding::PathfindingResultSuccess;
use crate::core::simulation::PhysicsRollingStock;
use crate::core::simulation::{RoutingRequirement, SimulationResponse, SpacingRequirement};
use crate::core::stdcm::STDCMCoreResponse;
use crate::core::stdcm::STDCMPathItem;
use crate::core::stdcm::STDCMRequest;
use crate::core::stdcm::STDCMResponse;
use crate::core::stdcm::STDCMStepTimingData;
use crate::core::AsCoreRequest;
use crate::core::CoreClient;
Expand All @@ -62,6 +64,26 @@ crate::routes! {
"/stdcm" => stdcm,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, ToSchema)]
#[serde(tag = "status", rename_all = "snake_case")]
// We accepted the difference of memory size taken by variants
// Since there is only on success and others are error cases
#[allow(clippy::large_enum_variant)]
pub enum STDCMResponse {
Success {
simulation: SimulationResponse,
path: PathfindingResultSuccess,
departure_time: DateTime<Utc>,
},
Conflicts {
pathfinding_result: PathfindingResult,
conflicts: Vec<Conflict>,
},
PreprocessingSimulationError {
error: SimulationResponse,
},
}

#[derive(Debug, Error, EditoastError, Serialize)]
#[editoast_error(base_id = "stdcm_v2")]
enum STDCMError {
Expand Down Expand Up @@ -247,28 +269,39 @@ async fn stdcm(

let stdcm_response = stdcm_request.fetch(core_client.as_ref()).await?;

// 8. Handle PathNotFound response of STDCM
if let STDCMResponse::PathNotFound = stdcm_response {
let path_not_found = PathNotFoundHandler {
core_client,
infra_id,
infra_version: infra.version,
train_schedules,
simulations,
work_schedules,
virtual_train_schedule,
virtual_train_sim_result,
virtual_train_pathfinding_result,
earliest_departure_time,
maximum_run_time,
latest_simulation_end,
};
let stdcm_response = path_not_found.handle().await?;

return Ok(Json(stdcm_response));
// 8. Handle STDCM Core Response
match stdcm_response {
STDCMCoreResponse::Success {
simulation,
path,
departure_time,
} => Ok(Json(STDCMResponse::Success {
simulation,
path,
departure_time,
})),
STDCMCoreResponse::PreprocessingSimulationError { error } => {
Ok(Json(STDCMResponse::PreprocessingSimulationError { error }))
}
STDCMCoreResponse::PathNotFound => {
let path_not_found = PathNotFoundHandler {
core_client,
infra_id,
infra_version: infra.version,
train_schedules,
simulations,
work_schedules,
virtual_train_schedule,
virtual_train_sim_result,
virtual_train_pathfinding_result,
earliest_departure_time,
maximum_run_time,
latest_simulation_end,
};
let stdcm_response = path_not_found.handle().await?;
Ok(Json(stdcm_response))
}
}

Ok(Json(stdcm_response))
}

/// Build the list of scheduled train requirements, only including requirements
Expand Down Expand Up @@ -520,7 +553,6 @@ mod tests {
use crate::core::simulation::SimulationParameters;
use crate::core::simulation::SimulationResponse;
use crate::core::simulation::SpeedLimitProperties;
use crate::core::stdcm::STDCMResponse;
use crate::models::fixtures::create_fast_rolling_stock;
use crate::models::fixtures::create_small_infra;
use crate::models::fixtures::create_timetable;
Expand Down
3 changes: 0 additions & 3 deletions front/src/common/api/generatedEditoastApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1514,9 +1514,6 @@ export type PostTimetableByIdStdcmApiResponse = /** status 201 The simulation re
simulation: SimulationResponse;
status: 'success';
}
| {
status: 'path_not_found';
}
| {
conflicts: Conflict[];
pathfinding_result: PathfindingResult;
Expand Down

0 comments on commit 217365c

Please sign in to comment.