perf(dashboard): count all-time readings from the DAY rollups

The summary's all-time aggregate grouped every live reading of the
account by type: one index entry per reading, millions on an account
with a watch, two to four seconds whenever they were not in memory and
the slowest read of the request on every cache miss. The count now sums
the DAY buckets that already hold a per-day count of every source,
counts live only the readings before the fold window and any type not
folded yet, and takes lastSeenAt from the newest reading the
latest-reading read already returns. Parity with the live count is
pinned against a real Postgres.
This commit is contained in:
Marc-André Bombeck committed 2026-10-20 20:27:32 +02:00
1 parent f01f53a1cc
commit 05ae8b1e99
6 files changed
+229 -54

No files matched your search

@@ -85,7 +85,7 @@ describe("dashboard summary reads — sparkline rollup buckets", () => {
// defeat statement reuse.
const sql = code(READS);
const casts = sql.match(/\}::"measurement_type"\[\]/g) ?? [];
expect(casts.length).toBe(2);
expect(casts.length).toBe(3);
expect(sql).not.toMatch(/\$queryRawUnsafe/);
});
});
@@ -134,6 +134,8 @@ const ALLOWED_SQL: Record<string, string> = {
"lib/rollups/measurement-coverage.ts::1":
"Selects from `unnest` of the caller's type list; `measurements` is read inside `EXISTS`, one row per type at most.",
"lib/dashboard/summary-reads.ts::1":
"Selects from `unnest` of the type list; `measurements` is read only inside COUNT scalar subqueries, before the fold window for a type the DAY rollups cover.",
"lib/dashboard/summary-reads.ts::2":
"Selects from `generate_series` of the streak window's local days; `measurements` is read inside `EXISTS`, one row per day at most.",
};