Skip to content

Commit

Permalink
editoast: fix list_infra hanging
Browse files Browse the repository at this point in the history
  • Loading branch information
Khoyo committed Jul 24, 2024
1 parent daa3b7b commit 816f7e6
Showing 1 changed file with 16 additions and 32 deletions.
48 changes: 16 additions & 32 deletions editoast/src/views/infra/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,22 +193,20 @@ struct InfraListResponse {
#[get("")]
async fn list(
db_pool: Data<DbConnectionPoolV2>,
_core: Data<CoreClient>,
pagination_params: Query<PaginationQueryParam>,
) -> Result<Json<InfraListResponse>> {
let settings = pagination_params
.validate(1000)?
.warn_page_size(100)
.into_selection_settings();

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

let infra_states = fetch_all_infra_states(&infras).await?;

let response = InfraListResponse {
stats,
results: infras
Expand Down Expand Up @@ -610,21 +608,18 @@ pub async fn fetch_infra_state(_infra_id: i64, _core: &CoreClient) -> Result<Inf
}

pub async fn fetch_all_infra_states(
db_pool: &DbConnectionPoolV2,
infras: &[Infra],
) -> Result<HashMap<String, InfraStateResponse>> {
let infras = Infra::all(db_pool.get().await?.deref_mut())
.await
.into_iter()
.map(|infra| {
(
infra.id.to_string(),
InfraStateResponse {
// TODO: have a way to actually report infras states
last_status: None,
status: InfraState::Cached,
},
)
});
let infras = infras.into_iter().map(|infra| {
(
infra.id.to_string(),
InfraStateResponse {
// TODO: have a way to actually report infras states
last_status: None,
status: InfraState::Cached,
},
)
});
Ok(HashMap::from_iter(infras))
}

Expand Down Expand Up @@ -786,18 +781,7 @@ pub mod tests {

#[rstest]
async fn infra_list() {
let db_pool = DbConnectionPoolV2::for_tests();
let mut core = MockingClient::new();
core.stub("/cache_status")
.method(reqwest::Method::POST)
.response(StatusCode::OK)
.body("{}")
.finish();

let app = TestAppBuilder::new()
.db_pool(db_pool.clone())
.core_client(core.into())
.build();
let app = TestAppBuilder::default_app();
let request = TestRequest::get().uri("/infra/").to_request();

app.fetch(request).assert_status(StatusCode::OK);
Expand Down

0 comments on commit 816f7e6

Please sign in to comment.