use decision object in incidentService

This commit is contained in:
Alex Holliday committed 2026-02-15 18:09:21 +00:00
1 parent 70ae15725e
commit 7061eb8eb0
2 files changed
+41 -34

No files matched your search

+18 -10
View File
@@ -4,6 +4,7 @@ import { AppError } from "@/utils/AppError.js";
import { ParseBoolean } from "@/utils/utils.js"; import { ParseBoolean } from "@/utils/utils.js";
import type { IIncidentsRepository, IMonitorsRepository, IUsersRepository } from "@/repositories/index.js"; import type { IIncidentsRepository, IMonitorsRepository, IUsersRepository } from "@/repositories/index.js";
import type { Incident } from "@/types/index.js"; import type { Incident } from "@/types/index.js";
import type { MonitorActionDecision } from "@/service/infrastructure/SuperSimpleQueue/SuperSimpleQueueHelper.js";
const dateRangeLookup: Record<string, Date | undefined> = { const dateRangeLookup: Record<string, Date | undefined> = {
recent: new Date(new Date().setHours(new Date().getHours() - 2)), recent: new Date(new Date().setHours(new Date().getHours() - 2)),
@@ -43,10 +44,14 @@ class IncidentService {
return IncidentService.SERVICE_NAME; return IncidentService.SERVICE_NAME;
} }
handleIncident = async (monitor: Monitor, code: number): Promise<Incident | null> => { handleIncident = async (monitor: Monitor, code: number, decision: MonitorActionDecision): Promise<Incident | null> => {
if (!decision.shouldCreateIncident && !decision.shouldResolveIncident) {
return null;
}
const activeIncident = await this.incidentsRepository.findActiveByMonitorId(monitor.id, monitor.teamId); const activeIncident = await this.incidentsRepository.findActiveByMonitorId(monitor.id, monitor.teamId);
// Monitor is down, create an incident
if (monitor.status === "down") { if (decision.shouldCreateIncident) {
if (activeIncident) { if (activeIncident) {
return activeIncident; return activeIncident;
} else { } else {
@@ -61,14 +66,17 @@ class IncidentService {
} }
} }
// Monitor is up, resolve active incidents if (decision.shouldResolveIncident) {
if (!activeIncident) { if (!activeIncident) {
return null; return null;
}
activeIncident.status = false;
activeIncident.endTime = Date.now().toString();
activeIncident.resolutionType = "automatic";
return await this.incidentsRepository.updateById(activeIncident.id, activeIncident.teamId, activeIncident);
} }
activeIncident.status = false;
activeIncident.endTime = Date.now().toString(); return null;
activeIncident.resolutionType = "automatic";
return await this.incidentsRepository.updateById(activeIncident.id, activeIncident.teamId, activeIncident);
}; };
resolveIncident = async (incidentId: string, userId: string, teamId: string, comment?: string, userEmail?: string) => { resolveIncident = async (incidentId: string, userId: string, teamId: string, comment?: string, userEmail?: string) => {
@@ -117,33 +117,32 @@ class SuperSimpleQueueHelper {
// Step 5. Get decisions // Step 5. Get decisions
const decision = this.evaluateMonitorAction(statusChangeResult); const decision = this.evaluateMonitorAction(statusChangeResult);
console.log(JSON.stringify(decision, null, 2));
// Step 5 handle notifications (best effort, continue even in event of failure, don't wait) // Step 6. Handle notifications (best effort, continue even in event of failure, don't wait)
this.notificationsService if (decision.shouldSendNotification) {
.handleNotifications(statusChangeResult.monitor, status, statusChangeResult.prevStatus, statusChangeResult.statusChanged) this.notificationsService
.catch((error: any) => { .handleNotifications(statusChangeResult.monitor, status, statusChangeResult.prevStatus, statusChangeResult.statusChanged)
this.logger.error({ .catch((error: any) => {
message: error.message, this.logger.error({
service: SERVICE_NAME, message: error.message,
method: "getMonitorJob", service: SERVICE_NAME,
details: `Error sending notifications for job ${statusChangeResult.monitor.id}: ${error.message}`, method: "getMonitorJob",
stack: error.stack, details: `Error sending notifications for job ${statusChangeResult.monitor.id}: ${error.message}`,
stack: error.stack,
});
}); });
});
// Step 6. Handle incidents (best effort, don't wait)
if (statusChangeResult.statusChanged) {
this.incidentService.handleIncident(statusChangeResult.monitor, statusChangeResult.code).catch((error: any) => {
this.logger.warn({
message: error.message,
service: SERVICE_NAME,
method: "getMonitorJob",
details: `Error handling incident for job ${monitor.id}: ${error.message}`,
stack: error.stack,
});
});
} }
// Step 7. Handle incidents (best effort, don't wait)
this.incidentService.handleIncident(statusChangeResult.monitor, statusChangeResult.code, decision).catch((error: any) => {
this.logger.warn({
message: error.message,
service: SERVICE_NAME,
method: "getMonitorJob",
details: `Error handling incident for job ${monitor.id}: ${error.message}`,
stack: error.stack,
});
});
} catch (error: any) { } catch (error: any) {
this.logger.warn({ this.logger.warn({
message: error.message, message: error.message,