rhit/md/
status.rs

1use {
2    super::*,
3    crate::*,
4    termimad::minimad::OwningTemplateExpander,
5};
6
7static MD_SHORT: &str = r#"
8## HTTP status codes:
9|:-:|:-:|:-:|:-:
10|**2xx**|**3xx**|**4xx**|**5xx**
11|:-:|:-:|:-:|:-:
12|${percent_2xx}|${percent_3xx}|${percent_4xx}|${percent_5xx}
13|-:
14"#;
15
16pub fn print_status_codes(
17    log_lines: &[LogLine],
18    printer: &Printer,
19    trend_computer: Option<&TrendComputer>,
20) {
21    if printer.detail_level == 0 {
22        print_status_summary(log_lines, printer);
23        return;
24    }
25    let section = Section {
26        groups_name: "HTTP status codes",
27        group_key: "status",
28        view: View::Full,
29        changes: false,
30    };
31    printer.print_groups(
32        &section,
33        log_lines,
34        |_| true,
35        |line| line.status,
36        trend_computer,
37    );
38}
39
40fn to_percent(count: usize, total: usize) -> String {
41    let percent = 100f32 * (count as f32) / (total as f32);
42    format!("{:.1}%", percent)
43}
44
45fn print_status_summary(
46    log_lines: &[LogLine],
47    printer: &Printer,
48){
49    let mut expander = OwningTemplateExpander::new();
50    let (mut s2, mut s3, mut s4, mut s5) = (0, 0, 0, 0);
51    for ll in log_lines {
52        match ll.status {
53            200..=299 => s2 += 1,
54            300..=399 => s3 += 1,
55            400..=499 => s4 += 1,
56            _ => s5 += 1,
57        }
58    }
59    expander
60        .set("percent_2xx", to_percent(s2, log_lines.len()))
61        .set("percent_3xx", to_percent(s3, log_lines.len()))
62        .set("percent_4xx", to_percent(s4, log_lines.len()))
63        .set("percent_5xx", to_percent(s5, log_lines.len()));
64    printer.print(expander, MD_SHORT);
65}
66