//! Aggregate health-check statistics fetched in a single query. use sqlx::PgPool; use crate::error::Result; /// Aggregate stats shown on the admin health-check page. pub struct DbHealthStats { pub user_count: i64, pub project_count: i64, pub item_count: i64, pub active_session_count: i64, pub active_creator_count: i64, pub transaction_count: i64, pub blog_post_count: i64, pub sync_app_count: i64, pub sync_device_count: i64, pub sync_log_entries: i64, } /// Fetch health-check stats in a single round-trip. #[tracing::instrument(skip_all)] pub async fn get_health_stats(pool: &PgPool) -> Result { let stats = sqlx::query_as::<_, (i64, i64, i64, i64, i64, i64, i64, i64, i64, i64)>( r#" SELECT (SELECT COUNT(*) FROM users), (SELECT COUNT(*) FROM projects), (SELECT COUNT(*) FROM items), (SELECT COUNT(*) FROM tower_sessions.session WHERE expiry_date > NOW()), (SELECT COUNT(*) FROM users WHERE stripe_account_id IS NOT NULL AND stripe_charges_enabled = true), (SELECT COUNT(*) FROM transactions), (SELECT COUNT(*) FROM blog_posts), (SELECT COUNT(*) FROM sync_apps), (SELECT COUNT(*) FROM sync_devices), (SELECT COUNT(*) FROM sync_log) "#, ) .fetch_one(pool) .await?; Ok(DbHealthStats { user_count: stats.0, project_count: stats.1, item_count: stats.2, active_session_count: stats.3, active_creator_count: stats.4, transaction_count: stats.5, blog_post_count: stats.6, sync_app_count: stats.7, sync_device_count: stats.8, sync_log_entries: stats.9, }) }