| 1 |
use pom::types::HealthStatus; |
| 2 |
|
| 3 |
#[derive(Debug, PartialEq, Eq)] |
| 4 |
pub(crate) enum IncidentAction { |
| 5 |
None, |
| 6 |
Open, |
| 7 |
Close, |
| 8 |
CloseAndOpen, |
| 9 |
} |
| 10 |
|
| 11 |
pub(crate) fn incident_action(prev: HealthStatus, curr: HealthStatus) -> IncidentAction { |
| 12 |
if prev == curr { |
| 13 |
return IncidentAction::None; |
| 14 |
} |
| 15 |
let prev_op = prev == HealthStatus::Operational; |
| 16 |
let curr_op = curr == HealthStatus::Operational; |
| 17 |
match (prev_op, curr_op) { |
| 18 |
(true, false) => IncidentAction::Open, |
| 19 |
(false, true) => IncidentAction::Close, |
| 20 |
(false, false) => IncidentAction::CloseAndOpen, |
| 21 |
(true, true) => IncidentAction::None, |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
#[cfg(test)] |
| 26 |
mod tests { |
| 27 |
use super::*; |
| 28 |
|
| 29 |
#[test] |
| 30 |
fn incident_operational_to_error_opens() { |
| 31 |
assert_eq!( |
| 32 |
incident_action(HealthStatus::Operational, HealthStatus::Error), |
| 33 |
IncidentAction::Open, |
| 34 |
); |
| 35 |
} |
| 36 |
|
| 37 |
#[test] |
| 38 |
fn incident_operational_to_degraded_opens() { |
| 39 |
assert_eq!( |
| 40 |
incident_action(HealthStatus::Operational, HealthStatus::Degraded), |
| 41 |
IncidentAction::Open, |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
#[test] |
| 46 |
fn incident_error_to_operational_closes() { |
| 47 |
assert_eq!( |
| 48 |
incident_action(HealthStatus::Error, HealthStatus::Operational), |
| 49 |
IncidentAction::Close, |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
#[test] |
| 54 |
fn incident_degraded_to_operational_closes() { |
| 55 |
assert_eq!( |
| 56 |
incident_action(HealthStatus::Degraded, HealthStatus::Operational), |
| 57 |
IncidentAction::Close, |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
#[test] |
| 62 |
fn incident_degraded_to_error_closes_and_opens() { |
| 63 |
assert_eq!( |
| 64 |
incident_action(HealthStatus::Degraded, HealthStatus::Error), |
| 65 |
IncidentAction::CloseAndOpen, |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
#[test] |
| 70 |
fn incident_error_to_degraded_closes_and_opens() { |
| 71 |
assert_eq!( |
| 72 |
incident_action(HealthStatus::Error, HealthStatus::Degraded), |
| 73 |
IncidentAction::CloseAndOpen, |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
#[test] |
| 78 |
fn incident_operational_to_operational_none() { |
| 79 |
assert_eq!( |
| 80 |
incident_action(HealthStatus::Operational, HealthStatus::Operational), |
| 81 |
IncidentAction::None, |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
#[test] |
| 86 |
fn incident_error_to_error_none() { |
| 87 |
assert_eq!( |
| 88 |
incident_action(HealthStatus::Error, HealthStatus::Error), |
| 89 |
IncidentAction::None, |
| 90 |
); |
| 91 |
} |
| 92 |
} |
| 93 |
|