47 lines
1.5 KiB
TypeScript
47 lines
1.5 KiB
TypeScript
import { StatusChangeResult } from "@/types/network.js";
|
|
import { MonitorActionDecision } from "@/worker/worker.helper.js";
|
|
|
|
export interface IMonitorStatusPolicy {
|
|
evaluate(statusChange: StatusChangeResult): MonitorActionDecision;
|
|
}
|
|
|
|
export class MonitorStatusPolicy implements IMonitorStatusPolicy {
|
|
evaluate = (statusChangeResult: StatusChangeResult): MonitorActionDecision => {
|
|
const { monitor, statusChanged, prevStatus } = statusChangeResult;
|
|
|
|
// Initialize result
|
|
const decision: MonitorActionDecision = {
|
|
shouldCreateIncident: false,
|
|
shouldResolveIncident: false,
|
|
shouldSendNotification: false,
|
|
incidentReason: null,
|
|
notificationReason: null,
|
|
};
|
|
|
|
if (!statusChanged) {
|
|
return decision;
|
|
}
|
|
|
|
if (monitor.status === "down") {
|
|
// Monitor went down (unreachable)
|
|
decision.shouldCreateIncident = true;
|
|
decision.shouldSendNotification = true;
|
|
decision.incidentReason = "status_down";
|
|
decision.notificationReason = "status_change";
|
|
} else if (monitor.status === "breached") {
|
|
// Hardware monitor exceeded thresholds
|
|
decision.shouldCreateIncident = true;
|
|
decision.shouldSendNotification = true;
|
|
decision.incidentReason = "threshold_breach";
|
|
decision.notificationReason = "threshold_breach";
|
|
} else if (monitor.status === "up" && (prevStatus === "down" || prevStatus === "breached")) {
|
|
// Monitor recovered from down or breached state
|
|
decision.shouldResolveIncident = true;
|
|
decision.shouldSendNotification = true;
|
|
decision.notificationReason = "status_change";
|
|
}
|
|
|
|
return decision;
|
|
};
|
|
}
|