Files
traefik-llmgateway/webui/src/lib/polling.ts
T
lukaszraczylo 758d7c7020 feat(admin-ui): alias labels, active-provider legends, denser tables and readable money
Label configured model aliases with an alias badge wherever a model id shows.
List only providers with traffic in the window in provider chart legends.
Page long tables, show a scroll shadow on wide ones and drop the duplicate model link.
Show costs with thousands separators and magnitude-scaled decimals on cards and chart axes.
Use whole-number ticks for count charts and replace all-zero charts with a message.
Back off polling after failures.
Keep the CSV status region from widening the page on phones.
Regenerate admin_assets_gen.go.
2026-11-08 02:22:22 +00:00

156 lines
6.4 KiB
TypeScript

// createVisibilityPoller is the ONE polling primitive stores/dashboard.ts,
// stores/spend.ts, stores/events.ts, and composables/useNow.ts each wire
// their own refresh action into (vue.md: "if you've written it twice,
// you owe an abstraction") — originally three near-identical
// setInterval-plus-immediate-tick blocks (including the pre-redesign
// stores/history.ts, since deleted) collapsed into one tested module.
// Beyond the plain interval every
// one of them already had, this adds visibility awareness: a background
// browser tab has no reason to keep polling a Redis-backed admin panel
// every 5s/30s for no reader to see, and it catches back up the instant
// the tab is looked at again instead of waiting out whatever fraction of
// the interval happened to be left.
/**
* The subset of `Document` this module reads — a structural type, not the
* real DOM `Document`, so a test's fake stand-in needs no jsdom (vitest's
* 'node' environment, vite.config.ts, has none).
*/
export interface VisibilityDocumentLike {
readonly hidden: boolean
addEventListener(type: 'visibilitychange', listener: () => void): void
removeEventListener(type: 'visibilitychange', listener: () => void): void
}
export interface VisibilityPollerOptions {
/** How often `tick` fires while the page is visible. */
intervalMs: number
/**
* The store's own refresh step. May return a promise resolving `true` when the poll failed: consecutive failures then skip ticks (see backoffSkips) so a down gateway is not hit every interval. Never awaited here — a caller that
* needs its own in-flight guard (history.ts keeps its "skip a tick
* while the previous fetch is still in flight" rule) builds that into
* `tick` itself, same as before this module existed.
*/
tick: () => void | Promise<boolean | void>
/**
* The document to watch for visibility changes. Defaults to the real
* `document` global when one exists. In a non-browser context (vitest's
* 'node' test environment, or any future non-DOM caller) `document` is
* undefined, which is deliberately the "no document -> plain interval"
* fallback: every access to `doc` below is guarded, so importing or
* calling this module never throws there — it just behaves like a bare
* setInterval with an immediate first tick.
*/
doc?: VisibilityDocumentLike
}
export interface VisibilityPoller {
/** Idempotent: calling start() while already running is a no-op, mirroring the stores' own pre-existing startPolling/startAutoRefresh guards. */
start(): void
/** Idempotent: safe to call whether or not start() ever ran. */
stop(): void
}
/** resolveDoc picks the explicit `doc` option when given, else the real global if one exists, else undefined — computed once at creation, not read live on every tick. */
function resolveDoc(doc: VisibilityDocumentLike | undefined): VisibilityDocumentLike | undefined {
if (doc) return doc
return typeof document === 'undefined' ? undefined : (document as unknown as VisibilityDocumentLike)
}
/** Most ticks skipped between attempts while failing: 2^3 - 1, so a down gateway is retried at least every 8 intervals. */
const MAX_BACKOFF_EXPONENT = 3
/** backoffSkips is how many ticks to skip after `failures` consecutive failed polls; 0 while healthy. */
export function backoffSkips(failures: number): number {
return 2 ** Math.min(failures, MAX_BACKOFF_EXPONENT) - 1
}
export function createVisibilityPoller({ intervalMs, tick: rawTick, doc }: VisibilityPollerOptions): VisibilityPoller {
const target = resolveDoc(doc)
let failures = 0
let skipsLeft = 0
/** run fires the store's tick and, when it reports failure, schedules the backoff. `force` (start, tab becoming visible) bypasses pending skips. */
function run(force: boolean): void {
if (!force && skipsLeft > 0) {
skipsLeft--
return
}
const result = rawTick()
if (result instanceof Promise) {
void result.then(
(failed) => {
failures = failed === true ? failures + 1 : 0
skipsLeft = backoffSkips(failures)
},
() => {
failures++
skipsLeft = backoffSkips(failures)
},
)
}
}
const tick = (): void => run(false)
let timer: ReturnType<typeof setInterval> | undefined
// P13 review fix: `timer !== undefined` alone used to double as start()'s
// idempotency guard, but a poller that STARTS while the page is hidden
// (below) never schedules a timer at all until the page is first looked
// at — with no separate flag, a second start() call while still hidden
// would find `timer` still undefined and incorrectly tick/schedule a
// second time. `started` is the idempotency guard now; `timer` is purely
// "is a tick currently scheduled".
let started = false
function clearTimer(): void {
if (timer === undefined) return
clearInterval(timer)
timer = undefined
}
function scheduleTimer(): void {
clearTimer()
timer = setInterval(tick, intervalMs)
}
// Pauses on hidden (clear the interval — nobody can see the result of a
// background poll) and catches up on visible again: an immediate tick,
// not just a freshly (re)started interval that would otherwise make the
// reader wait out the full intervalMs before seeing anything current.
function onVisibilityChange(): void {
if (!target) return
if (target.hidden) {
clearTimer()
} else {
run(true)
scheduleTimer()
}
}
// P13: a poller STARTED while the document is already hidden (e.g. this
// admin panel loaded in a background tab) must not tick at all until the
// reader actually looks at it — ticking immediately regardless of
// visibility, the pre-fix behavior, polled a Redis-backed admin endpoint
// for a tab nobody could see, for as long as it stayed backgrounded.
// Ticking is deferred to onVisibilityChange's own "became visible" branch
// above, which already ticks immediately and reschedules the moment the
// tab is first shown — so a start-while-hidden poller's first tick is
// simply later, never skipped outright.
function start(): void {
if (started) return
started = true
if (!target?.hidden) {
run(true)
scheduleTimer()
}
target?.addEventListener('visibilitychange', onVisibilityChange)
}
function stop(): void {
started = false
clearTimer()
target?.removeEventListener('visibilitychange', onVisibilityChange)
}
return { start, stop }
}