mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-11-26 10:06:31 +00:00
feat(webui): usage and spend page
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import { splitModelId } from './model-id'
|
||||
|
||||
describe('splitModelId', () => {
|
||||
it('splits a provider-prefixed id at the first slash', () => {
|
||||
expect(splitModelId('uni/glm-5.3-flash')).toEqual({ provider: 'uni', model: 'glm-5.3-flash' })
|
||||
})
|
||||
|
||||
it('keeps further slashes in the model part', () => {
|
||||
expect(splitModelId('nvidia-nim/nvidia/nemotron-3-super')).toEqual({ provider: 'nvidia-nim', model: 'nvidia/nemotron-3-super' })
|
||||
})
|
||||
|
||||
it('returns a null provider for a bare id', () => {
|
||||
expect(splitModelId('gpt-5')).toEqual({ provider: null, model: 'gpt-5' })
|
||||
})
|
||||
|
||||
it('treats a leading or trailing slash as part of a bare id', () => {
|
||||
expect(splitModelId('/x')).toEqual({ provider: null, model: '/x' })
|
||||
expect(splitModelId('x/')).toEqual({ provider: null, model: 'x/' })
|
||||
})
|
||||
|
||||
it('returns an empty model for an empty id', () => {
|
||||
expect(splitModelId('')).toEqual({ provider: null, model: '' })
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,11 @@
|
||||
export interface SplitModelId {
|
||||
provider: string | null
|
||||
model: string
|
||||
}
|
||||
|
||||
/** Splits `provider/model` at the first slash; later slashes stay in the model part. A bare id has no provider. */
|
||||
export function splitModelId(id: string): SplitModelId {
|
||||
const slash = id.indexOf('/')
|
||||
if (slash <= 0 || slash === id.length - 1) return { provider: null, model: id }
|
||||
return { provider: id.slice(0, slash), model: id.slice(slash + 1) }
|
||||
}
|
||||
@@ -13,7 +13,7 @@ export interface Delta {
|
||||
export interface ChartLine {
|
||||
name: string
|
||||
data: number[]
|
||||
tone?: 'muted' | 'primary'
|
||||
tone?: 'muted' | 'primary' | 'danger'
|
||||
stack?: false
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
DEFAULT_RANGE,
|
||||
isCmpMode,
|
||||
isRangeKey,
|
||||
modelDetailSupported,
|
||||
RANGE_KEYS,
|
||||
RANGE_PRESETS,
|
||||
seriesUrl,
|
||||
@@ -138,3 +139,17 @@ describe('totalsUrl', () => {
|
||||
expect(url).toBe('/admin/api/usage/totals?kind=usermodel&window=month&span=1&metrics=req%2Ccost&limit=5&user=alice')
|
||||
})
|
||||
})
|
||||
|
||||
describe('modelDetailSupported', () => {
|
||||
it('rejects the ranges the API answers 400 for', () => {
|
||||
expect(modelDetailSupported('hour', 48)).toBe(false)
|
||||
expect(modelDetailSupported('day', 30)).toBe(false)
|
||||
})
|
||||
|
||||
it('accepts the other presets', () => {
|
||||
for (const key of RANGE_KEYS) {
|
||||
const { window, span } = RANGE_PRESETS[key]
|
||||
expect(modelDetailSupported(window, span)).toBe(key !== '48h' && key !== '30d')
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
@@ -75,6 +75,15 @@ export function comparison(range: RangeKey): ComparisonResult {
|
||||
return { available: true, offset: span }
|
||||
}
|
||||
|
||||
/** 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 }
|
||||
|
||||
/** 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]
|
||||
return limit === undefined || span < limit
|
||||
}
|
||||
|
||||
/**
|
||||
* dayOrMonthWindow clamps an arbitrary HistoryWindow/span pair (usually the
|
||||
* global filters store's current range) down to the day/month-only window
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import { drilldownLevel, makeScope, parseScope, scopeBudgetLimits, scopeId } from './scope'
|
||||
import { drilldownLevel, makeScope, parseScope, scopeBudgetLimits, scopeId, scopeMtdMicros } from './scope'
|
||||
import type { AdminGroupView, AdminUsageEntryView } from '@/types/api'
|
||||
|
||||
function group(overrides: Partial<AdminGroupView> = {}): AdminGroupView {
|
||||
@@ -112,3 +112,23 @@ describe('scopeBudgetLimits', () => {
|
||||
expect(scopeBudgetLimits('provider:openai', groups, users)).toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
describe('scopeMtdMicros', () => {
|
||||
const entry = (id: string, micros: number) => ({ id, costPerMonthMicroUsd: micros }) as never
|
||||
const usage = { total: entry('total', 900), groups: [entry('g1', 300)], users: [entry('u1', 100)] } as never
|
||||
|
||||
it('reads the fleet total for the all scope', () => {
|
||||
expect(scopeMtdMicros('all', usage)).toBe(900)
|
||||
})
|
||||
|
||||
it('reads a group or user entry by id', () => {
|
||||
expect(scopeMtdMicros('group:g1', usage)).toBe(300)
|
||||
expect(scopeMtdMicros('user:u1', usage)).toBe(100)
|
||||
})
|
||||
|
||||
it('is 0 for an unknown entity, a provider scope, or no usage yet', () => {
|
||||
expect(scopeMtdMicros('user:nobody', usage)).toBe(0)
|
||||
expect(scopeMtdMicros('provider:x', usage)).toBe(0)
|
||||
expect(scopeMtdMicros('all', null)).toBe(0)
|
||||
})
|
||||
})
|
||||
|
||||
+11
-1
@@ -10,7 +10,7 @@
|
||||
// function here directly, no component mount required.
|
||||
import { sumGroupBudgetMicros } from '@/lib/kpi'
|
||||
import { microsToUsd } from '@/lib/usage-bars'
|
||||
import type { AdminGroupView, AdminUsageEntryView, LimitsConfig } from '@/types/api'
|
||||
import type { AdminGroupView, AdminUsageEntryView, AdminUsageResponse, LimitsConfig } from '@/types/api'
|
||||
|
||||
/** ScopeKind is every prefix a "kind:id" scope string can carry across this panel — not every caller accepts every kind (a Spend burn-down scope never carries 'provider', a Reliability series scope never carries 'group'/'user'), but the prefix syntax itself is the same everywhere. */
|
||||
export type ScopeKind = 'group' | 'user' | 'model' | 'provider'
|
||||
@@ -89,3 +89,13 @@ export function scopeBudgetLimits(scope: string, groups: AdminGroupView[], users
|
||||
if (parsed.kind === 'user') return users.find((u) => u.id === parsed.id)?.limits
|
||||
return undefined
|
||||
}
|
||||
|
||||
/** scopeMtdMicros reads the month-to-date cost (micro-USD) of the scope's own entry in the polled usage snapshot; 0 when it has none (provider scope, unknown id, no snapshot yet). */
|
||||
export function scopeMtdMicros(scope: string, usage: AdminUsageResponse | null): number {
|
||||
if (!usage) return 0
|
||||
const parsed = parseScope(scope)
|
||||
if (!parsed) return usage.total.costPerMonthMicroUsd
|
||||
if (parsed.kind === 'group') return usage.groups.find((g) => g.id === parsed.id)?.costPerMonthMicroUsd ?? 0
|
||||
if (parsed.kind === 'user') return usage.users.find((u) => u.id === parsed.id)?.costPerMonthMicroUsd ?? 0
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import { activeScopes, activeSeries, other, ratioSeries, stackTopN, sumByProvider } from './series'
|
||||
import { activeScopes, activeSeries, other, ratioSeries, seriesToStacked, stackTopN, sumByProvider } from './series'
|
||||
|
||||
describe('stackTopN', () => {
|
||||
it('returns the n series with the largest total, descending', () => {
|
||||
@@ -155,3 +155,31 @@ describe('activeSeries / activeScopes', () => {
|
||||
expect([...activeScopes(series)].sort()).toEqual(['provider:a', 'provider:b'])
|
||||
})
|
||||
})
|
||||
|
||||
describe('seriesToStacked', () => {
|
||||
const s = (scope: string, ...points: number[]) => ({ scope, points })
|
||||
|
||||
it('groups beyond top into other', () => {
|
||||
const out = seriesToStacked([s('a', 1, 1), s('b', 10, 10), s('c', 5, 5), s('d', 2, 0)], 2)
|
||||
expect(out.series.map((x) => x.scope)).toEqual(['b', 'c'])
|
||||
expect(out.other).toEqual([3, 1])
|
||||
})
|
||||
|
||||
it('returns null other when nothing falls beyond top', () => {
|
||||
const out = seriesToStacked([s('a', 1), s('b', 2)], 5)
|
||||
expect(out.series.map((x) => x.scope)).toEqual(['b', 'a'])
|
||||
expect(out.other).toBeNull()
|
||||
})
|
||||
|
||||
it('puts everything into other when top is 0', () => {
|
||||
expect(seriesToStacked([s('a', 1, 2)], 0)).toEqual({ series: [], other: [1, 2] })
|
||||
})
|
||||
|
||||
it('handles an empty input', () => {
|
||||
expect(seriesToStacked([], 5)).toEqual({ series: [], other: null })
|
||||
})
|
||||
|
||||
it('pads short series so other stays bucket-aligned', () => {
|
||||
expect(seriesToStacked([s('a', 9, 9), s('b', 1)], 1).other).toEqual([1, 0])
|
||||
})
|
||||
})
|
||||
|
||||
@@ -123,3 +123,21 @@ export function activeScopes(series: Series[]): Set<string> {
|
||||
export function ratioSeries(numerator: number[], denominator: number[]): (number | null)[] {
|
||||
return denominator.map((d, i) => (d > 0 ? (numerator[i] ?? 0) / d : null))
|
||||
}
|
||||
|
||||
export interface StackedSeries {
|
||||
series: Series[]
|
||||
/** Per-bucket sum of every series beyond `top`; null when none fall beyond it. */
|
||||
other: number[] | null
|
||||
}
|
||||
|
||||
/** seriesToStacked keeps the `top` series by total for stacking and folds the rest into one bucket-aligned `other`. */
|
||||
export function seriesToStacked(series: Series[], top: number): StackedSeries {
|
||||
const ranked = [...series].sort((a, b) => sumSeries(b.points) - sumSeries(a.points))
|
||||
const kept = ranked.slice(0, Math.max(0, top))
|
||||
const rest = ranked.slice(kept.length)
|
||||
if (rest.length === 0) return { series: kept, other: null }
|
||||
const width = Math.max(...series.map((s) => s.points.length))
|
||||
const sum = new Array<number>(width).fill(0)
|
||||
for (const s of rest) for (let i = 0; i < width; i++) sum[i] = (sum[i] ?? 0) + (s.points[i] ?? 0)
|
||||
return { series: kept, other: sum }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user