feat(webui): add limit bars, forecasts, events, exports and URL state

The Usage tab shows a bar for each configured limit. It turns amber at
80% and red at 100%. The tab also shows rejected requests per day, a
projected month-end cost with budget headroom, and CSV export for users
and groups. Names link to Charts with the matching scope.

The Charts tab ranks models over the full 24h, 30d or 12mo window. A
provider filter is applied on the server, and the ranking can be
exported as CSV. A new Events tab lists recent failures, filtered by
kind and user. The selected tab, filters and scope live in the URL
hash. Polling stops while the page is hidden. Views show which replica
answered, and a banner lists configuration warnings.

CSV cells that start with a formula character are neutralised. Links
sit outside the accordion buttons, so both controls keep their
accessible names. admin_assets_gen.go is regenerated.

Claude-Session: https://claude.ai/code/session_01V5jYJNxpDFdJkyyExpADrv
This commit is contained in:
2026-09-23 13:09:57 +01:00
parent 25e6a7779e
commit 4261efc41b
53 changed files with 4777 additions and 238 deletions
+106
View File
@@ -0,0 +1,106 @@
// F7 (cost forecast): pure month-to-date -> projected-month-end math for
// CostForecastCard.vue and usage-columns.ts's costMonthProj column. Kept
// out of the component/column the same way lib/usage-bars.ts and lib/
// provider-rate.ts keep their own math out of their components — vitest's
// node-environment config (vite.config.ts's `test` block) exercises it
// directly, no component mount or fake timers required (every function
// here takes `now`/`elapsedFraction` as an explicit argument, never reads
// Date.now() itself).
//
// All month-boundary math is UTC (coordinator brief): costPerMonthMicroUsd
// (admin.go, adminUsageEntryView) accumulates against a UTC calendar
// month bucket (limits.go's windowKey "200601" — same convention
// lib/format.ts's formatBucketLabel documents for the hour/day buckets),
// so projecting against the LOCAL calendar month would silently misalign
// with the figure actually being projected on the last/first day of a
// month in any timezone west or east of UTC.
// MICROS_PER_USD (P11 review fix, DRY): imported from lib/usage-bars.ts,
// the one shared definition — this module used to declare its own
// identical copy, one of three (usage-bars.ts, usage-columns.ts) the
// coordinator's review flagged.
import { MICROS_PER_USD } from '@/lib/usage-bars'
/**
* MIN_PROJECTION_FRACTION is the smallest elapsed-month fraction
* projectMonthEnd will extrapolate from — below it (coordinator: "null
* below MIN_PROJECTION_FRACTION"), too little of the month has elapsed for
* a mtd/elapsed extrapolation to mean anything: a single early request
* would otherwise project to a wildly overstated month-end figure. 0.02 of
* a 30-day month is 0.6 days — 14.4 hours; of a 31-day month, 0.62 days —
* 14.88 hours. Both round to "the first ~14-15 hours of the month",
* comfortably past the first request of any real deployment's day but
* still early enough that a genuinely idle scope reports no projection at
* all rather than a fabricated one.
*/
export const MIN_PROJECTION_FRACTION = 0.02
/**
* monthProgress returns how far `now` has advanced through ITS OWN UTC
* calendar month, as a fraction in [0, 1): 0 at the first instant of the
* month (UTC midnight on the 1st), approaching 1 as the month's last
* instant nears. Correct across every month length (28/29/30/31 days,
* including a leap February) because it measures against the ACTUAL
* elapsed span from this month's start to next month's start, computed
* with Date.UTC (which itself normalizes an out-of-range month index —
* `Date.UTC(y, 12, 1)` for December's "next month" already resolves to
* January 1 of `y + 1`), never a hardcoded days-per-month table.
*/
export function monthProgress(now: Date): number {
const year = now.getUTCFullYear()
const month = now.getUTCMonth()
const monthStart = Date.UTC(year, month, 1)
const nextMonthStart = Date.UTC(year, month + 1, 1)
const elapsedMs = now.getTime() - monthStart
const totalMs = nextMonthStart - monthStart
return elapsedMs / totalMs
}
/**
* projectMonthEnd linearly extrapolates a month-to-date micro-USD total to
* a projected month-end total: `mtdMicros / elapsedFraction`, rounded to
* the nearest whole micro-USD (matching metrics.go's own usdToMicros
* round convention, dashboard-plan.md section 0 ground truth). Returns
* null below MIN_PROJECTION_FRACTION (coordinator decision) — including
* for `elapsedFraction === 0`, which would otherwise divide by zero — so
* a caller renders "not enough data yet" instead of a meaningless or
* infinite figure for the first sliver of a month.
*/
export function projectMonthEnd(mtdMicros: number, elapsedFraction: number): number | null {
if (elapsedFraction < MIN_PROJECTION_FRACTION) return null
return Math.round(mtdMicros / elapsedFraction)
}
/**
* CostHeadroom is projectMonthEnd's projected total compared against a
* scope's configured costPerMonthUSD limit (LimitsConfig — 0/undefined
* means unlimited, formatLimits' own convention). `limitMicros`/
* `remainingMicros` are both null whenever there is nothing to compare
* against (no limit configured, or no projection yet); `willExceed` is
* true only when a real projection crosses a real, positive limit.
*/
export interface CostHeadroom {
limitMicros: number | null
/** limitMicros - projectedMicros — negative once the projection has crossed the limit. Null under the same conditions limitMicros is null. */
remainingMicros: number | null
willExceed: boolean
}
/**
* headroom compares a projected month-end micro-USD total against an
* optional costPerMonthUSD limit. `projectedMicros` is null whenever
* projectMonthEnd itself returned null (too early in the month) —
* headroom then has nothing to report either, regardless of whether a
* limit is configured, since "will it exceed" cannot be answered yet.
* `limitUsd` undefined, zero, or negative all read as "unlimited"
* (LimitsConfig's own convention — formatLimits' identical `if (limits.x)`
* gate), never divide-by-zero or a fabricated 0% headroom.
*/
export function headroom(projectedMicros: number | null, limitUsd: number | undefined): CostHeadroom {
if (projectedMicros === null || limitUsd === undefined || limitUsd <= 0) {
return { limitMicros: null, remainingMicros: null, willExceed: false }
}
const limitMicros = Math.round(limitUsd * MICROS_PER_USD)
const remainingMicros = limitMicros - projectedMicros
return { limitMicros, remainingMicros, willExceed: remainingMicros < 0 }
}