refactor(app): implement ports & adapters

* also move alertFormatter into internal actor
This commit is contained in:
2025-08-09 17:53:01 +02:00
parent 985ee54669
commit f53aef1821
13 changed files with 223 additions and 69 deletions

View File

@@ -0,0 +1 @@
pub mod violation;

View File

@@ -0,0 +1,35 @@
pub struct Violation {
name: String,
is_critical: bool,
metric_value: f32,
threshold: f32,
}
impl Violation {
pub fn new(name: String, is_critical: bool, metric_value: f32, threshold: f32) -> Self {
Violation {
name,
is_critical,
metric_value,
threshold,
}
}
}
impl crate::app::ports::driven::ForGettingViolationData for Violation {
fn get_metric_name(&self) -> String {
self.name.clone()
}
fn is_critical(&self) -> bool {
self.is_critical
}
fn get_metric_value(&self) -> f32 {
self.metric_value
}
fn get_threshold(&self) -> f32 {
self.threshold
}
}

View File

@@ -0,0 +1 @@
pub mod system_monitor;

View File

@@ -0,0 +1,40 @@
use crate::{
adapters::driven::for_getting_violation_data::violation::Violation,
app::ports::driven::ForMonitoringSystem,
};
pub struct SystemMonitorAdapter {
system_monitor: system_monitor::SystemMonitor,
}
impl SystemMonitorAdapter {
pub fn new(system_monitor: system_monitor::SystemMonitor) -> Self {
SystemMonitorAdapter { system_monitor }
}
fn threshold_violation_to_for_getting_violation_data(
&self,
threshold_violation: &system_monitor::ThresholdViolation,
) -> Box<dyn crate::app::ports::driven::ForGettingViolationData> {
Box::new(Violation::new(
threshold_violation.metric_name.clone(),
threshold_violation.severity == system_monitor::Severity::Critical,
threshold_violation.current_value,
threshold_violation.threshold,
))
}
}
impl ForMonitoringSystem for SystemMonitorAdapter {
fn check_thresholds(&self) -> Vec<Box<dyn crate::app::ports::driven::ForGettingViolationData>> {
let thresholds = self.system_monitor.check_thresholds();
thresholds
.into_iter()
.map(|t| self.threshold_violation_to_for_getting_violation_data(&t))
.collect()
}
fn get_metrics(&self) -> std::collections::HashMap<String, f32> {
self.system_monitor.get_metrics()
}
}

View File

@@ -0,0 +1 @@
pub mod discord_client;

View File

@@ -0,0 +1,17 @@
use crate::app::ports::driven::ForSendingNotification;
pub struct DiscordAdapter {
discord_client: discord_client::DiscordNotifier,
}
impl DiscordAdapter {
pub fn new(discord_client: discord_client::DiscordNotifier) -> Self {
DiscordAdapter { discord_client }
}
}
impl ForSendingNotification for DiscordAdapter {
fn send_notification(&self, message: &str) -> Result<(), Box<dyn std::error::Error>> {
self.discord_client.send_notification(message)
}
}