219 lines
6.8 KiB
TypeScript
219 lines
6.8 KiB
TypeScript
import { INetworkService } from "./NetworkService.js";
|
|
import { ICheckService } from "../business/CheckService.js";
|
|
import { IMonitorStatsService } from "../business/MonitorStatsService.js";
|
|
import { IStatusService } from "./StatusService.js";
|
|
import { INotificationService } from "./NotificationService.js";
|
|
import { IMaintenanceService } from "../business/MaintenanceService.js";
|
|
import { IIncidentService } from "../business/IncidentService.js";
|
|
import { IStatsAggregationService } from "@/services/business/StatsAggregationService.js";
|
|
import ApiError from "@/utils/ApiError.js";
|
|
import { getChildLogger } from "@/logger/Logger.js";
|
|
import type { Monitor } from "@/types/domain/index.js";
|
|
|
|
const SERVICE_NAME = "JobGenerator";
|
|
const logger = getChildLogger(SERVICE_NAME);
|
|
export interface IJobGenerator {
|
|
generateJob: () => (monitor: Monitor) => Promise<void>;
|
|
generateCleanupJob: () => () => Promise<void>;
|
|
}
|
|
|
|
class JobGenerator implements IJobGenerator {
|
|
public SERVICE_NAME: string;
|
|
private networkService: INetworkService;
|
|
private checkService: ICheckService;
|
|
private monitorStatsService: IMonitorStatsService;
|
|
private statusService: IStatusService;
|
|
private notificationService: INotificationService;
|
|
private maintenanceService: IMaintenanceService;
|
|
private incidentService: IIncidentService;
|
|
private statsAggregationService: IStatsAggregationService;
|
|
|
|
constructor(
|
|
networkService: INetworkService,
|
|
checkService: ICheckService,
|
|
monitorStatsService: IMonitorStatsService,
|
|
statusService: IStatusService,
|
|
notificationService: INotificationService,
|
|
incidentService: IIncidentService,
|
|
maintenanceService: IMaintenanceService,
|
|
statsAggregationService: IStatsAggregationService
|
|
) {
|
|
this.SERVICE_NAME = SERVICE_NAME;
|
|
this.networkService = networkService;
|
|
this.checkService = checkService;
|
|
this.monitorStatsService = monitorStatsService;
|
|
this.statusService = statusService;
|
|
this.notificationService = notificationService;
|
|
this.incidentService = incidentService;
|
|
this.maintenanceService = maintenanceService;
|
|
this.statsAggregationService = statsAggregationService;
|
|
}
|
|
|
|
generateJob = () => {
|
|
return async (monitor: Monitor) => {
|
|
try {
|
|
const monitorId = monitor.id.toString();
|
|
if (!monitorId) {
|
|
throw new ApiError("No monitorID for creating job", 400);
|
|
}
|
|
|
|
// Step 1. Check for active maintenance window, if found, skip the check
|
|
const isInMaintenance = await this.maintenanceService.isInMaintenance(
|
|
monitorId
|
|
);
|
|
if (isInMaintenance) {
|
|
return;
|
|
}
|
|
|
|
// Setp 2. Request monitor status
|
|
const status = await this.networkService.requestStatus(monitor);
|
|
|
|
// Step 3. Build check
|
|
const check = await this.checkService.buildCheck(status, monitor.type);
|
|
await check.save();
|
|
|
|
// Step 4. Update monitor's status
|
|
const [updatedMonitor, statusChanged] =
|
|
await this.statusService.updateMonitorStatus(monitor, status);
|
|
|
|
// Step 5. Handle status change
|
|
if (statusChanged) {
|
|
const incident = await this.incidentService.handleStatusChange(
|
|
updatedMonitor,
|
|
check
|
|
);
|
|
|
|
// Best effort, don't wait, don't fail
|
|
if (incident) {
|
|
this.notificationService
|
|
.handleNotifications(updatedMonitor, incident)
|
|
.catch((error) => {
|
|
logger.warn(error);
|
|
});
|
|
}
|
|
}
|
|
|
|
// Step 6. Update aggregate monitor stats
|
|
await this.statusService.updateMonitorStats(
|
|
updatedMonitor,
|
|
status,
|
|
statusChanged
|
|
);
|
|
|
|
// Step 7. Evaluate thresholds for Infrastrucutre monitors
|
|
if (updatedMonitor.type === "infrastructure") {
|
|
const evalResult = await this.statusService.evaluateThresholds(
|
|
updatedMonitor,
|
|
check
|
|
);
|
|
|
|
const incident = await this.incidentService.handleThresholdBreach(
|
|
updatedMonitor,
|
|
check,
|
|
evalResult
|
|
);
|
|
|
|
if (incident) {
|
|
this.notificationService
|
|
.handleNotifications(updatedMonitor, incident)
|
|
.catch((error) => {
|
|
logger.warn(error);
|
|
});
|
|
}
|
|
}
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
};
|
|
};
|
|
|
|
generateCleanupJob = () => {
|
|
return async () => {
|
|
try {
|
|
await this.checkService.cleanupOrphanedChecks();
|
|
await this.monitorStatsService.cleanupOrphanedMonitorStats();
|
|
await this.incidentService.cleanupOrphanedIncidents();
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
};
|
|
};
|
|
|
|
generateStatsAggregationJob = () => {
|
|
return async () => {
|
|
// Compute hour window in UTC based on flag
|
|
const now = new Date();
|
|
const utcNow = new Date(
|
|
Date.UTC(
|
|
now.getUTCFullYear(),
|
|
now.getUTCMonth(),
|
|
now.getUTCDate(),
|
|
now.getUTCHours(),
|
|
now.getUTCMinutes(),
|
|
now.getUTCSeconds(),
|
|
0
|
|
)
|
|
);
|
|
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);
|
|
|
|
// Roll up the current day so far so we have partial data for the current day
|
|
const todayStart = new Date(
|
|
Date.UTC(
|
|
now.getUTCFullYear(),
|
|
now.getUTCMonth(),
|
|
now.getUTCDate(),
|
|
0,
|
|
0,
|
|
0,
|
|
0
|
|
)
|
|
);
|
|
await this.statsAggregationService.rollupDaily(todayStart, utcNow, false);
|
|
|
|
// If at UTC midnight hour finalize the previous day
|
|
const hour = utcNow.getUTCHours();
|
|
if (hour === 0) {
|
|
const dayEnd = todayStart; // just reached 00:00, previous day ends now
|
|
const dayStart = new Date(dayEnd.getTime() - 24 * 60 * 60 * 1000);
|
|
await this.statsAggregationService.rollupDaily(dayStart, dayEnd, true);
|
|
}
|
|
};
|
|
};
|
|
}
|
|
|
|
export default JobGenerator;
|
|
import { config } from "@/config/index.js";
|