remove thresholds

This commit is contained in:
Alex Holliday committed 2026-02-14 21:21:18 +00:00
1 parent 4a73170583
commit a7b0ce4d8f
7 files changed
+36 -63

No files matched your search

-8
View File
@@ -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;
+1 -15
View File
@@ -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<CheckSnapshot, "createdAt"> & { 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<MonitorThresholds>(
{
usage_cpu: { type: Number },
usage_memory: { type: Number },
usage_disk: { type: Number },
usage_temperature: { type: Number },
},
{ _id: false }
);
const checkSnapshotSchema = new Schema<CheckSnapshotDocument>(
{
id: { type: String, required: true },
@@ -132,9 +121,6 @@ const MonitorSchema = new Schema<MonitorDocument>(
secret: {
type: String,
},
thresholds: {
type: thresholdsSchema,
},
alertThreshold: {
type: Number,
default: 5,
@@ -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,
@@ -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);
}
}
@@ -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 || {};
@@ -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
-8
View File
@@ -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;