incident cleanup

This commit is contained in:
Alex Holliday committed 2025-11-21 20:18:34 +00:00
1 parent 1683c2980e
commit 9bce91c20d
4 files changed
+29 -5

No files matched your search

+2 -2
View File
@@ -37,7 +37,7 @@ export const IncidentTable = ({
id: "name", id: "name",
content: t("incidentsTableMonitorName"), content: t("incidentsTableMonitorName"),
render: (row) => { render: (row) => {
return row.monitorId.name || "N/A"; return row.monitorId?.name || "N/A";
}, },
}, },
{ {
@@ -70,7 +70,7 @@ export const IncidentTable = ({
render: (row) => { render: (row) => {
return ( return (
<Typography textTransform={"capitalize"} color="textPrimary"> <Typography textTransform={"capitalize"} color="textPrimary">
{row.monitorId.status} {row.monitorId?.status}
</Typography> </Typography>
); );
}, },
+3 -1
View File
@@ -1,5 +1,5 @@
import mongoose, { Schema, Document, Types } from "mongoose"; import mongoose, { Schema, Document, Types } from "mongoose";
import { Check, MonitorStats } from "@/db/models/index.js"; import { Check, Incident, MonitorStats } from "@/db/models/index.js";
export const MonitorTypes = [ export const MonitorTypes = [
"http", "http",
@@ -99,6 +99,7 @@ MonitorSchema.pre(
const monitorId = this._id; const monitorId = this._id;
await Check.deleteMany({ "metadata.monitorId": monitorId }); await Check.deleteMany({ "metadata.monitorId": monitorId });
await MonitorStats.deleteMany({ monitorId }); await MonitorStats.deleteMany({ monitorId });
await Incident.deleteMany({ monitorId });
next(); next();
} catch (error: any) { } catch (error: any) {
next(error); next(error);
@@ -117,6 +118,7 @@ MonitorSchema.pre(
if (monitorIds.length > 0) { if (monitorIds.length > 0) {
await Check.deleteMany({ "metadata.monitorId": { $in: monitorIds } }); await Check.deleteMany({ "metadata.monitorId": { $in: monitorIds } });
await MonitorStats.deleteMany({ monitorId: { $in: monitorIds } }); await MonitorStats.deleteMany({ monitorId: { $in: monitorIds } });
await Incident.deleteMany({ monitorId: { $in: monitorIds } });
} }
next(); next();
} catch (error: any) { } catch (error: any) {
@@ -1,4 +1,10 @@
import { IIncident, Incident, IMonitor, ICheck } from "@/db/models/index.js"; import {
IIncident,
Incident,
IMonitor,
Monitor,
ICheck,
} from "@/db/models/index.js";
import type { ResolutionType } from "@/db/models/index.js"; import type { ResolutionType } from "@/db/models/index.js";
import mongoose from "mongoose"; import mongoose from "mongoose";
import { getChildLogger } from "@/logger/Logger.js"; import { getChildLogger } from "@/logger/Logger.js";
@@ -39,10 +45,11 @@ export interface IIncidentService {
teamId: mongoose.Types.ObjectId, teamId: mongoose.Types.ObjectId,
incidentId: mongoose.Types.ObjectId incidentId: mongoose.Types.ObjectId
) => Promise<boolean>; ) => Promise<boolean>;
cleanupOrphanedIncidents: () => Promise<boolean>;
} }
const SERVICE_NAME = "IncidentService"; const SERVICE_NAME = "IncidentService";
const logger = getChildLogger(SERVICE_NAME);
class IncidentService implements IIncidentService { class IncidentService implements IIncidentService {
public SERVICE_NAME: string; public SERVICE_NAME: string;
constructor() { constructor() {
@@ -199,6 +206,20 @@ class IncidentService implements IIncidentService {
}); });
return result.deletedCount === 1; return result.deletedCount === 1;
}; };
async cleanupOrphanedIncidents() {
try {
const monitorIds = await Monitor.find().distinct("_id");
const result = await Incident.deleteMany({
monitorId: { $nin: monitorIds },
});
logger.info(`Deleted ${result.deletedCount} orphaned Incidents.`);
return true;
} catch (error) {
logger.error("Error cleaning up orphaned Incidents:", error);
return false;
}
}
} }
export default IncidentService; export default IncidentService;
@@ -98,6 +98,7 @@ class JobGenerator implements IJobGenerator {
try { try {
await this.checkService.cleanupOrphanedChecks(); await this.checkService.cleanupOrphanedChecks();
await this.monitorStatsService.cleanupOrphanedMonitorStats(); await this.monitorStatsService.cleanupOrphanedMonitorStats();
await this.incidentService.cleanupOrphanedIncidents();
} catch (error) { } catch (error) {
throw error; throw error;
} }