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
19 changes: 14 additions & 5 deletions pm-stack/aggregator/src/quota-gather-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,15 @@ export const TURSO_SOURCE = "turso-api";
const gb = (bytes: number): number => Math.round((bytes / 1e9) * 1e6) / 1e6;
const pct3 = (used: number, limit: number): number => Math.round((used / limit) * 100 * 1000) / 1000;

/** Turso usage counts are counts: a negative value is impossible, so it is
* treated exactly like a metric the response does not carry (#2553) — the
* window is omitted, never published negative. Local to entryFromTurso:
* the shared num() keeps its semantics for the other mappers. */
const numNonNegative = (v: unknown): number | null => {
const n = num(v);
return n !== null && n < 0 ? null : n;
};

/**
* Map one Turso usage + subscription read onto a quota-current
* entry (serviceId "turso", the SaaS card's snapshot id, so the
Expand Down Expand Up @@ -486,33 +495,33 @@ export function entryFromTurso(
state: "ok",
});
};
const databases = num(usage.databases);
const databases = numNonNegative(usage.databases);
push("databases", databases, TURSO_LIMITS.databases, databases ?? 0, `${databases} / ${TURSO_LIMITS.databases}`);
const storageBytes = num(usage.storage_bytes);
const storageBytes = numNonNegative(usage.storage_bytes);
push(
"storage",
storageBytes,
TURSO_LIMITS.storageBytes,
storageBytes == null ? 0 : gb(storageBytes),
`${storageBytes == null ? "?" : gb(storageBytes)} GB / 5 GB`,
);
const rowsRead = num(usage.rows_read);
const rowsRead = numNonNegative(usage.rows_read);
push(
"rows-read",
rowsRead,
TURSO_LIMITS.rowsRead,
rowsRead ?? 0,
`${rowsRead?.toLocaleString("en-US")} / ${TURSO_LIMITS.rowsRead.toLocaleString("en-US")}`,
);
const rowsWritten = num(usage.rows_written);
const rowsWritten = numNonNegative(usage.rows_written);
push(
"rows-written",
rowsWritten,
TURSO_LIMITS.rowsWritten,
rowsWritten ?? 0,
`${rowsWritten?.toLocaleString("en-US")} / ${TURSO_LIMITS.rowsWritten.toLocaleString("en-US")}`,
);
const bytesSynced = num(usage.bytes_synced);
const bytesSynced = numNonNegative(usage.bytes_synced);
push(
"sync",
bytesSynced,
Expand Down
36 changes: 36 additions & 0 deletions pm-stack/aggregator/test/quota-gather-map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,42 @@ describe("entryFromTurso (#1049)", () => {
});
});

describe("entryFromTurso negative usage (#2553)", () => {
const subDoc = {
subscription: { plan: "starter", name: "starter", current_billing_period_start: "2026-10-01T04:00:00+00:00", current_billing_period_end: "2026-11-01T04:00:00+00:00" },
};
const at = "2026-10-29T02:30:00.000Z";
test("all-negative usage read is an error entry with no windows and no records", () => {
const entry = entryFromTurso(
{ organization: { usage: { databases: -3, storage_bytes: -2, rows_read: -5, rows_written: -1, bytes_synced: -1 } } },
subDoc,
{ measuredAt: at },
);
expect(entry.state).toBe("error");
expect(entry.error).toContain("no known metrics");
expect(entry.windows).toEqual([]);
expect(recordsFromEntries([entry], new Map(), "quota-gatherer").records).toEqual([]);
});
test("one negative metric is omitted; the valid metrics publish unchanged and no record is negative", () => {
const entry = entryFromTurso(
{ organization: { usage: { databases: -3, storage_bytes: 1_053_323_264, rows_read: 1_905_385, rows_written: 30_026, bytes_synced: -1 } } },
subDoc,
{ measuredAt: at },
);
expect(entry.state).toBe("ok");
expect(entry.windows.map((w) => w.name).sort()).toEqual(["rows-read", "rows-written", "storage"]);
for (const w of entry.windows) {
expect(w.value).toBeGreaterThanOrEqual(0);
expect(w.pctOfLimit).toBeGreaterThanOrEqual(0);
}
const { records } = recordsFromEntries([entry], new Map(), "quota-gatherer");
expect(records.map((r) => r.metricId).sort()).toEqual(["rows-read", "rows-written", "storage"]);
for (const r of records) {
expect(r.value).toBeGreaterThanOrEqual(0);
}
});
});

describe("own-key guards on quota-gather-map lookups (#2781)", () => {
const at = "2026-10-27T05:30:00.000Z";
test("entryFromFileExport: prototype spellings get windowSeconds null; known names unchanged", () => {
Expand Down
Loading