From 544502c6e56720d37f856d5a567f48dfc6fdb936 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Thu, 24 Sep 2026 14:19:13 -0700 Subject: [PATCH] move formatting --- .../src/domain/incidents/incident.service.ts | 21 ++------------ .../notification.message-builder.ts | 28 +++++++++++++++---- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/server/src/domain/incidents/incident.service.ts b/server/src/domain/incidents/incident.service.ts index 4adbf9b75..84ef104bd 100644 --- a/server/src/domain/incidents/incident.service.ts +++ b/server/src/domain/incidents/incident.service.ts @@ -1,4 +1,3 @@ -const SERVICE_NAME = "incidentService"; import type { Monitor } from "@/domain/monitors/monitor.type.js"; import type { Check } from "@/domain/checks/check.type.js"; import { AppError } from "@/utils/AppError.js"; @@ -12,8 +11,8 @@ import type { MonitorActionDecision } from "@/worker/worker.interface.js"; import type { INotificationMessageBuilder } from "@/domain/notifications/notification.message-builder.js"; import type { ILogger } from "@/utils/logger.js"; import { DateRange } from "@/types/query.js"; -import { describeContainerBreach, findContainerBreaches } from "@/domain/docker/docker-alert.js"; +const SERVICE_NAME = "incidentService"; export interface IIncidentService { handleIncident(monitor: Monitor, decision: MonitorActionDecision, check: Check): Promise; resolveIncident(incidentId: string, userId: string, teamId: string, comment?: string, userEmail?: string): Promise; @@ -71,7 +70,7 @@ export class IncidentService implements IIncidentService { // For threshold breaches, use 9999 status code and build descriptive message if (decision.incidentReason === "threshold_breach") { statusCode = 9999; - message = this.buildThresholdBreachMessage(monitor, check, decision); + message = this.notificationMessageBuilder.buildThresholdBreachMessage(monitor, check, decision.thresholdBreaches); } const incident = { @@ -96,22 +95,6 @@ export class IncidentService implements IIncidentService { return await this.incidentsRepository.updateById(activeIncident.id, activeIncident.teamId, activeIncident); }; - private buildThresholdBreachMessage(monitor: Monitor, check: Check, decision: MonitorActionDecision): string { - if (monitor.type === "docker") { - const containers = check.containers ?? []; - const breaches = findContainerBreaches(monitor, containers); - return breaches.length > 0 ? breaches.map(describeContainerBreach).join(", ") : "Container alert"; - } - - const breaches = this.notificationMessageBuilder.extractThresholdBreaches(monitor, check, decision.thresholdBreaches); - - if (breaches.length === 0) { - return "Threshold breach detected"; - } - - return breaches.map((b) => `${b.metric.toUpperCase()}: ${b.formattedValue} (threshold: ${b.threshold}${b.unit})`).join(", "); - } - resolveIncident = async (incidentId: string, userId: string, teamId: string, comment?: string, userEmail?: string) => { try { if (!incidentId) { diff --git a/server/src/domain/notifications/notification.message-builder.ts b/server/src/domain/notifications/notification.message-builder.ts index 55b2ae1ef..605868ddb 100644 --- a/server/src/domain/notifications/notification.message-builder.ts +++ b/server/src/domain/notifications/notification.message-builder.ts @@ -12,7 +12,7 @@ import type { Check } from "@/domain/checks/check.type.js"; export interface INotificationMessageBuilder { buildMessage(monitor: Monitor, check: Check, decision: MonitorActionDecision, clientHost: string): NotificationMessage; - extractThresholdBreaches(monitor: Monitor, check: Check, thresholdBreaches: HardwareBreaches | undefined): ThresholdBreach[]; + buildThresholdBreachMessage(monitor: Monitor, check: Check, thresholdBreaches: HardwareBreaches | undefined): string; } const SERVICE_NAME = "NotificationMessageBuilder"; @@ -167,11 +167,10 @@ export class NotificationMessageBuilder implements INotificationMessageBuilder { } private buildContainerBreachContent(monitor: Monitor, check: Check): NotificationContent { - const containers = check.containers ?? []; - const breaches = findContainerBreaches(monitor, containers); + const lines = this.describeContainerBreaches(monitor, check); const title = `Container Alert: ${monitor.name}`; - const summary = `${breaches.length} container(s) on "${monitor.name}" need attention.`; - const details = [`URL: ${monitor.url}`, `Type: ${monitor.type}`, ...breaches.map(describeContainerBreach)]; + const summary = `${lines.length} container(s) on "${monitor.name}" need attention.`; + const details = [`URL: ${monitor.url}`, `Type: ${monitor.type}`, ...lines]; return { title, summary, details, timestamp: new Date() }; } @@ -212,7 +211,7 @@ export class NotificationMessageBuilder implements INotificationMessageBuilder { } } - public extractThresholdBreaches(monitor: Monitor, check: Check, thresholdBreaches: HardwareBreaches | undefined): ThresholdBreach[] { + private extractThresholdBreaches(monitor: Monitor, check: Check, thresholdBreaches: HardwareBreaches | undefined): ThresholdBreach[] { const breaches: ThresholdBreach[] = []; // Check if this is a hardware monitor with threshold data @@ -227,4 +226,21 @@ export class NotificationMessageBuilder implements INotificationMessageBuilder { return breaches; } + + private describeContainerBreaches(monitor: Monitor, check: Check): string[] { + return findContainerBreaches(monitor, check.containers ?? []).map(describeContainerBreach); + } + + public buildThresholdBreachMessage(monitor: Monitor, check: Check, thresholdBreaches: HardwareBreaches | undefined): string { + if (monitor.type === "docker") { + const lines = this.describeContainerBreaches(monitor, check); + return lines.length > 0 ? lines.join(", ") : "Container alert"; + } + + const breaches = this.extractThresholdBreaches(monitor, check, thresholdBreaches); + if (breaches.length === 0) { + return "Threshold breach detected"; + } + return breaches.map((b) => `${b.metric.toUpperCase()}: ${b.formattedValue} (threshold: ${b.threshold}${b.unit})`).join(", "); + } }