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.
This commit is contained in:
+79
-53
@@ -1,77 +1,88 @@
|
||||
/**
|
||||
* @file Express router for analytics endpoints, providing aggregated statistics on token usage, tool usage, daily events/sessions, agent types, and more. It queries the database for various metrics and returns them in a structured JSON format for frontend consumption.
|
||||
* @file Express router for analytics endpoints, providing aggregated statistics on token usage, tool usage, daily events/sessions, agent types, and more, narrowed by data scope, AI tool and an optional `from`/`to` date range. `computeAnalytics` is the same bundle for scheduled reports.
|
||||
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
|
||||
*/
|
||||
|
||||
const { Router } = require("express");
|
||||
const { stmts, db } = require("../db");
|
||||
const { parseSources } = require("../lib/source-filter");
|
||||
const {
|
||||
parseScope,
|
||||
parseTool,
|
||||
BAD_TOOL,
|
||||
parseRange,
|
||||
inRange,
|
||||
BAD_RANGE,
|
||||
} = require("../lib/source-filter");
|
||||
const scoped = require("../lib/scoped-stats");
|
||||
|
||||
const { calculateCost } = require("./pricing");
|
||||
const { calculateCost, dailyTokenRows, costWhere } = require("./pricing");
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.get("/", (req, res) => {
|
||||
// Client sends tz_offset (minutes from getTimezoneOffset(), e.g. 420 for PDT)
|
||||
// Negate it to get the SQLite modifier: 420 → '-420 minutes'
|
||||
const rawOffset = parseInt(req.query.tz_offset, 10);
|
||||
const tzModifier = Number.isFinite(rawOffset) ? `${-rawOffset} minutes` : "+0 minutes";
|
||||
// Every session, for the scoped path when only a date range narrows.
|
||||
const ALL = { where: "1=1", params: [] };
|
||||
const USAGE_TOKEN_COLS = {
|
||||
total_input: "input_tokens",
|
||||
total_output: "output_tokens",
|
||||
total_cache_read: "cache_read_tokens",
|
||||
total_cache_write: "cache_write_tokens",
|
||||
};
|
||||
|
||||
// Data-scope: restrict every metric to a subset of source machines when the
|
||||
// user has chosen one; otherwise use the cached prepared statements.
|
||||
const sources = parseSources(req);
|
||||
const tokenTotals = sources ? scoped.tokenTotals(db, sources) : stmts.getTokenTotals.get();
|
||||
const toolUsage = sources ? scoped.toolUsageCounts(db, sources) : stmts.toolUsageCounts.all();
|
||||
const dailyEvents = sources
|
||||
? scoped.dailyEventCounts(db, sources, tzModifier)
|
||||
/**
|
||||
* The analytics bundle for a data scope (`parseScope`'s predicate or null) and
|
||||
* a date range (`parseRange`'s result or null). Also used by scheduled reports.
|
||||
* With a range, counts follow it and tokens/cost come from the cost rows of the
|
||||
* days in it (`token_usage` has no date); the daily charts keep 365 days.
|
||||
*/
|
||||
function computeAnalytics({ scope = null, range = null, tzModifier = "+0 minutes" } = {}) {
|
||||
const sc = scope || (range ? ALL : null);
|
||||
const toolUsage = sc ? scoped.toolUsageCounts(db, sc, range) : stmts.toolUsageCounts.all();
|
||||
const dailyEvents = sc
|
||||
? scoped.dailyEventCounts(db, sc, tzModifier)
|
||||
: stmts.dailyEventCounts.all(tzModifier);
|
||||
const dailySessions = sources
|
||||
? scoped.dailySessionCounts(db, sources, tzModifier)
|
||||
const dailySessions = sc
|
||||
? scoped.dailySessionCounts(db, sc, tzModifier)
|
||||
: stmts.dailySessionCounts.all(tzModifier);
|
||||
const agentTypes = sources
|
||||
? scoped.agentTypeDistribution(db, sources)
|
||||
const agentTypes = sc
|
||||
? scoped.agentTypeDistribution(db, sc, range)
|
||||
: stmts.agentTypeDistribution.all();
|
||||
const overview = sources ? scoped.statsOverview(db, sources) : stmts.stats.get();
|
||||
const agentsByStatus = sources
|
||||
? scoped.agentStatusCounts(db, sources)
|
||||
const overview = sc ? scoped.statsOverview(db, sc, range) : stmts.stats.get();
|
||||
const agentsByStatus = sc
|
||||
? scoped.agentStatusCounts(db, sc, range)
|
||||
: stmts.agentStatusCounts.all();
|
||||
const sessionsByStatus = sources
|
||||
? scoped.sessionStatusCounts(db, sources)
|
||||
const sessionsByStatus = sc
|
||||
? scoped.sessionStatusCounts(db, sc, range)
|
||||
: stmts.sessionStatusCounts.all();
|
||||
const totalSubagents = sources
|
||||
? scoped.totalSubagentCount(db, sources)
|
||||
const totalSubagents = sc
|
||||
? scoped.totalSubagentCount(db, sc, range)
|
||||
: stmts.totalSubagentCount.get();
|
||||
const eventTypes = sources ? scoped.eventTypeCounts(db, sources) : stmts.eventTypeCounts.all();
|
||||
const avgEvents = sources
|
||||
? scoped.avgEventsPerSession(db, sources)
|
||||
const eventTypes = sc ? scoped.eventTypeCounts(db, sc, range) : stmts.eventTypeCounts.all();
|
||||
const avgEvents = sc
|
||||
? scoped.avgEventsPerSession(db, sc, range)
|
||||
: stmts.avgEventsPerSession.get();
|
||||
|
||||
// Calculate total cost across all sessions
|
||||
const pricingRules = stmts.listPricing.all();
|
||||
// Join the owning session's start date so each bucket is priced at the rate
|
||||
// effective when it was used (date-effective promo rates, e.g. Sonnet 5 intro).
|
||||
const allTokenUsage = sources
|
||||
? scoped.scopedTokenUsageWithDate(db, sources)
|
||||
: db
|
||||
.prepare(
|
||||
"SELECT tu.*, DATE(s.started_at) as date FROM token_usage tu JOIN sessions s ON s.id = tu.session_id"
|
||||
)
|
||||
.all();
|
||||
|
||||
let totalCost = 0;
|
||||
for (const usage of allTokenUsage) {
|
||||
const { total_cost } = calculateCost([usage], pricingRules);
|
||||
totalCost += total_cost;
|
||||
// Total cost exactly as GET /api/pricing/cost computes it (baselines, the
|
||||
// per-day rate, the same scope and range), so the two numbers always agree.
|
||||
// A session that started after the range cannot have spent in it.
|
||||
const extra = range?.end ? [{ clause: "s.started_at < ?", params: [range.end] }] : [];
|
||||
const where = costWhere({ scope, extra });
|
||||
const rows = dailyTokenRows(tzModifier, where.whereClause, where.params).filter((r) =>
|
||||
inRange(range, r.date)
|
||||
);
|
||||
const totalCost = calculateCost(rows, stmts.listPricing.all()).total_cost;
|
||||
let tokens;
|
||||
if (range) {
|
||||
tokens = {};
|
||||
for (const [k, c] of Object.entries(USAGE_TOKEN_COLS)) {
|
||||
tokens[k] = Math.round(rows.reduce((n, r) => n + r[c], 0));
|
||||
}
|
||||
} else {
|
||||
const t = scope ? scoped.tokenTotals(db, scope) : stmts.getTokenTotals.get();
|
||||
tokens = Object.fromEntries(Object.keys(USAGE_TOKEN_COLS).map((k) => [k, t?.[k] ?? 0]));
|
||||
}
|
||||
|
||||
res.json({
|
||||
tokens: {
|
||||
total_input: tokenTotals?.total_input ?? 0,
|
||||
total_output: tokenTotals?.total_output ?? 0,
|
||||
total_cache_read: tokenTotals?.total_cache_read ?? 0,
|
||||
total_cache_write: tokenTotals?.total_cache_write ?? 0,
|
||||
},
|
||||
return {
|
||||
tokens,
|
||||
total_cost: totalCost,
|
||||
tool_usage: toolUsage,
|
||||
daily_events: dailyEvents,
|
||||
@@ -83,7 +94,22 @@ router.get("/", (req, res) => {
|
||||
overview,
|
||||
agents_by_status: Object.fromEntries(agentsByStatus.map((r) => [r.status, r.count])),
|
||||
sessions_by_status: Object.fromEntries(sessionsByStatus.map((r) => [r.status, r.count])),
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
router.get("/", (req, res) => {
|
||||
// Client sends tz_offset (minutes from getTimezoneOffset(), e.g. 420 for PDT)
|
||||
// Negate it to get the SQLite modifier: 420 → '-420 minutes'
|
||||
const rawOffset = parseInt(req.query.tz_offset, 10);
|
||||
const tzModifier = Number.isFinite(rawOffset) ? `${-rawOffset} minutes` : "+0 minutes";
|
||||
|
||||
// Data-scope: restrict every metric to a subset of source machines and/or one
|
||||
// AI tool when the user has chosen one, and to `?from=&to=` (local dates).
|
||||
if (parseTool(req) === false) return res.status(400).json(BAD_TOOL);
|
||||
const range = parseRange(req);
|
||||
if (range === false) return res.status(400).json(BAD_RANGE);
|
||||
res.json(computeAnalytics({ scope: parseScope(req), range, tzModifier }));
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
module.exports.computeAnalytics = computeAnalytics;
|
||||
|
||||
Reference in New Issue
Block a user