initial commit

This commit is contained in:
Alex Holliday committed 2025-11-13 13:47:26 -08:00
1 parent 7cb985116e
commit 75acf7b38f
955 files changed
+37333 -87054

No files matched your search

@@ -0,0 +1,112 @@
import {
IMonitor,
INotificationChannel,
IIncident,
} from "@/db/models/index.js";
import { IAlert, IMessageService } from "./IMessageService.js";
import got from "got";
import ApiError from "@/utils/ApiError.js";
import { getChildLogger } from "@/logger/Logger.js";
const SERVICE_NAME = "DiscordService";
const logger = getChildLogger(SERVICE_NAME);
class DiscordService implements IMessageService {
public SERVICE_NAME: string;
constructor() {
this.SERVICE_NAME = SERVICE_NAME;
}
private toDiscordEmbeds = (alert: IAlert) => {
return {
color: alert.status === "up" ? 65280 : 16711680,
title: `Monitor name: ${alert.name}`,
description: `Status: **${alert.status}**`,
fields: [
{
name: "Url",
value: alert.url,
},
{
name: "Checked at",
value: alert.checkTime ? alert.checkTime.toISOString() : "N/A",
},
{
name: "Resolved",
value: alert.resolved ? "Yes" : "No",
},
{
name: "Resolution type",
value: alert.resolutionType || "N/A",
},
{
name: "Resolution note",
value: alert.resolutionNote || "N/A",
},
{ name: "Alert time", value: alert.alertTime.toISOString() },
...(alert.details
? Object.entries(alert.details).map(([key, value]) => ({
name: key,
value,
}))
: []),
],
};
};
buildAlert = (monitor: IMonitor, incident: IIncident) => {
const name = monitor?.name || "Unnamed monitor";
const monitorStatus = monitor?.status || "unknown status";
const url = monitor?.url || "no URL";
const checkTime = monitor?.lastCheckedAt || null;
const alertTime = new Date();
return {
name,
url,
status: monitorStatus,
resolved: incident.resolved,
resolutionType: incident.resolutionType,
resolvedBy: incident.resolvedBy?.toString(),
resolutionNote: incident.resolutionNote,
checkTime,
alertTime,
};
};
sendMessage = async (alert: IAlert, channel: INotificationChannel) => {
const notificationUrl = channel?.config?.url;
if (!notificationUrl) {
throw new ApiError("Webhook URL not configured", 400);
}
try {
const payload = {
content: "Status Alert",
embeds: [this.toDiscordEmbeds(alert)],
};
await got.post(notificationUrl, { json: payload });
} catch (error) {
logger.warn("Failed to send Discord message", error);
return false;
}
return true;
};
testMessage = async (channel: INotificationChannel) => {
return this.sendMessage(
{
name: "This is a test",
url: "Test URL",
status: "Test status",
checkTime: new Date(),
alertTime: new Date(),
resolved: true,
resolutionType: "auto",
resolvedBy: "system",
resolutionNote: "This is a test message",
},
channel
);
};
}
export default DiscordService;
@@ -0,0 +1,113 @@
import {
IMonitor,
INotificationChannel,
IIncident,
} from "@/db/models/index.js";
import { IMessageService, IAlert } from "./IMessageService.js";
import nodemailer, { Transporter } from "nodemailer";
import { config } from "@/config/index.js";
import UserService from "../../business/UserService.js";
import ApiError from "@/utils/ApiError.js";
const SERVICE_NAME = "EmailService";
export interface IEmailService extends IMessageService {
sendGeneric: (
to: string,
subject: string,
content: string
) => Promise<boolean>;
}
class EmailService implements IEmailService {
public SERVICE_NAME = SERVICE_NAME;
private transporter: Transporter;
private userService: UserService;
constructor(userService: UserService) {
this.SERVICE_NAME = SERVICE_NAME;
this.userService = userService;
this.transporter = nodemailer.createTransport({
host: config.SMTP_HOST,
port: config.SMTP_PORT,
secure: config.SMTP_PORT === 465,
auth: {
user: config.SMTP_USER,
pass: config.SMTP_PASS,
},
});
}
buildAlert = (monitor: IMonitor, incident: IIncident) => {
const name = monitor?.name || "Unnamed monitor";
const monitorStatus = monitor?.status || "unknown status";
const url = monitor?.url || "no URL";
const checkTime = monitor?.lastCheckedAt || null;
const alertTime = new Date();
return {
name,
url,
status: monitorStatus,
resolved: incident.resolved,
resolutionType: incident.resolutionType,
resolvedBy: incident.resolvedBy?.toString(),
resolutionNote: incident.resolutionNote,
checkTime,
alertTime,
};
};
sendMessage = async (alert: IAlert, channel: INotificationChannel) => {
try {
const users = await this.userService.getAllUsers();
const emails = users.map((u) => u.email).join(",");
if (!emails || emails.length === 0) {
throw new ApiError("No user emails found", 500);
}
await this.transporter.sendMail({
from: `"Checkmate" <${config.SMTP_USER}>`,
to: emails,
subject: "Monitor Alert",
text: JSON.stringify(alert, null, 2),
});
return true;
} catch (error) {
return false;
}
};
testMessage = async (channel: INotificationChannel) => {
return this.sendMessage(
{
name: "This is a test",
url: "Test URL",
status: "Test status",
checkTime: new Date(),
alertTime: new Date(),
resolved: true,
resolutionType: "auto",
resolvedBy: "system",
resolutionNote: "This is a test message",
},
channel
);
};
sendGeneric = async (to: string, subject: string, content: string) => {
try {
await this.transporter.sendMail({
from: `"Checkmate" <${config.SMTP_USER}>`,
to: to,
subject: subject,
text: content,
});
return true;
} catch (error) {
throw error;
}
};
}
export default EmailService;
@@ -0,0 +1,27 @@
import {
IMonitor,
INotificationChannel,
IIncident,
ResolutionType,
} from "@/db/models/index.js";
export interface IAlert {
name: string;
url: string;
status: string;
resolved: boolean;
resolutionType: ResolutionType | undefined;
resolvedBy?: string | undefined;
resolutionNote?: string | undefined;
details?: Record<string, string>;
checkTime: Date | null;
alertTime: Date;
}
export interface IMessageService {
buildAlert: (monitor: IMonitor, incident: IIncident) => IAlert;
sendMessage: (
alert: IAlert,
channel: INotificationChannel
) => Promise<boolean>;
testMessage: (channel: INotificationChannel) => Promise<boolean>;
}
@@ -0,0 +1,173 @@
import {
IMonitor,
INotificationChannel,
IIncident,
} from "@/db/models/index.js";
import { IAlert, IMessageService } from "./IMessageService.js";
import got from "got";
import { getChildLogger } from "@/logger/Logger.js";
const SERVICE_NAME = "SlackService";
const logger = getChildLogger(SERVICE_NAME);
class SlackService implements IMessageService {
public SERVICE_NAME = SERVICE_NAME;
constructor() {
this.SERVICE_NAME = SERVICE_NAME;
}
private toSlackBlocks = (alert: IAlert) => {
return [
{
type: "header",
text: {
type: "plain_text",
text: "Status Alert",
},
},
{
type: "section",
text: {
type: "mrkdwn",
text: `*Monitor name:* ${alert.name}`,
},
},
{
type: "section",
text: {
type: "mrkdwn",
text: `*Status:* ${alert.status}`,
},
},
{
type: "section",
text: {
type: "mrkdwn",
text: `*URL:* ${alert.url}`,
},
},
{
type: "section",
text: {
type: "mrkdwn",
text: `resolved: ${alert.resolved ? "Yes" : "No"}`,
},
},
{
type: "section",
text: {
type: "mrkdwn",
text: `Resolution Type: ${alert.resolutionType || "N/A"}`,
},
},
{
type: "section",
text: {
type: "mrkdwn",
text: `Resolution note: ${alert.resolutionNote || "N/A"}`,
},
},
{
type: "section",
text: {
type: "mrkdwn",
text: `*Checked at:* ${alert?.checkTime?.toISOString() || "N/A"}`,
},
},
{
type: "section",
text: {
type: "mrkdwn",
text: `*Checked at:* ${alert?.checkTime?.toISOString() || "N/A"}`,
},
},
{
type: "divider",
},
...(alert.details
? Object.entries(alert.details).map(([key, value]) => ({
type: "section",
fields: [
{
type: "mrkdwn",
text: `*${key}:* ${value}`,
},
],
}))
: []),
{
type: "context",
elements: [
{
type: "mrkdwn",
text: `*Alert generated at:* ${
alert?.alertTime?.toISOString() || "N/A"
}`,
},
],
},
];
};
buildAlert = (monitor: IMonitor, incident: IIncident) => {
const name = monitor?.name || "Unnamed monitor";
const monitorStatus = monitor?.status || "unknown status";
const url = monitor?.url || "no URL";
const checkTime = monitor?.lastCheckedAt || null;
const alertTime = new Date();
return {
name,
url,
status: monitorStatus,
resolved: incident.resolved,
resolutionType: incident.resolutionType,
resolvedBy: incident.resolvedBy?.toString(),
resolutionNote: incident.resolutionNote,
checkTime,
alertTime,
};
};
sendMessage = async (alert: IAlert, channel: INotificationChannel) => {
const notificationUrl = channel?.config?.url;
if (!notificationUrl) {
throw new Error("Webhook URL not configured");
}
try {
const payload = {
text: "Status Alert",
blocks: this.toSlackBlocks(alert),
};
await got.post(notificationUrl, { json: payload });
} catch (error) {
logger.warn("Error sending Slack message:", error);
return false;
}
return true;
};
testMessage = async (channel: INotificationChannel) => {
return this.sendMessage(
{
name: "This is a test",
url: "Test URL",
status: "Test status",
checkTime: new Date(),
alertTime: new Date(),
resolved: true,
resolutionType: "auto",
resolvedBy: "system",
resolutionNote: "This is a test message",
},
channel
);
};
}
export default SlackService;
@@ -0,0 +1,60 @@
import { IMonitor, INotificationChannel } from "@/db/models/index.js";
import { IAlert, IMessageService } from "./IMessageService.js";
import ApiError from "@/utils/ApiError.js";
import got from "got";
import { getChildLogger } from "@/logger/Logger.js";
const SERVICE_NAME = "WebhookService";
const logger = getChildLogger(SERVICE_NAME);
class WebhookService implements IMessageService {
public SERVICE_NAME: string;
constructor() {
this.SERVICE_NAME = SERVICE_NAME;
}
buildAlert = (monitor: IMonitor) => {
const name = monitor?.name || "Unnamed monitor";
const monitorStatus = monitor?.status || "unknown status";
const url = monitor?.url || "no URL";
const checkTime = monitor?.lastCheckedAt || null;
const alertTime = new Date();
return {
name,
url,
status: monitorStatus,
checkTime,
alertTime,
};
};
sendMessage = async (alert: IAlert, channel: INotificationChannel) => {
const notificationUrl = channel?.config?.url;
if (!notificationUrl) {
throw new ApiError("Webhook URL not configured", 400);
}
try {
await got.post(notificationUrl, { json: { ...alert } });
} catch (error) {
logger.warn("Failed to send webhook notification:", error);
return false;
}
return true;
};
testMessage = async (channel: INotificationChannel) => {
return this.sendMessage(
{
name: "This is a test",
url: "Test URL",
status: "Test status",
checkTime: new Date(),
alertTime: new Date(),
},
channel
);
};
}
export default WebhookService;
@@ -0,0 +1,5 @@
export { default as DiscordService } from "./Discord.js";
export { default as EmailService } from "./Email.js";
export { default as SlackService } from "./Slack.js";
export { default as WebhookService } from "./Webhook.js";
export * from "./IMessageService.js";