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 {
|
pub fn format_summary(&self, monitor: &system_monitor::SystemMonitor) -> String {
|
||||||
let metrics = monitor.get_metrics();
|
let metrics = monitor.get_metrics();
|
||||||
|
|
||||||
format!(
|
let mut result = "📊 **Rapport Système**\n\n```\n".to_string();
|
||||||
"📊 **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),
|
for (key, value) in &metrics {
|
||||||
metrics.get("Memory").unwrap_or(&0.0),
|
result.push_str(&format!("{}: {:.1}%\n", key, value));
|
||||||
metrics.get("Swap").unwrap_or(&0.0),
|
}
|
||||||
metrics.get("Disk").unwrap_or(&0.0)
|
|
||||||
)
|
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 dotenvy::dotenv;
|
||||||
use system_monitor::SystemMonitor;
|
use system_monitor::SystemMonitor;
|
||||||
|
|
||||||
mod alert_formatter;
|
mod app;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
dotenv().ok();
|
dotenv().ok();
|
||||||
@@ -15,20 +15,8 @@ fn main() {
|
|||||||
|
|
||||||
let monitor =
|
let monitor =
|
||||||
SystemMonitor::new(system_monitor::resource_threshold::get_default_resource_thresholds());
|
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 service = app::service::new(notifier, monitor);
|
||||||
let summary = formatter.format_summary(&monitor);
|
service.run();
|
||||||
notifier
|
|
||||||
.send_notification(&summary)
|
|
||||||
.expect("Failed to send final notification");
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user