Skip to content

Commit 2b59f76

Browse files
committed
feat: add notification if no recent logs found
1 parent afb4809 commit 2b59f76

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ pub fn run(cli: &Cli) -> anyhow::Result<()> {
4343
.context("failed to send notification of processing failure")?,
4444
}
4545

46+
if let Some(msg) = app_state.generate_inactivity_msg() {
47+
send_notification(&msg, &config_folder)
48+
.context("failed to send notification of inactivity in logs")?
49+
}
50+
4651
if app_state.is_changed() {
4752
app_state
4853
.save(&cli.state_file)
@@ -82,7 +87,6 @@ pub fn build_err_msg_from_logs(log_infos: Vec<LogInfo>) -> String {
8287
pub fn process_logs_folder(app_state: &mut AppState) -> anyhow::Result<Vec<LogInfo>> {
8388
let mut result = Vec::new();
8489
let mut latest_timestamp = app_state.latest_log_datetime();
85-
// TODO 3: Send notification if no logs detected in over 24 hours or over 6 hours and uptime is less than 24 hours
8690
for dir_entry in read_dir(app_state.logs_dir())
8791
.with_context(|| format!("failed to read log folder: {:?}", app_state.logs_dir()))?
8892
{

src/state.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub struct AppState {
1414
alive_msg_time: Option<NaiveTime>,
1515
logs_dir: PathBuf,
1616
latest_log_datetime: NaiveDateTime,
17+
allowed_num_hours_without_log: Option<i64>,
1718
#[serde(skip)]
1819
is_changed: bool,
1920
}
@@ -61,6 +62,7 @@ impl AppState {
6162
NaiveTime::from_hms_opt(7, 0, 0)
6263
.expect("should be valid as it is set at build time"),
6364
),
65+
allowed_num_hours_without_log: Some(24),
6466
latest_log_datetime: Local::now().naive_local(),
6567
logs_dir,
6668
is_changed: Default::default(),
@@ -107,4 +109,17 @@ impl AppState {
107109
self.is_changed = true;
108110
self.latest_log_datetime = value;
109111
}
112+
113+
pub(crate) fn generate_inactivity_msg(&self) -> Option<String> {
114+
let allowed_hours = self.allowed_num_hours_without_log?;
115+
let num_hours_since_log = Local::now()
116+
.naive_local()
117+
.signed_duration_since(self.latest_log_datetime)
118+
.num_hours();
119+
if num_hours_since_log > allowed_hours {
120+
Some(format!("Most recent log found ({}) exceeds the allowed number of hours ({allowed_hours}) without a log. Currently {num_hours_since_log} hours without a log.", self.latest_log_datetime.format("%F %T")))
121+
} else {
122+
None
123+
}
124+
}
110125
}

tests/snapshots/log_parsing__output_snapshot-2.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ AppState(
77
alive_msg_time: Some("07:00:00"),
88
logs_dir: "tests/sample_logs",
99
latest_log_datetime: "2024-11-08T14:50:21",
10+
allowed_num_hours_without_log: Some(24),
1011
)

0 commit comments

Comments
 (0)