Files
AgentHub/server/lib/scoped-stats.js
T
nntrivi2001 ca821008e2 perf: v2.5.10 — ready for 10+ concurrent users, leaner locally
Measured on a copy of a real 1.2 GB / 253K-event database, 10 simulated
browsers + 10 hooks/s for 90 s: event-loop delay p99 1239 ms → 32 ms (max
2.0 s → 94 ms), 266 failed requests → 0, 74 → 111 requests/s served, server
RSS 633 → 346 MB. Entry bundle 975 KB raw → 394 KB (106 KB brotli).

Server
- /api/analytics: the events-table scans run in the read worker
  (scoped-stats eventAggregates, read-pool job analyticsEvents); runRead takes
  maxAgeMs — analytics 10 s, /api/workflows 30 s — keyed by the args
- /api/workflows/skill-usage: +event_type and a tool_name range instead of
  LIKE, so it uses idx_events_tool (784 ms → a few ms)
- idx_events_session_type_tool (session_id, event_type, tool_name,
  created_at) replaces idx_events_session_type: covering reads for session
  stats, the hook dedupes and the import resume point (1.2 s → 20 ms); 64 MB
  cache_size and 256 MB mmap_size on both connections
- WAL: the server sets wal_autocheckpoint = 0 and checkpoints PASSIVE every
  DASHBOARD_WAL_CHECKPOINT_MS (5 s) from the read worker's own writable
  connection; a failed checkpoint restores the automatic one (hook COMMIT p95
  109 ms → 15 ms at 100 hooks/s)
- tmux: one list-sessions answers a whole GET /api/lanes and the heal beat
  (liveSessions), shared for 1 s and cleared by this process's new/kill —
  GET /api/run's sync spawns had blocked 60 s of a 90 s run; the heal beat
  skips tmux when nothing runs; attach options chained into 2 spawns
- hooks: TurnDuration/APIError deduped against one read per hook instead of a
  query per transcript entry (23 ms/hook on a 763-turn session); one lane
  scan per hook; hourly upserts skip rows that did not grow; frames are sent
  after the transaction commits (and after the pane binding); new_event
  carries its row id; a split pane's (slot 1–9) hooks no longer rebind the
  lane's session_id
- watchdog liveness probe runs ps asynchronously; transcript newline scan via
  indexOf; startup subagent backfill fingerprints the files it already checked
- pty attach: backpressure (pause past 1 MB unsent, resume under 256 KB)
- static: hashed assets served from their pre-compressed .br/.gz sibling

Client
- hidden tabs neither refetch on WS events nor poll (useLiveRefresh, panes,
  lane cards, git header, changes panel, settings); no per-tab hourly
  /updates/check (the server already checks)
- refetch storms throttled (workspace tree/session list 3 s, /api/run only on
  a run-key change, notification pane lookup only when shown); WS reconnect
  jitter; terminal retry backoff 1.5 → 30 s, stops on 1008
- heavier throttles for aggregates (15 s) and detail views; Monitor keepIfSame;
  GET in-flight dedupe in request()
- lazy Dashboard/History, xterm (with the first terminal), lane modals, Sagi
  (only while enabled); only the active locale loads; Workspace chunk
  prefetched on a /workspace deep link
- Sagi: no work while disabled, pupil via CSS variables, memo avatar
- build: .br/.gz siblings, sourcemap hidden; service worker no longer caches
  /assets/ (cache v3 drops the old one)

Tests: scripts/test-server.js drops the GIT_DIR-family variables a hook
exports, so a suite run from a linked worktree's pre-commit can no longer
commit into the real repository or set core.bare.

Docs: ARCHITECTURE, DATABASE, API, DEPLOYMENT, client README,
docs/plans/perf/README.md (audit, checklist, results).
2026-10-07 13:24:15 +07:00

256 lines
8.4 KiB
JavaScript

/**
* @file scoped-stats.js
* @description Source-scoped variants of the dashboard's aggregate queries
* (stats + analytics). When the user restricts the "data scope" to a subset of
* machines (see server/lib/source-filter.js), the routes call these instead of
* the cached prepared statements in db.js so EVERY headline number — session /
* agent / event counts, token totals, cost, daily charts, tool + type
* distributions — reflects only the chosen sources.
*
* These build SQL dynamically (per request) and are used ONLY on the filtered
* 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 `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>
*/
/** Subquery restricting a `session_id` column to the scope's sessions. */
function sessionSubquery(scope) {
return `SELECT id FROM sessions WHERE ${scope.where}`;
}
/** ` 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, 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 ${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(
...p,
...s.params,
...p,
...s.params,
...p,
...s.params,
...p,
...s.params,
...p,
...e.params
);
}
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(
scope
)})${s.sql} GROUP BY status`
)
.all(...scope.params, ...s.params);
}
function sessionStatusCounts(db, scope, range) {
const s = within("started_at", range);
return db
.prepare(
`SELECT status, COUNT(*) as count FROM sessions WHERE ${scope.where}${s.sql} GROUP BY status`
)
.all(...scope.params, ...s.params);
}
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(scope)})`
)
.get(toLocal, toUTC, ...scope.params);
}
function tokenTotals(db, scope) {
return db
.prepare(
`SELECT
COALESCE(SUM(input_tokens + baseline_input), 0) as total_input,
COALESCE(SUM(output_tokens + baseline_output), 0) as total_output,
COALESCE(SUM(cache_read_tokens + baseline_cache_read), 0) as total_cache_read,
COALESCE(SUM(cache_write_tokens + baseline_cache_write), 0) as total_cache_write,
COALESCE(SUM(cache_write_1h_tokens + baseline_cache_write_1h), 0) as total_cache_write_1h,
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(scope)})`
)
.get(...scope.params);
}
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(scope)})${e.sql}
GROUP BY tool_name ORDER BY count DESC LIMIT 20`
)
.all(...scope.params, ...e.params);
}
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(scope)})
GROUP BY 1 ORDER BY date ASC`
)
.all(tzModifier, ...scope.params);
}
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 ${scope.where}
GROUP BY 1 ORDER BY date ASC`
)
.all(tzModifier, ...scope.params);
}
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(
scope
)})${s.sql}
GROUP BY subagent_type ORDER BY count DESC`
)
.all(...scope.params, ...s.params);
}
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(
scope
)})${s.sql}`
)
.get(...scope.params, ...s.params);
}
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(scope)})${e.sql}
GROUP BY event_type ORDER BY count DESC`
)
.all(...scope.params, ...e.params);
}
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 ${scope.where}${s.sql})), 1) as avg
FROM events WHERE session_id IN (${sessionSubquery(scope)})${e.sql}`
)
.get(...scope.params, ...s.params, ...scope.params, ...e.params);
}
/**
* The analytics numbers that scan `events` (tool usage, daily events, event
* types, events per session) for a scope or none. Takes the connection as an
* argument so the read worker can run it (`analyticsEvents` in read-pool.js).
*/
function eventAggregates(db, scope, range, tzModifier) {
if (scope) {
return {
toolUsage: toolUsageCounts(db, scope, range),
dailyEvents: dailyEventCounts(db, scope, tzModifier),
eventTypes: eventTypeCounts(db, scope, range),
avgEvents: avgEventsPerSession(db, scope, range),
};
}
return {
toolUsage: db
.prepare(
`SELECT tool_name, COUNT(*) as count FROM events WHERE tool_name IS NOT NULL
GROUP BY tool_name ORDER BY count DESC LIMIT 20`
)
.all(),
dailyEvents: db
.prepare(
`SELECT DATE(created_at, ?) as date, COUNT(*) as count FROM events
WHERE created_at >= DATE('now', '-365 days') GROUP BY 1 ORDER BY date ASC`
)
.all(tzModifier),
eventTypes: db
.prepare(
"SELECT event_type, COUNT(*) as count FROM events GROUP BY event_type ORDER BY count DESC"
)
.all(),
avgEvents: db
.prepare(
`SELECT ROUND(CAST(COUNT(*) AS REAL) / MAX(1, (SELECT COUNT(*) FROM sessions)), 1) as avg
FROM events`
)
.get(),
};
}
module.exports = {
eventAggregates,
statsOverview,
agentStatusCounts,
sessionStatusCounts,
countEventsToday,
tokenTotals,
toolUsageCounts,
dailyEventCounts,
dailySessionCounts,
agentTypeDistribution,
totalSubagentCount,
eventTypeCounts,
avgEventsPerSession,
};