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

Use ModelV2 List in infra listing endpoint #7541

Merged
merged 2 commits into from
May 29, 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
40 changes: 19 additions & 21 deletions editoast/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9287,42 +9287,40 @@ paths:
/infra/:
get:
parameters:
- description: Page number
in: query
- in: query
name: page
required: false
schema:
default: 1
format: int64
minimum: 1
type: integer
- description: Number of elements by page
in: query
- in: query
name: page_size
required: false
schema:
default: 25
maximum: 10000
format: int64
minimum: 1
nullable: true
type: integer
responses:
'200':
content:
application/json:
schema:
properties:
count:
type: number
next: {}
previous: {}
results:
items:
$ref: '#/components/schemas/Infra'
type: array
required:
- count
- next
- previous
type: object
description: The infras list
summary: Paginated list of all available infras
allOf:
- $ref: '#/components/schemas/PaginationStats'
- properties:
results:
items:
$ref: '#/components/schemas/InfraWithState'
type: array
required:
- results
type: object
description: All infras, paginated
summary: Lists all infras along with their current loading state in Core
tags:
- infra
post:
Expand Down
37 changes: 0 additions & 37 deletions editoast/openapi_legacy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,43 +22,6 @@ tags:

paths:
/infra/:
get:
tags:
- infra
summary: Paginated list of all available infras
parameters:
- description: Page number
in: query
name: page
schema:
default: 1
minimum: 1
type: integer
- description: Number of elements by page
in: query
name: page_size
schema:
default: 25
maximum: 10000
minimum: 1
type: integer
responses:
200:
description: The infras list
content:
application/json:
schema:
type: object
properties:
count:
type: number
next: {}
previous: {}
results:
type: array
items:
$ref: "#/components/schemas/Infra"
required: [count, next, previous]
post:
tags:
- infra
Expand Down
33 changes: 0 additions & 33 deletions editoast/src/modelsv2/infra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ mod voltage;

use std::pin::Pin;

use async_trait::async_trait;
use chrono::NaiveDateTime;
use chrono::Utc;
use derivative::Derivative;
use diesel::sql_query;
use diesel::sql_types::BigInt;
use diesel::QueryDsl;
use diesel_async::RunQueryDsl;
use editoast_derive::ModelV2;
use futures::future::try_join_all;
Expand All @@ -29,8 +27,6 @@ use uuid::Uuid;
use crate::error::Result;
use crate::generated_data;
use crate::infra_cache::InfraCache;
use crate::models::List;
use crate::models::NoParams;
use crate::modelsv2::get_geometry_layer_table;
use crate::modelsv2::get_table;
use crate::modelsv2::prelude::*;
Expand All @@ -39,8 +35,6 @@ use crate::modelsv2::Create;
use crate::modelsv2::DbConnection;
use crate::modelsv2::DbConnectionPool;
use crate::tables::infra::dsl;
use crate::views::pagination::Paginate;
use crate::views::pagination::PaginatedResponse;
use editoast_schemas::infra::RailJson;
use editoast_schemas::infra::RAILJSON_VERSION;
use editoast_schemas::primitives::ObjectType;
Expand Down Expand Up @@ -227,33 +221,6 @@ impl Infra {
}
}

#[async_trait]
impl List<NoParams> for Infra {
async fn list_conn(
conn: &mut DbConnection,
page: i64,
page_size: i64,
_: NoParams,
) -> Result<PaginatedResponse<Self>> {
let PaginatedResponse {
count,
previous,
next,
results,
} = dsl::infra
.distinct()
.paginate(page, page_size)
.load_and_count::<Row<Self>>(conn)
.await?;
Ok(PaginatedResponse {
count,
previous,
next,
results: results.into_iter().map(Self::from_row).collect(),
})
}
}

#[cfg(test)]
pub mod tests {
use actix_web::test as actix_test;
Expand Down
139 changes: 77 additions & 62 deletions editoast/src/views/infra/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ use editoast_derive::EditoastError;
use serde::Deserialize;
use serde::Serialize;
use std::collections::HashMap;
use std::ops::DerefMut as _;
use std::sync::Arc;
use thiserror::Error;
use utoipa::IntoParams;
use utoipa::ToSchema;

use self::edition::edit;
use self::edition::split_track_section;
use super::pagination::PaginationStats;
use super::params::List;
use crate::core::infra_loading::InfraLoadRequest;
use crate::core::infra_state::InfraStateRequest;
Expand All @@ -44,12 +46,10 @@ use crate::infra_cache::InfraCache;
use crate::infra_cache::ObjectCache;
use crate::map;
use crate::map::MapLayers;
use crate::models::List as ModelList;
use crate::models::NoParams;
use crate::modelsv2::prelude::*;
use crate::modelsv2::DbConnectionPool;
use crate::modelsv2::Infra;
use crate::views::pagination::PaginatedResponse;
use crate::views::pagination::PaginatedList as _;
use crate::views::pagination::PaginationQueryParam;
use crate::RedisClient;
use editoast_schemas::infra::SwitchType;
Expand All @@ -76,6 +76,7 @@ crate::routes! {
get_voltages,
get_switch_types,
},
list,
get_all_voltages,
railjson::routes(),
},
Expand Down Expand Up @@ -211,40 +212,54 @@ async fn refresh(
Ok(Json(RefreshResponse { infra_refreshed }))
}

/// Return a list of infras
#[derive(Serialize, ToSchema)]
struct InfraListResponse {
#[serde(flatten)]
stats: PaginationStats,
results: Vec<InfraWithState>,
}

/// Lists all infras along with their current loading state in Core
#[utoipa::path(
tag = "infra",
params(PaginationQueryParam),
responses(
(status = 200, description = "All infras, paginated", body = inline(InfraListResponse))
),
)]
#[get("")]
async fn list(
db_pool: Data<DbConnectionPool>,
core: Data<CoreClient>,
pagination_params: Query<PaginationQueryParam>,
) -> Result<Json<PaginatedResponse<InfraWithState>>> {
let (page, per_page) = pagination_params
) -> Result<Json<InfraListResponse>> {
let settings = pagination_params
.validate(1000)?
.warn_page_size(100)
.unpack();
let db_pool = db_pool.into_inner();
let infras = Infra::list(db_pool.clone(), page, per_page, NoParams).await?;
let infra_state = call_core_infra_state(None, db_pool, core).await?;
let infras_with_state: Vec<InfraWithState> = infras
.results
.into_iter()
.map(|infra| {
let infra_id = infra.id;
let state = infra_state
.get(&infra_id.to_string())
.unwrap_or(&InfraStateResponse::default())
.status;
InfraWithState { infra, state }
})
.collect();
let infras_with_state = PaginatedResponse::<InfraWithState> {
count: infras.count,
previous: infras.previous,
next: infras.next,
results: infras_with_state,
.into_selection_settings();

let ((infras, stats), infra_states) = {
let conn = &mut db_pool.get().await?;
futures::try_join!(
Infra::list_paginated(conn, settings),
fetch_all_infra_states(core.as_ref()),
)?
};

Ok(Json(infras_with_state))
let response = InfraListResponse {
stats,
results: infras
.into_iter()
.map(|infra| {
let state = infra_states
.get(&infra.id.to_string())
.map(|response| response.status)
.unwrap_or_default();
InfraWithState { infra, state }
})
.collect(),
};
Ok(Json(response))
}

#[derive(Debug, Clone, Copy, Default, Deserialize, PartialEq, Eq, Serialize, ToSchema)]
Expand Down Expand Up @@ -296,11 +311,7 @@ async fn get(
let conn = &mut db_pool.get().await?;
let infra =
Infra::retrieve_or_fail(conn, infra_id, || InfraApiError::NotFound { infra_id }).await?;
let infra_state = call_core_infra_state(Some(infra_id), db_pool.into_inner(), core).await?;
let state = infra_state
.get(&infra_id.to_string())
.unwrap_or(&InfraStateResponse::default())
.status;
let state = fetch_infra_state(infra.id, core.as_ref()).await?.status;
Ok(Json(InfraWithState { infra, state }))
}

Expand Down Expand Up @@ -562,12 +573,6 @@ async fn unlock(
Ok(HttpResponse::NoContent().finish())
}

#[derive(Debug, Default, Deserialize)]

pub struct StatePayload {
infra: Option<i64>,
}

/// Instructs Core to load an infra
#[utoipa::path(
tag = "infra",
Expand Down Expand Up @@ -595,21 +600,9 @@ async fn load(
Ok(HttpResponse::NoContent().finish())
}

/// Builds a Core cache_status request, runs it
pub async fn call_core_infra_state(
infra_id: Option<i64>,
db_pool: Arc<DbConnectionPool>,
core: Data<CoreClient>,
) -> Result<HashMap<String, InfraStateResponse>> {
if let Some(infra_id) = infra_id {
let conn = &mut db_pool.get().await?;
if !Infra::exists(conn, infra_id).await? {
return Err(InfraApiError::NotFound { infra_id }.into());
}
}
let infra_request = InfraStateRequest { infra: infra_id };
let response = infra_request.fetch(core.as_ref()).await?;
Ok(response)
#[derive(Debug, Default, Deserialize)]
pub struct StatePayload {
infra: Option<i64>,
}

#[get("/cache_status")]
Expand All @@ -618,14 +611,36 @@ async fn cache_status(
db_pool: Data<DbConnectionPool>,
core: Data<CoreClient>,
) -> Result<Json<HashMap<String, InfraStateResponse>>> {
let payload = match payload {
Either::Left(state) => state.into_inner(),
Either::Right(_) => Default::default(),
};
let infra_id = payload.infra;
Ok(Json(
call_core_infra_state(infra_id, db_pool.into_inner(), core).await?,
))
if let Either::Left(Json(StatePayload {
infra: Some(infra_id),
})) = payload
{
if !Infra::exists(db_pool.get().await?.deref_mut(), infra_id).await? {
return Err(InfraApiError::NotFound { infra_id }.into());
}
let infra_state = fetch_infra_state(infra_id, core.as_ref()).await?;
Ok(Json(HashMap::from([(infra_id.to_string(), infra_state)])))
} else {
Ok(Json(fetch_all_infra_states(core.as_ref()).await?))
}
}

/// Builds a Core cache_status request, runs it
pub async fn fetch_infra_state(infra_id: i64, core: &CoreClient) -> Result<InfraStateResponse> {
Ok(InfraStateRequest {
infra: Some(infra_id),
}
.fetch(core)
.await?
.get(&infra_id.to_string())
.cloned()
.unwrap_or_default())
}

pub async fn fetch_all_infra_states(
core: &CoreClient,
) -> Result<HashMap<String, InfraStateResponse>> {
InfraStateRequest::default().fetch(core).await
}

#[cfg(test)]
Expand Down
Loading
Loading