feat(webui): users page

This commit is contained in:
2026-11-25 11:52:36 +00:00
parent 713a8acb09
commit abdc059ed3
10 changed files with 760 additions and 10 deletions
+88
View File
@@ -0,0 +1,88 @@
import { describe, expect, it } from 'vitest'
import { effectiveLimitRows, isHot, meterModel, primaryLimit } from '@/lib/meters'
import type { LimitsConfig } from '@/types/api'
const PACING = { startPct: 80, maxDelay: '10s' }
const LIMITS: LimitsConfig = { requestsPerMinute: 30, requestsPerDay: 3000, tokensPerMinute: 1_200_000, pacing: PACING }
const ENTRY = { requestsPerMinute: 27, requestsPerDay: 1500, tokensPerMinute: 900_000 }
describe('meterModel', () => {
it('requests meter has pacing marker at startPct and limit marker', () => {
const m = meterModel(ENTRY, LIMITS).find((x) => x.label === 'Requests/min')
expect(m).toEqual({
label: 'Requests/min',
value: 27,
max: 30,
markers: [
{ value: 24, label: 'pacing 80%', tone: 'warning' },
{ value: 30, label: 'limit', tone: 'danger' },
],
})
})
it('absent tokensPerMinute shows limit only', () => {
const m = meterModel({ requestsPerMinute: 1, requestsPerDay: 2 }, LIMITS).find((x) => x.label === 'Tokens/min')
expect(m).toEqual({ label: 'Tokens/min', value: 0, max: 1_200_000, markers: [{ value: 1_200_000, label: 'limit', tone: 'danger' }] })
})
it('no limits -> []', () => {
expect(meterModel(ENTRY, undefined)).toEqual([])
expect(meterModel(ENTRY, {})).toEqual([])
})
it('day meter carries the limit only, never pacing', () => {
const m = meterModel(ENTRY, LIMITS).find((x) => x.label === 'Requests/day')
expect(m?.markers).toEqual([{ value: 3000, label: 'limit', tone: 'danger' }])
})
it('without pacing the minute meters have only the limit marker', () => {
const m = meterModel(ENTRY, { requestsPerMinute: 30 })
expect(m).toHaveLength(1)
expect(m[0]?.markers).toEqual([{ value: 30, label: 'limit', tone: 'danger' }])
})
it('lists the meters in a fixed order', () => {
expect(meterModel(ENTRY, LIMITS).map((m) => m.label)).toEqual(['Requests/min', 'Requests/day', 'Tokens/min'])
})
it('treats a zero limit as unset', () => {
expect(meterModel(ENTRY, { requestsPerMinute: 0, requestsPerDay: 0 })).toEqual([])
})
})
describe('isHot', () => {
it('is hot once a minute meter reaches the pacing threshold', () => {
expect(isHot({ requestsPerMinute: 24, requestsPerDay: 0 }, LIMITS)).toBe(true)
expect(isHot({ requestsPerMinute: 23, requestsPerDay: 0 }, LIMITS)).toBe(false)
})
it('reads live tokens too', () => {
expect(isHot({ requestsPerMinute: 0, requestsPerDay: 0, tokensPerMinute: 1_000_000 }, LIMITS)).toBe(true)
})
it('falls back to 80% without pacing and is false without limits', () => {
expect(isHot({ requestsPerMinute: 8, requestsPerDay: 0 }, { requestsPerMinute: 10 })).toBe(true)
expect(isHot({ requestsPerMinute: 7, requestsPerDay: 0 }, { requestsPerMinute: 10 })).toBe(false)
expect(isHot({ requestsPerMinute: 99, requestsPerDay: 0 }, undefined)).toBe(false)
})
})
describe('primaryLimit', () => {
it('prefers requests/min, then tokens/min, then requests/day', () => {
expect(primaryLimit(LIMITS)).toEqual({ limit: 30, unit: 'min' })
expect(primaryLimit({ tokensPerMinute: 5 })).toEqual({ limit: 5, unit: 'tok/min' })
expect(primaryLimit({ requestsPerDay: 9 })).toEqual({ limit: 9, unit: 'day' })
expect(primaryLimit(undefined)).toEqual({ limit: null, unit: '' })
})
})
describe('effectiveLimitRows', () => {
it('labels each limit with where it came from', () => {
const rows = effectiveLimitRows(LIMITS, { requestsPerMinute: 'memberLimits:friends', requestsPerDay: 'user' })
expect(rows.find((r) => r.label === 'Requests/min')).toMatchObject({ value: '30', source: 'group friends (per member)' })
expect(rows.find((r) => r.label === 'Requests/day')).toMatchObject({ value: '3,000', source: 'user' })
expect(rows.find((r) => r.label === 'Pacing')).toMatchObject({ value: 'from 80%, up to 10s delay' })
})
it('returns no rows without limits', () => {
expect(effectiveLimitRows(undefined, undefined)).toEqual([])
})
})
+109
View File
@@ -0,0 +1,109 @@
import { formatCompactCount } from '@/lib/format'
import type { AdminUsageEntryView, LimitSources, LimitsConfig } from '@/types/api'
/** Pacing starts here when a scope sets no pacing block; matches the budget bars' amber tier. */
const DEFAULT_HOT_FRACTION = 0.8
const PERCENT = 100
export interface MeterMarker {
value: number
label: string
tone: 'warning' | 'danger'
}
export interface Meter {
label: string
value: number
max: number
markers: MeterMarker[]
}
type LiveEntry = Pick<AdminUsageEntryView, 'requestsPerMinute' | 'requestsPerDay' | 'tokensPerMinute'>
const has = (n: number | undefined): n is number => typeof n === 'number' && Number.isFinite(n) && n > 0
function limitMarker(limit: number): MeterMarker {
return { value: limit, label: 'limit', tone: 'danger' }
}
function pacingMarker(limit: number, limits: LimitsConfig): MeterMarker | null {
const pct = limits.pacing?.startPct
return has(pct) ? { value: (limit * pct) / PERCENT, label: `pacing ${pct}%`, tone: 'warning' } : null
}
function minuteMarkers(limit: number, limits: LimitsConfig, live: boolean): MeterMarker[] {
const pacing = live ? pacingMarker(limit, limits) : null
return pacing ? [pacing, limitMarker(limit)] : [limitMarker(limit)]
}
/** meterModel builds one meter per set limit; pacing applies to minute meters only, and needs a live reading. */
export function meterModel(entry: LiveEntry, limits: LimitsConfig | undefined): Meter[] {
if (!limits) return []
const out: Meter[] = []
if (has(limits.requestsPerMinute)) {
out.push({ label: 'Requests/min', value: entry.requestsPerMinute, max: limits.requestsPerMinute, markers: minuteMarkers(limits.requestsPerMinute, limits, true) })
}
if (has(limits.requestsPerDay)) {
out.push({ label: 'Requests/day', value: entry.requestsPerDay, max: limits.requestsPerDay, markers: [limitMarker(limits.requestsPerDay)] })
}
if (has(limits.tokensPerMinute)) {
const live = entry.tokensPerMinute !== undefined
out.push({ label: 'Tokens/min', value: entry.tokensPerMinute ?? 0, max: limits.tokensPerMinute, markers: minuteMarkers(limits.tokensPerMinute, limits, live) })
}
return out
}
/** isHot reports a minute meter at or past the pacing start (80% when pacing is unset). */
export function isHot(entry: LiveEntry, limits: LimitsConfig | undefined): boolean {
if (!limits) return false
const threshold = has(limits.pacing?.startPct) ? (limits.pacing as { startPct: number }).startPct / PERCENT : DEFAULT_HOT_FRACTION
return meterModel(entry, limits)
.filter((m) => m.label.endsWith('/min'))
.some((m) => m.value / m.max >= threshold)
}
/** primaryLimit picks the single limit a table cell shows. */
export function primaryLimit(limits: LimitsConfig | undefined): { limit: number | null; unit: string } {
if (has(limits?.requestsPerMinute)) return { limit: limits.requestsPerMinute, unit: 'min' }
if (has(limits?.tokensPerMinute)) return { limit: limits.tokensPerMinute, unit: 'tok/min' }
if (has(limits?.requestsPerDay)) return { limit: limits.requestsPerDay, unit: 'day' }
return { limit: null, unit: '' }
}
export interface LimitRow {
key: string
label: string
value: string
source: string
}
const LIMIT_FIELDS: Array<{ key: Exclude<keyof LimitsConfig, 'pacing'>; label: string; usd?: boolean }> = [
{ key: 'requestsPerMinute', label: 'Requests/min' },
{ key: 'requestsPerDay', label: 'Requests/day' },
{ key: 'tokensPerMinute', label: 'Tokens/min' },
{ key: 'tokensPerDay', label: 'Tokens/day' },
{ key: 'tokensPerMonth', label: 'Tokens/month' },
{ key: 'costPerDayUSD', label: 'Cost/day', usd: true },
{ key: 'costPerMonthUSD', label: 'Cost/month', usd: true },
]
const MEMBER_PREFIX = 'memberLimits:'
function sourceLabel(source: string | undefined): string {
if (!source) return ''
return source.startsWith(MEMBER_PREFIX) ? `group ${source.slice(MEMBER_PREFIX.length)} (per member)` : source
}
/** effectiveLimitRows lists each set limit with the config layer it came from. */
export function effectiveLimitRows(limits: LimitsConfig | undefined, sources: LimitSources | undefined): LimitRow[] {
if (!limits) return []
const rows: LimitRow[] = []
for (const f of LIMIT_FIELDS) {
const v = limits[f.key]
if (has(v)) rows.push({ key: f.key, label: f.label, value: f.usd ? `$${v}` : formatCompactCount(v), source: sourceLabel(sources?.[f.key]) })
}
if (limits.pacing) {
rows.push({ key: 'pacing', label: 'Pacing', value: `from ${limits.pacing.startPct}%, up to ${limits.pacing.maxDelay} delay`, source: sourceLabel(sources?.pacing) })
}
return rows
}