Skip to main content

Mountain/IPC/Common/HealthStatus/
HealthMonitor.rs

1#![allow(non_snake_case)]
2
3//! Aggregate health monitor: a 0-100 score, the list of currently
4//! tracked issues (each paired with its severity for fast filtering),
5//! a recovery-attempt counter, and the timestamp of the last update.
6//! `Recalculate` derives the score deterministically from the issue
7//! list using the severity → penalty table:
8//! Low=5, Medium=15, High=25, Critical=40.
9
10use std::time::Instant;
11
12use serde::Serialize;
13
14use crate::IPC::Common::HealthStatus::{HealthIssue, SeverityLevel};
15
16#[derive(Debug, Clone, Serialize)]
17pub struct Struct {
18	pub HealthScore:u8,
19
20	pub Issues:Vec<(HealthIssue::Enum, SeverityLevel::Enum)>,
21
22	pub RecoveryAttempts:u32,
23
24	#[serde(skip)]
25	pub LastCheck:Instant,
26}
27
28impl Default for Struct {
29	fn default() -> Self { Self { HealthScore:100, Issues:Vec::new(), RecoveryAttempts:0, LastCheck:Instant::now() } }
30}
31
32impl Struct {
33	pub fn new() -> Self { Self::default() }
34
35	pub fn AddIssue(&mut self, Issue:HealthIssue::Enum) {
36		let Severity = Issue.Severity();
37
38		self.Issues.push((Issue, Severity));
39
40		self.Recalculate();
41	}
42
43	pub fn RemoveIssue(&mut self, Issue:&HealthIssue::Enum) {
44		self.Issues.retain(|(I, _)| I != Issue);
45
46		self.Recalculate();
47	}
48
49	pub fn ClearIssues(&mut self) {
50		self.Issues.clear();
51
52		self.HealthScore = 100;
53
54		self.LastCheck = Instant::now();
55	}
56
57	fn Recalculate(&mut self) {
58		let mut Score:i32 = 100;
59
60		for (_, Severity) in &self.Issues {
61			Score -= match Severity {
62				SeverityLevel::Enum::Low => 5,
63
64				SeverityLevel::Enum::Medium => 15,
65
66				SeverityLevel::Enum::High => 25,
67
68				SeverityLevel::Enum::Critical => 40,
69			};
70		}
71
72		self.HealthScore = Score.max(0).min(100) as u8;
73
74		self.LastCheck = Instant::now();
75	}
76
77	pub fn IsHealthy(&self) -> bool { self.HealthScore >= 70 }
78
79	pub fn IsCritical(&self) -> bool { self.HealthScore < 50 }
80
81	pub fn IssuesBySeverity(&self, Severity:SeverityLevel::Enum) -> Vec<&HealthIssue::Enum> {
82		self.Issues.iter().filter(|(_, S)| *S == Severity).map(|(I, _)| I).collect()
83	}
84
85	pub fn IncrementRecoveryAttempts(&mut self) { self.RecoveryAttempts += 1; }
86}