Files
Claude-Code-Monitor/server/routes/stats.js
T
nntrivi2001 57dc91585d feat: Claude Code Monitor — lanes, pipelines and a merged workspace
Internal SmartGift build of a Claude Code monitoring dashboard.

Lanes: a durable unit of parallel agent work, one per working directory,
tracked across session restarts. Managed lanes are git worktrees the
dashboard provisions and can reset or remove behind a three-check destroy
guard and a counted preflight; adopted lanes are directories you already
own and are never destroyable.

Pipelines: a lane moves through pipeline stages. A stage the agent declares
with evidence renders green; a stage inferred from the tool-event stream
renders dashed amber and never counts as done. Detection is forward-only
within a 30-minute window, and never writes the declared stage.

Workspace: one page at /run with a lane grid, the selected lane's pipeline,
and a full Claude console behind a disclosure.
2026-07-30 14:39:03 +07:00

46 lines
1.9 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 { parseSources } = 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, compute
// every count against that subset; otherwise use the cached prepared stmts.
const sources = parseSources(req);
const overview = sources ? scoped.statsOverview(db, sources) : stmts.stats.get();
const agentsByStatus = sources
? scoped.agentStatusCounts(db, sources)
: stmts.agentStatusCounts.all();
const sessionsByStatus = sources
? scoped.sessionStatusCounts(db, sources)
: stmts.sessionStatusCounts.all();
const eventsToday = sources
? scoped.countEventsToday(db, sources, 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;