Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions pm-stack/aggregator/src/quota-current.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -582,6 +583,28 @@ const TURSO_WINDOW_META: Record<string, { label: string; unit: UsageUnit; limit:
sync: { label: "Sync / month", unit: "gb", limit: 3 },
};

/** The card limit for a Turso window under the read's plan (#10436),
* in the card's units (storage and sync in decimal GB, as billed). */
function tursoCardLimit(name: string, limits: TursoLimits, fallback: number): number {
switch (name) {
case "databases":
return limits.databases;
case "storage":
return limits.storageBytes / 1e9;
case "rows-read":
return limits.rowsRead;
case "rows-written":
return limits.rowsWritten;
case "sync":
return limits.bytesSynced / 1e9;
default:
return fallback;
}
}

/** "scaler" -> "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
Expand All @@ -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;
Expand All @@ -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,
Expand All @@ -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;
Expand Down
51 changes: 51 additions & 0 deletions pm-stack/aggregator/test/quota-current.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand Down