refactor: move business logic into app
* also move all at directory root crate * all actors are into lib directory * formatter is into system_monitor now
This commit is contained in:
38
src/app/service.rs
Normal file
38
src/app/service.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
use discord_client::DiscordNotifier;
|
||||
use system_monitor::{SystemMonitor, alert_formatter::AlertFormatter};
|
||||
pub struct Service {
|
||||
notifier: DiscordNotifier,
|
||||
monitor: SystemMonitor,
|
||||
formatter: AlertFormatter,
|
||||
}
|
||||
|
||||
pub fn new(
|
||||
notifier: DiscordNotifier,
|
||||
monitor: SystemMonitor,
|
||||
formatter: AlertFormatter,
|
||||
) -> Service {
|
||||
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");
|
||||
}
|
||||
}
|
||||
25
src/main.rs
Normal file
25
src/main.rs
Normal file
@@ -0,0 +1,25 @@
|
||||
use discord_client::DiscordNotifier;
|
||||
use dotenvy::dotenv;
|
||||
use system_monitor::SystemMonitor;
|
||||
|
||||
mod app {
|
||||
pub mod service;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
dotenv().ok();
|
||||
|
||||
let notifier = DiscordNotifier::new(
|
||||
std::env::var("DISCORD_WEBHOOK").expect("DISCORD_WEBHOOK environment variable not set"),
|
||||
"System Monitor".to_string(),
|
||||
"https://cdn.shopify.com/s/files/1/0262/1423/6212/files/Lord_of_the_Rings_eye_of_Sauron_-_Ghtic.com_-_Blog.png?v=1579680018".to_string(),
|
||||
);
|
||||
|
||||
let monitor =
|
||||
SystemMonitor::new(system_monitor::resource_threshold::get_default_resource_thresholds());
|
||||
|
||||
let formatter = system_monitor::alert_formatter::AlertFormatter;
|
||||
|
||||
let service = app::service::new(notifier, monitor, formatter);
|
||||
service.run();
|
||||
}
|
||||
Reference in New Issue
Block a user