simplify hardware changes
This commit is contained in:
1 parent
d74f1bb6a8
commit
bf60c52692
1 file changed
+20
-26
@@ -164,7 +164,7 @@ export class StatusService implements IStatusService {
|
|||||||
private computeHardwareStatus = (params: {
|
private computeHardwareStatus = (params: {
|
||||||
currentStatus: MonitorStatus;
|
currentStatus: MonitorStatus;
|
||||||
reachabilityDown: boolean;
|
reachabilityDown: boolean;
|
||||||
metrics: HardwareStatusMetrics | undefined;
|
metrics: HardwareStatusMetrics;
|
||||||
thresholds: { cpu: number; memory: number; disk: number; temp: number };
|
thresholds: { cpu: number; memory: number; disk: number; temp: number };
|
||||||
counters: HardwareCounters;
|
counters: HardwareCounters;
|
||||||
}): {
|
}): {
|
||||||
@@ -172,9 +172,8 @@ export class StatusService implements IStatusService {
|
|||||||
transitioned: boolean;
|
transitioned: boolean;
|
||||||
breaches: HardwareBreaches;
|
breaches: HardwareBreaches;
|
||||||
nextCounters: HardwareCounters;
|
nextCounters: HardwareCounters;
|
||||||
} | null => {
|
} => {
|
||||||
const { metrics, thresholds, counters, currentStatus, reachabilityDown } = params;
|
const { metrics, thresholds, counters, currentStatus, reachabilityDown } = params;
|
||||||
if (!metrics) return null;
|
|
||||||
|
|
||||||
const cpuUsage = metrics.cpu?.usage_percent ?? -1;
|
const cpuUsage = metrics.cpu?.usage_percent ?? -1;
|
||||||
const memoryUsage = metrics.memory?.usage_percent ?? -1;
|
const memoryUsage = metrics.memory?.usage_percent ?? -1;
|
||||||
@@ -195,24 +194,21 @@ export class StatusService implements IStatusService {
|
|||||||
nextCounters[key] = breaches[key] ? Math.max(0, counters[key] - 1) : HARDWARE_ALERT_COUNTER_START;
|
nextCounters[key] = breaches[key] ? Math.max(0, counters[key] - 1) : HARDWARE_ALERT_COUNTER_START;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Status transition: reachability "down" takes precedence; hardware can only act when reachable.
|
// Status transition: reachability "down" takes precedence; hardware can only drive
|
||||||
|
// transitions into "breached" and back out to "up". Any other case leaves status untouched.
|
||||||
let nextStatus: MonitorStatus = currentStatus;
|
let nextStatus: MonitorStatus = currentStatus;
|
||||||
let transitioned = false;
|
let transitioned = false;
|
||||||
|
|
||||||
if (!reachabilityDown) {
|
if (!reachabilityDown) {
|
||||||
|
// A counter can only reach zero via the decrement path, which only runs when that
|
||||||
|
// metric is currently breaching — so anyCounterZero already implies anyBreached.
|
||||||
const anyCounterZero = HARDWARE_METRIC_KEYS.some((k) => nextCounters[k] === 0);
|
const anyCounterZero = HARDWARE_METRIC_KEYS.some((k) => nextCounters[k] === 0);
|
||||||
const anyBreached = HARDWARE_METRIC_KEYS.some((k) => breaches[k]);
|
const allNormal = HARDWARE_METRIC_KEYS.every((k) => !breaches[k]);
|
||||||
const allNormal = !anyBreached;
|
|
||||||
|
|
||||||
if (anyCounterZero && anyBreached && currentStatus !== "breached") {
|
if (anyCounterZero && currentStatus !== "breached") {
|
||||||
// Initial breach: sustained N consecutive breaches, flip to 'breached'.
|
|
||||||
nextStatus = "breached";
|
nextStatus = "breached";
|
||||||
transitioned = true;
|
transitioned = true;
|
||||||
} else if (anyCounterZero && anyBreached && currentStatus === "breached") {
|
|
||||||
// Already breached; stay breached without marking as a transition.
|
|
||||||
nextStatus = "breached";
|
|
||||||
} else if (allNormal && currentStatus === "breached") {
|
} else if (allNormal && currentStatus === "breached") {
|
||||||
// All thresholds back to normal: recover from breached state.
|
|
||||||
nextStatus = "up";
|
nextStatus = "up";
|
||||||
transitioned = true;
|
transitioned = true;
|
||||||
}
|
}
|
||||||
@@ -272,14 +268,14 @@ export class StatusService implements IStatusService {
|
|||||||
statusChanged = true;
|
statusChanged = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Evaluate hardware threshold breaches
|
// Evaluate hardware threshold breaches (only for hardware monitors with metrics payload)
|
||||||
let thresholdBreaches: HardwareBreaches | undefined;
|
let thresholdBreaches: HardwareBreaches | undefined;
|
||||||
if (monitor.type === "hardware" && statusResponse.payload) {
|
const hardwarePayload = statusResponse.payload as HardwareStatusPayload | undefined;
|
||||||
const payload = statusResponse.payload as HardwareStatusPayload;
|
if (monitor.type === "hardware" && hardwarePayload?.data) {
|
||||||
const hardware = this.computeHardwareStatus({
|
const hardware = this.computeHardwareStatus({
|
||||||
currentStatus: monitor.status,
|
currentStatus: monitor.status,
|
||||||
reachabilityDown: newStatus === "down",
|
reachabilityDown: newStatus === "down",
|
||||||
metrics: payload.data,
|
metrics: hardwarePayload.data,
|
||||||
thresholds: {
|
thresholds: {
|
||||||
cpu: monitor.cpuAlertThreshold,
|
cpu: monitor.cpuAlertThreshold,
|
||||||
memory: monitor.memoryAlertThreshold,
|
memory: monitor.memoryAlertThreshold,
|
||||||
@@ -294,16 +290,14 @@ export class StatusService implements IStatusService {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (hardware) {
|
monitor.cpuAlertCounter = hardware.nextCounters.cpu;
|
||||||
monitor.cpuAlertCounter = hardware.nextCounters.cpu;
|
monitor.memoryAlertCounter = hardware.nextCounters.memory;
|
||||||
monitor.memoryAlertCounter = hardware.nextCounters.memory;
|
monitor.diskAlertCounter = hardware.nextCounters.disk;
|
||||||
monitor.diskAlertCounter = hardware.nextCounters.disk;
|
monitor.tempAlertCounter = hardware.nextCounters.temp;
|
||||||
monitor.tempAlertCounter = hardware.nextCounters.temp;
|
thresholdBreaches = hardware.breaches;
|
||||||
thresholdBreaches = hardware.breaches;
|
if (hardware.transitioned) {
|
||||||
if (hardware.transitioned) {
|
newStatus = hardware.nextStatus;
|
||||||
newStatus = hardware.nextStatus;
|
statusChanged = true;
|
||||||
statusChanged = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user