mirror of
https://github.com/csehviktor/status-monitor.git
synced 2025-08-08 18:06:14 +02:00
cleanup code
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<HashMap<String, UptimeModel>>
|
||||
agents: Mutex<HashMap<String, UptimeStorageModel>>
|
||||
}
|
||||
|
||||
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,
|
||||
|
||||
@@ -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<Utc>,
|
||||
pub last_seen: DateTime<Utc>,
|
||||
pub message_count: u64,
|
||||
}
|
||||
|
||||
impl Into<UptimeMessage> for UptimeModel {
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct UptimeMessage {
|
||||
pub agent: String,
|
||||
pub uptime: f64,
|
||||
pub last_seen: DateTime<Utc>,
|
||||
}
|
||||
|
||||
impl Into<UptimeMessage> 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<UptimeMessage> for UptimeModel {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct UptimeMessage {
|
||||
pub agent: String,
|
||||
pub uptime: f64,
|
||||
pub last_seen: DateTime<Utc>,
|
||||
}
|
||||
|
||||
#[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<Vec<UptimeMessage>>;
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Connection>
|
||||
@@ -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<Utc> = row.get::<_, String>(1)?.parse().unwrap();
|
||||
let last_seen: DateTime<Utc> = 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<Vec<UptimeModel>, _> = result.collect();
|
||||
let models: Result<Vec<UptimeStorageModel>, _> = result.collect();
|
||||
|
||||
Ok(models?.into_iter().map(Into::into).collect())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user