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",
content: t("incidentsTableMonitorName"),
render: (row) => {
return row.monitorId.name || "N/A";
return row.monitorId?.name || "N/A";
},
},
{
@@ -70,7 +70,7 @@ export const IncidentTable = ({
render: (row) => {
return (
<Typography textTransform={"capitalize"} color="textPrimary">
{row.monitorId.status}
{row.monitorId?.status}
</Typography>
);
},
+3 -1
View File
@@ -1,5 +1,5 @@
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 = [
"http",
@@ -99,6 +99,7 @@ MonitorSchema.pre(
const monitorId = this._id;
await Check.deleteMany({ "metadata.monitorId": monitorId });
await MonitorStats.deleteMany({ monitorId });
await Incident.deleteMany({ monitorId });
next();
} catch (error: any) {
next(error);
@@ -117,6 +118,7 @@ MonitorSchema.pre(
if (monitorIds.length > 0) {
await Check.deleteMany({ "metadata.monitorId": { $in: monitorIds } });
await MonitorStats.deleteMany({ monitorId: { $in: monitorIds } });
await Incident.deleteMany({ monitorId: { $in: monitorIds } });
}
next();
} 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 mongoose from "mongoose";
import { getChildLogger } from "@/logger/Logger.js";
@@ -39,10 +45,11 @@ export interface IIncidentService {
teamId: mongoose.Types.ObjectId,
incidentId: mongoose.Types.ObjectId
) => Promise<boolean>;
cleanupOrphanedIncidents: () => Promise<boolean>;
}
const SERVICE_NAME = "IncidentService";
const logger = getChildLogger(SERVICE_NAME);
class IncidentService implements IIncidentService {
public SERVICE_NAME: string;
constructor() {
@@ -199,6 +206,20 @@ class IncidentService implements IIncidentService {
});
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;
@@ -98,6 +98,7 @@ class JobGenerator implements IJobGenerator {
try {
await this.checkService.cleanupOrphanedChecks();
await this.monitorStatsService.cleanupOrphanedMonitorStats();
await this.incidentService.cleanupOrphanedIncidents();
} catch (error) {
throw error;
}