diff --git a/server/src/broker/subscriber.rs b/server/src/broker/subscriber.rs index e99b244..3831cd3 100644 --- a/server/src/broker/subscriber.rs +++ b/server/src/broker/subscriber.rs @@ -29,9 +29,8 @@ impl MqttSubscriber { self.clients.broadcast(&payload).await; - if let Err(e) = self.storage.record_message(&payload.agent).await { - eprintln!("failed to record message for {}: {}", &payload.agent, e); - } + if let Err(e) = self.storage.record_uptime(&payload.agent).await { + eprintln!("failed to record uptime for {}: {}", &payload.agent, e); } } } diff --git a/server/src/storage/memory.rs b/server/src/storage/memory.rs index 97bb138..30bd035 100644 --- a/server/src/storage/memory.rs +++ b/server/src/storage/memory.rs @@ -3,10 +3,10 @@ use chrono::Utc; use tokio::sync::Mutex; use async_trait::async_trait; -use super::{StorageRepository, UptimeMessage, UptimeModel}; +use super::{StorageRepository, UptimeMessage, UptimeStorageModel}; pub struct InMemoryRepository { - agents: Mutex> + agents: Mutex> } impl InMemoryRepository { @@ -19,7 +19,7 @@ impl InMemoryRepository { #[async_trait] impl StorageRepository for InMemoryRepository { - async fn record_message(&self, agent: &str) -> anyhow::Result<()> { + async fn record_uptime(&self, agent: &str) -> anyhow::Result<()> { let mut agents = self.agents.lock().await; let now = Utc::now(); @@ -28,7 +28,7 @@ impl StorageRepository for InMemoryRepository { a.last_seen = now; a.message_count += 1; }) - .or_insert_with(|| UptimeModel { + .or_insert_with(|| UptimeStorageModel { id: agent.to_string(), first_seen: now, last_seen: now, diff --git a/server/src/storage/mod.rs b/server/src/storage/mod.rs index 1023c3e..07a8a72 100644 --- a/server/src/storage/mod.rs +++ b/server/src/storage/mod.rs @@ -10,14 +10,21 @@ pub mod memory; pub mod sqlite; #[derive(Debug, Clone, Serialize, Deserialize)] -pub struct UptimeModel { +pub struct UptimeStorageModel { pub id: String, pub first_seen: DateTime, pub last_seen: DateTime, pub message_count: u64, } -impl Into for UptimeModel { +#[derive(Debug, Serialize, Deserialize)] +pub struct UptimeMessage { + pub agent: String, + pub uptime: f64, + pub last_seen: DateTime, +} + +impl Into for UptimeStorageModel { fn into(self) -> UptimeMessage { let duration = Utc::now().signed_duration_since(self.first_seen); let expected_messages = duration.num_seconds() as f64 / MQTT_SEND_INTERVAL as f64; @@ -32,16 +39,9 @@ impl Into for UptimeModel { } } -#[derive(Debug, Serialize, Deserialize)] -pub struct UptimeMessage { - pub agent: String, - pub uptime: f64, - pub last_seen: DateTime, -} - #[async_trait] pub trait StorageRepository: Send + Sync { - async fn record_message(&self, agent: &str) -> anyhow::Result<()>; + async fn record_uptime(&self, agent: &str) -> anyhow::Result<()>; async fn get_agents(&self) -> anyhow::Result>; } diff --git a/server/src/storage/sqlite.rs b/server/src/storage/sqlite.rs index c3ff57c..2785660 100644 --- a/server/src/storage/sqlite.rs +++ b/server/src/storage/sqlite.rs @@ -4,7 +4,7 @@ use tokio::sync::Mutex; use std::path::Path; use chrono::{DateTime, Utc}; -use super::{StorageRepository, UptimeMessage, UptimeModel}; +use super::{StorageRepository, UptimeMessage, UptimeStorageModel}; pub struct SQLiteRepository { conn: Mutex @@ -40,7 +40,7 @@ impl SQLiteRepository { #[async_trait] impl StorageRepository for SQLiteRepository { - async fn record_message(&self, agent: &str) -> anyhow::Result<()> { + async fn record_uptime(&self, agent: &str) -> anyhow::Result<()> { let conn = self.conn.lock().await; let now = Utc::now().to_rfc3339(); @@ -64,7 +64,7 @@ impl StorageRepository for SQLiteRepository { let first_seen: DateTime = row.get::<_, String>(1)?.parse().unwrap(); let last_seen: DateTime = row.get::<_, String>(2)?.parse().unwrap(); - Ok(UptimeModel { + Ok(UptimeStorageModel { id: row.get(0)?, first_seen, last_seen, @@ -72,7 +72,7 @@ impl StorageRepository for SQLiteRepository { }) })?; - let models: Result, _> = result.collect(); + let models: Result, _> = result.collect(); Ok(models?.into_iter().map(Into::into).collect()) }