Skip to content

Commit da7f1df

Browse files
committed
feat(api): add statistics endpoint
1 parent b29d04e commit da7f1df

3 files changed

Lines changed: 53 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
pub mod config;
22
pub mod restart;
33
pub mod start;
4+
pub mod statistics;
45
pub mod status;
56
pub mod stop;
67
pub mod tickets;
78

89
pub use config::*;
910
pub use restart::*;
1011
pub use start::*;
12+
pub use statistics::*;
1113
pub use status::*;
1214
pub use stop::*;
1315
pub use tickets::*;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use crate::prelude::db::*;
2+
use crate::prelude::types::*;
3+
use axum::Json;
4+
use axum::extract::{Query, State};
5+
use axum::http::StatusCode;
6+
use axum::response::IntoResponse;
7+
use std::sync::Arc;
8+
use tokio::sync::Mutex;
9+
10+
#[derive(serde::Deserialize)]
11+
pub struct StatisticsQuery {
12+
#[serde(default = "default_days")]
13+
pub days: i64,
14+
}
15+
16+
fn default_days() -> i64 {
17+
30
18+
}
19+
20+
pub async fn handle_statistics(
21+
State(bot_state): State<Arc<Mutex<BotState>>>,
22+
Query(query): Query<StatisticsQuery>,
23+
) -> impl IntoResponse {
24+
let days = match query.days {
25+
7 | 30 | 90 => query.days,
26+
_ => 30,
27+
};
28+
29+
let state_lock = bot_state.lock().await;
30+
31+
let pool = match &state_lock.db_pool {
32+
Some(p) => p.clone(),
33+
None => {
34+
return (
35+
StatusCode::SERVICE_UNAVAILABLE,
36+
Json(serde_json::json!({"error": "Database not available"})),
37+
);
38+
}
39+
};
40+
41+
drop(state_lock);
42+
43+
match get_statistics(&pool, days).await {
44+
Ok(stats) => (StatusCode::OK, Json(serde_json::to_value(stats).unwrap())),
45+
Err(e) => (
46+
StatusCode::INTERNAL_SERVER_ERROR,
47+
Json(serde_json::json!({"error": format!("Failed to fetch statistics: {}", e)})),
48+
),
49+
}
50+
}

rustmail/src/api/routes/bot.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub fn create_bot_router(bot_state: Arc<Mutex<BotState>>) -> Router<Arc<Mutex<Bo
3232
.route("/status", get(handle_status_bot))
3333
.route("/tickets", get(handle_tickets_bot))
3434
.route("/config", get(handle_get_config))
35+
.route("/statistics", get(handle_statistics))
3536
.layer(axum::middleware::from_fn_with_state(
3637
bot_state.clone(),
3738
move |state, jar, req, next| {

0 commit comments

Comments
 (0)