mirror of
				https://github.com/csehviktor/status-monitor.git
				synced 2025-08-08 18:06:14 +02:00 
			
		
		
		
	update serialization
This commit is contained in:
		| @@ -8,7 +8,7 @@ pub mod metrics; | |||||||
| pub const MQTT_TOPIC: &str = "system/metrics"; | pub const MQTT_TOPIC: &str = "system/metrics"; | ||||||
| pub const MQTT_SEND_INTERVAL: u64 = 5; | pub const MQTT_SEND_INTERVAL: u64 = 5; | ||||||
|  |  | ||||||
| #[derive(Debug, Serialize, Deserialize)] | #[derive(Debug, Clone, Serialize, Deserialize)] | ||||||
| pub struct StatusMessage { | pub struct StatusMessage { | ||||||
|     pub agent: String, |     pub agent: String, | ||||||
|     pub metrics: Metrics, |     pub metrics: Metrics, | ||||||
| @@ -36,3 +36,9 @@ impl<'a> TryFrom<&'a [u8]> for StatusMessage { | |||||||
|         serde_json::from_slice(value) |         serde_json::from_slice(value) | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | impl From<StatusMessage> for String { | ||||||
|  |     fn from(msg: StatusMessage) -> String { | ||||||
|  |         serde_json::to_string(&msg).unwrap_or_else(|_| String::new()) | ||||||
|  |     } | ||||||
|  | } | ||||||
|   | |||||||
| @@ -1,6 +1,6 @@ | |||||||
| use serde::{Deserialize, Serialize}; | use serde::{Deserialize, Serialize}; | ||||||
|  |  | ||||||
| #[derive(Debug, Serialize, Deserialize)] | #[derive(Debug, Clone, Serialize, Deserialize)] | ||||||
| pub struct Metrics { | pub struct Metrics { | ||||||
|     pub system_info: SystemInfo, |     pub system_info: SystemInfo, | ||||||
|     pub cpu: CPU, |     pub cpu: CPU, | ||||||
| @@ -9,7 +9,7 @@ pub struct Metrics { | |||||||
|     pub network: Network, |     pub network: Network, | ||||||
| } | } | ||||||
|  |  | ||||||
| #[derive(Debug, Serialize, Deserialize)] | #[derive(Debug, Clone, Serialize, Deserialize)] | ||||||
| pub struct SystemInfo { | pub struct SystemInfo { | ||||||
|     pub uptime: u64, |     pub uptime: u64, | ||||||
|     pub host: Option<String>, |     pub host: Option<String>, | ||||||
| @@ -18,14 +18,14 @@ pub struct SystemInfo { | |||||||
|     pub os_version: Option<String>, |     pub os_version: Option<String>, | ||||||
| } | } | ||||||
|  |  | ||||||
| #[derive(Debug, Serialize, Deserialize)] | #[derive(Debug, Clone, Serialize, Deserialize)] | ||||||
| pub struct CPU { | pub struct CPU { | ||||||
|     pub usage: f32, |     pub usage: f32, | ||||||
|     pub threads: usize, |     pub threads: usize, | ||||||
|     pub breakdown: CPUBreakdown, |     pub breakdown: CPUBreakdown, | ||||||
| } | } | ||||||
|  |  | ||||||
| #[derive(Debug, Serialize, Deserialize, Default)] | #[derive(Debug, Clone, Serialize, Deserialize, Default)] | ||||||
| pub struct CPUBreakdown { | pub struct CPUBreakdown { | ||||||
|     pub system: f32, |     pub system: f32, | ||||||
|     pub user: f32, |     pub user: f32, | ||||||
| @@ -34,13 +34,13 @@ pub struct CPUBreakdown { | |||||||
|     pub iowait: f32, |     pub iowait: f32, | ||||||
| } | } | ||||||
|  |  | ||||||
| #[derive(Debug, Serialize, Deserialize)] | #[derive(Debug, Clone, Serialize, Deserialize)] | ||||||
| pub struct Disk { | pub struct Disk { | ||||||
|     pub free: u64, |     pub free: u64, | ||||||
|     pub total: u64, |     pub total: u64, | ||||||
| } | } | ||||||
|  |  | ||||||
| #[derive(Debug, Serialize, Deserialize)] | #[derive(Debug, Clone, Serialize, Deserialize)] | ||||||
| pub struct Memory { | pub struct Memory { | ||||||
|     pub used: u64, |     pub used: u64, | ||||||
|     pub total: u64, |     pub total: u64, | ||||||
| @@ -48,7 +48,7 @@ pub struct Memory { | |||||||
|     pub swap_total: u64, |     pub swap_total: u64, | ||||||
| } | } | ||||||
|  |  | ||||||
| #[derive(Debug, Serialize, Deserialize)] | #[derive(Debug, Clone, Serialize, Deserialize)] | ||||||
| pub struct Network { | pub struct Network { | ||||||
|     pub down: u64, |     pub down: u64, | ||||||
|     pub up: u64, |     pub up: u64, | ||||||
|   | |||||||
| @@ -1,7 +1,8 @@ | |||||||
|  | use common::StatusMessage; | ||||||
| use tokio::sync::{mpsc::Sender, Mutex}; | use tokio::sync::{mpsc::Sender, Mutex}; | ||||||
|  |  | ||||||
| pub struct ClientManager { | pub struct ClientManager { | ||||||
|     clients: Mutex<Vec<Sender<String>>>, |     clients: Mutex<Vec<Sender<StatusMessage>>>, | ||||||
| } | } | ||||||
|  |  | ||||||
| impl ClientManager { | impl ClientManager { | ||||||
| @@ -11,12 +12,12 @@ impl ClientManager { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     pub async fn add_client(&self, client: Sender<String>) { |     pub async fn add_client(&self, client: Sender<StatusMessage>) { | ||||||
|         let mut clients = self.clients.lock().await; |         let mut clients = self.clients.lock().await; | ||||||
|         clients.push(client); |         clients.push(client); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     pub async fn broadcast(&self, message: String) { |     pub async fn broadcast(&self, message: &StatusMessage) { | ||||||
|         let mut clients = self.clients.lock().await; |         let mut clients = self.clients.lock().await; | ||||||
|  |  | ||||||
|         clients.retain(|client| { |         clients.retain(|client| { | ||||||
|   | |||||||
| @@ -27,8 +27,7 @@ impl MqttSubscriber { | |||||||
|             if let Notification::Forward(forward) = notification.unwrap() { |             if let Notification::Forward(forward) = notification.unwrap() { | ||||||
|                 let payload = StatusMessage::try_from(&forward.publish.payload[..])?; |                 let payload = StatusMessage::try_from(&forward.publish.payload[..])?; | ||||||
|  |  | ||||||
|                 if let Ok(payload_str) = payload.to_string() { |                 self.clients.broadcast(&payload).await; | ||||||
|                     self.clients.broadcast(payload_str).await; |  | ||||||
|  |  | ||||||
|                     if let Err(e) = self.storage.record_message(&payload.agent).await { |                     if let Err(e) = self.storage.record_message(&payload.agent).await { | ||||||
|                         eprintln!("failed to record message for {}: {}", &payload.agent, e); |                         eprintln!("failed to record message for {}: {}", &payload.agent, e); | ||||||
|   | |||||||
| @@ -26,7 +26,7 @@ impl WebsocketRoutes { | |||||||
|  |  | ||||||
|     async fn handle_ws_connection(websocket: WebSocket, clients: Arc<ClientManager>) { |     async fn handle_ws_connection(websocket: WebSocket, clients: Arc<ClientManager>) { | ||||||
|         let (mut ws_tx, _) = websocket.split(); |         let (mut ws_tx, _) = websocket.split(); | ||||||
|         let (tx, mut rx) = channel::<String>(100); |         let (tx, mut rx) = channel::<StatusMessage>(100); | ||||||
|  |  | ||||||
|         clients.add_client(tx).await; |         clients.add_client(tx).await; | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user