mecha10_core/messages/
system.rs

1// System Message Types
2
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6/// Health status of a node or system
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct HealthStatus {
9    /// Timestamp in microseconds since epoch
10    pub timestamp: u64,
11
12    /// Overall health level
13    pub level: HealthLevel,
14
15    /// Human-readable status message
16    pub message: String,
17
18    /// Additional diagnostic data (key-value pairs)
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub diagnostics: Option<HashMap<String, String>>,
21}
22
23impl HealthStatus {
24    /// Create a healthy status
25    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    /// Create a degraded status with a message
35    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    /// Create an unhealthy status with a message
45    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    /// Add diagnostic data
55    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/// Health level
64#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
65pub enum HealthLevel {
66    /// Everything is functioning normally
67    Ok,
68
69    /// Operating but with reduced performance
70    Degraded,
71
72    /// Not functioning properly
73    Error,
74
75    /// Cannot determine health status
76    Unknown,
77}
78
79/// Node status information
80#[derive(Debug, Clone, Serialize, Deserialize)]
81pub struct NodeStatus {
82    /// Timestamp in microseconds since epoch
83    pub timestamp: u64,
84
85    /// Node name
86    pub name: String,
87
88    /// Current state
89    pub state: NodeState,
90
91    /// Uptime in seconds
92    pub uptime: f64,
93
94    /// CPU usage (0.0 to 1.0)
95    #[serde(skip_serializing_if = "Option::is_none")]
96    pub cpu_usage: Option<f32>,
97
98    /// Memory usage in bytes
99    #[serde(skip_serializing_if = "Option::is_none")]
100    pub memory_usage: Option<u64>,
101
102    /// Message throughput (messages/second)
103    #[serde(skip_serializing_if = "Option::is_none")]
104    pub message_rate: Option<f32>,
105
106    /// Health status
107    pub health: HealthStatus,
108}
109
110/// Node execution state
111#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
112pub enum NodeState {
113    /// Node is starting up
114    Starting,
115
116    /// Node is running normally
117    Running,
118
119    /// Node is paused
120    Paused,
121
122    /// Node is shutting down
123    Stopping,
124
125    /// Node has stopped
126    Stopped,
127
128    /// Node has crashed
129    Crashed,
130}
131
132/// System command
133///
134/// Commands to control node execution
135#[derive(Debug, Clone, Serialize, Deserialize)]
136pub struct SystemCommand {
137    /// Timestamp in microseconds since epoch
138    pub timestamp: u64,
139
140    /// Target node name (or "*" for all nodes)
141    pub target: String,
142
143    /// Command to execute
144    pub command: Command,
145}
146
147/// Command types
148#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
149pub enum Command {
150    /// Start the node
151    Start,
152
153    /// Stop the node gracefully
154    Stop,
155
156    /// Pause execution
157    Pause,
158
159    /// Resume execution
160    Resume,
161
162    /// Restart the node
163    Restart,
164
165    /// Request health check
166    HealthCheck,
167
168    /// Reload configuration
169    ReloadConfig,
170
171    /// Shutdown the runtime
172    Shutdown,
173}
174
175impl SystemCommand {
176    /// Create a start command
177    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    /// Create a stop command
186    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    /// Create a restart command
195    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    /// Create a health check request
204    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}