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 = `
`;
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 = `
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
' : `
${(() => {
const max = models[0]?.count || 1;
return models.map(m => {
const pct = (m.count / max * 100).toFixed(1);
return `-
`;
}).join('');
})()}
`}
Top Tools ${tools.length}
${tools.length === 0 ? '
No tool data yet
' : `
`}
`;
}