Skip to content
Open
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
26 changes: 12 additions & 14 deletions apps/api/src/routes/admin-global-stats.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@ const BUCKETS: Bucket[] = [
];

// The per-bucket fixture above also lands in the window every query covers.
const BASE_FIXTURE_COST = BUCKETS.reduce(
(sum, bucket) => sum + Math.fround(bucket.cost),
0,
);
// The cost columns are double precision, so these literals round-trip exactly
// and the expectation is simply their sum.
const BASE_FIXTURE_COST = BUCKETS.reduce((sum, bucket) => sum + bucket.cost, 0);

interface GlobalStatsResponse {
totals: {
Expand Down Expand Up @@ -244,12 +243,11 @@ describe("admin — global stats mode/kind dimensions", () => {
).toBe("default");
});

// Costs are stored in float4 columns. Summing them *as* float4 (Postgres'
// default for SUM(real)) runs out of significant digits well before the
// all-time totals this page shows, and the error depends on how the rows were
// grouped — so the headline stopped matching the slices underneath it. A
// handful of rows never showed it; the drift only appears at production
// magnitude, hence the bulk fixture.
// Costs are stored in double precision columns and summed in NUMERIC, so the
// all-time totals this page shows are exact however the rows were grouped.
// Under the previous float4 storage the headline stopped matching the slices
// underneath it. A handful of rows never showed it; the drift only appears at
// production magnitude, hence the bulk fixture.
test("large all-time sums stay exact and grouping-independent", async () => {
const DAYS = 700;
const PER_ROW_COST = 4321.1234;
Expand Down Expand Up @@ -293,11 +291,11 @@ describe("admin — global stats mode/kind dimensions", () => {
expect(byKind).toBeCloseTo(body.totals.cost, 2);
expect(timeseriesTotal).toBeCloseTo(body.totals.cost, 2);

// float4 rounds PER_ROW_COST on the way in, so compare against the value
// the column actually holds rather than the literal above.
const stored = Math.fround(PER_ROW_COST);
// Double precision holds PER_ROW_COST exactly, so the total must match the
// literal rather than a storage-rounded stand-in. The same fixture drifted
// by ~0.38 under float4 storage, far beyond a cent.
expect(body.totals.cost).toBeCloseTo(
stored * DAYS * BUCKETS.length + BASE_FIXTURE_COST, // eslint-disable-line no-mixed-operators
PER_ROW_COST * DAYS * BUCKETS.length + BASE_FIXTURE_COST, // eslint-disable-line no-mixed-operators
2,
);
});
Expand Down
Loading
Loading