import { app, isRouteCurrent } from '../router.js'; import { api } from '../api.js'; import { escapeHTML, formatTokenCount, formatCost } from '../utils.js'; export async function renderUsage(routeToken) { app.innerHTML = `
Loading…
`; const [summary, toolsData, modelsData, tsData] = await Promise.all([ api('/v1/stats/summary').catch(() => null), api('/v1/stats/top-tools?limit=20').catch(() => ({ tools: [] })), api('/v1/stats/top-models?limit=10').catch(() => ({ models: [] })), api('/v1/stats/timeseries?window=7d').catch(() => ({ series: [] })), ]); if (routeToken && !isRouteCurrent(routeToken)) return; const tools = toolsData.tools || []; const models = modelsData.models || []; const series = tsData.series || []; // Aggregate 7d totals from timeseries const totals7d = series.reduce((acc, b) => { acc.runs += b.runs || 0; acc.tools += b.tools || 0; acc.errors += b.errors || 0; acc.tokens += b.tokens || 0; acc.cost += b.cost || 0; return acc; }, { runs: 0, tools: 0, errors: 0, tokens: 0, cost: 0 }); const s = summary || {}; const content = document.getElementById('usage-content'); if (!content) return; content.innerHTML = `
Active Sessions
${s.active_sessions || 0}
Runs Today
${s.runs_today || 0}
Tool Calls Today
${s.tool_calls_today || 0}
Errors Today
${s.errors_today || 0}
Tokens Today
${formatTokenCount(s.tokens_today || 0)}
Cost Today
${formatCost(s.cost_today || 0)}
7-Day Totals
Runs${totals7d.runs}
Tool Calls${totals7d.tools}
Errors${totals7d.errors}
Tokens${formatTokenCount(totals7d.tokens)}
Est. Cost${formatCost(totals7d.cost)}
Top Models ${models.length}
${models.length === 0 ? '

No model data yet

' : ` `}
Top Tools ${tools.length}
${tools.length === 0 ? '

No tool data yet

' : ` `}
`; }