Files
node-notifier/src/app/service.rs

43 lines
1.2 KiB
Rust

use crate::app::ports::driven::{
ForFormattingMessage, ForMonitoringSystem, ForSendingNotification,
};
pub struct Service {
notifier: Box<dyn ForSendingNotification>,
monitor: Box<dyn ForMonitoringSystem>,
formatter: Box<dyn ForFormattingMessage>,
}
pub fn new(
notifier: Box<dyn ForSendingNotification>,
monitor: Box<dyn ForMonitoringSystem>,
formatter: Box<dyn ForFormattingMessage>,
) -> Service {
Service {
notifier,
monitor,
formatter,
}
}
impl Service {
pub fn alert_on_threshold_violation(&self) -> Result<(), Box<dyn std::error::Error>> {
// 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)?;
}
Ok(())
}
pub fn send_all_metrics_notification(&self) -> Result<(), Box<dyn std::error::Error>> {
// Send a final notification with all system information
let summary = self.formatter.format_summary(&self.monitor.get_metrics());
self.notifier.send_notification(&summary)?;
Ok(())
}
}