From 3bb8f39e34d4f155725a0b435102530ebf8a8874 Mon Sep 17 00:00:00 2001 From: JeremyLARDENOIS Date: Sat, 9 Aug 2025 11:21:37 +0200 Subject: [PATCH] refactor: implement app directory structure also fix a bug in alert_formatter.rs --- node_notifier/src/app.rs | 2 + .../src/{ => app}/alert_formatter.rs | 18 +++++---- node_notifier/src/app/service.rs | 37 +++++++++++++++++++ node_notifier/src/main.rs | 18 ++------- 4 files changed, 52 insertions(+), 23 deletions(-) create mode 100644 node_notifier/src/app.rs rename node_notifier/src/{ => app}/alert_formatter.rs (83%) create mode 100644 node_notifier/src/app/service.rs diff --git a/node_notifier/src/app.rs b/node_notifier/src/app.rs new file mode 100644 index 0000000..6338804 --- /dev/null +++ b/node_notifier/src/app.rs @@ -0,0 +1,2 @@ +mod alert_formatter; +pub mod service; diff --git a/node_notifier/src/alert_formatter.rs b/node_notifier/src/app/alert_formatter.rs similarity index 83% rename from node_notifier/src/alert_formatter.rs rename to node_notifier/src/app/alert_formatter.rs index c61fb7b..3981420 100644 --- a/node_notifier/src/alert_formatter.rs +++ b/node_notifier/src/app/alert_formatter.rs @@ -40,13 +40,15 @@ impl AlertFormatter { pub fn format_summary(&self, monitor: &system_monitor::SystemMonitor) -> String { let metrics = monitor.get_metrics(); - - format!( - "📊 **Rapport Système**\n\n```\nCPU: {:.1}%\nMémoire: {:.1}%\nSwap: {:.1}%\nDisque: {:.1}%\n```\n\n*Surveillance automatique*", - metrics.get("CPU").unwrap_or(&0.0), - metrics.get("Memory").unwrap_or(&0.0), - metrics.get("Swap").unwrap_or(&0.0), - metrics.get("Disk").unwrap_or(&0.0) - ) + + let mut result = "📊 **Rapport Système**\n\n```\n".to_string(); + + for (key, value) in &metrics { + result.push_str(&format!("{}: {:.1}%\n", key, value)); + } + + result.push_str("```\n\n*Surveillance automatique*"); + + result } } \ No newline at end of file diff --git a/node_notifier/src/app/service.rs b/node_notifier/src/app/service.rs new file mode 100644 index 0000000..fc0cdbd --- /dev/null +++ b/node_notifier/src/app/service.rs @@ -0,0 +1,37 @@ +use crate::app::alert_formatter; +use discord_client::DiscordNotifier; + +pub struct Service { + notifier: DiscordNotifier, + monitor: system_monitor::SystemMonitor, + formatter: alert_formatter::AlertFormatter, +} + +pub fn new(notifier: DiscordNotifier, monitor: system_monitor::SystemMonitor) -> Service { + let formatter = alert_formatter::AlertFormatter; + + Service { + notifier, + monitor, + formatter, + } +} + +impl Service { + pub fn run(&self) { + // Check for threshold violations and send alerts + let violations = self.monitor.check_thresholds(); + for violation in violations { + let message = self.formatter.format_violation(&violation); + self.notifier + .send_notification(&message) + .expect("Failed to send notification"); + } + + // Send a final notification with all system information + let summary = self.formatter.format_summary(&self.monitor); + self.notifier + .send_notification(&summary) + .expect("Failed to send final notification"); + } +} diff --git a/node_notifier/src/main.rs b/node_notifier/src/main.rs index 63fe979..f43e0ca 100644 --- a/node_notifier/src/main.rs +++ b/node_notifier/src/main.rs @@ -2,7 +2,7 @@ use discord_client::DiscordNotifier; use dotenvy::dotenv; use system_monitor::SystemMonitor; -mod alert_formatter; +mod app; fn main() { dotenv().ok(); @@ -15,20 +15,8 @@ fn main() { let monitor = SystemMonitor::new(system_monitor::resource_threshold::get_default_resource_thresholds()); - let formatter = alert_formatter::AlertFormatter; - // Check for threshold violations and send alerts - let violations = monitor.check_thresholds(); - for violation in violations { - let message = formatter.format_violation(&violation); - notifier - .send_notification(&message) - .expect("Failed to send notification"); - } - // Send a final notification with all system information - let summary = formatter.format_summary(&monitor); - notifier - .send_notification(&summary) - .expect("Failed to send final notification"); + let service = app::service::new(notifier, monitor); + service.run(); }