From 55626e3d3fe396fa6e6c75113a1292d2719959b9 Mon Sep 17 00:00:00 2001 From: Egor Date: Thu, 4 Jul 2024 10:00:11 +0200 Subject: [PATCH] editoast: app health view refactor --- editoast/editoast_models/src/app_health.rs | 13 +++++++++++++ editoast/editoast_models/src/lib.rs | 2 ++ editoast/src/views/layers/mod.rs | 2 +- editoast/src/views/mod.rs | 9 +++------ 4 files changed, 19 insertions(+), 7 deletions(-) create mode 100644 editoast/editoast_models/src/app_health.rs diff --git a/editoast/editoast_models/src/app_health.rs b/editoast/editoast_models/src/app_health.rs new file mode 100644 index 00000000000..0fa3b9cbe91 --- /dev/null +++ b/editoast/editoast_models/src/app_health.rs @@ -0,0 +1,13 @@ +use diesel::sql_query; +use diesel_async::RunQueryDsl; + +use crate::DbConnection; +use crate::EditoastModelsError; + +pub struct AppHealth; + +impl AppHealth { + pub async fn run_simple_request(conn: &mut DbConnection) -> Result { + Ok(sql_query("SELECT 1").execute(conn).await?) + } +} diff --git a/editoast/editoast_models/src/lib.rs b/editoast/editoast_models/src/lib.rs index cb07797edd9..f97bad10ab8 100644 --- a/editoast/editoast_models/src/lib.rs +++ b/editoast/editoast_models/src/lib.rs @@ -3,6 +3,8 @@ use diesel_async::{pooled_connection::deadpool::Pool, AsyncPgConnection}; mod db_connection_pool; mod error; +pub mod app_health; + pub use db_connection_pool::create_connection_pool; pub use db_connection_pool::DbConnectionPoolV2; pub use error::EditoastModelsError; diff --git a/editoast/src/views/layers/mod.rs b/editoast/src/views/layers/mod.rs index 7e1b54f90f4..bd9d05f678f 100644 --- a/editoast/src/views/layers/mod.rs +++ b/editoast/src/views/layers/mod.rs @@ -207,7 +207,7 @@ async fn cache_and_get_mvt_tile( let conn = &mut db_pool.get().await?; let records = - GeoJsonAndData::get_records(conn, &layer, view, infra, &GeoPoint::new(z, x, y)).await?; + GeoJsonAndData::get_records(conn, layer, view, infra, &GeoPoint::new(z, x, y)).await?; let mvt_bytes: Vec = create_and_fill_mvt_tile(layer_slug, records) .to_bytes() diff --git a/editoast/src/views/mod.rs b/editoast/src/views/mod.rs index e03c633476d..3fb291eef0a 100644 --- a/editoast/src/views/mod.rs +++ b/editoast/src/views/mod.rs @@ -31,7 +31,8 @@ pub use openapi::OpenApiRoot; use actix_web::get; use actix_web::web::Data; use actix_web::web::Json; -use diesel::sql_query; +use editoast_models::app_health::AppHealth; +use editoast_models::DbConnectionPoolV2; use redis::cmd; use serde_derive::Deserialize; use serde_derive::Serialize; @@ -49,7 +50,6 @@ use crate::infra_cache::operation; use crate::models; use crate::modelsv2; use crate::RedisClient; -use editoast_models::DbConnectionPoolV2; crate::routes! { (health, version, core_version), @@ -105,10 +105,7 @@ async fn health( db_pool: Data, redis_client: Data, ) -> Result<&'static str> { - use diesel_async::RunQueryDsl; - sql_query("SELECT 1") - .execute(db_pool.get().await?.deref_mut()) - .await?; + AppHealth::run_simple_request(db_pool.get().await?.deref_mut()).await?; let mut conn = redis_client.get_connection().await?; cmd("PING").query_async::<_, ()>(&mut conn).await.unwrap();