feat: enhance usage analytics dashboard (#666)

- visualize input, output, and cache token segments in usage charts
- add usage period selector for 7d, 30d, 90d, and 365d
- guard usage stats against stale overlapping period requests
- normalize blank model usage into unknown buckets
- add client and server coverage for usage analytics behavior
This commit is contained in:
Zhicheng Han
2026-05-13 01:41:49 +02:00
committed by GitHub
parent 57cdf87bef
commit c2068302c3
18 changed files with 683 additions and 113 deletions
+37
View File
@@ -220,6 +220,43 @@ describe('session conversations controller', () => {
})
})
it('keeps blank model usage under an unknown bucket', async () => {
getLocalUsageStatsMock.mockReturnValue({
input_tokens: 3,
output_tokens: 1,
cache_read_tokens: 2,
cache_write_tokens: 0,
reasoning_tokens: 0,
sessions: 1,
by_model: [
{ model: '', input_tokens: 3, output_tokens: 1, cache_read_tokens: 2, cache_write_tokens: 0, reasoning_tokens: 0, sessions: 1 },
],
by_day: [],
})
getUsageStatsFromDbMock.mockResolvedValue({
input_tokens: 2,
output_tokens: 1,
cache_read_tokens: 1,
cache_write_tokens: 1,
reasoning_tokens: 0,
sessions: 1,
cost: 0,
total_api_calls: 0,
by_model: [
{ model: ' ', input_tokens: 2, output_tokens: 1, cache_read_tokens: 1, cache_write_tokens: 1, reasoning_tokens: 0, sessions: 1 },
],
by_day: [],
})
const mod = await import('../../packages/server/src/controllers/hermes/sessions')
const ctx: any = { query: { days: '2' }, body: null }
await mod.usageStats(ctx)
expect(ctx.body.model_usage).toEqual([
{ model: 'unknown', input_tokens: 5, output_tokens: 2, cache_read_tokens: 3, cache_write_tokens: 1, reasoning_tokens: 0, sessions: 2 },
])
})
describe('exportSession', () => {
it('returns session as JSON download with correct headers (full mode)', async () => {
const sessionData = { id: 'abc-123', title: 'Test Session', messages: [{ id: 1, role: 'user', content: 'hello' }] }