diff --git a/server/src/service/infrastructure/SuperSimpleQueue/SuperSimpleQueueHelper.ts b/server/src/service/infrastructure/SuperSimpleQueue/SuperSimpleQueueHelper.ts index 85e54f793..ec8a792f2 100644 --- a/server/src/service/infrastructure/SuperSimpleQueue/SuperSimpleQueueHelper.ts +++ b/server/src/service/infrastructure/SuperSimpleQueue/SuperSimpleQueueHelper.ts @@ -220,59 +220,38 @@ class SuperSimpleQueueHelper { return decision; // No metrics, can't evaluate } - // Check if thresholds exist - const hasThresholds = - monitor.cpuAlertThreshold !== undefined || - monitor.memoryAlertThreshold !== undefined || - monitor.diskAlertThreshold !== undefined || - monitor.tempAlertThreshold !== undefined; - // Evaluate threshold breaches + // CPU check + const cpuUsage = metrics.cpu?.usage_percent ?? -1; + const cpuBreach = cpuUsage !== -1 && cpuUsage > monitor.cpuAlertThreshold / 100; + + // Memory check + const memoryUsage = metrics.memory?.usage_percent ?? -1; + const memoryBreach = memoryUsage !== -1 && memoryUsage > monitor.memoryAlertThreshold / 100; + + // Disk check + const diskBreach = + 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) + const temps = metrics.cpu?.temperature ?? []; + const tempBreach = temps.some((temp: number) => temp > monitor.tempAlertThreshold); + const breaches = { - cpu: false, - memory: false, - disk: false, - temp: false, + cpu: cpuBreach, + memory: memoryBreach, + disk: diskBreach, + temp: tempBreach, }; - if (hasThresholds) { - // CPU check - if (monitor.cpuAlertThreshold !== undefined) { - const cpuUsage = metrics.cpu?.usage_percent ?? -1; - // Convert threshold from percentage (0-100) to decimal (0-1) for comparison - breaches.cpu = cpuUsage !== -1 && cpuUsage > monitor.cpuAlertThreshold / 100; - } - - // Memory check - if (monitor.memoryAlertThreshold !== undefined) { - const memoryUsage = metrics.memory?.usage_percent ?? -1; - // Convert threshold from percentage (0-100) to decimal (0-1) for comparison - breaches.memory = memoryUsage !== -1 && memoryUsage > monitor.memoryAlertThreshold / 100; - } - - // Disk check - 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 (monitor.tempAlertThreshold !== undefined) { - const temps = metrics.cpu?.temperature ?? []; - // Temperature threshold is in Celsius, compare directly - breaches.temp = temps.some((temp: number) => temp > monitor.tempAlertThreshold); - } - } - const anyThresholdBreached = Object.values(breaches).some((b) => b); // Check if countdown has reached zero for any threshold const shouldNotifyThreshold = - (breaches.cpu && (monitor.cpuAlertThreshold ?? 100) <= 0) || - (breaches.memory && (monitor.memoryAlertThreshold ?? 100) <= 0) || - (breaches.disk && (monitor.diskAlertThreshold ?? 100) <= 0) || - (breaches.temp && (monitor.tempAlertThreshold ?? 100) <= 0); + (breaches.cpu && monitor.cpuAlertThreshold <= 0) || + (breaches.memory && monitor.memoryAlertThreshold <= 0) || + (breaches.disk && monitor.diskAlertThreshold <= 0) || + (breaches.temp && monitor.tempAlertThreshold <= 0); // Decision logic for hardware if (statusChanged && monitor.status === "down") { diff --git a/server/src/service/infrastructure/notificationProviders/utils.ts b/server/src/service/infrastructure/notificationProviders/utils.ts index 284855846..ed9d0177c 100644 --- a/server/src/service/infrastructure/notificationProviders/utils.ts +++ b/server/src/service/infrastructure/notificationProviders/utils.ts @@ -88,7 +88,7 @@ export const buildHardwareAlerts = ( const thresholdKey = alertThresholdKeyMap[type]; // Iterate over each alert type to see if any need to be decremented if (alerts[type] === true) { - const nextValue = ((monitor[thresholdKey] ?? 100) as number) - 1; + const nextValue = monitor[thresholdKey] - 1; monitor[thresholdKey] = nextValue; // Decrement threshold if an alert is triggered if (monitor[thresholdKey] <= 0) { @@ -136,17 +136,17 @@ export const shouldSendHardwareAlert = (monitor: Monitor, networkResponse: Monit const { cpu: { usage_percent: cpuUsage = -1 } = {}, memory: { usage_percent: memoryUsage = -1 } = {}, disk = [] } = metrics; const cpuBreach = cpuThreshold !== -1 && cpuUsage > cpuThreshold; - if (cpuBreach && ((monitor.cpuAlertThreshold ?? 100) as number) - 1 <= 0) { + if (cpuBreach && monitor.cpuAlertThreshold - 1 <= 0) { return true; } const memoryBreach = memoryThreshold !== -1 && memoryUsage > memoryThreshold; - if (memoryBreach && ((monitor.memoryAlertThreshold ?? 100) as number) - 1 <= 0) { + if (memoryBreach && monitor.memoryAlertThreshold - 1 <= 0) { return true; } const diskBreach = disk?.some((d) => diskThreshold !== -1 && typeof d?.usage_percent === "number" && d?.usage_percent > diskThreshold); - if (diskBreach && ((monitor.diskAlertThreshold ?? 100) as number) - 1 <= 0) { + if (diskBreach && monitor.diskAlertThreshold - 1 <= 0) { return true; }