update rollup

This commit is contained in:
2026-04-09 01:34:41 +04:00
parent ae5a8f4597
commit 1732a9d055
4 changed files with 30 additions and 39 deletions
+5 -8
View File
@@ -2,17 +2,16 @@ import sql from "../db";
// Aggregates raw pings into monitor_uptime_rollup so status pages and dashboard
// widgets can compute uptime % over arbitrary windows without ever scanning the
// pings table at read time. Three resolutions: hourly, daily, weekly.
// pings table at read time. Two resolutions: hourly and daily.
//
// Each pass aggregates the *current* bucket only. The query is bounded by the
// bucket size, not the table size, so it's cheap regardless of history depth.
type BucketType = "hourly" | "daily" | "weekly";
type BucketType = "hourly" | "daily";
const BUCKET_TRUNC: Record<BucketType, string> = {
hourly: "hour",
daily: "day",
weekly: "week",
};
async function rollupCurrent(bucket: BucketType): Promise<number> {
@@ -83,12 +82,11 @@ export async function startRollupJob() {
// we throw so a broken rollup query trips the api process and shows in the
// service logs immediately, instead of leaving the table mysteriously empty.
try {
const [h, d, w] = await Promise.all([
const [h, d] = await Promise.all([
backfillRecent("hourly", 48),
backfillRecent("daily", 90),
backfillRecent("weekly", 26),
]);
console.log(`[rollup] backfilled rows: hourly=${h} daily=${d} weekly=${w}`);
console.log(`[rollup] backfilled rows: hourly=${h} daily=${d}`);
} catch (e) {
console.error("[rollup] backfill FAILED — rollup table will be empty until fixed:", e);
}
@@ -97,7 +95,7 @@ export async function startRollupJob() {
// run rollupCurrent immediately so we have at least one row per type. This
// covers the "fresh deploy with very recent pings only" case.
try {
for (const b of ["hourly", "daily", "weekly"] as BucketType[]) {
for (const b of ["hourly", "daily"] as BucketType[]) {
if (await rollupIsEmpty(b)) {
console.log(`[rollup] ${b} still empty — forcing current-bucket aggregation`);
await rollupCurrent(b);
@@ -110,5 +108,4 @@ export async function startRollupJob() {
// Periodic refreshes for the *current* bucket of each resolution.
setInterval(() => { rollupCurrent("hourly").catch((e) => console.warn("[rollup] hourly failed:", e)); }, 5 * 60 * 1000);
setInterval(() => { rollupCurrent("daily").catch((e) => console.warn("[rollup] daily failed:", e)); }, 30 * 60 * 1000);
setInterval(() => { rollupCurrent("weekly").catch((e) => console.warn("[rollup] weekly failed:", e)); }, 6 * 60 * 60 * 1000);
}