notificaitons provider
This commit is contained in:
1 parent
c2487f0134
commit
7d6bc4b2dc
8 files changed
+134
-25
No files matched your search
@@ -75,6 +75,7 @@ import {
|
||||
MongoInvitesRepository,
|
||||
MongoRecoveryTokensRepository,
|
||||
MongoSettingsRepository,
|
||||
MongoNotificationsRepository,
|
||||
IMonitorsRepository,
|
||||
IChecksRepository,
|
||||
IMonitorStatsRepository,
|
||||
@@ -83,6 +84,7 @@ import {
|
||||
IInvitesRepository,
|
||||
IRecoveryTokensRepository,
|
||||
ISettingsRepository,
|
||||
INotificationsRepository,
|
||||
} from "@/repositories/index.js";
|
||||
|
||||
export type InitializedSerivces = {
|
||||
@@ -116,6 +118,7 @@ export type InitializedSerivces = {
|
||||
invitesRepository: IInvitesRepository;
|
||||
recoveryTokensRepository: IRecoveryTokensRepository;
|
||||
settingsRepository: ISettingsRepository;
|
||||
notificationsRepository: INotificationsRepository;
|
||||
};
|
||||
|
||||
export const initializeServices = async ({
|
||||
@@ -171,7 +174,7 @@ export const initializeServices = async ({
|
||||
const invitesRepository = new MongoInvitesRepository();
|
||||
const recoveryTokensRepository = new MongoRecoveryTokensRepository();
|
||||
const settingsRepository = new MongoSettingsRepository();
|
||||
|
||||
const notificationsRepository = new MongoNotificationsRepository();
|
||||
const networkService = new NetworkService({
|
||||
axios,
|
||||
got,
|
||||
@@ -225,7 +228,7 @@ export const initializeServices = async ({
|
||||
notificationUtils,
|
||||
});
|
||||
|
||||
const notificationsService = new NotificationsService();
|
||||
const notificationsService = new NotificationsService(notificationsRepository);
|
||||
|
||||
const superSimpleQueueHelper = new SuperSimpleQueueHelper({
|
||||
db,
|
||||
|
||||
@@ -21,3 +21,6 @@ export { default as MongoRecoveryTokensRepository } from "@/repositories/recover
|
||||
|
||||
export * from "@/repositories/settings/ISettingsRepository.js";
|
||||
export { default as MongoSettingsRepository } from "@/repositories/settings/MongoSettingsRepository.js";
|
||||
|
||||
export * from "@/repositories/notifications/INotificationsRepository.js";
|
||||
export { default as MongoNotificationsRepository } from "@/repositories/notifications/MongoNotificationsRepository.js";
|
||||
@@ -0,0 +1,7 @@
|
||||
import type { Notification } from "@/types/index.js";
|
||||
export interface INotificationsRepository {
|
||||
// create
|
||||
// fetch
|
||||
// update
|
||||
// delete
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import mongoose from "mongoose";
|
||||
import { NotificationModel, type NotificationDocument } from "@/db/models/index.js";
|
||||
import { INotificationsRepository } from "@/repositories/index.js";
|
||||
import type { Notification } from "@/types/index.js";
|
||||
|
||||
class MongoNotificationsRepository implements INotificationsRepository {
|
||||
private mapDocuments = (documents: NotificationDocument[]): Notification[] => {
|
||||
if (!documents?.length) {
|
||||
return [];
|
||||
}
|
||||
return documents.map((doc) => this.toEntity(doc));
|
||||
};
|
||||
|
||||
private toEntity = (doc: NotificationDocument): Notification => {
|
||||
const toStringId = (value: mongoose.Types.ObjectId | string): string => {
|
||||
return value instanceof mongoose.Types.ObjectId ? value.toString() : value;
|
||||
};
|
||||
|
||||
const toDateString = (value: Date | string): string => {
|
||||
return value instanceof Date ? value.toISOString() : value;
|
||||
};
|
||||
|
||||
return {
|
||||
id: toStringId(doc._id),
|
||||
userId: toStringId(doc.userId),
|
||||
teamId: toStringId(doc.teamId),
|
||||
type: doc.type,
|
||||
notificationName: doc.notificationName,
|
||||
address: doc.address ?? undefined,
|
||||
phone: doc.phone ?? undefined,
|
||||
homeserverUrl: doc.homeserverUrl ?? undefined,
|
||||
roomId: doc.roomId ?? undefined,
|
||||
accessToken: doc.accessToken ?? undefined,
|
||||
createdAt: toDateString(doc.createdAt),
|
||||
updatedAt: toDateString(doc.updatedAt),
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
export default MongoNotificationsRepository;
|
||||
@@ -305,7 +305,7 @@ class IncidentService {
|
||||
|
||||
const groupedByMonitor = {};
|
||||
for (const item of createItems) {
|
||||
if (!item.monitor || !item.monitor._id || !item.check || !item.check._id) {
|
||||
if (!item.monitor || !item.monitor.id || !item.check || !item.check.id) {
|
||||
this.logger.warn({
|
||||
service: this.SERVICE_NAME,
|
||||
method: "processIncidentsFromBuffer",
|
||||
@@ -315,7 +315,7 @@ class IncidentService {
|
||||
continue;
|
||||
}
|
||||
|
||||
const monitorId = item.monitor._id.toString();
|
||||
const monitorId = item.monitor.id.toString();
|
||||
if (!groupedByMonitor[monitorId]) {
|
||||
groupedByMonitor[monitorId] = [];
|
||||
}
|
||||
@@ -341,20 +341,20 @@ class IncidentService {
|
||||
for (const item of items) {
|
||||
checksToAddToIncidents.push({
|
||||
incidentId,
|
||||
checkId: item.check._id.toString(),
|
||||
checkId: item.check.id.toString(),
|
||||
});
|
||||
}
|
||||
} else {
|
||||
const firstItem = items[0];
|
||||
const incidentData = {
|
||||
monitorId: firstItem.monitor._id,
|
||||
monitorId: firstItem.monitor.id,
|
||||
teamId: firstItem.monitor.teamId,
|
||||
type: firstItem.monitor.type,
|
||||
startTime: new Date(),
|
||||
status: true,
|
||||
message: firstItem.check.message || null,
|
||||
statusCode: firstItem.check.statusCode || null,
|
||||
checks: [firstItem.check._id],
|
||||
checks: [firstItem.check.id],
|
||||
};
|
||||
|
||||
newIncidentsToCreate.push({
|
||||
@@ -380,7 +380,7 @@ class IncidentService {
|
||||
for (const remainingItem of item.remainingChecks) {
|
||||
checksToAddToIncidents.push({
|
||||
incidentId,
|
||||
checkId: remainingItem.check._id.toString(),
|
||||
checkId: remainingItem.check.id,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,7 +84,6 @@ class SuperSimpleQueueHelper {
|
||||
const statusChangeResult = await this.statusService.updateMonitorStatus(status, check);
|
||||
|
||||
// Step 5 handle notifications
|
||||
|
||||
this.notificationsService.handleNotifications(monitor, status, statusChangeResult.prevStatus, statusChangeResult.statusChanged);
|
||||
|
||||
// this.notificationService
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
import { Monitor, HardwareStatusPayload, MonitorStatusResponse } from "@/types/index.js";
|
||||
|
||||
const buildHardwareAlerts = async (clientHost: string, monitor: Monitor, networkResponse: MonitorStatusResponse) => {
|
||||
export const buildHardwareAlerts = (
|
||||
clientHost: string,
|
||||
monitor: Monitor,
|
||||
networkResponse: MonitorStatusResponse
|
||||
): { alertsToSend: string[]; discordPayload: any } => {
|
||||
const thresholds = monitor.thresholds || {};
|
||||
const { usage_cpu: cpuThreshold = -1, usage_memory: memoryThreshold = -1, usage_disk: diskThreshold = -1 } = thresholds;
|
||||
|
||||
@@ -54,15 +58,22 @@ const buildHardwareAlerts = async (clientHost: string, monitor: Monitor, network
|
||||
],
|
||||
}),
|
||||
};
|
||||
const alertTypes = ["cpu", "memory", "disk"];
|
||||
const alertTypes = ["cpu", "memory", "disk"] as const;
|
||||
const alertThresholdKeyMap: Record<(typeof alertTypes)[number], "cpuAlertThreshold" | "memoryAlertThreshold" | "diskAlertThreshold"> = {
|
||||
cpu: "cpuAlertThreshold",
|
||||
memory: "memoryAlertThreshold",
|
||||
disk: "diskAlertThreshold",
|
||||
};
|
||||
for (const type of alertTypes) {
|
||||
const thresholdKey = alertThresholdKeyMap[type];
|
||||
// Iterate over each alert type to see if any need to be decremented
|
||||
if (alerts[type] === true) {
|
||||
monitor[`${type}AlertThreshold`]--; // Decrement threshold if an alert is triggered
|
||||
const nextValue = ((monitor[thresholdKey] ?? monitor.alertThreshold) as number) - 1;
|
||||
monitor[thresholdKey] = nextValue; // Decrement threshold if an alert is triggered
|
||||
|
||||
if (monitor[`${type}AlertThreshold`] <= 0) {
|
||||
if (monitor[thresholdKey] <= 0) {
|
||||
// If threshold drops below 0, reset and send notification
|
||||
monitor[`${type}AlertThreshold`] = monitor.alertThreshold;
|
||||
monitor[thresholdKey] = monitor.alertThreshold;
|
||||
|
||||
const formatAlert = {
|
||||
cpu: () => `Your current CPU usage (${(cpuUsage * 100).toFixed(0)}%) is above your threshold (${(cpuThreshold * 100).toFixed(0)}%)`,
|
||||
@@ -70,7 +81,7 @@ const buildHardwareAlerts = async (clientHost: string, monitor: Monitor, network
|
||||
`Your current memory usage (${(memoryUsage * 100).toFixed(0)}%) is above your threshold (${(memoryThreshold * 100).toFixed(0)}%)`,
|
||||
disk: () =>
|
||||
`Your current disk usage: ${disk
|
||||
.map((d, idx) => `(Disk${idx}: ${(d.usage_percent * 100).toFixed(0)}%)`)
|
||||
.map((d, idx) => `(Disk${idx}: ${(d?.usage_percent ?? 0 * 100).toFixed(0)}%)`)
|
||||
.join(", ")} is above your threshold (${(diskThreshold * 100).toFixed(0)}%)`,
|
||||
};
|
||||
alertsToSend.push(formatAlert[type]());
|
||||
@@ -79,13 +90,38 @@ const buildHardwareAlerts = async (clientHost: string, monitor: Monitor, network
|
||||
}
|
||||
}
|
||||
const discordPayload = discordEmbeds.length ? { embeds: discordEmbeds } : null;
|
||||
return [alertsToSend, discordPayload];
|
||||
return { alertsToSend, discordPayload };
|
||||
};
|
||||
|
||||
const buildHardwareNotificationMessage = (alerts: any, monitor: Monitor) => {
|
||||
const { clientHost } = this.settingsService.getSettings();
|
||||
const buildHardwareNotificationMessage = (clientHost: string, alerts: any, monitor: Monitor) => {
|
||||
const alertsHeader = [`Monitor: ${monitor.name}`, `URL: ${monitor.url}`];
|
||||
const alertFooter = [`Go to incident: ${clientHost}/infrastructure/${monitor._id}`];
|
||||
const alertFooter = [`Go to incident: ${clientHost}/infrastructure/${monitor.id}`];
|
||||
const alertText = alerts.length > 0 ? [...alertsHeader, ...alerts, ...alertFooter] : [];
|
||||
return alertText.map((alert) => alert).join("\n");
|
||||
};
|
||||
|
||||
export const shouldSendHardwareAlert = (monitor: Monitor, networkResponse: MonitorStatusResponse): boolean => {
|
||||
const thresholds = monitor.thresholds || {};
|
||||
const { usage_cpu: cpuThreshold = -1, usage_memory: memoryThreshold = -1, usage_disk: diskThreshold = -1 } = thresholds;
|
||||
|
||||
const payload = networkResponse?.payload as HardwareStatusPayload;
|
||||
const metrics = payload.data || {};
|
||||
const { cpu: { usage_percent: cpuUsage = -1 } = {}, memory: { usage_percent: memoryUsage = -1 } = {}, disk = [] } = metrics;
|
||||
|
||||
const cpuBreach = cpuThreshold !== -1 && cpuUsage > cpuThreshold;
|
||||
if (cpuBreach && ((monitor.cpuAlertThreshold ?? monitor.alertThreshold) as number) - 1 <= 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const memoryBreach = memoryThreshold !== -1 && memoryUsage > memoryThreshold;
|
||||
if (memoryBreach && ((monitor.memoryAlertThreshold ?? monitor.alertThreshold) as number) - 1 <= 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const diskBreach = disk?.some((d) => diskThreshold !== -1 && typeof d?.usage_percent === "number" && d?.usage_percent > diskThreshold);
|
||||
if (diskBreach && ((monitor.diskAlertThreshold ?? monitor.alertThreshold) as number) - 1 <= 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
@@ -1,4 +1,7 @@
|
||||
import type { HardwareStatusPayload, Monitor, MonitorStatusResponse } from "@/types/index.js";
|
||||
import type { HardwareStatusPayload, Monitor, MonitorStatusResponse, Notification } from "@/types/index.js";
|
||||
import { NotificationModel } from "@/db/models/index.js";
|
||||
import { buildHardwareAlerts, shouldSendHardwareAlert } from "@/service/infrastructure/notificationProviders/utils.js";
|
||||
import { INotificationsRepository } from "@/repositories/index.js";
|
||||
export interface INotificationsService {
|
||||
handleNotifications: (
|
||||
monitor: Monitor,
|
||||
@@ -9,6 +12,24 @@ export interface INotificationsService {
|
||||
}
|
||||
|
||||
export class NotificationsService implements INotificationsService {
|
||||
private notificationsRepository: INotificationsRepository;
|
||||
constructor(notificationsRepository: INotificationsRepository) {
|
||||
this.notificationsRepository = notificationsRepository;
|
||||
}
|
||||
|
||||
private send = (notification: Notification, monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
|
||||
console.log(notification);
|
||||
};
|
||||
|
||||
private sendNotifications = async (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
|
||||
const notifications = await this.notificationsRepository.findByMonitorId(monitor.id);
|
||||
console.log({ notifications });
|
||||
const tasks = notifications.map((notification) => {
|
||||
this.send(notification, monitor, monitorStatusResponse);
|
||||
});
|
||||
return true;
|
||||
};
|
||||
|
||||
handleNotifications = async (
|
||||
monitor: Monitor,
|
||||
monitorStatusResponse: MonitorStatusResponse,
|
||||
@@ -32,18 +53,18 @@ export class NotificationsService implements INotificationsService {
|
||||
|
||||
// We shoul dsend a notificaiton
|
||||
|
||||
const shouldSend = shouldSendHardwareAlert(monitor, monitorStatusResponse);
|
||||
if (shouldSend === false) return false;
|
||||
console.log(JSON.stringify(monitor, null, 2));
|
||||
console.log(JSON.stringify(monitorStatusResponse, null, 2));
|
||||
// const [alerts, discordContent] = await this.notificationUtils.buildHardwareAlerts(networkResponse);
|
||||
// if (alerts.length === 0) return false;
|
||||
|
||||
// const { subject, html } = await this.notificationUtils.buildHardwareEmail(networkResponse, alerts);
|
||||
// const content = await this.notificationUtils.buildHardwareNotificationMessage(alerts, monitor);
|
||||
// const webhookBody = await this.notificationUtils.buildHardwareWebhookBody(alerts, monitor);
|
||||
// const success = await this.notifyAll({ notificationIDs, subject, html, content, discordContent, webhookBody });
|
||||
return true;
|
||||
return await this.sendNotifications(monitor, monitorStatusResponse);
|
||||
}
|
||||
|
||||
return false;
|
||||
// We should send a notification for non-hardware monitor status change
|
||||
return await this.sendNotifications(monitor, monitorStatusResponse);
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user