refactor: implement app directory structure
also fix a bug in alert_formatter.rs
This commit is contained in:
2
node_notifier/src/app.rs
Normal file
2
node_notifier/src/app.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
mod alert_formatter;
|
||||
pub mod service;
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
37
node_notifier/src/app/service.rs
Normal file
37
node_notifier/src/app/service.rs
Normal file
@@ -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");
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user