This commit is contained in:
Alex Holliday committed 2026-01-19 22:16:33 +00:00
1 parent bebb5d6942
commit 5f17520ba0
7 files changed
+124 -42

No files matched your search

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