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:
+93
-71
@@ -11,69 +11,98 @@
|
||||
* path; the unfiltered default keeps using db.js's prepared statements, so the
|
||||
* common zero-config case pays nothing for this feature.
|
||||
*
|
||||
* Every function takes a non-empty `sources` string array. The predicate is
|
||||
* either `source IN (...)` (queries over `sessions`) or a `session_id IN
|
||||
* (SELECT id FROM sessions WHERE source IN (...))` subquery (queries over
|
||||
* events / agents / token_usage), always via bound parameters.
|
||||
* Every function takes a `scope` = `{where, params}`, a predicate over
|
||||
* `sessions` with unqualified columns. It is applied either directly (queries
|
||||
* over `sessions`) or as a `session_id IN (SELECT id FROM sessions WHERE …)`
|
||||
* subquery (queries over events / agents / token_usage), always via bound
|
||||
* parameters.
|
||||
*
|
||||
* The analytics ones also take an optional `range` (`parseRange` in
|
||||
* source-filter.js): sessions and agents count by `started_at`, events by
|
||||
* `created_at`. The daily charts keep their own 365-day window.
|
||||
*
|
||||
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
|
||||
*/
|
||||
|
||||
/** `?,?,…` for N sources. */
|
||||
function ph(sources) {
|
||||
return sources.map(() => "?").join(",");
|
||||
/** Subquery restricting a `session_id` column to the scope's sessions. */
|
||||
function sessionSubquery(scope) {
|
||||
return `SELECT id FROM sessions WHERE ${scope.where}`;
|
||||
}
|
||||
|
||||
/** Subquery restricting a `session_id` column to the chosen sources. */
|
||||
function sessionSubquery(sources) {
|
||||
return `SELECT id FROM sessions WHERE source IN (${ph(sources)})`;
|
||||
/** ` AND <col> >= ? AND <col> < ?` for a range (`col` is always a constant). */
|
||||
function within(col, range) {
|
||||
const sql = [];
|
||||
const params = [];
|
||||
if (range?.start) {
|
||||
sql.push(` AND ${col} >= ?`);
|
||||
params.push(range.start);
|
||||
}
|
||||
if (range?.end) {
|
||||
sql.push(` AND ${col} < ?`);
|
||||
params.push(range.end);
|
||||
}
|
||||
return { sql: sql.join(""), params };
|
||||
}
|
||||
|
||||
function statsOverview(db, sources) {
|
||||
const sq = sessionSubquery(sources);
|
||||
const p = ph(sources);
|
||||
const row = db
|
||||
function statsOverview(db, scope, range) {
|
||||
const sq = sessionSubquery(scope);
|
||||
const p = scope.params;
|
||||
const s = within("started_at", range);
|
||||
const e = within("created_at", range);
|
||||
return db
|
||||
.prepare(
|
||||
`SELECT
|
||||
(SELECT COUNT(*) FROM sessions WHERE source IN (${p})) as total_sessions,
|
||||
(SELECT COUNT(*) FROM sessions WHERE status = 'active' AND source IN (${p})) as active_sessions,
|
||||
(SELECT COUNT(*) FROM agents WHERE status IN ('working','waiting') AND session_id IN (${sq})) as active_agents,
|
||||
(SELECT COUNT(*) FROM agents WHERE session_id IN (${sq})) as total_agents,
|
||||
(SELECT COUNT(*) FROM events WHERE session_id IN (${sq})) as total_events`
|
||||
(SELECT COUNT(*) FROM sessions WHERE ${scope.where}${s.sql}) as total_sessions,
|
||||
(SELECT COUNT(*) FROM sessions WHERE status = 'active' AND ${scope.where}${s.sql}) as active_sessions,
|
||||
(SELECT COUNT(*) FROM agents WHERE status IN ('working','waiting') AND session_id IN (${sq})${s.sql}) as active_agents,
|
||||
(SELECT COUNT(*) FROM agents WHERE session_id IN (${sq})${s.sql}) as total_agents,
|
||||
(SELECT COUNT(*) FROM events WHERE session_id IN (${sq})${e.sql}) as total_events`
|
||||
)
|
||||
.get(...sources, ...sources, ...sources, ...sources, ...sources);
|
||||
return row;
|
||||
.get(
|
||||
...p,
|
||||
...s.params,
|
||||
...p,
|
||||
...s.params,
|
||||
...p,
|
||||
...s.params,
|
||||
...p,
|
||||
...s.params,
|
||||
...p,
|
||||
...e.params
|
||||
);
|
||||
}
|
||||
|
||||
function agentStatusCounts(db, sources) {
|
||||
function agentStatusCounts(db, scope, range) {
|
||||
const s = within("started_at", range);
|
||||
return db
|
||||
.prepare(
|
||||
`SELECT status, COUNT(*) as count FROM agents WHERE session_id IN (${sessionSubquery(
|
||||
sources
|
||||
)}) GROUP BY status`
|
||||
scope
|
||||
)})${s.sql} GROUP BY status`
|
||||
)
|
||||
.all(...sources);
|
||||
.all(...scope.params, ...s.params);
|
||||
}
|
||||
|
||||
function sessionStatusCounts(db, sources) {
|
||||
function sessionStatusCounts(db, scope, range) {
|
||||
const s = within("started_at", range);
|
||||
return db
|
||||
.prepare(
|
||||
`SELECT status, COUNT(*) as count FROM sessions WHERE source IN (${ph(sources)}) GROUP BY status`
|
||||
`SELECT status, COUNT(*) as count FROM sessions WHERE ${scope.where}${s.sql} GROUP BY status`
|
||||
)
|
||||
.all(...sources);
|
||||
.all(...scope.params, ...s.params);
|
||||
}
|
||||
|
||||
function countEventsToday(db, sources, toLocal, toUTC) {
|
||||
function countEventsToday(db, scope, toLocal, toUTC) {
|
||||
return db
|
||||
.prepare(
|
||||
`SELECT COUNT(*) as count FROM events
|
||||
WHERE created_at >= datetime('now', ?, 'start of day', ?)
|
||||
AND session_id IN (${sessionSubquery(sources)})`
|
||||
AND session_id IN (${sessionSubquery(scope)})`
|
||||
)
|
||||
.get(toLocal, toUTC, ...sources);
|
||||
.get(toLocal, toUTC, ...scope.params);
|
||||
}
|
||||
|
||||
function tokenTotals(db, sources) {
|
||||
function tokenTotals(db, scope) {
|
||||
return db
|
||||
.prepare(
|
||||
`SELECT
|
||||
@@ -85,93 +114,87 @@ function tokenTotals(db, sources) {
|
||||
COALESCE(SUM(web_search_requests + baseline_web_search), 0) as total_web_search,
|
||||
COALESCE(SUM(web_fetch_requests + baseline_web_fetch), 0) as total_web_fetch,
|
||||
COALESCE(SUM(code_execution_requests + baseline_code_execution), 0) as total_code_execution
|
||||
FROM token_usage WHERE session_id IN (${sessionSubquery(sources)})`
|
||||
FROM token_usage WHERE session_id IN (${sessionSubquery(scope)})`
|
||||
)
|
||||
.get(...sources);
|
||||
.get(...scope.params);
|
||||
}
|
||||
|
||||
function toolUsageCounts(db, sources) {
|
||||
function toolUsageCounts(db, scope, range) {
|
||||
const e = within("created_at", range);
|
||||
return db
|
||||
.prepare(
|
||||
`SELECT tool_name, COUNT(*) as count FROM events
|
||||
WHERE tool_name IS NOT NULL AND session_id IN (${sessionSubquery(sources)})
|
||||
WHERE tool_name IS NOT NULL AND session_id IN (${sessionSubquery(scope)})${e.sql}
|
||||
GROUP BY tool_name ORDER BY count DESC LIMIT 20`
|
||||
)
|
||||
.all(...sources);
|
||||
.all(...scope.params, ...e.params);
|
||||
}
|
||||
|
||||
function dailyEventCounts(db, sources, tzModifier) {
|
||||
function dailyEventCounts(db, scope, tzModifier) {
|
||||
return db
|
||||
.prepare(
|
||||
`SELECT DATE(created_at, ?) as date, COUNT(*) as count FROM events
|
||||
WHERE created_at >= DATE('now', '-365 days') AND session_id IN (${sessionSubquery(sources)})
|
||||
WHERE created_at >= DATE('now', '-365 days') AND session_id IN (${sessionSubquery(scope)})
|
||||
GROUP BY 1 ORDER BY date ASC`
|
||||
)
|
||||
.all(tzModifier, ...sources);
|
||||
.all(tzModifier, ...scope.params);
|
||||
}
|
||||
|
||||
function dailySessionCounts(db, sources, tzModifier) {
|
||||
function dailySessionCounts(db, scope, tzModifier) {
|
||||
return db
|
||||
.prepare(
|
||||
`SELECT DATE(started_at, ?) as date, COUNT(*) as count FROM sessions
|
||||
WHERE started_at >= DATE('now', '-365 days') AND source IN (${ph(sources)})
|
||||
WHERE started_at >= DATE('now', '-365 days') AND ${scope.where}
|
||||
GROUP BY 1 ORDER BY date ASC`
|
||||
)
|
||||
.all(tzModifier, ...sources);
|
||||
.all(tzModifier, ...scope.params);
|
||||
}
|
||||
|
||||
function agentTypeDistribution(db, sources) {
|
||||
function agentTypeDistribution(db, scope, range) {
|
||||
const s = within("started_at", range);
|
||||
return db
|
||||
.prepare(
|
||||
`SELECT subagent_type, COUNT(*) as count FROM agents
|
||||
WHERE type = 'subagent' AND subagent_type IS NOT NULL AND session_id IN (${sessionSubquery(
|
||||
sources
|
||||
)})
|
||||
scope
|
||||
)})${s.sql}
|
||||
GROUP BY subagent_type ORDER BY count DESC`
|
||||
)
|
||||
.all(...sources);
|
||||
.all(...scope.params, ...s.params);
|
||||
}
|
||||
|
||||
function totalSubagentCount(db, sources) {
|
||||
function totalSubagentCount(db, scope, range) {
|
||||
const s = within("started_at", range);
|
||||
return db
|
||||
.prepare(
|
||||
`SELECT COUNT(*) as count FROM agents WHERE type = 'subagent' AND session_id IN (${sessionSubquery(
|
||||
sources
|
||||
)})`
|
||||
scope
|
||||
)})${s.sql}`
|
||||
)
|
||||
.get(...sources);
|
||||
.get(...scope.params, ...s.params);
|
||||
}
|
||||
|
||||
function eventTypeCounts(db, sources) {
|
||||
function eventTypeCounts(db, scope, range) {
|
||||
const e = within("created_at", range);
|
||||
return db
|
||||
.prepare(
|
||||
`SELECT event_type, COUNT(*) as count FROM events
|
||||
WHERE session_id IN (${sessionSubquery(sources)})
|
||||
WHERE session_id IN (${sessionSubquery(scope)})${e.sql}
|
||||
GROUP BY event_type ORDER BY count DESC`
|
||||
)
|
||||
.all(...sources);
|
||||
.all(...scope.params, ...e.params);
|
||||
}
|
||||
|
||||
function avgEventsPerSession(db, sources) {
|
||||
const sq = sessionSubquery(sources);
|
||||
function avgEventsPerSession(db, scope, range) {
|
||||
const s = within("started_at", range);
|
||||
const e = within("created_at", range);
|
||||
return db
|
||||
.prepare(
|
||||
`SELECT ROUND(CAST(COUNT(*) AS REAL) /
|
||||
MAX(1, (SELECT COUNT(*) FROM sessions WHERE source IN (${ph(sources)}))), 1) as avg
|
||||
FROM events WHERE session_id IN (${sq})`
|
||||
MAX(1, (SELECT COUNT(*) FROM sessions WHERE ${scope.where}${s.sql})), 1) as avg
|
||||
FROM events WHERE session_id IN (${sessionSubquery(scope)})${e.sql}`
|
||||
)
|
||||
.get(...sources, ...sources);
|
||||
}
|
||||
|
||||
/** token_usage rows joined to their session start date, scoped to sources. */
|
||||
function scopedTokenUsageWithDate(db, sources) {
|
||||
return db
|
||||
.prepare(
|
||||
`SELECT tu.*, DATE(s.started_at) as date
|
||||
FROM token_usage tu JOIN sessions s ON s.id = tu.session_id
|
||||
WHERE s.source IN (${ph(sources)})`
|
||||
)
|
||||
.all(...sources);
|
||||
.get(...scope.params, ...s.params, ...scope.params, ...e.params);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
@@ -187,5 +210,4 @@ module.exports = {
|
||||
totalSubagentCount,
|
||||
eventTypeCounts,
|
||||
avgEventsPerSession,
|
||||
scopedTokenUsageWithDate,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user