mirror of
				https://github.com/csehviktor/status-monitor.git
				synced 2025-08-08 18:06:14 +02:00 
			
		
		
		
	cleaup code
This commit is contained in:
		@@ -1,2 +1,37 @@
 | 
			
		||||
use chrono::{DateTime, Utc};
 | 
			
		||||
use serde::{Deserialize, Serialize};
 | 
			
		||||
use serde_json::Error;
 | 
			
		||||
use crate::metrics::Metrics;
 | 
			
		||||
 | 
			
		||||
pub mod metrics;
 | 
			
		||||
pub mod mqtt;
 | 
			
		||||
 | 
			
		||||
pub const MQTT_TOPIC: &str = "system/metrics";
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Serialize, Deserialize)]
 | 
			
		||||
pub struct StatusMessage<'a> {
 | 
			
		||||
    pub agent: &'a str,
 | 
			
		||||
    pub metrics: Metrics,
 | 
			
		||||
    pub timestamp: DateTime<Utc>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<'a> StatusMessage<'a> {
 | 
			
		||||
    pub fn new(agent: &'a str, metrics: Metrics) -> Self {
 | 
			
		||||
        Self {
 | 
			
		||||
            agent,
 | 
			
		||||
            metrics,
 | 
			
		||||
            timestamp: Utc::now(),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn to_string(&self) -> Result<String, Error> {
 | 
			
		||||
        serde_json::to_string_pretty(&self)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<'a> TryFrom<&'a [u8]> for StatusMessage<'a> {
 | 
			
		||||
    type Error = serde_json::Error;
 | 
			
		||||
 | 
			
		||||
    fn try_from(value: &'a [u8]) -> Result<Self, Self::Error> {
 | 
			
		||||
        serde_json::from_slice(value)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user