|
31 | 31 |
|
32 | 32 | use chrono::{DateTime, Local};
|
33 | 33 | use deepsize::DeepSizeOf;
|
| 34 | +use tracing::error; |
34 | 35 |
|
35 | 36 | use crate::errors::AnalysisError;
|
36 |
| -use crate::records::{Check, CheckType, IpType}; |
| 37 | +use crate::records::{display_group, indented_check, Check, CheckType, IpType}; |
37 | 38 | use crate::store::Store;
|
38 | 39 |
|
39 | 40 | use std::fmt::{Display, Write};
|
@@ -85,38 +86,57 @@ impl<'check> Outage<'check> {
|
85 | 86 | ) -> Self {
|
86 | 87 | Self {
|
87 | 88 | start,
|
88 |
| - end, |
| 89 | + end: if Some(start) == end { None } else { end }, |
89 | 90 | all: all_checks.to_vec(),
|
90 | 91 | }
|
91 | 92 | }
|
| 93 | + |
| 94 | + /// Returns the length of this [`Outage`]. |
| 95 | + pub fn len(&self) -> usize { |
| 96 | + self.all.len() |
| 97 | + } |
| 98 | + |
| 99 | + /// Returns true if this [`Outage`] is empty. |
| 100 | + #[must_use] |
| 101 | + pub fn is_empty(&self) -> bool { |
| 102 | + self.len() == 0 |
| 103 | + } |
92 | 104 | }
|
93 | 105 |
|
94 | 106 | impl Display for Outage<'_> {
|
95 | 107 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
| 108 | + let mut buf: String = String::new(); |
96 | 109 | if self.end.is_some() {
|
97 |
| - writeln!( |
98 |
| - f, |
99 |
| - "From {} To {}", |
100 |
| - fmt_timestamp(self.start.timestamp_parsed()), |
101 |
| - fmt_timestamp(self.end.unwrap().timestamp_parsed()) |
| 110 | + key_value_write( |
| 111 | + &mut buf, |
| 112 | + "From", |
| 113 | + fmt_timestamp(self.end.unwrap().timestamp_parsed()), |
| 114 | + )?; |
| 115 | + key_value_write( |
| 116 | + &mut buf, |
| 117 | + "To", |
| 118 | + fmt_timestamp(self.end.unwrap().timestamp_parsed()), |
102 | 119 | )?;
|
103 | 120 | } else {
|
104 |
| - writeln!( |
105 |
| - f, |
106 |
| - "From {} STILL ONGOING", |
| 121 | + key_value_write( |
| 122 | + &mut buf, |
| 123 | + "From", |
107 | 124 | fmt_timestamp(self.start.timestamp_parsed()),
|
108 | 125 | )?;
|
| 126 | + key_value_write(&mut buf, "To", "(None)")?; |
109 | 127 | }
|
110 |
| - writeln!(f, "Checks: {}", self.all.len())?; |
111 |
| - writeln!( |
112 |
| - f, |
113 |
| - "Type: {}", |
114 |
| - self.start.calc_type().unwrap_or(CheckType::Unknown) |
115 |
| - )?; |
| 128 | + key_value_write(&mut buf, "Total", self.len())?; |
| 129 | + writeln!(buf, "\nDetails")?; |
| 130 | + display_group(&self.all, &mut buf)?; |
| 131 | + write!(f, "{buf}")?; |
116 | 132 | Ok(())
|
117 | 133 | }
|
118 | 134 | }
|
119 | 135 |
|
| 136 | +fn more_indent(buf: &str) -> String { |
| 137 | + format!("\t{}", buf.to_string().replace("\n", "\n\t")) |
| 138 | +} |
| 139 | + |
120 | 140 | /// Generate a comprehensive analysis report for the given store.
|
121 | 141 | ///
|
122 | 142 | /// The report includes:
|
@@ -205,44 +225,42 @@ fn key_value_write(
|
205 | 225 | writeln!(f, "{:<24}: {}", title, content)
|
206 | 226 | }
|
207 | 227 |
|
| 228 | +/// Writes a key-value pair to the report in aligned columns. |
| 229 | +/// |
| 230 | +/// Format: `<key>: <value>` |
| 231 | +fn key_value_write_indented( |
| 232 | + f: &mut String, |
| 233 | + title: &str, |
| 234 | + content: impl Display, |
| 235 | +) -> Result<(), std::fmt::Error> { |
| 236 | + writeln!(f, " {:<16}: {}", title, content) |
| 237 | +} |
| 238 | + |
208 | 239 | /// Analyzes and formats outage information from the store.
|
209 | 240 | ///
|
210 | 241 | /// Groups consecutive failed checks by check type and creates
|
211 | 242 | /// Outage records for reporting.
|
212 | 243 | fn outages(store: &Store, f: &mut String) -> Result<(), AnalysisError> {
|
213 | 244 | let all_checks: Vec<&Check> = store.checks().iter().collect();
|
214 |
| - let mut outages: Vec<Outage> = Vec::new(); |
215 |
| - let fails_exist = all_checks |
216 |
| - .iter() |
217 |
| - .fold(true, |fails_exist, c| fails_exist & !c.is_success()); |
| 245 | + let fails_exist = !all_checks.iter().all(|c| c.is_success()); |
218 | 246 | if !fails_exist || all_checks.is_empty() {
|
219 | 247 | writeln!(f, "None\n")?;
|
220 | 248 | return Ok(());
|
221 | 249 | }
|
222 | 250 |
|
223 |
| - for check_type in CheckType::all() { |
224 |
| - let checks: Vec<&&Check> = all_checks |
225 |
| - .iter() |
226 |
| - .filter(|c| c.calc_type().unwrap_or(CheckType::Unknown) == *check_type) |
227 |
| - .collect(); |
| 251 | + let failed_checks: Vec<&&Check> = all_checks.iter().filter(|c| !c.is_success()).collect(); |
228 | 252 |
|
229 |
| - let fail_groups = fail_groups(&checks); |
230 |
| - for group in fail_groups { |
231 |
| - // writeln!(f, "Group {gidx}:")?; |
232 |
| - // display_group(group, f)?; |
233 |
| - if !group.is_empty() { |
234 |
| - outages.push(Outage::new( |
235 |
| - checks.first().unwrap(), |
236 |
| - Some(checks.last().unwrap()), |
237 |
| - &group, |
238 |
| - )); |
239 |
| - } |
| 253 | + let fail_groups = fail_groups(&failed_checks); |
| 254 | + for (outage_idx, group) in fail_groups.into_iter().enumerate() { |
| 255 | + if group.is_empty() { |
| 256 | + error!("empty outage group"); |
| 257 | + continue; |
240 | 258 | }
|
| 259 | + let outage = Outage::new(group.first().unwrap(), group.last().copied(), &group); |
| 260 | + writeln!(f, "{outage_idx}:\n{}", more_indent(&outage.to_string()))?; |
241 | 261 | }
|
| 262 | + writeln!(f)?; |
242 | 263 |
|
243 |
| - for outage in outages { |
244 |
| - writeln!(f, "{outage}")?; |
245 |
| - } |
246 | 264 | Ok(())
|
247 | 265 | }
|
248 | 266 |
|
|
0 commit comments