Skip to main content

agentmux_srv/server/
message_stats_handler.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! `GET /api/v1/message-stats?days=N`: what messages cost per agent per day (`Agents message_stats`), counted
5//! from the jekt audit file. Read-only.
6
7use axum::extract::Query;
8use axum::response::{IntoResponse, Json};
9use serde::Deserialize;
10use serde_json::json;
11
12use crate::backend::audit_query::message_stats;
13
14#[derive(Debug, Default, Deserialize)]
15pub struct StatsParams {
16    pub days: Option<u64>,
17}
18
19pub async fn handle_message_stats(Query(p): Query<StatsParams>) -> impl IntoResponse {
20    let days = p.days.unwrap_or(7);
21    let stats = tokio::task::spawn_blocking(move || {
22        let paths: Vec<_> = [
23            crate::backend::reactive::audit_store::shared_path_for_reading(),
24            crate::backend::reactive::audit_store::own_path(),
25        ]
26        .into_iter()
27        .flatten()
28        .collect();
29        message_stats::from_files(&paths, agentmux_common::time::now_ms() as u64, days)
30    })
31    .await
32    .unwrap_or_default();
33    Json(json!({ "days": days.clamp(1, message_stats::MAX_DAYS), "stats": stats }))
34}