Skip to main content

Mountain/IPC/Common/HealthStatus/
HealthMonitor.rs

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