1use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct HealthStatus {
9 pub timestamp: u64,
11
12 pub level: HealthLevel,
14
15 pub message: String,
17
18 #[serde(skip_serializing_if = "Option::is_none")]
20 pub diagnostics: Option<HashMap<String, String>>,
21}
22
23impl HealthStatus {
24 pub fn healthy() -> Self {
26 Self {
27 timestamp: crate::prelude::now_micros(),
28 level: HealthLevel::Ok,
29 message: "Operating normally".to_string(),
30 diagnostics: None,
31 }
32 }
33
34 pub fn degraded(message: impl Into<String>) -> Self {
36 Self {
37 timestamp: crate::prelude::now_micros(),
38 level: HealthLevel::Degraded,
39 message: message.into(),
40 diagnostics: None,
41 }
42 }
43
44 pub fn unhealthy(message: impl Into<String>) -> Self {
46 Self {
47 timestamp: crate::prelude::now_micros(),
48 level: HealthLevel::Error,
49 message: message.into(),
50 diagnostics: None,
51 }
52 }
53
54 pub fn with_diagnostic(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
56 self.diagnostics
57 .get_or_insert_with(HashMap::new)
58 .insert(key.into(), value.into());
59 self
60 }
61}
62
63#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
65pub enum HealthLevel {
66 Ok,
68
69 Degraded,
71
72 Error,
74
75 Unknown,
77}
78
79#[derive(Debug, Clone, Serialize, Deserialize)]
81pub struct NodeStatus {
82 pub timestamp: u64,
84
85 pub name: String,
87
88 pub state: NodeState,
90
91 pub uptime: f64,
93
94 #[serde(skip_serializing_if = "Option::is_none")]
96 pub cpu_usage: Option<f32>,
97
98 #[serde(skip_serializing_if = "Option::is_none")]
100 pub memory_usage: Option<u64>,
101
102 #[serde(skip_serializing_if = "Option::is_none")]
104 pub message_rate: Option<f32>,
105
106 pub health: HealthStatus,
108}
109
110#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
112pub enum NodeState {
113 Starting,
115
116 Running,
118
119 Paused,
121
122 Stopping,
124
125 Stopped,
127
128 Crashed,
130}
131
132#[derive(Debug, Clone, Serialize, Deserialize)]
136pub struct SystemCommand {
137 pub timestamp: u64,
139
140 pub target: String,
142
143 pub command: Command,
145}
146
147#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
149pub enum Command {
150 Start,
152
153 Stop,
155
156 Pause,
158
159 Resume,
161
162 Restart,
164
165 HealthCheck,
167
168 ReloadConfig,
170
171 Shutdown,
173}
174
175impl SystemCommand {
176 pub fn start(target: impl Into<String>) -> Self {
178 Self {
179 timestamp: crate::prelude::now_micros(),
180 target: target.into(),
181 command: Command::Start,
182 }
183 }
184
185 pub fn stop(target: impl Into<String>) -> Self {
187 Self {
188 timestamp: crate::prelude::now_micros(),
189 target: target.into(),
190 command: Command::Stop,
191 }
192 }
193
194 pub fn restart(target: impl Into<String>) -> Self {
196 Self {
197 timestamp: crate::prelude::now_micros(),
198 target: target.into(),
199 command: Command::Restart,
200 }
201 }
202
203 pub fn health_check(target: impl Into<String>) -> Self {
205 Self {
206 timestamp: crate::prelude::now_micros(),
207 target: target.into(),
208 command: Command::HealthCheck,
209 }
210 }
211}