| 1 |
|
| 2 |
|
| 3 |
use sqlx::PgPool; |
| 4 |
|
| 5 |
use crate::error::Result; |
| 6 |
|
| 7 |
|
| 8 |
pub struct DbHealthStats { |
| 9 |
pub user_count: i64, |
| 10 |
pub project_count: i64, |
| 11 |
pub item_count: i64, |
| 12 |
pub active_session_count: i64, |
| 13 |
pub active_creator_count: i64, |
| 14 |
pub transaction_count: i64, |
| 15 |
pub blog_post_count: i64, |
| 16 |
pub sync_app_count: i64, |
| 17 |
pub sync_device_count: i64, |
| 18 |
pub sync_log_entries: i64, |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
#[tracing::instrument(skip_all)] |
| 23 |
pub async fn get_health_stats(pool: &PgPool) -> Result<DbHealthStats> { |
| 24 |
let stats = sqlx::query_as::<_, (i64, i64, i64, i64, i64, i64, i64, i64, i64, i64)>( |
| 25 |
r#" |
| 26 |
SELECT |
| 27 |
(SELECT COUNT(*) FROM users), |
| 28 |
(SELECT COUNT(*) FROM projects), |
| 29 |
(SELECT COUNT(*) FROM items), |
| 30 |
(SELECT COUNT(*) FROM tower_sessions.session WHERE expiry_date > NOW()), |
| 31 |
(SELECT COUNT(*) FROM users WHERE stripe_account_id IS NOT NULL AND stripe_charges_enabled = true), |
| 32 |
(SELECT COUNT(*) FROM transactions), |
| 33 |
(SELECT COUNT(*) FROM blog_posts), |
| 34 |
(SELECT COUNT(*) FROM sync_apps), |
| 35 |
(SELECT COUNT(*) FROM sync_devices), |
| 36 |
(SELECT COUNT(*) FROM sync_log) |
| 37 |
"#, |
| 38 |
) |
| 39 |
.fetch_one(pool) |
| 40 |
.await?; |
| 41 |
|
| 42 |
Ok(DbHealthStats { |
| 43 |
user_count: stats.0, |
| 44 |
project_count: stats.1, |
| 45 |
item_count: stats.2, |
| 46 |
active_session_count: stats.3, |
| 47 |
active_creator_count: stats.4, |
| 48 |
transaction_count: stats.5, |
| 49 |
blog_post_count: stats.6, |
| 50 |
sync_app_count: stats.7, |
| 51 |
sync_device_count: stats.8, |
| 52 |
sync_log_entries: stats.9, |
| 53 |
}) |
| 54 |
} |
| 55 |
|