Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

editoast: get paced train path endpont #11051

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions editoast/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5142,6 +5142,7 @@ components:
- $ref: '#/components/schemas/EditoastOperationErrorObjectNotFound'
- $ref: '#/components/schemas/EditoastPacedTrainErrorBatchPacedTrainNotFound'
- $ref: '#/components/schemas/EditoastPacedTrainErrorDatabase'
- $ref: '#/components/schemas/EditoastPacedTrainErrorInfraNotFound'
- $ref: '#/components/schemas/EditoastPacedTrainErrorPacedTrainNotFound'
- $ref: '#/components/schemas/EditoastPaginationErrorInvalidPage'
- $ref: '#/components/schemas/EditoastPaginationErrorInvalidPageSize'
Expand Down Expand Up @@ -5680,6 +5681,30 @@ components:
type: string
enum:
- editoast:paced_train:Database
EditoastPacedTrainErrorInfraNotFound:
type: object
required:
- type
- status
- message
properties:
context:
type: object
required:
- infra_id
properties:
infra_id:
type: integer
message:
type: string
status:
type: integer
enum:
- 404
type:
type: string
enum:
- editoast:paced_train:InfraNotFound
EditoastPacedTrainErrorPacedTrainNotFound:
type: object
required:
Expand Down
26 changes: 24 additions & 2 deletions editoast/src/models/paced_train.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ use editoast_schemas::train_schedule::ScheduleItem;
use editoast_schemas::train_schedule::TrainScheduleBase;
use editoast_schemas::train_schedule::TrainScheduleOptions;

use crate::models::prelude::*;

use super::train_schedule::TrainSchedule;
use super::Tags;
use crate::models::prelude::*;

#[derive(Debug, Clone, Model)]
#[model(table = editoast_models::tables::paced_train)]
Expand Down Expand Up @@ -101,3 +101,25 @@ impl From<PacedTrain> for PacedTrainBase {
}
}
}

impl From<PacedTrain> for TrainSchedule {
fn from(paced_train: PacedTrain) -> Self {
Self {
id: paced_train.id,
train_name: paced_train.train_name,
labels: paced_train.labels.into(),
rolling_stock_name: paced_train.rolling_stock_name,
timetable_id: paced_train.timetable_id,
path: paced_train.path,
start_time: paced_train.start_time,
schedule: paced_train.schedule,
margins: paced_train.margins,
initial_speed: paced_train.initial_speed,
comfort: paced_train.comfort,
constraint_distribution: paced_train.constraint_distribution,
speed_limit_tag: paced_train.speed_limit_tag,
power_restrictions: paced_train.power_restrictions,
options: paced_train.options,
}
}
}
98 changes: 87 additions & 11 deletions editoast/src/views/paced_train.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use thiserror::Error;
use utoipa::IntoParams;
use utoipa::ToSchema;

use super::path::pathfinding::pathfinding_from_train;
use super::path::pathfinding::PathfindingResult;
use super::projection::ProjectPathForm;
use super::AppState;
Expand All @@ -27,6 +28,7 @@ use crate::core::simulation::SimulationResponse;
use crate::error::Result;
use crate::models::paced_train::PacedTrain;
use crate::models::prelude::*;
use crate::models::Infra;
use crate::views::projection::ProjectPathTrainResult;
use crate::views::AuthorizationError;
use crate::views::ListId;
Expand Down Expand Up @@ -60,6 +62,9 @@ enum PacedTrainError {
#[editoast_error(status = 404)]
#[error("Paced train {paced_train_id} does not exist")]
PacedTrainNotFound { paced_train_id: i64 },
#[error("Infra '{infra_id}', could not be found")]
#[editoast_error(status = 404)]
InfraNotFound { infra_id: i64 },
#[error(transparent)]
#[editoast_error(status = 500)]
Database(#[from] editoast_models::model::Error),
Expand Down Expand Up @@ -229,20 +234,36 @@ async fn simulation_summary(
)]
async fn get_path(
State(AppState {
db_pool: _db_pool,
valkey: _valkey_client,
core_client: _core,
db_pool,
valkey: valkey_client,
core_client: core,
..
}): State<AppState>,
Extension(_auth): AuthenticationExt,
Path(PacedTrainIdParam {
id: _paced_train_id,
}): Path<PacedTrainIdParam>,
Query(InfraIdQueryParam {
infra_id: _infra_id,
}): Query<InfraIdQueryParam>,
Extension(auth): AuthenticationExt,
Path(PacedTrainIdParam { id: paced_train_id }): Path<PacedTrainIdParam>,
Query(InfraIdQueryParam { infra_id }): Query<InfraIdQueryParam>,
) -> Result<Json<PathfindingResult>> {
todo!();
let authorized = auth
.check_roles([BuiltinRole::OperationalStudies, BuiltinRole::Stdcm].into())
.await
.map_err(AuthorizationError::AuthError)?;
if !authorized {
return Err(AuthorizationError::Forbidden.into());
}
let conn = &mut db_pool.get().await?;
let mut valkey_conn = valkey_client.get_connection().await?;

let infra = Infra::retrieve_or_fail(conn, infra_id, || PacedTrainError::InfraNotFound {
infra_id,
})
.await?;
let paced_train = PacedTrain::retrieve_or_fail(conn, paced_train_id, || {
PacedTrainError::PacedTrainNotFound { paced_train_id }
})
.await?;
Ok(Json(
pathfinding_from_train(conn, &mut valkey_conn, core, &infra, paced_train.into()).await?,
))
}

#[derive(Debug, Default, Clone, Serialize, Deserialize, IntoParams, ToSchema)]
Expand Down Expand Up @@ -316,17 +337,23 @@ async fn project_path(
#[cfg(test)]
mod tests {
use axum::http::StatusCode;
use editoast_models::DbConnectionPoolV2;
use pretty_assertions::assert_eq;
use rstest::rstest;
use serde_json::json;

use crate::core::mocking::MockingClient;
use crate::core::pathfinding::PathfindingResultSuccess;
use crate::error::InternalError;
use crate::models::fixtures::create_fast_rolling_stock;
use crate::models::fixtures::create_simple_paced_train;
use crate::models::fixtures::create_small_infra;
use crate::models::fixtures::create_timetable;
use crate::models::fixtures::simple_paced_train_base;
use crate::models::paced_train::PacedTrain;
use crate::models::prelude::*;
use crate::views::paced_train::PacedTrainResult;
use crate::views::path::pathfinding::PathfindingResult;
use crate::views::test_app::TestAppBuilder;

#[rstest]
Expand Down Expand Up @@ -406,4 +433,53 @@ mod tests {
);
assert_eq!(paced_train.step, response.paced_train.paced.step.into());
}

#[rstest]
async fn get_paced_train_path() {
let db_pool = DbConnectionPoolV2::for_tests();
let mut core = MockingClient::new();
core.stub("/v2/pathfinding/blocks")
.method(reqwest::Method::POST)
.response(StatusCode::OK)
.json(json!({
"blocks":[],
"routes": [],
"track_section_ranges": [],
"path_item_positions": [],
"length": 1,
"status": "success"
}))
.finish();
let app = TestAppBuilder::new()
.db_pool(db_pool.clone())
.core_client(core.into())
.build();
let pool = app.db_pool();

create_fast_rolling_stock(&mut db_pool.get_ok(), "R2D2").await;
let timetable = create_timetable(&mut pool.get_ok()).await;
let paced_train = create_simple_paced_train(&mut pool.get_ok(), timetable.id).await;
let small_infra = create_small_infra(&mut db_pool.get_ok()).await;

let request = app.get(&format!(
"/paced_train/{}/path?infra_id={}",
paced_train.id, small_infra.id
));

let response = app
.fetch(request)
.assert_status(StatusCode::OK)
.json_into::<PathfindingResult>();

assert_eq!(
response,
PathfindingResult::Success(PathfindingResultSuccess {
blocks: vec![],
routes: vec![],
track_section_ranges: vec![],
path_item_positions: vec![],
length: 1
})
)
}
}
Loading