Skip to main content

agentmux_srv/server/app_api/
kanban_rollup.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! `kanban.tokens`: what a board's tasks spent in a range, rolled up (docs/specs/SPEC_TOKEN_ANALYSIS_PER_KANBAN_TASK_2026_11_22.md
5//! section 5.3): the median and 90th percentile per merged task, by kind, by stage, the cache hit rate, and the tasks that
6//! ended without merging apart. All counts.
7
8use serde_json::Value;
9
10use agentmux_common::time::now_ms;
11
12use super::AppState;
13use crate::backend::storage::work_rollup::since_of;
14
15/// `board` is a board's slug (the default board when absent, `all` for every board); `range` is `7d`, `30d` or `all`.
16/// The answer is the roll-up's own fields with the board and range it answers for.
17pub(super) async fn tokens(state: &AppState, data: &Value) -> Result<Value, String> {
18    let now = now_ms();
19    let range = data.get("range").and_then(Value::as_str).unwrap_or("30d").to_string();
20    let since = since_of(&range, now).ok_or_else(|| format!("kanban.tokens: range is 7d, 30d or all, not \"{range}\""))?;
21    let key = data.get("board").and_then(Value::as_str).map(str::trim).filter(|b| !b.is_empty()).map(str::to_string);
22    let work = state.work_store.clone();
23    tokio::task::spawn_blocking(move || {
24        let board = match key.as_deref() {
25            Some(k) if k.eq_ignore_ascii_case("all") => None,
26            k => Some(work.work_board_resolve(k, now).map_err(|e| e.to_string())?),
27        };
28        let rollup = work.work_tokens_rollup(board.as_ref().map(|b| b.id.as_str()), since, now).map_err(|e| e.to_string())?;
29        let mut out = serde_json::to_value(rollup).map_err(|e| e.to_string())?;
30        out["board"] = Value::String(board.map(|b| b.slug).unwrap_or_else(|| "all".to_string()));
31        out["range"] = Value::String(range);
32        Ok(out)
33    })
34    .await
35    .map_err(|e| e.to_string())?
36}