thresholds be

This commit is contained in:
Alex Holliday committed 2025-12-05 22:46:46 +00:00
1 parent 2ab983eff5
commit 7631e65cd2
3 files changed
+215 -110

No files matched your search

@@ -9,12 +9,18 @@ import type { ResolutionType } from "@/db/models/index.js";
import mongoose from "mongoose";
import { getChildLogger } from "@/logger/Logger.js";
import ApiError from "@/utils/ApiError.js";
import { ThresholdEvaluationResult } from "@/services/infrastructure/StatusService.js";
export interface IIncidentService {
handleStatusChange: (
updatedMonitor: IMonitor,
lastCheck: ICheck
) => Promise<IIncident | null>;
handleThresholdBreach: (
updatedMonitor: IMonitor,
lastCheck: ICheck,
evalResult: ThresholdEvaluationResult
) => Promise<IIncident | null>;
create: (
teamId: mongoose.Types.ObjectId,
monitorId: mongoose.Types.ObjectId,
@@ -102,6 +108,64 @@ class IncidentService implements IIncidentService {
return null;
};
handleThresholdBreach = async (
updatedMonitor: IMonitor,
lastCheck: ICheck,
evalResult: ThresholdEvaluationResult
) => {
const existing = await Incident.findOne({
monitorId: updatedMonitor._id,
teamId: updatedMonitor.teamId,
resolved: false,
});
if (evalResult.hasBreach) {
if (!existing) {
const incident = await this.create(
updatedMonitor.teamId,
updatedMonitor._id,
lastCheck._id
);
if (evalResult.notes.length) {
await Incident.updateOne(
{ _id: incident._id },
{
$set: {
resolutionNote: `threshold breach: ${evalResult.notes.join(
", "
)}`,
},
}
);
}
return incident;
}
if (evalResult.notes.length) {
await Incident.updateOne(
{ _id: existing._id },
{
$set: {
resolutionNote: `threshold breach: ${evalResult.notes.join(
", "
)}`,
},
}
);
}
return existing;
} else if (existing) {
const resolved = await this.resolve(
updatedMonitor.teamId.toString(),
existing._id.toString(),
"auto",
lastCheck
);
return resolved;
}
return null;
};
create = async (
teamId: mongoose.Types.ObjectId,
monitorId: mongoose.Types.ObjectId,