diff --git a/server/src/app.ts b/server/src/app.ts index fea81ba07..58684532c 100644 --- a/server/src/app.ts +++ b/server/src/app.ts @@ -74,8 +74,22 @@ export const createApp = ({ }, }) ); - // Swagger UI - app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(openApiSpec)); + // Swagger UI — dynamically set server URL from request + app.use("/api-docs", swaggerUi.serve, (req: express.Request, res: express.Response, next: express.NextFunction) => { + const protocol = req.protocol; + const host = req.get("host"); + const dynamicSpec = { + ...openApiSpec, + servers: [ + { + url: `${protocol}://${host}/api/v1`, + description: "Current Server", + }, + ...openApiSpec.servers, + ], + }; + swaggerUi.setup(dynamicSpec)(req, res, next); + }); app.use("/api/v1/health", (req, res) => { res.json({ diff --git a/server/src/db/migration/0004_fixInfrastructureThresholds.ts b/server/src/db/migration/0004_fixInfrastructureThresholds.ts new file mode 100644 index 000000000..6d369f311 --- /dev/null +++ b/server/src/db/migration/0004_fixInfrastructureThresholds.ts @@ -0,0 +1,60 @@ +import { MonitorModel } from "../models/Monitor.js"; +import { logger } from "@/utils/logger.js"; + +/** + * Fix infrastructure monitors that had thresholds incorrectly set to 0 or 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 0 or 5 + const filter = { + type: "hardware", + $or: thresholdFields.flatMap((field) => [{ [field]: 0 }, { [field]: 5 }]), + }; + + const monitors = await MonitorModel.find(filter); + + if (monitors.length === 0) { + logger.info({ service: SERVICE_NAME, message: "No monitors with threshold value of 0 or 5 found" }); + return; + } + + logger.info({ + service: SERVICE_NAME, + message: `Found ${monitors.length} monitors with threshold value of 0 or 5`, + }); + + let updatedCount = 0; + + for (const monitor of monitors) { + const update: Record = {}; + + for (const field of thresholdFields) { + if ((monitor as any)[field] === 0 || (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 0/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 }) => {