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
15 changes: 10 additions & 5 deletions pm-stack/aggregator/src/snapshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export function isSnapshotRollup(v: unknown): v is SnapshotRollup {
if (typeof v !== "object" || v === null) return false;
const r = v as Partial<SnapshotRollup>;
if (r.kind !== "snapshot-rollup/v1") return false;
if (typeof r.month !== "string" || !/^\d{4}-\d{2}$/.test(r.month)) return false;
if (typeof r.month !== "string" || !/^\d{4}-(?:0[1-9]|1[0-2])$/.test(r.month)) return false;
if (typeof r.serviceId !== "string" || r.serviceId === "") return false;
if (typeof r.metricId !== "string" || r.metricId === "") return false;
if (typeof r.unit !== "string" || !UNITS.has(r.unit)) return false;
Expand All @@ -104,15 +104,20 @@ export function isSnapshotRollup(v: unknown): v is SnapshotRollup {
if (typeof r.count !== "number" || !Number.isInteger(r.count) || r.count < 1) return false;
if (typeof r.observedBy !== "string" || r.observedBy === "") return false;
for (const f of [r.firstTs, r.lastTs, r.minTs, r.maxTs]) {
if (typeof f !== "string" || Number.isNaN(Date.parse(f))) return false;
if (!isIsoDate(f)) return false;
}
if (r.resetsAt !== undefined && r.resetsAt !== null && !isIsoDate(r.resetsAt)) return false;
if (r.resetsAtTs !== undefined && !isIsoDate(r.resetsAtTs)) return false;
if (r.limit !== undefined && r.limit !== null && !isNonNegative(r.limit)) return false;
if (r.limitTs !== undefined && !isIsoDate(r.limitTs)) return false;
if (r.keys !== undefined && (!Array.isArray(r.keys) || r.keys.some((k) => typeof k !== "string"))) return false;
const val = (x: unknown): boolean =>
(typeof x === "number" && Number.isFinite(x)) ||
isFiniteNumber(x) ||
(typeof x === "object" &&
x !== null &&
typeof (x as { low?: unknown }).low === "number" &&
typeof (x as { high?: unknown }).high === "number");
isFiniteNumber((x as { low?: unknown }).low) &&
isFiniteNumber((x as { high?: unknown }).high) &&
(x as { low: number }).low <= (x as { high: number }).high);
return val(r.valueMin) && val(r.valueMax) && val(r.valueLast);
}

Expand Down
66 changes: 66 additions & 0 deletions pm-stack/aggregator/test/snapshot-retention.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import {
SnapshotStore,
compactRecordsToRollups,
expandRollup,
isSnapshotRecord,
isSnapshotRollup,
rollupFileFor,
shiftMonth,
withSnapshotStoreLock,
} from "../src/snapshots.ts";
import type { SnapshotRollup } from "../src/snapshots.ts";

const NOW = new Date("2026-10-29T12:00:00Z"); // cutoff month: 2025-09

Expand All @@ -39,6 +41,70 @@ function rec(
};
}

function rollup(overrides: Partial<SnapshotRollup> = {}): SnapshotRollup {
const ts = "2025-06-03T12:00:00Z";
return {
kind: "snapshot-rollup/v1",
month: "2025-06",
serviceId: "svc-a",
metricId: "weekly",
unit: "pct",
source: "api",
observedBy: "test",
count: 1,
firstTs: ts,
lastTs: ts,
valueMin: 10,
minTs: ts,
valueMax: 10,
maxTs: ts,
valueLast: 10,
keys: [],
...overrides,
};
}

describe("isSnapshotRollup", () => {
test("rejects impossible month, date, metadata, and value shapes", () => {
const invalid: Partial<SnapshotRollup>[] = [
{ month: "2025-00" },
{ month: "2025-13" },
{ firstTs: "2026-02-30T12:00:00Z" },
{ lastTs: "2026-02-30T12:00:00Z" },
{ minTs: "2026-02-30T12:00:00Z" },
{ maxTs: "2026-02-30T12:00:00Z" },
{ resetsAt: "2026-02-30T12:00:00Z" },
{ resetsAtTs: "2026-02-30T12:00:00Z" },
{ limit: -1 },
{ limit: "100" as unknown as number },
{ limitTs: "2026-02-30T12:00:00Z" },
{ valueMin: { low: Number.NaN, high: 2 } },
{ valueMax: { low: 1, high: Number.POSITIVE_INFINITY } },
{ valueLast: { low: 2, high: 1 } },
];

for (const overrides of invalid) {
expect(isSnapshotRollup(rollup(overrides))).toBe(false);
}
});

test("valid pair-valued rollups with quota metadata expand to valid records", () => {
const pair = { low: 10, high: 12 };
const valid = rollup({
valueMin: pair,
valueMax: pair,
valueLast: pair,
resetsAt: "2025-06-08T00:00:00Z",
resetsAtTs: "2025-06-03T12:00:00Z",
limit: 100,
limitTs: "2025-06-03T12:00:00Z",
});

expect(isSnapshotRollup(valid)).toBe(true);
expect(expandRollup(valid).every(isSnapshotRecord)).toBe(true);
});
});

async function seedStore(dir: string): Promise<void> {
await mkdir(dir, { recursive: true });
// Eligible month (2025-06): two services, one with a resetsAt window.
Expand Down
Loading