aggregates

This commit is contained in:
Alex Holliday committed 2025-12-17 21:11:51 +00:00
1 parent 95e415ed13
commit c8dc5b93cb
14 files changed
+302 -99

No files matched your search

@@ -144,7 +144,7 @@ class JobGenerator implements IJobGenerator {
generateStatsAggregationJob = () => {
return async () => {
// Get previous hour window in UTC
// Compute hour window in UTC based on flag
const now = new Date();
const utcNow = new Date(
Date.UTC(
@@ -157,18 +157,37 @@ class JobGenerator implements IJobGenerator {
0
)
);
const hourEnd = new Date(
Date.UTC(
now.getUTCFullYear(),
now.getUTCMonth(),
now.getUTCDate(),
now.getUTCHours(),
0,
0,
0
)
);
const hourStart = new Date(hourEnd.getTime() - 60 * 60 * 1000);
let hourStart: Date;
let hourEnd: Date;
if (config.STATS_USE_CURRENT_HOUR) {
// Current hour [hh:00, hh+1:00)
hourStart = new Date(
Date.UTC(
now.getUTCFullYear(),
now.getUTCMonth(),
now.getUTCDate(),
now.getUTCHours(),
0,
0,
0
)
);
hourEnd = new Date(hourStart.getTime() + 60 * 60 * 1000);
} else {
// Previous hour [hh-1:00, hh:00)
hourEnd = new Date(
Date.UTC(
now.getUTCFullYear(),
now.getUTCMonth(),
now.getUTCDate(),
now.getUTCHours(),
0,
0,
0
)
);
hourStart = new Date(hourEnd.getTime() - 60 * 60 * 1000);
}
// Upsert hourly stats for the last full hour
await this.statsAggregationService.upsertHourly(hourStart, hourEnd);
@@ -199,3 +218,4 @@ class JobGenerator implements IJobGenerator {
}
export default JobGenerator;
import { config } from "@/config/index.js";
+10 -1
View File
@@ -3,6 +3,7 @@ import { Monitor, IMonitor } from "@/db/models/index.js";
import Scheduler from "super-simple-scheduler";
import { IJobGenerator } from "./JobGenerator.js";
import { getChildLogger } from "@/logger/Logger.js";
import { config } from "@/config/index.js";
const SERVICE_NAME = "JobQueue";
const logger = getChildLogger(SERVICE_NAME);
@@ -86,7 +87,7 @@ export default class JobQueue implements IJobQueue {
active: true,
});
// Stats aggregation job (hourly)
// Stats aggregation job (frequency depends on config)
this.scheduler.addTemplate(
"stats-aggregation-job",
this.jobGenerator.generateStatsAggregationJob()
@@ -98,6 +99,14 @@ export default class JobQueue implements IJobQueue {
active: true,
});
// Trigger an immediate run to populate stats on startup
try {
const runNow = this.jobGenerator.generateStatsAggregationJob();
await runNow();
} catch (e) {
logger.warn("Initial stats aggregation run failed", e as any);
}
const monitors = await Monitor.find();
for (const monitor of monitors) {
const randomOffset = 1000 + Math.random() * 1000;