diff --git a/pm-stack/aggregator/src/quota-current.ts b/pm-stack/aggregator/src/quota-current.ts index f625662642..765afd5b4b 100644 --- a/pm-stack/aggregator/src/quota-current.ts +++ b/pm-stack/aggregator/src/quota-current.ts @@ -22,6 +22,7 @@ import { poolState } from "./derive.ts"; import { isProviderAuthAlert, reevaluateProviderAuth } from "./provider-auth-alerts.ts"; import { lifecycleForEntry, safeProviderReason } from "./credential-lifecycle.ts"; import { computeChoices } from "./quota-guidance.ts"; +import { resolveTursoLimits, type TursoLimits } from "./quota-gather-map.ts"; import type { GathererSummary, PoolFreshness, @@ -582,6 +583,28 @@ const TURSO_WINDOW_META: Record "Scaler": the API's plan name, as a card label. */ +const titleCase = (plan: string): string => plan.charAt(0).toUpperCase() + plan.slice(1); + /** * Apply the gatherer's Turso entry (#1049) to the SaaS services — * the usage() counterpart of overlayQuotaCurrent, with the same @@ -606,6 +629,13 @@ export function overlayTursoUsage(services: SaasService[], file: QuotaCurrentFil if (freshOk.length > 0) { const entry = freshOk[0]; const prev = new Map(svc.metrics.map((m) => [m.id, m])); + // The plan is part of the read (#10436): the card's label and the + // limits it shows follow the read's plan, as the metrics do, rather + // than the dated local record (which said Starter Free while the + // org read Scaler). A read with no plan keeps the record's label + // and the Starter limits the windows were divided by. + const livePlan = entry.planLabel?.trim() || undefined; + const planLimits = livePlan ? resolveTursoLimits(livePlan, null) : null; const metrics: UsageMetric[] = []; for (const w of entry.windows) { const meta = Object.hasOwn(TURSO_WINDOW_META, w.name) ? TURSO_WINDOW_META[w.name] : undefined; @@ -614,7 +644,7 @@ export function overlayTursoUsage(services: SaasService[], file: QuotaCurrentFil id: w.name, label: prev.get(w.name)?.label ?? meta.label, used: w.value, - limit: meta.limit, + limit: planLimits ? tursoCardLimit(w.name, planLimits, meta.limit) : meta.limit, unit: meta.unit, ...(usablePct(w.pctOfLimit) ? { percent: w.pctOfLimit } : {}), highlight: prev.get(w.name)?.highlight ?? true, @@ -640,7 +670,10 @@ export function overlayTursoUsage(services: SaasService[], file: QuotaCurrentFil metrics, measuredAt: entry.measuredAt, sourceClass: "api", - sourceNote: `${svc.sourceNote} · live gatherer read (turso-api) at ${entry.measuredAt}`, + ...(livePlan ? { plan: `${titleCase(livePlan)} (live read)` } : {}), + sourceNote: livePlan + ? `${svc.sourceNote} · live gatherer read (turso-api) at ${entry.measuredAt}; plan and limits from the read (${livePlan})` + : `${svc.sourceNote} · live gatherer read (turso-api) at ${entry.measuredAt}`, ...(resetsAt ? { nextDate: `cycle ends ${resetsAt.slice(0, 10)}` } : {}), }; return out; diff --git a/pm-stack/aggregator/test/quota-current.test.ts b/pm-stack/aggregator/test/quota-current.test.ts index cbe260be33..8858975a07 100644 --- a/pm-stack/aggregator/test/quota-current.test.ts +++ b/pm-stack/aggregator/test/quota-current.test.ts @@ -465,6 +465,57 @@ describe("overlayTursoUsage (#1049)", () => { ["storage", 1.053323, "gb", 21.066], ]); }); + test("the card's plan label and limits follow the read's plan (#10436)", () => { + const at = iso(5 * 60_000); + const out = overlayTursoUsage( + [tursoSvc()], + file([ + entry({ + serviceId: "turso", + providerId: "turso", + source: "turso-api", + planLabel: "scaler", + measuredAt: at, + windows: [ + tursoWin("storage", 10.92672, 45.528, at), + tursoWin("rows-read", 2_849_000_000, 2.849, at), + tursoWin("rows-written", 6_670_000, 6.67, at), + tursoWin("sync", 1.22112, 5.088, at), + ], + }), + ]), + NOW, + ); + const svc = out[0]; + expect(svc.plan).toBe("Scaler (live read)"); + expect(svc.sourceNote).toContain("plan and limits from the read (scaler)"); + expect(svc.metrics.map((m) => [m.id, m.limit, m.percent])).toEqual([ + ["storage", 24, 45.528], + ["rows-read", 100_000_000_000, 2.849], + ["rows-written", 100_000_000, 6.67], + ["sync", 24, 5.088], + // Absent from the fresh read: kept as a stale dated value with its own limit. + ["databases", 100, 1], + ]); + }); + test("a read without a plan keeps the record's label and the Starter limits", () => { + const at = iso(5 * 60_000); + const out = overlayTursoUsage( + [tursoSvc()], + file([ + entry({ + serviceId: "turso", + providerId: "turso", + source: "turso-api", + measuredAt: at, + windows: [tursoWin("storage", 1.053323, 21.066, at)], + }), + ]), + NOW, + ); + expect(out[0].plan).toBe("Starter Free $0"); + expect(out[0].metrics[0].limit).toBe(5); + }); test("prototype-spelling Turso windows are ignored like unknown names", () => { const at = iso(5 * 60_000); const names = ["databases", "constructor", "__proto__", "toString", "hasOwnProperty", "valueOf", "unknown-window"];