From 93b9abd5082c3237b6665ae320e0b42e1949eefc Mon Sep 17 00:00:00 2001 From: gorkem-bwl Date: Wed, 4 Mar 2026 15:38:34 -0500 Subject: [PATCH] fix: add migration to reset infrastructure thresholds from 5 to 100 ## Changes - Added migration 0004_fixInfrastructureThresholds that finds hardware monitors with threshold values of 5 (the old incorrect default) and updates them to 100 (effectively disabled) - Runs automatically on server startup like other migrations ## Closes Fixes #3327 --- .../0004_fixInfrastructureThresholds.ts | 65 +++++++++++++++++++ server/src/db/migration/index.ts | 2 + 2 files changed, 67 insertions(+) create mode 100644 server/src/db/migration/0004_fixInfrastructureThresholds.ts diff --git a/server/src/db/migration/0004_fixInfrastructureThresholds.ts b/server/src/db/migration/0004_fixInfrastructureThresholds.ts new file mode 100644 index 000000000..041f8a9dc --- /dev/null +++ b/server/src/db/migration/0004_fixInfrastructureThresholds.ts @@ -0,0 +1,65 @@ +import { MonitorModel } from "../models/Monitor.js"; +import { logger } from "@/utils/logger.js"; + +/** + * Fix infrastructure monitors that had thresholds incorrectly set to 5 + * due to the old default value. Updates them to 100 (disabled). + */ +export async function fixInfrastructureThresholds(): Promise { + const SERVICE_NAME = "Migration:FixInfrastructureThresholds"; + + try { + logger.info({ service: SERVICE_NAME, message: "Starting infrastructure threshold fix" }); + + const thresholdFields = [ + "cpuAlertThreshold", + "memoryAlertThreshold", + "diskAlertThreshold", + "tempAlertThreshold", + ]; + + // Find hardware monitors with any threshold set to 5 + const filter = { + type: "hardware", + $or: thresholdFields.map((field) => ({ [field]: 5 })), + }; + + const monitors = await MonitorModel.find(filter); + + if (monitors.length === 0) { + logger.info({ service: SERVICE_NAME, message: "No monitors with threshold value of 5 found" }); + return; + } + + logger.info({ + service: SERVICE_NAME, + message: `Found ${monitors.length} monitors with threshold value of 5`, + }); + + let updatedCount = 0; + + for (const monitor of monitors) { + const update: Record = {}; + + for (const field of thresholdFields) { + if ((monitor as any)[field] === 5) { + update[field] = 100; + } + } + + if (Object.keys(update).length > 0) { + await MonitorModel.updateOne({ _id: monitor._id }, { $set: update }); + updatedCount++; + } + } + + logger.info({ + service: SERVICE_NAME, + message: `Fixed ${updatedCount} monitors — thresholds updated from 5 to 100`, + }); + } catch (error) { + const errorMessage = error instanceof Error ? error.message : String(error); + logger.error({ service: SERVICE_NAME, message: `Error fixing thresholds: ${errorMessage}` }); + throw error; + } +} diff --git a/server/src/db/migration/index.ts b/server/src/db/migration/index.ts index 8ea374514..3b7b45ada 100644 --- a/server/src/db/migration/index.ts +++ b/server/src/db/migration/index.ts @@ -1,6 +1,7 @@ import { migrateStatusWindowThreshold } from "./0001_migrateStatusWindowThreshold.js"; import { convertChecksToTimeSeries } from "./0002_convertChecksToTimeSeries.js"; import { cleanupDuplicateMonitorStats } from "./0003_cleanupDuplicateMonitorStats.js"; +import { fixInfrastructureThresholds } from "./0004_fixInfrastructureThresholds.js"; import MigrationModel from "../models/Migration.js"; type MigrationEntry = { @@ -12,6 +13,7 @@ const migrations: MigrationEntry[] = [ { name: "0001_migrateStatusWindowThreshold", execute: migrateStatusWindowThreshold }, { name: "0002_convertChecksToTimeSeries", execute: convertChecksToTimeSeries }, { name: "0003_cleanupDuplicateMonitorStats", execute: cleanupDuplicateMonitorStats }, + { name: "0004_fixInfrastructureThresholds", execute: fixInfrastructureThresholds }, ]; const runMigrations = async (logger?: { info: Function; error: Function }) => {