|
| 1 | +/** |
| 2 | + * Shapes the recorded usage-limit history for the Limits view: one burn-down |
| 3 | + * line per account window, and the resets that happened in the period. |
| 4 | + * |
| 5 | + * @module usageLimitTrend |
| 6 | + */ |
| 7 | +import type { |
| 8 | + ServerProvider, |
| 9 | + UsageLimitHistory, |
| 10 | + UsageLimitHistoryPoint, |
| 11 | + UsageLimitReset, |
| 12 | +} from "@t3tools/contracts"; |
| 13 | +import { resolveProviderInstanceDisplayName } from "@t3tools/client-runtime/state/provider-instance-display"; |
| 14 | + |
| 15 | +export interface UsageLimitTrendLine { |
| 16 | + readonly windowId: string; |
| 17 | + readonly label: string; |
| 18 | + /** Remaining allowance now, by the last recorded reading. */ |
| 19 | + readonly remainingPercent: number; |
| 20 | + /** |
| 21 | + * SVG polyline points in a 0..100 by 0..100 box: x is time across the |
| 22 | + * period, y is 0 at a full allowance and 100 at an empty one. Stepped, |
| 23 | + * because a reading holds until the next one. |
| 24 | + */ |
| 25 | + readonly path: string; |
| 26 | +} |
| 27 | + |
| 28 | +export interface UsageLimitTrendAccount { |
| 29 | + readonly instanceId: string; |
| 30 | + readonly name: string; |
| 31 | + readonly lines: ReadonlyArray<UsageLimitTrendLine>; |
| 32 | + /** Newest first. */ |
| 33 | + readonly resets: ReadonlyArray<UsageLimitReset>; |
| 34 | +} |
| 35 | + |
| 36 | +const round = (value: number) => Math.round(value * 100) / 100; |
| 37 | + |
| 38 | +function steppedPath( |
| 39 | + points: ReadonlyArray<UsageLimitHistoryPoint>, |
| 40 | + sinceMs: number, |
| 41 | + nowMs: number, |
| 42 | +): string { |
| 43 | + const span = Math.max(1, nowMs - sinceMs); |
| 44 | + const x = (atMs: number) => |
| 45 | + round(((Math.min(Math.max(atMs, sinceMs), nowMs) - sinceMs) / span) * 100); |
| 46 | + const coordinates: string[] = []; |
| 47 | + let previousY: number | null = null; |
| 48 | + for (const point of points) { |
| 49 | + const px = x(Date.parse(point.at)); |
| 50 | + const py = round(point.usedPercent); |
| 51 | + // Hold the previous reading up to this moment, then step to the new one. |
| 52 | + if (previousY !== null) coordinates.push(`${px},${previousY}`); |
| 53 | + coordinates.push(`${px},${py}`); |
| 54 | + previousY = py; |
| 55 | + } |
| 56 | + if (previousY !== null) coordinates.push(`100,${previousY}`); |
| 57 | + return coordinates.join(" "); |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Accounts with at least one recorded reading, in the order the providers are |
| 62 | + * listed. An account the server no longer knows keeps its instance id as name, |
| 63 | + * so its history does not vanish with the account. |
| 64 | + */ |
| 65 | +export function buildUsageLimitTrend(input: { |
| 66 | + readonly history: UsageLimitHistory; |
| 67 | + readonly providers: ReadonlyArray<Pick<ServerProvider, "instanceId" | "driver" | "displayName">>; |
| 68 | + readonly sinceMs: number; |
| 69 | + readonly nowMs: number; |
| 70 | +}): ReadonlyArray<UsageLimitTrendAccount> { |
| 71 | + const byInstance = new Map<string, Map<string, UsageLimitHistoryPoint[]>>(); |
| 72 | + for (const point of input.history.points) { |
| 73 | + const windows = byInstance.get(point.instanceId) ?? new Map<string, UsageLimitHistoryPoint[]>(); |
| 74 | + byInstance.set(point.instanceId, windows); |
| 75 | + const points = windows.get(point.windowId) ?? []; |
| 76 | + windows.set(point.windowId, points); |
| 77 | + points.push(point); |
| 78 | + } |
| 79 | + const order = new Map( |
| 80 | + input.providers.map((provider, index) => [provider.instanceId as string, index]), |
| 81 | + ); |
| 82 | + return [...byInstance.entries()] |
| 83 | + .sort( |
| 84 | + ([left], [right]) => |
| 85 | + (order.get(left) ?? Number.MAX_SAFE_INTEGER) - |
| 86 | + (order.get(right) ?? Number.MAX_SAFE_INTEGER) || left.localeCompare(right), |
| 87 | + ) |
| 88 | + .map(([instanceId, windows]) => { |
| 89 | + const provider = input.providers.find((entry) => entry.instanceId === instanceId); |
| 90 | + return { |
| 91 | + instanceId, |
| 92 | + name: provider ? resolveProviderInstanceDisplayName(provider) : instanceId, |
| 93 | + lines: [...windows.entries()].map(([windowId, points]) => { |
| 94 | + const sorted = [...points].sort( |
| 95 | + (left, right) => Date.parse(left.at) - Date.parse(right.at), |
| 96 | + ); |
| 97 | + const last = sorted.at(-1)!; |
| 98 | + return { |
| 99 | + windowId, |
| 100 | + label: last.label, |
| 101 | + remainingPercent: Math.max(0, 100 - last.usedPercent), |
| 102 | + path: steppedPath(sorted, input.sinceMs, input.nowMs), |
| 103 | + }; |
| 104 | + }), |
| 105 | + resets: input.history.resets |
| 106 | + .filter((reset) => reset.instanceId === instanceId) |
| 107 | + .sort((left, right) => Date.parse(right.at) - Date.parse(left.at)), |
| 108 | + }; |
| 109 | + }); |
| 110 | +} |
0 commit comments