initial commit

This commit is contained in:
Alex Holliday committed 2026-01-19 23:41:24 +00:00
1 parent 33dfedb4d3
commit e91a54b75c
12 files changed
+295 -242

No files matched your search

@@ -2,6 +2,8 @@ const SERVICE_NAME = "JobQueueHelper";
import type { Monitor } from "@/types/monitor.js";
import { AppError } from "@/utils/AppError.js";
import { INetworkService, INotificationsService, IStatusService } from "@/service/index.js";
import IncidentService from "@/service/business/incidentService.js";
class SuperSimpleQueueHelper {
static SERVICE_NAME = SERVICE_NAME;
@@ -12,6 +14,7 @@ class SuperSimpleQueueHelper {
private notificationsService: INotificationsService;
private checkService: any;
private buffer: any;
private incidentService: IncidentService;
constructor({
db,
@@ -21,6 +24,7 @@ class SuperSimpleQueueHelper {
notificationsService,
checkService,
buffer,
incidentService,
}: {
db: any;
logger: any;
@@ -29,6 +33,7 @@ class SuperSimpleQueueHelper {
notificationsService: INotificationsService;
checkService: any;
buffer: any;
incidentService: IncidentService;
}) {
this.db = db;
this.logger = logger;
@@ -37,6 +42,7 @@ class SuperSimpleQueueHelper {
this.checkService = checkService;
this.buffer = buffer;
this.notificationsService = notificationsService;
this.incidentService = incidentService;
}
get serviceName() {
@@ -90,6 +96,11 @@ class SuperSimpleQueueHelper {
stack: error.stack,
});
});
// Step 6. Handle incidents
if (statusChangeResult.statusChanged) {
await this.incidentService.handleIncident(statusChangeResult.monitor, statusChangeResult.code);
}
} catch (error: any) {
this.logger.warn({
message: error.message,
@@ -218,70 +218,6 @@ export class StatusService implements IStatusService {
statusChanged = true;
}
if (statusChanged) {
this.logger.info({
service: SERVICE_NAME,
message: `${monitor.name} went from ${this.getStatusString(prevStatus)} to ${this.getStatusString(newStatus)}`,
prevStatus,
newStatus,
});
if (newStatus === false) {
await this.handleIncidentForCheck(check, monitor, "create", "status change to down");
} else if (prevStatus === false) {
await this.handleIncidentForCheck(check, monitor, "resolve", "status change to up");
}
}
if (monitor.status === false && !statusChanged) {
try {
const lastManuallyResolvedIncident = await this.db.incidentModule.getLastManuallyResolvedIncident(monitor.id);
let calculatedFailureRate = failureRate;
if (lastManuallyResolvedIncident && lastManuallyResolvedIncident.endTime) {
try {
const checksAfterResolution = await CheckModel.find({
monitorId: monitor.id,
createdAt: { $gt: lastManuallyResolvedIncident.endTime },
})
.sort({ createdAt: 1 })
.limit(monitor.statusWindowSize)
.select("status")
.lean();
if (checksAfterResolution.length > 0) {
const checksStatuses = checksAfterResolution.map((c) => c.status);
const failuresAfterResolution = checksStatuses.filter((s) => s === false).length;
calculatedFailureRate = (failuresAfterResolution / monitor.statusWindow.length) * 100;
} else {
calculatedFailureRate = 0;
}
} catch (checkQueryError: any) {
this.logger.error({
service: SERVICE_NAME,
method: "updateStatus",
message: `Failed to query checks after manual resolution: ${checkQueryError.message}`,
monitorId: monitor.id,
stack: checkQueryError.stack,
});
}
}
if (calculatedFailureRate >= monitor.statusWindowThreshold) {
await this.handleIncidentForCheck(check, monitor, "create", "threshold check without status change");
}
} catch (error: any) {
this.logger.error({
service: SERVICE_NAME,
method: "updateStatus",
message: `Error handling threshold check without status change: ${error.message}`,
monitorId: monitor.id,
stack: error.stack,
});
}
}
monitor.status = newStatus;
const updated = await this.monitorsRepository.updateById(monitor.id, monitor.teamId, monitor);