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: improve naming of extracted variables in several handlers #8674

Merged
merged 1 commit into from
Sep 3, 2024
Merged
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
4 changes: 2 additions & 2 deletions editoast/src/views/electrical_profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ async fn post_electrical_profile(
State(db_pool): State<DbConnectionPoolV2>,
Extension(authorizer): AuthorizerExt,
Query(ep_set_name): Query<ElectricalProfileQueryArgs>,
Json(data): Json<ElectricalProfileSetData>,
Json(ep_data): Json<ElectricalProfileSetData>,
) -> Result<Json<ElectricalProfileSet>> {
let authorized = authorizer
.check_roles([BuiltinRole::InfraWrite].into())
Expand All @@ -205,7 +205,7 @@ async fn post_electrical_profile(
}
let ep_set = ElectricalProfileSet::changeset()
.name(ep_set_name.name)
.data(data);
.data(ep_data);
let conn = &mut db_pool.get().await?;
Ok(Json(ep_set.create(conn).await?))
}
Expand Down
4 changes: 2 additions & 2 deletions editoast/src/views/infra/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ impl From<InfraCreateForm> for Changeset<Infra> {
async fn create(
db_pool: State<DbConnectionPoolV2>,
Extension(authorizer): AuthorizerExt,
Json(data): Json<InfraCreateForm>,
Json(infra_form): Json<InfraCreateForm>,
) -> Result<impl IntoResponse> {
let authorized = authorizer
.check_roles([BuiltinRole::InfraWrite].into())
Expand All @@ -345,7 +345,7 @@ async fn create(
return Err(AuthorizationError::Unauthorized.into());
}

let infra: Changeset<Infra> = data.into();
let infra: Changeset<Infra> = infra_form.into();
let infra = infra.create(db_pool.get().await?.deref_mut()).await?;
Ok((StatusCode::CREATED, Json(infra)))
}
Expand Down
4 changes: 2 additions & 2 deletions editoast/src/views/v2/timetable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ async fn train_schedule(
db_pool: State<DbConnectionPoolV2>,
Extension(authorizer): AuthorizerExt,
Path(timetable_id): Path<TimetableIdParam>,
Json(data): Json<Vec<TrainScheduleBase>>,
Json(train_schedules): Json<Vec<TrainScheduleBase>>,
) -> Result<Json<Vec<TrainScheduleResult>>> {
let authorized = authorizer
.check_roles([BuiltinRole::TimetableWrite].into())
Expand All @@ -237,7 +237,7 @@ async fn train_schedule(
})
.await?;

let changesets: Vec<TrainScheduleChangeset> = data
let changesets: Vec<TrainScheduleChangeset> = train_schedules
.into_iter()
.map(|ts| TrainScheduleForm {
timetable_id: Some(timetable_id),
Expand Down
42 changes: 24 additions & 18 deletions editoast/src/views/v2/timetable/stdcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ async fn stdcm(
Extension(authorizer): AuthorizerExt,
Path(id): Path<i64>,
Query(query): Query<InfraIdQueryParam>,
Json(data): Json<STDCMRequestPayload>,
Json(stdcm_request): Json<STDCMRequestPayload>,
) -> Result<Json<STDCMResponse>> {
let authorized = authorizer
.check_roles([BuiltinRole::Stdcm].into())
Expand Down Expand Up @@ -188,9 +188,9 @@ async fn stdcm(

let rolling_stock = RollingStockModel::retrieve_or_fail(
db_pool.get().await?.deref_mut(),
data.rolling_stock_id,
stdcm_request.rolling_stock_id,
|| STDCMError::RollingStockNotFound {
rolling_stock_id: data.rolling_stock_id,
rolling_stock_id: stdcm_request.rolling_stock_id,
},
)
.await?;
Expand All @@ -201,7 +201,7 @@ async fn stdcm(
core_client.clone(),
&trains,
&infra,
data.electrical_profile_set_id,
stdcm_request.electrical_profile_set_id,
)
.await?;

Expand All @@ -210,7 +210,7 @@ async fn stdcm(
db_pool.clone(),
redis_client.clone(),
core_client.clone(),
&data,
&stdcm_request,
&infra,
&rolling_stock,
timetable_id,
Expand All @@ -224,25 +224,31 @@ async fn stdcm(
}))
}
};
let earliest_step_tolerance_window = get_earliest_step_tolerance_window(&data);
let maximum_departure_delay =
get_maximum_departure_delay(&data, simulation_run_time, earliest_step_tolerance_window);
let maximum_run_time_without_tolerance = 2 * simulation_run_time + get_total_stop_time(&data);
let earliest_step_tolerance_window = get_earliest_step_tolerance_window(&stdcm_request);
let maximum_departure_delay = get_maximum_departure_delay(
&stdcm_request,
simulation_run_time,
earliest_step_tolerance_window,
);
let maximum_run_time_without_tolerance =
2 * simulation_run_time + get_total_stop_time(&stdcm_request);
let maximum_run_time = get_maximum_run_time(
&data,
&stdcm_request,
maximum_run_time_without_tolerance,
earliest_step_tolerance_window,
);

let departure_time = get_earliest_departure_time(&data, maximum_run_time_without_tolerance);
let departure_time =
get_earliest_departure_time(&stdcm_request, maximum_run_time_without_tolerance);
let latest_simulation_end = departure_time + Duration::milliseconds((maximum_run_time) as i64);

// 3. Get scheduled train requirements
let trains_requirements =
build_train_requirements(trains, simulations, departure_time, latest_simulation_end);

// 4. Parse stdcm path items
let path_items = parse_stdcm_steps(db_pool.get().await?.deref_mut(), &data, &infra).await?;
let path_items =
parse_stdcm_steps(db_pool.get().await?.deref_mut(), &stdcm_request, &infra).await?;

// 5. Build STDCM request
let stdcm_response = STDCMRequest {
Expand All @@ -253,18 +259,18 @@ async fn stdcm(
rolling_stock_supported_signaling_systems: rolling_stock
.supported_signaling_systems
.clone(),
comfort: data.comfort,
comfort: stdcm_request.comfort,
path_items,
start_time: departure_time,
trains_requirements,
maximum_departure_delay,
maximum_run_time,
speed_limit_tag: data.speed_limit_tags,
time_gap_before: data.time_gap_before,
time_gap_after: data.time_gap_after,
margin: data.margin,
speed_limit_tag: stdcm_request.speed_limit_tags,
time_gap_before: stdcm_request.time_gap_before,
time_gap_after: stdcm_request.time_gap_after,
margin: stdcm_request.margin,
time_step: Some(2000),
work_schedules: match data.work_schedule_group_id {
work_schedules: match stdcm_request.work_schedule_group_id {
Some(work_schedule_group_id) => {
build_work_schedules(
db_pool.get().await?.deref_mut(),
Expand Down
24 changes: 10 additions & 14 deletions editoast/src/views/v2/train_schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ struct BatchRequest {
async fn get_batch(
app_state: State<AppState>,
Extension(authorizer): AuthorizerExt,
Json(data): Json<BatchRequest>,
Json(BatchRequest { ids: train_ids }): Json<BatchRequest>,
) -> Result<Json<Vec<TrainScheduleResult>>> {
let authorized = authorizer
.check_roles([BuiltinRole::InfraRead, BuiltinRole::TimetableRead].into())
Expand All @@ -229,7 +229,6 @@ async fn get_batch(

let db_pool = app_state.db_pool_v2.clone();
let conn = &mut db_pool.get().await?;
let train_ids = data.ids;
let train_schedules: Vec<TrainSchedule> =
TrainSchedule::retrieve_batch_or_fail(conn, train_ids, |missing| {
TrainScheduleError::BatchTrainScheduleNotFound {
Expand All @@ -252,7 +251,7 @@ async fn get_batch(
async fn delete(
app_state: State<AppState>,
Extension(authorizer): AuthorizerExt,
Json(data): Json<BatchRequest>,
Json(BatchRequest { ids: train_ids }): Json<BatchRequest>,
) -> Result<impl IntoResponse> {
let authorized = authorizer
.check_roles([BuiltinRole::InfraRead, BuiltinRole::TimetableWrite].into())
Expand All @@ -266,8 +265,7 @@ async fn delete(

use crate::modelsv2::DeleteBatch;
let conn = &mut db_pool.get().await?;
let train_schedule_ids = data.ids;
TrainSchedule::delete_batch_or_fail(conn, train_schedule_ids, |number| {
TrainSchedule::delete_batch_or_fail(conn, train_ids, |number| {
TrainScheduleError::BatchTrainScheduleNotFound { number }
})
.await?;
Expand All @@ -289,7 +287,7 @@ async fn put(
db_pool: State<DbConnectionPoolV2>,
Extension(authorizer): AuthorizerExt,
train_schedule_id: Path<TrainScheduleIdParam>,
Json(data): Json<TrainScheduleForm>,
Json(train_schedule_form): Json<TrainScheduleForm>,
) -> Result<Json<TrainScheduleResult>> {
let authorized = authorizer
.check_roles([BuiltinRole::InfraRead, BuiltinRole::TimetableWrite].into())
Expand All @@ -302,7 +300,7 @@ async fn put(
let conn = &mut db_pool.get().await?;

let train_schedule_id = train_schedule_id.id;
let ts_changeset: TrainScheduleChangeset = data.into();
let ts_changeset: TrainScheduleChangeset = train_schedule_form.into();

let ts_result = ts_changeset
.update_or_fail(conn, train_schedule_id, || TrainScheduleError::NotFound {
Expand Down Expand Up @@ -672,7 +670,11 @@ enum SimulationSummaryResult {
async fn simulation_summary(
app_state: State<AppState>,
Extension(authorizer): AuthorizerExt,
Json(data): Json<SimulationBatchForm>,
Json(SimulationBatchForm {
infra_id,
electrical_profile_set_id,
ids: train_schedule_ids,
}): Json<SimulationBatchForm>,
) -> Result<Json<HashMap<i64, SimulationSummaryResult>>> {
let authorized = authorizer
.check_roles([BuiltinRole::InfraRead, BuiltinRole::TimetableRead].into())
Expand All @@ -686,12 +688,6 @@ async fn simulation_summary(
let redis_client = app_state.redis.clone();
let core = app_state.core_client.clone();

let SimulationBatchForm {
infra_id,
electrical_profile_set_id,
ids: train_schedule_ids,
} = data;

let infra = Infra::retrieve_or_fail(db_pool.get().await?.deref_mut(), infra_id, || {
TrainScheduleError::InfraNotFound { infra_id }
})
Expand Down
13 changes: 6 additions & 7 deletions editoast/src/views/v2/train_schedule/projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,12 @@ struct CachedProjectPathTrainResult {
async fn project_path(
app_state: State<AppState>,
Extension(authorizer): AuthorizerExt,
Json(data): Json<ProjectPathForm>,
Json(ProjectPathForm {
infra_id,
ids: train_ids,
path,
electrical_profile_set_id,
}): Json<ProjectPathForm>,
) -> Result<Json<HashMap<i64, ProjectPathTrainResult>>> {
let authorized = authorizer
.check_roles(
Expand All @@ -149,12 +154,6 @@ async fn project_path(
let redis_client = app_state.redis.clone();
let core_client = app_state.core_client.clone();

let ProjectPathForm {
infra_id,
ids: train_ids,
path,
electrical_profile_set_id,
} = data;
let ProjectPathInput {
track_section_ranges: path_track_ranges,
routes: path_routes,
Expand Down
16 changes: 8 additions & 8 deletions editoast/src/views/work_schedules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ struct WorkScheduleCreateResponse {
async fn create(
State(app_state): State<AppState>,
Extension(authorizer): AuthorizerExt,
Json(data): Json<WorkScheduleCreateForm>,
Json(WorkScheduleCreateForm {
work_schedule_group_name,
work_schedules,
}): Json<WorkScheduleCreateForm>,
) -> Result<Json<WorkScheduleCreateResponse>> {
let authorized = authorizer
.check_roles([BuiltinRole::WorkScheduleWrite].into())
Expand All @@ -153,20 +156,17 @@ async fn create(
let db_pool = app_state.db_pool_v2.clone();
let conn = &mut db_pool.get().await?;

let work_schedule_create_form = data;

// Create the work_schedule_group
let work_schedule_group = WorkScheduleGroup::changeset()
.name(work_schedule_create_form.work_schedule_group_name.clone())
.name(work_schedule_group_name.clone())
.creation_date(Utc::now().naive_utc())
.create(conn)
.await;
let work_schedule_group = work_schedule_group
.map_err(|e| map_diesel_error(e, work_schedule_create_form.work_schedule_group_name))?;
let work_schedule_group =
work_schedule_group.map_err(|e| map_diesel_error(e, work_schedule_group_name))?;

// Create work schedules
let work_schedules_changesets = work_schedule_create_form
.work_schedules
let work_schedules_changesets = work_schedules
.into_iter()
.map(|work_schedule| work_schedule.into_work_schedule_changeset(work_schedule_group.id))
.collect::<Vec<_>>();
Expand Down
Loading