This commit is contained in:
Alex Holliday
2026-01-19 22:16:33 +00:00
parent bebb5d6942
commit 5f17520ba0
7 changed files with 124 additions and 42 deletions
@@ -1,10 +1,15 @@
const SERVICE_NAME = "DiscordProvider";
import type { Monitor, Notification, MonitorStatusResponse } from "@/types/index.js";
import { INotificationProvider } from "@/service/index.js";
import { buildHardwareAlerts, buildDiscordBody, getTestMessage } from "@/service/infrastructure/notificationProviders/utils.js";
import got from "got";
export class DiscordProvider implements INotificationProvider {
constructor(private logger: any) {}
private logger: any;
constructor(logger: any) {
this.logger = logger;
}
private getHardwareContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
const { discordPayload } = buildHardwareAlerts("HOST_PLACEHOLDER", monitor, monitorStatusResponse);
return discordPayload;
@@ -23,17 +28,21 @@ export class DiscordProvider implements INotificationProvider {
}
try {
this.logger?.debug?.("Sending Discord alert", { address: notification.address });
await got.post(notification.address, {
json: body,
headers: {
"Content-Type": "application/json",
},
responseType: "json",
});
return true;
} catch (error: any) {
this.logger?.error?.("Discord alert failed", { error });
} catch (error) {
const err = error as Error;
this.logger.warn({
message: "Discord alert failed",
service: SERVICE_NAME,
method: "sendAlert",
stack: err?.stack,
});
return false;
}
};
@@ -42,17 +51,21 @@ export class DiscordProvider implements INotificationProvider {
return false;
}
try {
this.logger?.debug?.("Sending Discord test alert", { address: notification.address });
await got.post(notification.address, {
json: { content: getTestMessage() },
headers: {
"Content-Type": "application/json",
},
responseType: "json",
});
return true;
} catch (error) {
this.logger?.error?.("Discord test alert failed", { error });
const err = error as Error;
this.logger.warn({
message: "Discord test alert failed",
service: SERVICE_NAME,
method: "sendTestAlert",
stack: err?.stack,
});
return false;
}
};
@@ -1,11 +1,15 @@
const SERVICE_NAME = "EmailProvider";
import type { Monitor, Notification, MonitorStatusResponse } from "@/types/index.js";
import { INotificationProvider } from "@/service/index.js";
import { buildHardwareAlerts, buildHardwareEmail, buildEmail, buildTestEmail } from "@/service/infrastructure/notificationProviders/utils.js";
export class EmailProvider implements INotificationProvider {
private emailService: any;
constructor(emailService: any, private logger: any) {
private logger: any;
constructor(emailService: any, logger: any) {
this.emailService = emailService;
this.logger = logger;
}
private buildHardwareEmail = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
@@ -27,18 +31,31 @@ export class EmailProvider implements INotificationProvider {
return false;
}
this.logger?.debug?.("Sending Email alert", { address: notification.address });
const messageId = await this.emailService.sendEmail(notification.address, subject, html);
if (!messageId) return false;
if (!messageId) {
this.logger.warn({
message: "Email alert failed",
service: SERVICE_NAME,
method: "sendAlert",
});
return false;
}
return true;
}
async sendTestAlert(notification: Notification): Promise<boolean> {
const subject = "Test notification";
const html = await buildTestEmail(this.emailService);
this.logger?.debug?.("Sending Email test alert", { address: notification.address });
const messageId = await this.emailService.sendEmail(notification.address, subject, html);
if (!messageId) return false;
if (!messageId) {
this.logger.warn({
message: "Email test alert failed",
service: SERVICE_NAME,
method: "sendTestAlert",
});
return false;
}
return true;
}
}
@@ -1,3 +1,4 @@
const SERVICE_NAME = "MatrixProvider";
import got from "got";
import type { INotificationProvider } from "@/service/index.js";
import type { Notification, Monitor, MonitorStatusResponse } from "@/types/index.js";
@@ -9,7 +10,11 @@ import {
} from "@/service/infrastructure/notificationProviders/utils.js";
export class MatrixProvider implements INotificationProvider {
constructor(private logger: any) {}
private logger: any;
constructor(logger: any) {
this.logger = logger;
}
private getHardwareContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
const { alertsToSend } = buildHardwareAlerts("HOST_PLACEHOLDER", monitor, monitorStatusResponse);
const body = buildHardwareWebhookBody(alertsToSend, monitor);
@@ -43,17 +48,21 @@ export class MatrixProvider implements INotificationProvider {
formatted_body: formattedMessage,
};
try {
this.logger?.debug?.("Sending Matrix alert", { room: roomId });
await got.post(url, {
json: body,
headers: {
"Content-Type": "application/json",
},
responseType: "json",
});
return true;
} catch (error) {
this.logger?.error?.("Matrix alert failed", { error });
const err = error as Error;
this.logger.warn({
message: "Matrix alert failed",
service: SERVICE_NAME,
method: "sendAlert",
stack: err?.stack,
});
return false;
}
};
@@ -69,17 +78,21 @@ export class MatrixProvider implements INotificationProvider {
body: getTestMessage(),
};
try {
this.logger?.debug?.("Sending Matrix test alert", { room: roomId });
await got.post(url, {
json: body,
headers: {
"Content-Type": "application/json",
},
responseType: "json",
});
return true;
} catch (error) {
this.logger?.error?.("Matrix test alert failed", { error });
const err = error as Error;
this.logger.warn({
message: "Matrix test alert failed",
service: SERVICE_NAME,
method: "sendTestAlert",
stack: err?.stack,
});
return false;
}
};
@@ -1,3 +1,4 @@
const SERVICE_NAME = "PagerDutyProvider";
import got from "got";
import type { Monitor, Notification, MonitorStatusResponse } from "@/types/index.js";
import { INotificationProvider } from "@/service/index.js";
@@ -9,7 +10,11 @@ import {
} from "@/service/infrastructure/notificationProviders/utils.js";
export class PagerDutyProvider implements INotificationProvider {
constructor(private logger: any) {}
private logger: any;
constructor(logger: any) {
this.logger = logger;
}
private getHardwareContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
const { alertsToSend } = buildHardwareAlerts("HOST_PLACEHOLDER", monitor, monitorStatusResponse);
const body = buildHardwareWebhookBody(alertsToSend, monitor);
@@ -30,7 +35,6 @@ export class PagerDutyProvider implements INotificationProvider {
}
try {
this.logger?.debug?.("Sending PagerDuty alert", { routingKey: notification.address });
await got.post("https://events.pagerduty.com/v2/enqueue", {
json: {
routing_key: notification.address,
@@ -42,19 +46,23 @@ export class PagerDutyProvider implements INotificationProvider {
timestamp: new Date().toISOString(),
},
},
responseType: "json",
});
return true;
} catch (error) {
this.logger?.error?.("PagerDuty alert failed", { error });
const err = error as Error;
this.logger.warn({
message: "PagerDuty alert failed",
service: SERVICE_NAME,
method: "sendAlert",
stack: err?.stack,
});
return false;
}
}
async sendTestAlert(notification: Notification): Promise<boolean> {
try {
this.logger?.debug?.("Sending PagerDuty test alert", { routingKey: notification.address });
await got.post("https://events.pagerduty.com/v2/enqueue", {
json: {
routing_key: notification.address,
@@ -70,7 +78,13 @@ export class PagerDutyProvider implements INotificationProvider {
});
return true;
} catch (error) {
this.logger?.error?.("PagerDuty test alert failed", { error });
const err = error as Error;
this.logger.warn({
message: "PagerDuty test alert failed",
service: SERVICE_NAME,
method: "sendTestAlert",
stack: err?.stack,
});
return false;
}
}
@@ -1,3 +1,4 @@
const SERVICE_NAME = "SlackProvider";
import type { Monitor, Notification, MonitorStatusResponse } from "@/types/index.js";
import { INotificationProvider } from "@/service/index.js";
import {
@@ -6,10 +7,14 @@ import {
buildWebhookBody,
getTestMessage,
} from "@/service/infrastructure/notificationProviders/utils.js";
import got from "got";
import got, { HTTPError } from "got";
export class SlackProvider implements INotificationProvider {
constructor(private logger: any) {}
private logger: any;
constructor(logger: any) {
this.logger = logger;
}
private getHardwareContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
const { alertsToSend } = buildHardwareAlerts("HOST_PLACEHOLDER", monitor, monitorStatusResponse);
const body = buildHardwareWebhookBody(alertsToSend, monitor);
@@ -34,17 +39,21 @@ export class SlackProvider implements INotificationProvider {
}
try {
this.logger?.debug?.("Sending Slack alert", { address: notification.address });
await got.post(notification.address, {
json: { text: body },
headers: {
"Content-Type": "application/json",
},
responseType: "json",
});
return true;
} catch (error) {
this.logger?.error?.("Slack alert failed", { error });
const err = error as Error;
this.logger.warn({
message: "Slack alert failed",
service: SERVICE_NAME,
method: "sendAlert",
stack: err?.stack,
});
return false;
}
}
@@ -55,17 +64,21 @@ export class SlackProvider implements INotificationProvider {
}
try {
this.logger?.debug?.("Sending Slack test alert", { address: notification.address });
await got.post(notification.address, {
json: { text: getTestMessage() },
headers: {
"Content-Type": "application/json",
},
responseType: "json",
});
return true;
} catch (error) {
this.logger?.error?.("Slack test alert failed", { error });
const err = error as HTTPError;
this.logger.warn({
message: "Slack test alert failed",
service: SERVICE_NAME,
method: "sendTestAlert",
stack: err?.stack,
});
return false;
}
}
@@ -1,3 +1,4 @@
const SERVICE_NAME = "WebhookProvider";
import type { Monitor, Alert, Notification, MonitorStatusResponse } from "@/types/index.js";
import { INotificationProvider } from "@/service/index.js";
import {
@@ -9,7 +10,11 @@ import {
import got from "got";
export class WebhookProvider implements INotificationProvider {
constructor(private logger: any) {}
private logger: any;
constructor(logger: any) {
this.logger = logger;
}
private getHardwareContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
const { alertsToSend } = buildHardwareAlerts("HOST_PLACEHOLDER", monitor, monitorStatusResponse);
const body = buildHardwareWebhookBody(alertsToSend, monitor);
@@ -34,17 +39,21 @@ export class WebhookProvider implements INotificationProvider {
}
try {
this.logger?.debug?.("Sending webhook alert", { address: notification.address });
await got.post(notification.address, {
json: { text: body },
headers: {
"Content-Type": "application/json",
},
responseType: "json",
});
return true;
} catch (error) {
this.logger?.error?.("Webhook alert failed", { error });
const err = error as Error;
this.logger.warn({
message: "Webhook alert failed",
service: SERVICE_NAME,
method: "sendAlert",
stack: err?.stack,
});
return false;
}
};
@@ -54,17 +63,21 @@ export class WebhookProvider implements INotificationProvider {
return false;
}
try {
this.logger?.debug?.("Sending webhook test alert", { address: notification.address });
await got.post(notification.address, {
json: { text: getTestMessage() },
headers: {
"Content-Type": "application/json",
},
responseType: "json",
});
return true;
} catch (error) {
this.logger?.error?.("Webhook test alert failed", { error });
const err = error as Error;
this.logger.warn({
message: "Webhook test alert failed",
service: SERVICE_NAME,
method: "sendTestAlert",
stack: err?.stack,
});
return false;
}
};
@@ -140,7 +140,6 @@ export class NotificationsService implements INotificationsService {
const notifications = await this.notificationsRepository.findNotificationsByIds(notificationIds);
const tasks = notifications.map((notification) => this.sendTestNotification(notification));
const outcomes = await Promise.all(tasks);
console.log(outcomes);
const succeeded = outcomes.filter(Boolean).length;
const failed = outcomes.length - succeeded;
if (failed > 0) {