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,108 @@
import { IMonitor } from "@/db/models/index.js";
import { INetworkService } from "./NetworkService.js";
import { ICheckService } from "../business/CheckService.js";
import { IMonitorStatsService } from "../business/MonitorStatsService.js";
import { IStatusService } from "./StatusService.js";
import { INotificationService } from "./NotificationService.js";
import { IMaintenanceService } from "../business/MaintenanceService.js";
import { IIncidentService } from "../business/IncidentService.js";
import ApiError from "@/utils/ApiError.js";
import { getChildLogger } from "@/logger/Logger.js";
const SERVICE_NAME = "JobGenerator";
const logger = getChildLogger(SERVICE_NAME);
export interface IJobGenerator {
generateJob: () => (Monitor: IMonitor) => Promise<void>;
generateCleanupJob: () => () => Promise<void>;
}
class JobGenerator implements IJobGenerator {
public SERVICE_NAME: string;
private networkService: INetworkService;
private checkService: ICheckService;
private monitorStatsService: IMonitorStatsService;
private statusService: IStatusService;
private notificationService: INotificationService;
private maintenanceService: IMaintenanceService;
private incidentService: IIncidentService;
constructor(
networkService: INetworkService,
checkService: ICheckService,
monitorStatsService: IMonitorStatsService,
statusService: IStatusService,
notificationService: INotificationService,
incidentService: IIncidentService,
maintenanceService: IMaintenanceService
) {
this.SERVICE_NAME = SERVICE_NAME;
this.networkService = networkService;
this.checkService = checkService;
this.monitorStatsService = monitorStatsService;
this.statusService = statusService;
this.notificationService = notificationService;
this.incidentService = incidentService;
this.maintenanceService = maintenanceService;
}
generateJob = () => {
return async (monitor: IMonitor) => {
try {
const monitorId = monitor._id.toString();
if (!monitorId) {
throw new ApiError("No monitorID for creating job", 400);
}
// Check for active maintenance window, if found, skip the check
const isInMaintenance = await this.maintenanceService.isInMaintenance(
monitorId
);
if (isInMaintenance) {
return;
}
const status = await this.networkService.requestStatus(monitor);
const check = await this.checkService.buildCheck(status, monitor.type);
await check.save();
const [updatedMonitor, statusChanged] =
await this.statusService.updateMonitorStatus(monitor, status);
if (statusChanged) {
const incident = await this.incidentService.handleStatusChange(
updatedMonitor,
check
);
// Best effort, don't wait, don't fail
if (incident) {
this.notificationService
.handleNotifications(updatedMonitor, incident)
.catch((error) => {
logger.warn(error);
});
}
}
await this.statusService.updateMonitorStats(
updatedMonitor,
status,
statusChanged
);
} catch (error) {
throw error;
}
};
};
generateCleanupJob = () => {
return async () => {
try {
await this.checkService.cleanupOrphanedChecks();
await this.monitorStatsService.cleanupOrphanedMonitorStats();
} catch (error) {
throw error;
}
};
};
}
export default JobGenerator;
@@ -0,0 +1,252 @@
import { IJob } from "super-simple-scheduler/dist/job/job.js";
import { Monitor, IMonitor } from "@/db/models/index.js";
import Scheduler from "super-simple-scheduler";
import { IJobGenerator } from "./JobGenerator.js";
import { getChildLogger } from "@/logger/Logger.js";
const SERVICE_NAME = "JobQueue";
const logger = getChildLogger(SERVICE_NAME);
export interface IJobMetrics {
jobs: number;
activeJobs: number;
failingJobs: number;
jobsWithFailures: Array<{
monitorId: string | number;
monitorUrl: string | null;
monitorType: string | null;
failedAt: number | null;
failCount: number | null;
failReason: string | null;
}>;
totalRuns: number;
totalFailures: number;
}
export interface IJobData extends IJob {
lastRunTook: number | null;
}
export interface IJobQueue {
init: () => Promise<boolean>;
addJob: (monitor: IMonitor) => Promise<boolean>;
pauseJob: (monitor: IMonitor) => Promise<boolean>;
resumeJob: (monitor: IMonitor) => Promise<boolean>;
updateJob: (monitor: IMonitor) => Promise<boolean>;
deleteJob: (monitor: IMonitor) => Promise<boolean>;
getMetrics: () => Promise<IJobMetrics | null>;
getJobs: () => Promise<IJobData[] | null>;
flush: () => Promise<boolean>;
shutdown: () => Promise<boolean>;
}
export default class JobQueue implements IJobQueue {
public SERVICE_NAME: string;
private scheduler: Scheduler;
private static instance: JobQueue | null = null;
private jobGenerator: any;
constructor() {
this.SERVICE_NAME = SERVICE_NAME;
this.scheduler = new Scheduler({
logLevel: "debug",
});
}
static async create(jobGenerator: IJobGenerator) {
if (!JobQueue.instance) {
const instance = new JobQueue();
instance.jobGenerator = jobGenerator;
await instance.init();
JobQueue.instance = instance;
}
return JobQueue.instance;
}
static getInstance(): JobQueue | null {
return JobQueue.instance;
}
init = async () => {
try {
this.scheduler.start();
// Add template and jobs
this.scheduler.addTemplate(
"monitor-job",
this.jobGenerator.generateJob()
);
// Add a cleanup job
this.scheduler.addTemplate(
"cleanup-job",
this.jobGenerator.generateCleanupJob()
);
await this.scheduler.addJob({
id: "cleanup-orphaned-checks",
template: "cleanup-job",
repeat: 24 * 60 * 60 * 1000, // 24 hours
active: true,
});
const monitors = await Monitor.find();
for (const monitor of monitors) {
const randomOffset = 1000 + Math.random() * 1000;
setTimeout(async () => {
const exists = await Monitor.exists({ _id: monitor._id });
if (exists) {
this.addJob(monitor);
}
}, randomOffset);
}
return true;
} catch (error) {
logger.error(error);
return false;
}
};
addJob = async (monitor: IMonitor) => {
try {
return await this.scheduler?.addJob({
id: monitor._id.toString(),
template: "monitor-job",
repeat: monitor.interval,
active: monitor.isActive,
data: monitor,
});
} catch (error) {
logger.error(error);
return false;
}
};
pauseJob = async (monitor: IMonitor) => {
try {
return await this.scheduler?.pauseJob(monitor._id.toString());
} catch (error) {
logger.error(error);
return false;
}
};
resumeJob = async (monitor: IMonitor) => {
try {
return await this.scheduler.resumeJob(monitor._id.toString());
} catch (error) {
logger.error(error);
return false;
}
};
updateJob = async (monitor: IMonitor) => {
try {
return await this.scheduler.updateJob(monitor._id.toString(), {
repeat: monitor.interval,
data: monitor,
});
} catch (error) {
logger.error(error);
return false;
}
};
deleteJob = async (monitor: IMonitor) => {
try {
this.scheduler?.removeJob(monitor._id.toString());
return true;
} catch (error) {
logger.error(error);
return false;
}
};
getMetrics = async (): Promise<IJobMetrics | null> => {
try {
const jobs = await this.scheduler.getJobs();
const metrics: IJobMetrics = jobs.reduce<IJobMetrics>(
(acc, job) => {
if (!job.data) return acc;
acc.totalRuns += job.runCount || 0;
acc.totalFailures += job.failCount || 0;
acc.jobs++;
// Check if job is currently failing (has recent failures)
const hasFailures = job.failCount && job.failCount > 0;
const isCurrentlyFailing =
hasFailures &&
job.lastFailedAt &&
(!job.lastRunAt || job.lastFailedAt > job.lastRunAt);
if (isCurrentlyFailing) {
acc.failingJobs++;
}
if (job.lockedAt) {
acc.activeJobs++;
}
if (hasFailures) {
acc.jobsWithFailures.push({
monitorId: job.id,
monitorUrl: job.data?.url || null,
monitorType: job.data?.type || null,
failedAt: job.lastFailedAt || null,
failCount: job.failCount || null,
failReason: job.lastFailReason || null,
});
}
return acc;
},
{
jobs: 0,
activeJobs: 0,
failingJobs: 0,
jobsWithFailures: [],
totalRuns: 0,
totalFailures: 0,
}
);
return metrics;
} catch (error) {
logger.error(error);
return null;
}
};
getJobs = async (): Promise<IJobData[] | null> => {
try {
const jobs = await this.scheduler.getJobs();
return jobs.map((job) => {
return {
...job,
lastRunTook:
job.lockedAt || !job.lastFinishedAt || !job.lastRunAt
? null
: job.lastFinishedAt - job.lastRunAt,
};
});
} catch (error) {
logger.error(error);
return null;
}
};
flush = async () => {
try {
return await this.scheduler.flushJobs();
} catch (error) {
logger.error(error);
return false;
}
};
shutdown = async () => {
try {
return await this.scheduler.stop();
} catch (error) {
logger.error(error);
return false;
}
};
}
@@ -0,0 +1,232 @@
import { Got, HTTPError } from "got";
import got from "got";
import ping from "ping";
import { IMonitor } from "@/db/models/index.js";
import { GotTimings } from "@/db/models/checks/Check.js";
import type { Response } from "got";
import type {
ISystemInfo,
ICaptureInfo,
ILighthouseResult,
} from "@/db/models/index.js";
import { MonitorType, MonitorStatus } from "@/db/models/monitors/Monitor.js";
import ApiError from "@/utils/ApiError.js";
import { config } from "@/config/index.js";
import CacheableLookup from "cacheable-lookup";
import http from "http";
import https from "https";
const SERVICE_NAME = "NetworkService";
export interface INetworkService {
requestHttp: (monitor: IMonitor) => Promise<StatusResponse>;
requestInfrastructure: (monitor: IMonitor) => Promise<StatusResponse>;
requestStatus: (monitor: IMonitor) => Promise<StatusResponse>;
requestPagespeed: (monitor: IMonitor) => Promise<StatusResponse>;
requestPing: (monitor: IMonitor) => Promise<StatusResponse>;
}
export interface ICapturePayload {
data: ISystemInfo;
capture: ICaptureInfo;
}
export interface ILighthousePayload {
lighthouseResult: ILighthouseResult;
}
export interface StatusResponse<TPayload = unknown> {
monitorId: string;
teamId: string;
type: MonitorType;
code?: number;
status: MonitorStatus;
message: string;
responseTime: number;
timings?: GotTimings;
payload?: TPayload;
}
class NetworkService implements INetworkService {
public SERVICE_NAME = SERVICE_NAME;
private got: Got;
private NETWORK_ERROR: number;
constructor() {
this.SERVICE_NAME = SERVICE_NAME;
this.NETWORK_ERROR = 5000;
const cacheable = new CacheableLookup();
this.got = got.extend({
dnsCache: cacheable,
timeout: {
request: 30000,
},
retry: { limit: 1 },
});
}
private buildStatusResponse = <T>(
monitor: IMonitor,
response: Response<T> | null,
error: any | null
): StatusResponse<T> => {
if (error) {
const statusResponse: StatusResponse<T> = {
monitorId: monitor._id.toString(),
teamId: monitor.teamId.toString(),
type: monitor.type,
status: "down" as MonitorStatus,
code: this.NETWORK_ERROR,
message: error.message || "Network error",
responseTime: 0,
timings: { phases: {} } as GotTimings,
};
if (error instanceof HTTPError) {
statusResponse.code = error?.response?.statusCode || this.NETWORK_ERROR;
statusResponse.message = error.message || "HTTP error";
statusResponse.responseTime = error.timings?.phases?.total || 0;
statusResponse.timings = error.timings;
}
return statusResponse;
}
const statusResponse: StatusResponse<T> = {
monitorId: monitor._id.toString(),
teamId: monitor.teamId.toString(),
type: monitor.type,
code: response?.statusCode || this.NETWORK_ERROR,
status: response?.ok === true ? "up" : "down",
message: response?.statusMessage || "",
responseTime: response?.timings?.phases?.total || 0,
timings: response?.timings || ({ phases: {} } as GotTimings),
};
return statusResponse;
};
requestHttp = async (monitor: IMonitor) => {
try {
const url = monitor.url;
if (!url) {
throw new Error("No URL provided");
}
try {
const response: Response = await this.got(url);
return this.buildStatusResponse(monitor, response, null);
} catch (error) {
return this.buildStatusResponse(monitor, null, error);
}
} catch (error) {
throw error;
}
};
requestInfrastructure = async (monitor: IMonitor) => {
const url = monitor.url;
if (!url) {
throw new Error("No URL provided");
}
const secret = monitor.secret;
if (!secret) {
throw new Error("No secret provided for infrastructure monitor");
}
let statusResponse: StatusResponse<ICapturePayload>;
try {
const response: Response<ICapturePayload> | null = await this.got(url, {
headers: { Authorization: `Bearer ${secret}` },
responseType: "json",
});
statusResponse = this.buildStatusResponse(monitor, response, null);
if (!response?.body) {
throw new ApiError(
"No payload received from infrastructure monitor",
500
);
}
statusResponse.payload = response?.body;
return statusResponse;
} catch (error) {
statusResponse = this.buildStatusResponse(monitor, null, error);
}
return statusResponse;
};
requestPagespeed = async (monitor: IMonitor) => {
const apiKey = config.PAGESPEED_API_KEY;
if (!apiKey) {
throw new Error("No API key provided for pagespeed monitor");
}
const url = monitor.url;
if (!url) {
throw new Error("No URL provided");
}
let statusResponse: StatusResponse<ILighthousePayload>;
try {
const response: Response = await this.got(url);
statusResponse = this.buildStatusResponse(
monitor,
response,
null
) as StatusResponse<ILighthousePayload>;
} catch (error) {
statusResponse = this.buildStatusResponse(monitor, null, error);
}
const pagespeedUrl = `https://pagespeedonline.googleapis.com/pagespeedonline/v5/runPagespeed?url=${url}&category=seo&category=accessibility&category=best-practices&category=performance&key=${apiKey}`;
const pagespeedResponse = await this.got<ILighthousePayload>(pagespeedUrl, {
responseType: "json",
});
const payload = pagespeedResponse.body;
if (payload) {
statusResponse.payload = payload;
return statusResponse;
} else {
throw new ApiError("No payload received from pagespeed monitor", 500);
}
};
requestPing = async (monitor: IMonitor) => {
const response = await ping.promise.probe(monitor.url);
const status = response?.alive === true ? "up" : "down";
const rawTime =
typeof response?.time === "string"
? parseFloat(response.time)
: Number(response?.time);
const responseTime = Number.isFinite(rawTime) ? rawTime : 0;
return {
monitorId: monitor._id.toString(),
teamId: monitor.teamId.toString(),
type: monitor.type,
status: status as MonitorStatus,
message: "Ping successful",
responseTime,
timings: { phases: {} } as GotTimings,
};
};
requestStatus = async (monitor: IMonitor) => {
switch (monitor?.type) {
case "http":
return await this.requestHttp(monitor); // uses GOT
case "https":
return await this.requestHttp(monitor); // uses GOT
case "infrastructure":
return await this.requestInfrastructure(monitor); // uses GOT
case "pagespeed":
return await this.requestPagespeed(monitor); // uses GOT
case "ping":
return await this.requestPing(monitor); // uses PING
default:
throw new Error("Not implemented");
}
};
}
export default NetworkService;
@@ -0,0 +1,186 @@
import UserService from "../business/UserService.js";
import {
IMonitor,
Monitor,
NotificationChannel,
INotificationChannel,
IIncident,
} from "@/db/models/index.js";
import {
EmailService,
SlackService,
DiscordService,
WebhookService,
} from "./NotificationServices/index.js";
import ApiError from "@/utils/ApiError.js";
import { getChildLogger } from "@/logger/Logger.js";
const SERVICE_NAME = "NotificationService";
const logger = getChildLogger(SERVICE_NAME);
export interface ITestResult {
channelName: string;
channelUrl: string;
channelType: string;
sent: boolean;
}
export interface INotificationService {
handleNotifications: (
monitor: IMonitor,
incident: IIncident
) => Promise<void>;
testNotificationChannels: (
monitorId: string,
teamId: string
) => Promise<ITestResult[]>;
testNotificationChannel: (
notificationChannel: INotificationChannel
) => Promise<Boolean>;
}
class NotificationService implements INotificationService {
public SERVICE_NAME: string;
private emailService: EmailService;
private slackService: SlackService;
private discordService: DiscordService;
private webhookService: WebhookService;
private userService: UserService;
constructor(userService: UserService) {
this.SERVICE_NAME = SERVICE_NAME;
this.userService = userService;
this.emailService = new EmailService(userService);
this.slackService = new SlackService();
this.discordService = new DiscordService();
this.webhookService = new WebhookService();
}
handleNotifications = async (monitor: IMonitor, incident: IIncident) => {
const notificationIds = monitor.notificationChannels || [];
if (notificationIds.length === 0) {
return;
}
const notificationChannels = await NotificationChannel.find({
_id: { $in: notificationIds },
});
for (const channel of notificationChannels) {
// Implement sending logic based on channel.type and channel.config
let service;
switch (channel.type) {
case "email":
await this.emailService.sendMessage(
this.emailService.buildAlert(monitor, incident),
channel
);
break;
case "slack":
await this.slackService.sendMessage(
this.slackService.buildAlert(monitor, incident),
channel
);
break;
case "discord":
await this.discordService.sendMessage(
this.discordService.buildAlert(monitor, incident),
channel
);
break;
case "webhook":
await this.webhookService.sendMessage(
this.webhookService.buildAlert(monitor, incident),
channel
);
break;
default:
logger.warn(`Unknown notification channel type: ${channel.type}`);
}
}
return;
};
private testNotification = async (
channel: INotificationChannel,
results: any[]
) => {
switch (channel.type) {
case "email":
const sentEmail = await this.emailService.testMessage(channel);
results.push({
channelName: channel.name,
channelType: channel.type,
channelUrl: channel.config?.emailAddress || "N/A",
sent: sentEmail,
});
break;
case "slack":
const sentSlack = await this.slackService.testMessage(channel);
results.push({
channelName: channel.name,
channelType: channel.type,
channelUrl: channel.config?.url || "N/A",
sent: sentSlack,
});
break;
case "discord":
const sentDiscord = await this.discordService.testMessage(channel);
results.push({
channelName: channel.name,
channelType: channel.type,
channelUrl: channel.config?.url || "N/A",
sent: sentDiscord,
});
break;
case "webhook":
const sentWebhook = await this.webhookService.testMessage(channel);
results.push({
channelName: channel.name,
channelType: channel.type,
channelUrl: channel.config?.url || "N/A",
sent: sentWebhook,
});
break;
default:
logger.warn(`Unknown notification channel type: ${channel.type}`);
}
return results;
};
testNotificationChannels = async (monitorId: string, teamId: string) => {
const monitor = await Monitor.findOne({
_id: monitorId,
teamId: teamId,
});
if (!monitor) {
throw new ApiError("Monitor not found", 404);
}
const notificationIds = monitor.notificationChannels || [];
const notificationChannels = await NotificationChannel.find({
_id: { $in: notificationIds },
}).lean();
const results: any[] = [];
for (const channel of notificationChannels) {
await this.testNotification(channel, results);
}
return results;
};
testNotificationChannel = async (
notificationChannel: INotificationChannel
) => {
const result: any[] = [];
await this.testNotification(notificationChannel, result);
if (result.length > 0) {
return true;
}
return false;
};
}
export default NotificationService;
@@ -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";
@@ -0,0 +1,141 @@
import { IMonitor, IMonitorStats, MonitorStats } from "@/db/models/index.js";
import { StatusResponse } from "./NetworkService.js";
import ApiError from "@/utils/ApiError.js";
const SERVICE_NAME = "StatusService";
const MAX_LATEST_CHECKS = 25;
export interface IStatusService {
updateMonitorStatus: (
monitor: IMonitor,
status: StatusResponse
) => Promise<StatusChangeResult>;
calculateAvgResponseTime: (
stats: IMonitorStats,
statusResponse: StatusResponse
) => number;
updateMonitorStats: (
monitor: IMonitor,
status: StatusResponse,
statusChanged: boolean
) => Promise<IMonitorStats | null>;
}
export type StatusChangeResult = [
updatedMonitor: IMonitor,
statusChanged: boolean
];
class StatusService implements IStatusService {
public SERVICE_NAME: string;
constructor() {
this.SERVICE_NAME = SERVICE_NAME;
}
updateMonitorStatus = async (
monitor: IMonitor,
statusResponse: StatusResponse
): Promise<StatusChangeResult> => {
const newStatus = statusResponse.status;
monitor.lastCheckedAt = new Date();
// Store latest checks for display
monitor.latestChecks = monitor.latestChecks || [];
monitor.latestChecks.push({
status: newStatus,
responseTime: statusResponse.responseTime,
checkedAt: monitor.lastCheckedAt,
});
while (monitor.latestChecks.length > MAX_LATEST_CHECKS) {
monitor.latestChecks.shift();
}
// Update monitor status
if (monitor.status === "initializing") {
monitor.status = newStatus;
return [await monitor.save(), monitor.latestChecks.length === 0];
} else {
const { n } = monitor;
const latestChecks = monitor.latestChecks.slice(-n);
// Return early if not enough statuses to evaluate
if (latestChecks.length < n) {
return [await monitor.save(), false];
}
// If all different than current status, update status
const allDifferent = latestChecks.every(
(check) => check.status !== monitor.status
);
if (allDifferent && monitor.status !== newStatus) {
monitor.status = newStatus;
}
return [await monitor.save(), allDifferent];
}
};
calculateAvgResponseTime = (
stats: IMonitorStats,
statusResponse: StatusResponse
): number => {
let avgResponseTime = stats.avgResponseTime;
// Set initial
if (avgResponseTime === 0) {
avgResponseTime = statusResponse.responseTime;
} else {
avgResponseTime =
(avgResponseTime * (stats.totalChecks - 1) +
statusResponse.responseTime) /
stats.totalChecks;
}
return avgResponseTime;
};
updateMonitorStats = async (
monitor: IMonitor,
statusResponse: StatusResponse,
statusChanged: boolean
) => {
const stats = await MonitorStats.findOne({ monitorId: monitor._id });
if (!stats) {
throw new ApiError("MonitorStats not found", 500);
}
// Update check counts
stats.totalChecks += 1;
stats.totalUpChecks += statusResponse.status === "up" ? 1 : 0;
stats.totalDownChecks += statusResponse.status === "down" ? 1 : 0;
// Update streak
if (!statusChanged) {
stats.currentStreak += 1;
} else {
stats.currentStreak = 1;
stats.currentStreakStatus = statusResponse.status;
stats.currentStreakStartedAt = Date.now();
}
// Update time stamps
stats.lastCheckTimestamp = Date.now();
stats.timeOfLastFailure =
statusResponse.status === "down" ? Date.now() : stats.timeOfLastFailure;
// Update stats that need updated check counts
stats.avgResponseTime = this.calculateAvgResponseTime(
stats,
statusResponse
);
stats.uptimePercentage = stats.totalUpChecks / stats.totalChecks;
// Other
stats.lastResponseTime = statusResponse.responseTime;
stats.maxResponseTime = Math.max(
stats.maxResponseTime,
statusResponse.responseTime
);
return await stats.save();
};
}
export default StatusService;