use std::{sync::Arc, time::Duration};
use crate::status::{HealthState, TaskStatus};
const HEALTH_STATES: [HealthState; 5] = [
HealthState::Starting,
HealthState::Learning,
HealthState::Healthy,
HealthState::Late,
HealthState::Stopped,
];
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum HealthImpact {
Ignore,
Nominal,
Degraded,
Unhealthy,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum HealthVerdict {
Empty,
Nominal,
Degraded,
Unhealthy,
}
impl HealthVerdict {
#[must_use]
pub const fn is_nominal(&self) -> bool {
matches!(self, Self::Nominal)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct HealthPolicy {
starting: HealthImpact,
learning: HealthImpact,
healthy: HealthImpact,
late: HealthImpact,
stopped: HealthImpact,
}
impl Default for HealthPolicy {
fn default() -> Self {
Self::readiness()
}
}
impl HealthPolicy {
#[must_use]
pub const fn readiness() -> Self {
Self {
starting: HealthImpact::Degraded,
learning: HealthImpact::Degraded,
healthy: HealthImpact::Nominal,
late: HealthImpact::Unhealthy,
stopped: HealthImpact::Unhealthy,
}
}
#[must_use]
pub const fn liveness() -> Self {
Self {
starting: HealthImpact::Nominal,
learning: HealthImpact::Nominal,
healthy: HealthImpact::Nominal,
late: HealthImpact::Unhealthy,
stopped: HealthImpact::Ignore,
}
}
#[must_use]
pub const fn strict() -> Self {
Self {
starting: HealthImpact::Unhealthy,
learning: HealthImpact::Unhealthy,
healthy: HealthImpact::Nominal,
late: HealthImpact::Unhealthy,
stopped: HealthImpact::Unhealthy,
}
}
#[must_use]
pub const fn with_impact(mut self, state: HealthState, impact: HealthImpact) -> Self {
match state {
HealthState::Starting => self.starting = impact,
HealthState::Learning => self.learning = impact,
HealthState::Healthy => self.healthy = impact,
HealthState::Late => self.late = impact,
HealthState::Stopped => self.stopped = impact,
}
self
}
#[must_use]
pub const fn impact(self, state: HealthState) -> HealthImpact {
match state {
HealthState::Starting => self.starting,
HealthState::Learning => self.learning,
HealthState::Healthy => self.healthy,
HealthState::Late => self.late,
HealthState::Stopped => self.stopped,
}
}
fn evaluate(self, counts: HealthCounts) -> (HealthVerdict, usize, usize) {
let mut verdict = HealthVerdict::Empty;
let mut considered_tasks = 0;
let mut ignored_tasks = 0;
for state in HEALTH_STATES {
let count = counts.count(state);
if count == 0 {
continue;
}
match self.impact(state) {
HealthImpact::Ignore => ignored_tasks += count,
HealthImpact::Nominal => {
considered_tasks += count;
if verdict == HealthVerdict::Empty {
verdict = HealthVerdict::Nominal;
}
}
HealthImpact::Degraded => {
considered_tasks += count;
if verdict != HealthVerdict::Unhealthy {
verdict = HealthVerdict::Degraded;
}
}
HealthImpact::Unhealthy => {
considered_tasks += count;
verdict = HealthVerdict::Unhealthy;
}
}
}
(verdict, considered_tasks, ignored_tasks)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct HealthCounts {
pub starting: usize,
pub learning: usize,
pub healthy: usize,
pub late: usize,
pub stopped: usize,
}
impl HealthCounts {
#[must_use]
pub const fn total(self) -> usize {
self.starting + self.learning + self.healthy + self.late + self.stopped
}
#[must_use]
pub const fn count(self, state: HealthState) -> usize {
match state {
HealthState::Starting => self.starting,
HealthState::Learning => self.learning,
HealthState::Healthy => self.healthy,
HealthState::Late => self.late,
HealthState::Stopped => self.stopped,
}
}
pub(crate) fn observe(&mut self, state: HealthState) {
match state {
HealthState::Starting => self.starting += 1,
HealthState::Learning => self.learning += 1,
HealthState::Healthy => self.healthy += 1,
HealthState::Late => self.late += 1,
HealthState::Stopped => self.stopped += 1,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct HealthSummary {
pub observed_after: Duration,
pub policy: HealthPolicy,
pub verdict: HealthVerdict,
pub counts: HealthCounts,
pub considered_tasks: usize,
pub ignored_tasks: usize,
}
impl HealthSummary {
pub(crate) fn from_counts(
policy: HealthPolicy,
observed_after: Duration,
counts: HealthCounts,
) -> Self {
let (verdict, considered_tasks, ignored_tasks) = policy.evaluate(counts);
Self {
observed_after,
policy,
verdict,
counts,
considered_tasks,
ignored_tasks,
}
}
#[must_use]
pub const fn is_nominal(&self) -> bool {
self.verdict.is_nominal()
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HealthReport {
pub summary: HealthSummary,
pub tasks: Arc<[TaskStatus]>,
}
impl HealthReport {
pub(crate) fn from_statuses(
policy: HealthPolicy,
observed_after: Duration,
statuses: Vec<TaskStatus>,
) -> Self {
let mut counts = HealthCounts::default();
for status in &statuses {
counts.observe(status.health.state());
}
Self {
summary: HealthSummary::from_counts(policy, observed_after, counts),
tasks: Arc::from(statuses),
}
}
#[must_use]
pub const fn is_nominal(&self) -> bool {
self.summary.is_nominal()
}
}