notificationMessageBuilder
This commit is contained in:
1 parent
d665e95f02
commit
e0608ad3ed
1 file changed
+17
-28
@@ -1,4 +1,4 @@
|
|||||||
import type { Monitor, MonitorStatusResponse } from "@/types/index.js";
|
import type { HardwareStatusPayload, Monitor, MonitorStatusResponse } from "@/types/index.js";
|
||||||
import type { MonitorActionDecision } from "@/service/infrastructure/SuperSimpleQueue/SuperSimpleQueueHelper.js";
|
import type { MonitorActionDecision } from "@/service/infrastructure/SuperSimpleQueue/SuperSimpleQueueHelper.js";
|
||||||
import type {
|
import type {
|
||||||
NotificationMessage,
|
NotificationMessage,
|
||||||
@@ -30,8 +30,8 @@ export class NotificationMessageBuilder implements INotificationMessageBuilder {
|
|||||||
clientHost: string
|
clientHost: string
|
||||||
): NotificationMessage {
|
): NotificationMessage {
|
||||||
const type = this.determineNotificationType(decision, monitor);
|
const type = this.determineNotificationType(decision, monitor);
|
||||||
const severity = this.determineSeverity(type, monitor.status);
|
const severity = this.determineSeverity(type);
|
||||||
const content = this.buildContent(type, monitor, monitorStatusResponse, decision, clientHost);
|
const content = this.buildContent(type, monitor, monitorStatusResponse);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
type,
|
type,
|
||||||
@@ -77,7 +77,7 @@ export class NotificationMessageBuilder implements INotificationMessageBuilder {
|
|||||||
return "monitor_up";
|
return "monitor_up";
|
||||||
}
|
}
|
||||||
|
|
||||||
private determineSeverity(type: NotificationType, monitorStatus: string): NotificationSeverity {
|
private determineSeverity(type: NotificationType): NotificationSeverity {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case "monitor_down":
|
case "monitor_down":
|
||||||
return "critical";
|
return "critical";
|
||||||
@@ -93,28 +93,22 @@ export class NotificationMessageBuilder implements INotificationMessageBuilder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private buildContent(
|
private buildContent(type: NotificationType, monitor: Monitor, monitorStatusResponse: MonitorStatusResponse): NotificationContent {
|
||||||
type: NotificationType,
|
|
||||||
monitor: Monitor,
|
|
||||||
monitorStatusResponse: MonitorStatusResponse,
|
|
||||||
decision: MonitorActionDecision,
|
|
||||||
clientHost: string
|
|
||||||
): NotificationContent {
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case "monitor_down":
|
case "monitor_down":
|
||||||
return this.buildMonitorDownContent(monitor, monitorStatusResponse, clientHost);
|
return this.buildMonitorDownContent(monitor, monitorStatusResponse);
|
||||||
case "monitor_up":
|
case "monitor_up":
|
||||||
return this.buildMonitorUpContent(monitor, clientHost);
|
return this.buildMonitorUpContent(monitor);
|
||||||
case "threshold_breach":
|
case "threshold_breach":
|
||||||
return this.buildThresholdBreachContent(monitor, monitorStatusResponse, decision, clientHost);
|
return this.buildThresholdBreachContent(monitor, monitorStatusResponse as MonitorStatusResponse<HardwareStatusPayload>);
|
||||||
case "threshold_resolved":
|
case "threshold_resolved":
|
||||||
return this.buildThresholdResolvedContent(monitor, clientHost);
|
return this.buildThresholdResolvedContent(monitor);
|
||||||
default:
|
default:
|
||||||
return this.buildDefaultContent(monitor, clientHost);
|
return this.buildDefaultContent(monitor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private buildMonitorDownContent(monitor: Monitor, monitorStatusResponse: MonitorStatusResponse, clientHost: string): NotificationContent {
|
private buildMonitorDownContent(monitor: Monitor, monitorStatusResponse: MonitorStatusResponse): NotificationContent {
|
||||||
const title = `Monitor Down: ${monitor.name}`;
|
const title = `Monitor Down: ${monitor.name}`;
|
||||||
const summary = `Monitor "${monitor.name}" is currently down and unreachable.`;
|
const summary = `Monitor "${monitor.name}" is currently down and unreachable.`;
|
||||||
const details = [`URL: ${monitor.url}`, `Status: Down`, `Type: ${monitor.type}`];
|
const details = [`URL: ${monitor.url}`, `Status: Down`, `Type: ${monitor.type}`];
|
||||||
@@ -137,7 +131,7 @@ export class NotificationMessageBuilder implements INotificationMessageBuilder {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private buildMonitorUpContent(monitor: Monitor, clientHost: string): NotificationContent {
|
private buildMonitorUpContent(monitor: Monitor): NotificationContent {
|
||||||
const title = `Monitor Recovered: ${monitor.name}`;
|
const title = `Monitor Recovered: ${monitor.name}`;
|
||||||
const summary = `Monitor "${monitor.name}" is back up and operational.`;
|
const summary = `Monitor "${monitor.name}" is back up and operational.`;
|
||||||
const details = [`URL: ${monitor.url}`, `Status: Up`, `Type: ${monitor.type}`];
|
const details = [`URL: ${monitor.url}`, `Status: Up`, `Type: ${monitor.type}`];
|
||||||
@@ -150,12 +144,7 @@ export class NotificationMessageBuilder implements INotificationMessageBuilder {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private buildThresholdBreachContent(
|
private buildThresholdBreachContent(monitor: Monitor, monitorStatusResponse: MonitorStatusResponse<HardwareStatusPayload>): NotificationContent {
|
||||||
monitor: Monitor,
|
|
||||||
monitorStatusResponse: MonitorStatusResponse,
|
|
||||||
decision: MonitorActionDecision,
|
|
||||||
clientHost: string
|
|
||||||
): NotificationContent {
|
|
||||||
const title = `Threshold Breach: ${monitor.name}`;
|
const title = `Threshold Breach: ${monitor.name}`;
|
||||||
const summary = `Monitor "${monitor.name}" has breached one or more thresholds.`;
|
const summary = `Monitor "${monitor.name}" has breached one or more thresholds.`;
|
||||||
const details = [`URL: ${monitor.url}`, `Status: Breached`, `Type: ${monitor.type}`];
|
const details = [`URL: ${monitor.url}`, `Status: Breached`, `Type: ${monitor.type}`];
|
||||||
@@ -171,7 +160,7 @@ export class NotificationMessageBuilder implements INotificationMessageBuilder {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private buildThresholdResolvedContent(monitor: Monitor, clientHost: string): NotificationContent {
|
private buildThresholdResolvedContent(monitor: Monitor): NotificationContent {
|
||||||
const title = `Thresholds Resolved: ${monitor.name}`;
|
const title = `Thresholds Resolved: ${monitor.name}`;
|
||||||
const summary = `Monitor "${monitor.name}" thresholds have returned to normal.`;
|
const summary = `Monitor "${monitor.name}" thresholds have returned to normal.`;
|
||||||
const details = [`URL: ${monitor.url}`, `Status: Up`, `Type: ${monitor.type}`];
|
const details = [`URL: ${monitor.url}`, `Status: Up`, `Type: ${monitor.type}`];
|
||||||
@@ -184,7 +173,7 @@ export class NotificationMessageBuilder implements INotificationMessageBuilder {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private buildDefaultContent(monitor: Monitor, clientHost: string): NotificationContent {
|
private buildDefaultContent(monitor: Monitor): NotificationContent {
|
||||||
return {
|
return {
|
||||||
title: `Monitor: ${monitor.name}`,
|
title: `Monitor: ${monitor.name}`,
|
||||||
summary: `Status update for monitor "${monitor.name}".`,
|
summary: `Status update for monitor "${monitor.name}".`,
|
||||||
@@ -193,7 +182,7 @@ export class NotificationMessageBuilder implements INotificationMessageBuilder {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public extractThresholdBreaches(monitor: Monitor, monitorStatusResponse: MonitorStatusResponse): ThresholdBreach[] {
|
public extractThresholdBreaches(monitor: Monitor, monitorStatusResponse: MonitorStatusResponse<HardwareStatusPayload>): ThresholdBreach[] {
|
||||||
const breaches: ThresholdBreach[] = [];
|
const breaches: ThresholdBreach[] = [];
|
||||||
|
|
||||||
// Check if this is a hardware monitor with threshold data
|
// Check if this is a hardware monitor with threshold data
|
||||||
@@ -202,7 +191,7 @@ export class NotificationMessageBuilder implements INotificationMessageBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Cast to HardwareStatusPayload type
|
// Cast to HardwareStatusPayload type
|
||||||
const payload = monitorStatusResponse.payload as { data?: any };
|
const payload = monitorStatusResponse.payload;
|
||||||
const hardware = payload.data;
|
const hardware = payload.data;
|
||||||
|
|
||||||
if (!hardware) {
|
if (!hardware) {
|
||||||
|
|||||||
Reference in new issue
Block a user