Files
AgentHub/server/routes/stats.js
T
nntrivi2001 4aae61297f feat: analytics parity — tool cost, date ranges, budgets, query explorer, replay, snapshots (v2.4.0)
Analytics parity phases 2–7 (docs/plans/analytics-parity/), built on
138e706 and ported onto v2.3.1:
- Cost by AI tool (?tool= on cost/analytics/stats/workflows, by_tool,
  Codex fast/priority and flex tiers, agenthub_cost_usd_total{tool,model})
- Date ranges (from/to) on cost/analytics/workflows; Analytics range select
  with Today / This week (Monday) / This month (1st) / 7d / 30d / 90d / All /
  Custom; workflow filters carried into workflow-stats.js (read worker)
- Budgets and cost alerts as alert rule types (alert_rules rebuilt without
  its rule_type CHECK), GET /api/budgets/status, rule preview, spend pace,
  status-bar budget chip; rules stay editable in place
- Query Explorer (/query, allowlisted sort, CSV/JSON export) and scheduled
  reports (report_definitions/report_runs, Settings → Reports)
- Session Replay tab and immutable snapshots (session_snapshots, /snapshot/:token)
- Remote Codex sync, push ingest with a hashed token, privacy redaction and
  ignored folders (Settings → Data), Cursor sessions (no cost)
- Read-only MCP server, statusline, Grafana/Prometheus files

Also: resizable sidebar, pet run/wave animations, the workspace home keeps a
half-typed prompt, and the pre-commit hook runs only the related tests for a
small commit.
2026-10-06 17:12:28 +07:00

48 lines
2.0 KiB
JavaScript

/**
* @file Express router for stats endpoints, providing aggregated statistics about agents, sessions, events, and WebSocket connections. It queries the database for various counts and statuses, and returns a comprehensive overview in JSON format for frontend display on the dashboard.
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
*/
const { Router } = require("express");
const { stmts, db } = require("../db");
const { getConnectionCount } = require("../websocket");
const { parseScope, parseTool, BAD_TOOL } = require("../lib/source-filter");
const scoped = require("../lib/scoped-stats");
const router = Router();
router.get("/", (req, res) => {
// Client sends tz_offset (minutes from getTimezoneOffset(), e.g. 420 for PDT)
const rawOffset = parseInt(req.query.tz_offset, 10);
const offsetMin = Number.isFinite(rawOffset) ? rawOffset : 0;
const toLocal = `${-offsetMin} minutes`; // shift UTC → local
const toUTC = `${offsetMin} minutes`; // shift local → UTC
// Data-scope: when the user restricts to a subset of source machines and/or
// one AI tool, compute every count against that subset; otherwise use the
// cached prepared stmts.
if (parseTool(req) === false) return res.status(400).json(BAD_TOOL);
const scope = parseScope(req);
const overview = scope ? scoped.statsOverview(db, scope) : stmts.stats.get();
const agentsByStatus = scope
? scoped.agentStatusCounts(db, scope)
: stmts.agentStatusCounts.all();
const sessionsByStatus = scope
? scoped.sessionStatusCounts(db, scope)
: stmts.sessionStatusCounts.all();
const eventsToday = scope
? scoped.countEventsToday(db, scope, toLocal, toUTC)
: stmts.countEventsToday.get(toLocal, toUTC);
res.json({
...overview,
events_today: eventsToday?.count ?? 0,
ws_connections: getConnectionCount(),
agents_by_status: Object.fromEntries(agentsByStatus.map((r) => [r.status, r.count])),
sessions_by_status: Object.fromEntries(sessionsByStatus.map((r) => [r.status, r.count])),
});
});
module.exports = router;