mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-11-26 10:06:31 +00:00
fix(webui): usage CSV, detail flags, burn-down labels
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import { thinLabels } from './chart-labels'
|
||||
|
||||
const nums = (n: number) => Array.from({ length: n }, (_, i) => String(i))
|
||||
|
||||
describe('thinLabels', () => {
|
||||
it('keeps every label when within max', () => {
|
||||
expect(thinLabels(nums(6))).toEqual(nums(6))
|
||||
})
|
||||
|
||||
it('keeps every Nth label and blanks the rest', () => {
|
||||
// 10 labels, max 6 -> N = 2
|
||||
expect(thinLabels(nums(10))).toEqual(['0', '', '2', '', '4', '', '6', '', '8', '9'])
|
||||
})
|
||||
|
||||
it('always keeps the last label', () => {
|
||||
const out = thinLabels(nums(25), 6)
|
||||
expect(out.at(-1)).toBe('24')
|
||||
expect(out).toHaveLength(25)
|
||||
})
|
||||
|
||||
it('keeps the output length and handles empty input', () => {
|
||||
expect(thinLabels([])).toEqual([])
|
||||
expect(thinLabels(['a'])).toEqual(['a'])
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,6 @@
|
||||
/** thinLabels blanks every label except every Nth (N = ceil(len / max)), always keeping the last, so long axes stay legible. */
|
||||
export function thinLabels(labels: string[], max = 6): string[] {
|
||||
const step = Math.max(1, Math.ceil(labels.length / Math.max(1, max)))
|
||||
const last = labels.length - 1
|
||||
return labels.map((label, i) => (i % step === 0 || i === last ? label : ''))
|
||||
}
|
||||
@@ -78,6 +78,7 @@ export function comparison(range: RangeKey): ComparisonResult {
|
||||
/** Spans from which GET /usage/models?detail=1 answers 400 "range too large" (observed on the live API: 48h and 30d fail, 24h and 14d pass). */
|
||||
const DETAIL_REJECTED_FROM_SPAN: Partial<Record<HistoryWindow, number>> = { hour: 48, day: 30 }
|
||||
|
||||
/** Pre-flight optimisation only (avoids a failed request); the retry-on-400 in stores/spend.ts fetchModelsRanking is the real guard for any range the backend rejects. */
|
||||
/** modelDetailSupported is false for ranges where asking for per-model detail is known to fail, so callers skip the doomed request. */
|
||||
export function modelDetailSupported(window: HistoryWindow, span: number): boolean {
|
||||
const limit = DETAIL_REJECTED_FROM_SPAN[window]
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import { usageCsv } from './usage-csv'
|
||||
import { breakdownCsv, usageCsv } from './usage-csv'
|
||||
import type { AdminUsageEntryView } from '@/types/api'
|
||||
|
||||
function testEntry(overrides: Partial<AdminUsageEntryView> = {}): AdminUsageEntryView {
|
||||
@@ -93,3 +93,14 @@ describe('usageCsv', () => {
|
||||
// modelRankingCsv/modelDetailCsv (and their own describe blocks here)
|
||||
// were removed with the functions themselves (P3 item 25, dead code) —
|
||||
// see usage-csv.ts's own doc comment.
|
||||
|
||||
describe('breakdownCsv', () => {
|
||||
it('exports id, selected metric and share', () => {
|
||||
const csv = breakdownCsv('user', 'req', [{ id: 'alice', value: 9, share: 0.75 }, { id: 'bob', value: 3, share: null }])
|
||||
expect(csv.trim().split(/\r?\n/)).toEqual(['user,req,share', 'alice,9,0.75', 'bob,3,'])
|
||||
})
|
||||
|
||||
it('labels the cost column with its unit', () => {
|
||||
expect(breakdownCsv('group', 'cost', []).split(/\r?\n/)[0]).toBe('group,cost (micro-USD),share')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -125,3 +125,15 @@ export function usageCsv(
|
||||
// redesigned shell — grep confirms only this file and its own now-deleted
|
||||
// spec section ever referenced them. The redesigned Models page
|
||||
// (ModelCatalogTable.vue) has no CSV export of its own.
|
||||
|
||||
export interface BreakdownCsvRow {
|
||||
id: string
|
||||
value: number
|
||||
share: number | null
|
||||
}
|
||||
|
||||
/** breakdownCsv exports exactly what a Usage breakdown table shows: id, the range-selected metric (raw; cost stays micro-USD) and its share. */
|
||||
export function breakdownCsv(idHeader: string, metric: string, rows: BreakdownCsvRow[]): string {
|
||||
const metricHeader = metric === 'cost' ? 'cost (micro-USD)' : metric
|
||||
return toCsv([idHeader, metricHeader, 'share'], rows.map((r) => [r.id, r.value, r.share === null ? '' : r.share]))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user