use core_types::Timestamp;
use crate::{
alert::{Alert, AlertSeverity},
aggregator::MetricsAggregator,
bus::AlertBus,
};
pub struct HealthWatcher {
pub name: String,
}
impl HealthWatcher {
pub fn new(name: impl Into<String>) -> Self {
Self { name: name.into() }
}
pub fn poll(&self, agg: &MetricsAggregator, bus: &mut AlertBus, now: Timestamp) -> usize {
use core_types::HealthStatus;
let report = agg.health_report();
let mut n = 0;
for comp in &report.components {
let (severity, reason) = match &comp.status {
HealthStatus::Healthy => continue,
HealthStatus::Degraded { reason } => (AlertSeverity::Warn, reason.clone()),
HealthStatus::Unhealthy { reason } => (AlertSeverity::Error, reason.clone()),
};
bus.fire(Alert::new(
comp.name.clone(),
format!("{}_HEALTH", comp.name.to_uppercase()),
reason,
severity,
now,
));
n += 1;
}
n
}
}