From a7b0ce4d8ffd5d7d7cded37a1b0c9c4c91b91578 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Sat, 14 Feb 2026 21:21:18 +0000 Subject: [PATCH] remove thresholds --- client/src/Types/Monitor.ts | 8 ---- server/src/db/models/Monitor.ts | 16 +------ .../monitors/MongoMonitorsRepository.ts | 2 - .../SuperSimpleQueueHelper.ts | 44 +++++++++---------- .../notificationProviders/utils.ts | 12 +++-- .../infrastructure/notificationsService.ts | 9 +++- server/src/types/monitor.ts | 8 ---- 7 files changed, 36 insertions(+), 63 deletions(-) diff --git a/client/src/Types/Monitor.ts b/client/src/Types/Monitor.ts index ea6f4d019..2f1c1838d 100644 --- a/client/src/Types/Monitor.ts +++ b/client/src/Types/Monitor.ts @@ -21,13 +21,6 @@ export const MonitorStatuses = [ ] as const; export type MonitorStatus = (typeof MonitorStatuses)[number]; -export interface MonitorThresholds { - usage_cpu?: number; - usage_memory?: number; - usage_disk?: number; - usage_temperature?: number; -} - export type MonitorMatchMethod = "equal" | "include" | "regex" | ""; export interface Monitor { @@ -53,7 +46,6 @@ export interface Monitor { uptimePercentage?: number; notifications: string[]; secret?: string; - thresholds?: MonitorThresholds; alertThreshold: number; cpuAlertThreshold: number; memoryAlertThreshold: number; diff --git a/server/src/db/models/Monitor.ts b/server/src/db/models/Monitor.ts index 9111ce47f..bb3108ed3 100644 --- a/server/src/db/models/Monitor.ts +++ b/server/src/db/models/Monitor.ts @@ -1,5 +1,5 @@ import { Schema, model, Types, type UpdateQuery } from "mongoose"; -import type { Monitor, MonitorMatchMethod, MonitorThresholds, CheckSnapshot } from "@/types/monitor.js"; +import type { Monitor, MonitorMatchMethod, CheckSnapshot } from "@/types/monitor.js"; import { MonitorTypes, MonitorStatuses } from "@/types/monitor.js"; type CheckSnapshotDocument = Omit & { createdAt: Date }; @@ -13,7 +13,6 @@ type MonitorDocumentBase = Omit< notifications: Types.ObjectId[]; selectedDisks: string[]; matchMethod?: MonitorMatchMethod; - thresholds?: MonitorThresholds; }; interface MonitorDocument extends MonitorDocumentBase { @@ -24,16 +23,6 @@ interface MonitorDocument extends MonitorDocumentBase { updatedAt: Date; } -const thresholdsSchema = new Schema( - { - usage_cpu: { type: Number }, - usage_memory: { type: Number }, - usage_disk: { type: Number }, - usage_temperature: { type: Number }, - }, - { _id: false } -); - const checkSnapshotSchema = new Schema( { id: { type: String, required: true }, @@ -132,9 +121,6 @@ const MonitorSchema = new Schema( secret: { type: String, }, - thresholds: { - type: thresholdsSchema, - }, alertThreshold: { type: Number, default: 5, diff --git a/server/src/repositories/monitors/MongoMonitorsRepository.ts b/server/src/repositories/monitors/MongoMonitorsRepository.ts index 65d93421e..7390d0e0e 100644 --- a/server/src/repositories/monitors/MongoMonitorsRepository.ts +++ b/server/src/repositories/monitors/MongoMonitorsRepository.ts @@ -317,7 +317,6 @@ class MongoMonitorsRepository implements IMonitorsRepository { uptimePercentage: doc.uptimePercentage ?? undefined, notifications: notificationIds, secret: doc.secret ?? undefined, - thresholds: doc.thresholds ?? undefined, alertThreshold: doc.alertThreshold, cpuAlertThreshold: doc.cpuAlertThreshold, memoryAlertThreshold: doc.memoryAlertThreshold, @@ -370,7 +369,6 @@ class MongoMonitorsRepository implements IMonitorsRepository { uptimePercentage: doc.uptimePercentage ?? undefined, notifications: notificationIds, secret: doc.secret ?? undefined, - thresholds: doc.thresholds ?? undefined, alertThreshold: doc.alertThreshold, cpuAlertThreshold: doc.cpuAlertThreshold, memoryAlertThreshold: doc.memoryAlertThreshold, diff --git a/server/src/service/infrastructure/SuperSimpleQueue/SuperSimpleQueueHelper.ts b/server/src/service/infrastructure/SuperSimpleQueue/SuperSimpleQueueHelper.ts index e9439feef..0b143450c 100644 --- a/server/src/service/infrastructure/SuperSimpleQueue/SuperSimpleQueueHelper.ts +++ b/server/src/service/infrastructure/SuperSimpleQueue/SuperSimpleQueueHelper.ts @@ -182,14 +182,6 @@ class SuperSimpleQueueHelper { return maintenanceWindowIsActive; } - /** - * Evaluates what actions should be taken based on monitor status and hardware thresholds. - * This is a pure decision function that returns what should happen without executing anything. - * - * @param statusChangeResult - Result from statusService.updateMonitorStatus() - * @param networkResponse - Network response containing payload data - * @returns Decision object indicating what actions to take - */ private evaluateMonitorAction(statusChangeResult: StatusChangeResult, networkResponse: MonitorStatusResponse): MonitorActionDecision { const { monitor, statusChanged, prevStatus } = statusChangeResult; @@ -202,10 +194,10 @@ class SuperSimpleQueueHelper { notificationReason: null, }; - // CASE 1: Non-hardware monitors (http, ping, port, docker, pagespeed, game) + // CASE 1: Non-hardware if (monitor.type !== "hardware") { - // Only act on status changes if (statusChanged) { + // This is the current state of the monitor AFTER the most recent check if (monitor.status === "down") { decision.shouldCreateIncident = true; decision.shouldSendNotification = true; @@ -220,7 +212,7 @@ class SuperSimpleQueueHelper { return decision; } - // CASE 2: Hardware monitors + // CASE 2: Hardware const payload = networkResponse.payload as HardwareStatusPayload; const metrics = payload?.data; @@ -229,12 +221,11 @@ class SuperSimpleQueueHelper { } // Check if thresholds exist - const thresholds = monitor.thresholds || {}; const hasThresholds = - thresholds.usage_cpu !== undefined || - thresholds.usage_memory !== undefined || - thresholds.usage_disk !== undefined || - thresholds.usage_temperature !== undefined; + monitor.cpuAlertThreshold !== undefined || + monitor.memoryAlertThreshold !== undefined || + monitor.diskAlertThreshold !== undefined || + monitor.tempAlertThreshold !== undefined; // Evaluate threshold breaches const breaches = { @@ -246,26 +237,31 @@ class SuperSimpleQueueHelper { if (hasThresholds) { // CPU check - if (thresholds.usage_cpu !== undefined) { + if (monitor.cpuAlertThreshold !== undefined) { const cpuUsage = metrics.cpu?.usage_percent ?? -1; - breaches.cpu = cpuUsage !== -1 && cpuUsage > thresholds.usage_cpu; + // Convert threshold from percentage (0-100) to decimal (0-1) for comparison + breaches.cpu = cpuUsage !== -1 && cpuUsage > monitor.cpuAlertThreshold / 100; } // Memory check - if (thresholds.usage_memory !== undefined) { + if (monitor.memoryAlertThreshold !== undefined) { const memoryUsage = metrics.memory?.usage_percent ?? -1; - breaches.memory = memoryUsage !== -1 && memoryUsage > thresholds.usage_memory; + // Convert threshold from percentage (0-100) to decimal (0-1) for comparison + breaches.memory = memoryUsage !== -1 && memoryUsage > monitor.memoryAlertThreshold / 100; } // Disk check - if (thresholds.usage_disk !== undefined) { - breaches.disk = metrics.disk?.some((d: any) => typeof d?.usage_percent === "number" && d.usage_percent > thresholds.usage_disk!) ?? false; + if (monitor.diskAlertThreshold !== undefined) { + // Convert threshold from percentage (0-100) to decimal (0-1) for comparison + breaches.disk = + metrics.disk?.some((d: any) => typeof d?.usage_percent === "number" && d.usage_percent > monitor.diskAlertThreshold / 100) ?? false; } // Temperature check (alert if ANY sensor exceeds threshold) - if (thresholds.usage_temperature !== undefined) { + if (monitor.tempAlertThreshold !== undefined) { const temps = metrics.cpu?.temperature ?? []; - breaches.temp = temps.some((temp: number) => temp > thresholds.usage_temperature!); + // Temperature threshold is in Celsius, compare directly + breaches.temp = temps.some((temp: number) => temp > monitor.tempAlertThreshold); } } diff --git a/server/src/service/infrastructure/notificationProviders/utils.ts b/server/src/service/infrastructure/notificationProviders/utils.ts index 2fe22e834..ac964d93f 100644 --- a/server/src/service/infrastructure/notificationProviders/utils.ts +++ b/server/src/service/infrastructure/notificationProviders/utils.ts @@ -23,8 +23,10 @@ export const buildHardwareAlerts = ( 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; + // Thresholds are stored as percentages (0-100), convert to decimal (0-1) for comparison + const cpuThreshold = monitor.cpuAlertThreshold !== undefined ? monitor.cpuAlertThreshold / 100 : -1; + const memoryThreshold = monitor.memoryAlertThreshold !== undefined ? monitor.memoryAlertThreshold / 100 : -1; + const diskThreshold = monitor.diskAlertThreshold !== undefined ? monitor.diskAlertThreshold / 100 : -1; const payload = networkResponse?.payload as HardwareStatusPayload; const metrics = payload.data || {}; @@ -124,8 +126,10 @@ export const buildHardwareWebhookBody = (alerts: string[], monitor: Monitor): st }; 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; + // Thresholds are stored as percentages (0-100), convert to decimal (0-1) for comparison + const cpuThreshold = monitor.cpuAlertThreshold !== undefined ? monitor.cpuAlertThreshold / 100 : -1; + const memoryThreshold = monitor.memoryAlertThreshold !== undefined ? monitor.memoryAlertThreshold / 100 : -1; + const diskThreshold = monitor.diskAlertThreshold !== undefined ? monitor.diskAlertThreshold / 100 : -1; const payload = networkResponse?.payload as HardwareStatusPayload; const metrics = payload.data || {}; diff --git a/server/src/service/infrastructure/notificationsService.ts b/server/src/service/infrastructure/notificationsService.ts index 531014a7a..b2ae59b7c 100644 --- a/server/src/service/infrastructure/notificationsService.ts +++ b/server/src/service/infrastructure/notificationsService.ts @@ -239,9 +239,14 @@ export class NotificationsService implements INotificationsService { // Deal with hardware thresholds if (type === "hardware") { - const thresholds = monitor.thresholds; + // Check if any thresholds are set + const hasThresholds = + monitor.cpuAlertThreshold !== undefined || + monitor.memoryAlertThreshold !== undefined || + monitor.diskAlertThreshold !== undefined || + monitor.tempAlertThreshold !== undefined; - if (thresholds === undefined) return false; // No thresholds set, we're done + if (!hasThresholds) return false; // No thresholds set, we're done const metrics = payload?.data ?? null; if (metrics === null) return false; // No metrics, we're done diff --git a/server/src/types/monitor.ts b/server/src/types/monitor.ts index 384e14fe0..83a207e2d 100644 --- a/server/src/types/monitor.ts +++ b/server/src/types/monitor.ts @@ -7,13 +7,6 @@ export type MonitorType = (typeof MonitorTypes)[number]; export const MonitorStatuses = ["up", "down", "paused", "initializing", "maintenance"] as const; export type MonitorStatus = (typeof MonitorStatuses)[number]; -export interface MonitorThresholds { - usage_cpu?: number; - usage_memory?: number; - usage_disk?: number; - usage_temperature?: number; -} - export type MonitorMatchMethod = "equal" | "include" | "regex" | ""; export interface Monitor { @@ -39,7 +32,6 @@ export interface Monitor { uptimePercentage?: number; notifications: string[]; secret?: string; - thresholds?: MonitorThresholds; alertThreshold: number; cpuAlertThreshold: number; memoryAlertThreshold: number;