Skip to content

Commit f2feab1

Browse files
committed
fix(analyze): only one outage group was found
1 parent 20a309f commit f2feab1

File tree

3 files changed

+36
-34
lines changed

3 files changed

+36
-34
lines changed

src/analyze.rs

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl Display for Outage<'_> {
115115
key_value_write(
116116
&mut buf,
117117
"From",
118-
fmt_timestamp(self.end.unwrap().timestamp_parsed()),
118+
fmt_timestamp(self.start.timestamp_parsed()),
119119
)?;
120120
key_value_write(
121121
&mut buf,
@@ -131,8 +131,16 @@ impl Display for Outage<'_> {
131131
key_value_write(&mut buf, "To", "(None)")?;
132132
}
133133
key_value_write(&mut buf, "Total", self.len())?;
134-
writeln!(buf, "\nDetails")?;
135-
display_group(&self.all, &mut buf)?;
134+
writeln!(buf, "\nFirst\n{}", self.start)?;
135+
writeln!(
136+
buf,
137+
"\nLast\n{}",
138+
if let Some(c) = self.end {
139+
c.to_string()
140+
} else {
141+
"(None)".to_string()
142+
}
143+
)?;
136144
write!(f, "{buf}")?;
137145
Ok(())
138146
}
@@ -230,17 +238,6 @@ fn key_value_write(
230238
writeln!(f, "{:<24}: {}", title, content)
231239
}
232240

233-
/// Writes a key-value pair to the report in aligned columns.
234-
///
235-
/// Format: `<key>: <value>`
236-
fn key_value_write_indented(
237-
f: &mut String,
238-
title: &str,
239-
content: impl Display,
240-
) -> Result<(), std::fmt::Error> {
241-
writeln!(f, " {:<16}: {}", title, content)
242-
}
243-
244241
/// Analyzes and formats outage information from the store.
245242
///
246243
/// Groups consecutive failed checks by check type and creates
@@ -253,9 +250,7 @@ fn outages(store: &Store, f: &mut String) -> Result<(), AnalysisError> {
253250
return Ok(());
254251
}
255252

256-
let failed_checks: Vec<&&Check> = all_checks.iter().filter(|c| !c.is_success()).collect();
257-
258-
let fail_groups = fail_groups(&failed_checks);
253+
let fail_groups = fail_groups(&all_checks);
259254
for (outage_idx, group) in fail_groups.into_iter().enumerate() {
260255
if group.is_empty() {
261256
error!("empty outage group");
@@ -275,32 +270,44 @@ fn outages(store: &Store, f: &mut String) -> Result<(), AnalysisError> {
275270
/// - Checks are consecutive by index
276271
/// - All checks in group are failures
277272
/// - Gap between groups is > 1 check
278-
fn fail_groups<'check>(checks: &[&&'check Check]) -> Vec<Vec<&'check Check>> {
273+
fn fail_groups<'check>(checks: &[&'check Check]) -> Vec<Vec<&'check Check>> {
279274
let failed_idxs: Vec<usize> = checks
280275
.iter()
281276
.enumerate()
282277
.filter(|(_idx, c)| !c.is_success())
283278
.map(|(idx, _c)| idx)
284279
.collect();
280+
285281
if failed_idxs.is_empty() {
286282
return Vec::new();
287283
}
284+
288285
let mut groups: Vec<Vec<&Check>> = Vec::new();
286+
let mut current_group: Vec<&Check> = Vec::new();
289287

290-
let mut first = failed_idxs[0];
291-
let mut last = first;
292-
for idx in failed_idxs {
293-
if idx == last + 1 {
294-
last = idx;
288+
// Start with the first index
289+
let mut last_idx = failed_idxs[0];
290+
current_group.push(checks[last_idx]);
291+
292+
// Process remaining indices
293+
for &idx in failed_idxs.iter().skip(1) {
294+
if idx == last_idx + 1 {
295+
// Consecutive failure, add to current group
296+
current_group.push(checks[idx]);
295297
} else {
296-
let mut group: Vec<&Check> = Vec::new();
297-
for check in checks.iter().take(last + 1).skip(first) {
298-
group.push(*check);
298+
// Gap found, start new group
299+
if !current_group.is_empty() {
300+
groups.push(current_group);
301+
current_group = Vec::new();
299302
}
300-
groups.push(group);
301-
302-
first = idx;
303+
current_group.push(checks[idx]);
303304
}
305+
last_idx = idx;
306+
}
307+
308+
// Don't forget to add the last group
309+
if !current_group.is_empty() {
310+
groups.push(current_group);
304311
}
305312

306313
groups

src/records.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -462,10 +462,6 @@ pub fn display_group(group: &[&Check], f: &mut String) -> Result<(), std::fmt::E
462462
Ok(())
463463
}
464464

465-
pub(crate) fn indented_check(buf: &mut String, check: &Check) -> Result<(), std::fmt::Error> {
466-
writeln!(buf, "\t{}", check.to_string().replace("\n", "\n\t"))
467-
}
468-
469465
#[cfg(test)]
470466
mod test {
471467
use crate::TIMEOUT_MS;

src/store.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,6 @@ impl Store {
353353
Err(err) => match &err {
354354
StoreError::DoesNotExist => Self::create(),
355355
StoreError::Load { source } => {
356-
dbg!(source);
357356
error!("{err}");
358357

359358
#[allow(clippy::single_match)] // more will certainly come later

0 commit comments

Comments
 (0)