forked from
lewis.moe/bspds-sandbox
PDS software with bells & whistles you didn’t even know you needed. will move this to its own account when ready.
1use crate::auth::BearerAuthAdmin;
2use crate::state::AppState;
3use axum::{
4 Json,
5 extract::State,
6 response::{IntoResponse, Response},
7};
8use serde::Serialize;
9
10#[derive(Serialize)]
11#[serde(rename_all = "camelCase")]
12pub struct ServerStatsResponse {
13 pub user_count: i64,
14 pub repo_count: i64,
15 pub record_count: i64,
16 pub blob_storage_bytes: i64,
17}
18
19pub async fn get_server_stats(State(state): State<AppState>, _auth: BearerAuthAdmin) -> Response {
20 let user_count: i64 = match sqlx::query_scalar!("SELECT COUNT(*) FROM users")
21 .fetch_one(&state.db)
22 .await
23 {
24 Ok(Some(count)) => count,
25 Ok(None) => 0,
26 Err(_) => 0,
27 };
28
29 let repo_count: i64 = match sqlx::query_scalar!("SELECT COUNT(*) FROM repos")
30 .fetch_one(&state.db)
31 .await
32 {
33 Ok(Some(count)) => count,
34 Ok(None) => 0,
35 Err(_) => 0,
36 };
37
38 let record_count: i64 = match sqlx::query_scalar!("SELECT COUNT(*) FROM records")
39 .fetch_one(&state.db)
40 .await
41 {
42 Ok(Some(count)) => count,
43 Ok(None) => 0,
44 Err(_) => 0,
45 };
46
47 let blob_storage_bytes: i64 =
48 match sqlx::query_scalar!("SELECT COALESCE(SUM(size_bytes), 0)::BIGINT FROM blobs")
49 .fetch_one(&state.db)
50 .await
51 {
52 Ok(Some(bytes)) => bytes,
53 Ok(None) => 0,
54 Err(_) => 0,
55 };
56
57 Json(ServerStatsResponse {
58 user_count,
59 repo_count,
60 record_count,
61 blob_storage_bytes,
62 })
63 .into_response()
64}