Skip to content

Commit

Permalink
implement 'alarm_last_duration' and 'alarm_last_duration_iso' generic…
Browse files Browse the repository at this point in the history
… placeholders
  • Loading branch information
flo-at committed Feb 12, 2024
1 parent 3ed43b8 commit 01de229
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
6 changes: 6 additions & 0 deletions doc/check.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ Name of the alarm that triggered the action.
### alarm_timestamp
ISO8601 timestamp of the alarm's state change event.

### alarm_last_duration
Duration the last state lasted in seconds.

### alarm_last_duration_iso
Duration the last state lasted as ISO8601.

### alarm_state
Current state of the alarm.

Expand Down
46 changes: 42 additions & 4 deletions src/alarm/state_machine.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::PlaceholderMap;
use crate::{Error, Result};
use crate::{duration_iso8601, Error, PlaceholderMap, Result};

#[cfg_attr(test, mockall::automock)]
pub trait StateHandler: Send + Sync + Sized {
Expand Down Expand Up @@ -45,13 +44,17 @@ impl State {
#[derive(Clone)]
struct GoodState {
timestamp: std::time::SystemTime,
instant: std::time::Instant,
last_state_duration: Option<std::time::Duration>,
bad_cycles: u32,
}

impl Default for GoodState {
fn default() -> Self {
Self {
timestamp: std::time::SystemTime::now(),
instant: std::time::Instant::now(),
last_state_duration: None,
bad_cycles: 0,
}
}
Expand All @@ -60,13 +63,16 @@ impl Default for GoodState {
#[derive(Clone)]
struct BadState {
timestamp: std::time::SystemTime,
instant: std::time::Instant,
last_state_duration: std::time::Duration,
cycles: u32,
good_cycles: u32,
}

#[derive(Clone)]
struct ErrorState {
timestamp: std::time::SystemTime,
last_state_duration: std::time::Duration,
shadowed_state: Box<State>,
cycles: u32,
}
Expand Down Expand Up @@ -105,6 +111,14 @@ impl StateHandler for StateMachine {
String::from("alarm_timestamp"),
crate::datetime_iso8601(bad.timestamp),
);
placeholders.insert(
String::from("alarm_last_duration"),
bad.last_state_duration.as_secs().to_string(),
);
placeholders.insert(
String::from("alarm_last_duration_iso"),
duration_iso8601(bad.last_state_duration),
);
}

State::Good(good) => {
Expand All @@ -113,6 +127,16 @@ impl StateHandler for StateMachine {
String::from("alarm_timestamp"),
crate::datetime_iso8601(good.timestamp),
);
if let Some(last_state_duration) = good.last_state_duration {
placeholders.insert(
String::from("alarm_last_duration"),
last_state_duration.as_secs().to_string(),
);
placeholders.insert(
String::from("alarm_last_duration_iso"),
duration_iso8601(last_state_duration),
);
}
}

State::Error(error) => {
Expand All @@ -121,28 +145,38 @@ impl StateHandler for StateMachine {
String::from("alarm_timestamp"),
crate::datetime_iso8601(error.timestamp),
);
placeholders.insert(
String::from("alarm_last_duration"),
error.last_state_duration.as_secs().to_string(),
);
placeholders.insert(
String::from("alarm_last_duration_iso"),
duration_iso8601(error.last_state_duration),
);
}
}
}

fn error(&mut self) -> bool {
let mut trigger = false;
self.state = match &self.state {
State::Good(_) => {
State::Good(good) => {
trigger = true;
log::warn!("{} changing from good to error state.", self.log_id);
State::Error(ErrorState {
timestamp: std::time::SystemTime::now(),
last_state_duration: good.instant.elapsed(),
shadowed_state: Box::new(self.state.clone()),
cycles: 1,
})
}

State::Bad(_) => {
State::Bad(bad) => {
trigger = true;
log::warn!("{} changing from bad to error state.", self.log_id);
State::Error(ErrorState {
timestamp: std::time::SystemTime::now(),
last_state_duration: bad.instant.elapsed(),
shadowed_state: Box::new(self.state.clone()),
cycles: 1,
})
Expand Down Expand Up @@ -174,6 +208,8 @@ impl StateHandler for StateMachine {
log::warn!("{} changing from good to bad state.", self.log_id);
State::Bad(BadState {
timestamp: std::time::SystemTime::now(),
instant: std::time::Instant::now(),
last_state_duration: good.instant.elapsed(),
cycles: 1,
good_cycles: 0,
})
Expand Down Expand Up @@ -227,6 +263,8 @@ impl StateHandler for StateMachine {
log::info!("{} changing from bad to good state.", self.log_id);
State::Good(GoodState {
timestamp: std::time::SystemTime::now(),
instant: std::time::Instant::now(),
last_state_duration: Some(bad.instant.elapsed()),
bad_cycles: 0,
})
} else {
Expand Down

0 comments on commit 01de229

Please sign in to comment.