refactor(app): implement ports & adapters
* also move alertFormatter into internal actor
This commit is contained in:
1
src/adapters/driven/for_getting_violation_data.rs
Normal file
1
src/adapters/driven/for_getting_violation_data.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod violation;
|
||||
35
src/adapters/driven/for_getting_violation_data/violation.rs
Normal file
35
src/adapters/driven/for_getting_violation_data/violation.rs
Normal 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
|
||||
}
|
||||
}
|
||||
1
src/adapters/driven/for_monitoring_system.rs
Normal file
1
src/adapters/driven/for_monitoring_system.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod system_monitor;
|
||||
40
src/adapters/driven/for_monitoring_system/system_monitor.rs
Normal file
40
src/adapters/driven/for_monitoring_system/system_monitor.rs
Normal 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()
|
||||
}
|
||||
}
|
||||
1
src/adapters/driven/for_sending_notification.rs
Normal file
1
src/adapters/driven/for_sending_notification.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod discord_client;
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user