ST-1216: Insights sprint-widget — token KPI-kaartjes & per-job tabel (#114)
* feat(ST-vmc7vpps): lib/insights/token-stats.ts — sprint KPI + per-job query SQL-queries voor totale tokens/kosten (KPI) en per-job tabel met ModelPrice JOIN. Guard op lege sprintId. Tests voor empty guard, KPI-mapping en null token-data. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat(ST-vmc7vpps): TokenUsageCard — KPI-kaartjes + sorteerbare per-job tabel Client-component met drie KPI-strips (totaal tokens, kosten USD, gem. per job) en sorteerbare tabel op kosten of duur. Nulls als '—', MD3-tokens, geen hardcoded kleuren. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat(ST-vmc7vpps): insights page — TokenUsageCard integreren Voeg getTokenStats + TokenUsageCard imports toe aan insights/page.tsx. tokenStats apart awaiten na activeSprints (kan niet in dezelfde Promise.all). TokenUsageCard-sectie toegevoegd na AgentThroughputCard. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
b147f813d4
commit
a2c8bd41af
4 changed files with 318 additions and 0 deletions
67
__tests__/lib/insights/token-stats.test.ts
Normal file
67
__tests__/lib/insights/token-stats.test.ts
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
|
||||
const { mockQueryRaw } = vi.hoisted(() => ({ mockQueryRaw: vi.fn() }))
|
||||
|
||||
vi.mock('@/lib/prisma', () => ({
|
||||
prisma: { $queryRaw: mockQueryRaw },
|
||||
}))
|
||||
|
||||
import { getTokenStats } from '@/lib/insights/token-stats'
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
describe('getTokenStats', () => {
|
||||
it('returns empty result for empty sprintId', async () => {
|
||||
const result = await getTokenStats('user-1', '')
|
||||
|
||||
expect(result.kpi.totalTokens).toBe(0)
|
||||
expect(result.kpi.totalCostUsd).toBe(0)
|
||||
expect(result.kpi.avgCostPerJob).toBe(0)
|
||||
expect(result.kpi.jobCount).toBe(0)
|
||||
expect(result.jobs).toHaveLength(0)
|
||||
expect(mockQueryRaw).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('maps kpi rows correctly', async () => {
|
||||
const kpiRows = [{ total_tokens: BigInt(10000), total_cost: 0.15, avg_cost: 0.05, job_count: BigInt(3) }]
|
||||
const jobRows: unknown[] = []
|
||||
mockQueryRaw.mockResolvedValueOnce(kpiRows).mockResolvedValueOnce(jobRows)
|
||||
|
||||
const result = await getTokenStats('user-1', 'sprint-1')
|
||||
|
||||
expect(result.kpi.totalTokens).toBe(10000)
|
||||
expect(result.kpi.totalCostUsd).toBe(0.15)
|
||||
expect(result.kpi.avgCostPerJob).toBe(0.05)
|
||||
expect(result.kpi.jobCount).toBe(3)
|
||||
})
|
||||
|
||||
it('maps job rows and handles null token data', async () => {
|
||||
const kpiRows = [{ total_tokens: BigInt(0), total_cost: null, avg_cost: null, job_count: BigInt(0) }]
|
||||
const jobRows = [
|
||||
{
|
||||
job_id: 'job-1',
|
||||
task_title: 'My Task',
|
||||
idea_code: null,
|
||||
model_id: 'claude-sonnet-4-6',
|
||||
input_tokens: null,
|
||||
output_tokens: null,
|
||||
cache_read_tokens: null,
|
||||
cache_write_tokens: null,
|
||||
cost_usd: null,
|
||||
duration_seconds: 42,
|
||||
},
|
||||
]
|
||||
mockQueryRaw.mockResolvedValueOnce(kpiRows).mockResolvedValueOnce(jobRows)
|
||||
|
||||
const result = await getTokenStats('user-1', 'sprint-1')
|
||||
|
||||
expect(result.jobs).toHaveLength(1)
|
||||
const job = result.jobs[0]
|
||||
expect(job.jobId).toBe('job-1')
|
||||
expect(job.taskTitle).toBe('My Task')
|
||||
expect(job.costUsd).toBeNull()
|
||||
expect(job.durationSeconds).toBe(42)
|
||||
})
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue