Add a live statistics dashboard over persistent history

Replace the four-row stats modal with a real dashboard — our own angle on
the reference's ephemeral request monitor: because our context lives in
SQLite we can aggregate over ALL history, not just the last N requests.

Backend:
- Usage carries the engine's prompt_ms/decode_ms (from DONE); a token-
  weighted in-memory PerfMeter in ChatService records every served
  request (complete + stream) and exposes decode/prefill tok/s.
- store::Stats grows total_tokens, tool_calls (json_array_length over
  assistant tool_calls_json), a per-session breakdown (busiest first)
  and a per-UTC-day token series; all over existing columns, no schema
  change, still behind the Store trait.
- GET /api/stats returns the store aggregates flattened plus a live
  engine status (model, max_context, busy) and the perf gauge.

Frontend (mobile-first, vanilla, i18n catalog):
- Overview cards: engine state dot + model + context, decode speed
  (run average) + prefill, total/prompt/completion tokens, sessions +
  messages, tool calls, requests served.
- Daily stacked prompt/completion bar chart (continuous zero-filled
  UTC window, last 30 days) and per-session token-share bars.
- Polls every 2s while open, re-renders only on change; closes via
  button, backdrop or Escape.

Tests: new store breakdown test + extended stats integration assertions
(124 passed, 2 ignored). Verified live on the real engine: decode/prefill
gauge populated from DONE timings; dashboard renders on desktop.
This commit is contained in:
adm committed 2026-11-25 17:52:38 +01:00
1 parent e79ee0e20e
commit 765c197b95
11 files changed
+642 -42

No files matched your search

+32 -3
View File
@@ -13,9 +13,10 @@ use axum::extract::{Path, State};
use axum::http::StatusCode;
use axum::response::{sse::Event, sse::Sse, IntoResponse, Json};
use futures::StreamExt;
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use super::routes::{AppState, Error};
use super::service::PerfSnapshot;
use super::types::{
completion_id, now_unix, ChatChunk, ChatMessage, ChatRequest, ChatResponse, Choice,
FinishReason, FunctionCall, Role, ToolCall, Usage,
@@ -189,8 +190,36 @@ pub(crate) async fn get_messages(
Ok(Json(with_store(&st, move |s| s.messages(&id)).await?))
}
pub(crate) async fn stats(State(st): State<AppState>) -> Result<Json<Stats>, Error> {
Ok(Json(with_store(&st, |s| s.stats()).await?))
/// The stats dashboard payload: persistent history aggregates (from the store)
/// plus a live engine status and the since-startup performance gauge, in one
/// JSON object so the GUI renders the whole view from a single poll.
#[derive(Serialize)]
pub(crate) struct StatsResponse {
#[serde(flatten)]
totals: Stats,
engine: EngineStatus,
perf: PerfSnapshot,
}
#[derive(Serialize)]
struct EngineStatus {
model: String,
max_context: u32,
busy: bool,
}
pub(crate) async fn stats(State(st): State<AppState>) -> Result<Json<StatsResponse>, Error> {
let totals = with_store(&st, |s| s.stats()).await?;
let engine = EngineStatus {
model: st.service.model_name().to_string(),
max_context: st.service.max_context(),
busy: !st.service.is_idle(),
};
Ok(Json(StatsResponse {
totals,
engine,
perf: st.service.perf(),
}))
}
fn role_of(s: &str) -> Role {