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
+17 -10

No files matched your search

+12 -4
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,7 +66,7 @@ class IncidentService {
} }
} }
// Monitor is up, resolve active incidents if (decision.shouldResolveIncident) {
if (!activeIncident) { if (!activeIncident) {
return null; return null;
} }
@@ -69,6 +74,9 @@ class IncidentService {
activeIncident.endTime = Date.now().toString(); activeIncident.endTime = Date.now().toString();
activeIncident.resolutionType = "automatic"; activeIncident.resolutionType = "automatic";
return await this.incidentsRepository.updateById(activeIncident.id, activeIncident.teamId, activeIncident); return await this.incidentsRepository.updateById(activeIncident.id, activeIncident.teamId, activeIncident);
}
return null;
}; };
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,9 +117,9 @@ 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)
if (decision.shouldSendNotification) {
this.notificationsService this.notificationsService
.handleNotifications(statusChangeResult.monitor, status, statusChangeResult.prevStatus, statusChangeResult.statusChanged) .handleNotifications(statusChangeResult.monitor, status, statusChangeResult.prevStatus, statusChangeResult.statusChanged)
.catch((error: any) => { .catch((error: any) => {
@@ -131,10 +131,10 @@ class SuperSimpleQueueHelper {
stack: error.stack, stack: error.stack,
}); });
}); });
}
// Step 6. Handle incidents (best effort, don't wait) // Step 7. Handle incidents (best effort, don't wait)
if (statusChangeResult.statusChanged) { this.incidentService.handleIncident(statusChangeResult.monitor, statusChangeResult.code, decision).catch((error: any) => {
this.incidentService.handleIncident(statusChangeResult.monitor, statusChangeResult.code).catch((error: any) => {
this.logger.warn({ this.logger.warn({
message: error.message, message: error.message,
service: SERVICE_NAME, service: SERVICE_NAME,
@@ -143,7 +143,6 @@ class SuperSimpleQueueHelper {
stack: error.stack, stack: error.stack,
}); });
}); });
}
} catch (error: any) { } catch (error: any) {
this.logger.warn({ this.logger.warn({
message: error.message, message: error.message,