forked from lycheeverse/lychee
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown.rs
More file actions
88 lines (74 loc) · 2.26 KB
/
Copy pathmarkdown.rs
File metadata and controls
88 lines (74 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use std::{
collections::HashMap,
fmt::{self, Display},
};
use super::HostStatsFormatter;
use anyhow::Result;
use lychee_lib::ratelimit::HostStats;
use tabled::{
Table, Tabled,
settings::{Alignment, Modify, Style, object::Segment},
};
#[derive(Tabled)]
struct HostStatsTableEntry {
#[tabled(rename = "Host")]
host: String,
#[tabled(rename = "Requests")]
requests: u64,
#[tabled(rename = "Success Rate")]
success_rate: String,
#[tabled(rename = "Median Time")]
median_time: String,
#[tabled(rename = "Cache Hit Rate")]
cache_hit_rate: String,
}
fn host_stats_table(host_stats: &HashMap<String, HostStats>) -> String {
let sorted_hosts = super::sort_host_stats(host_stats);
let entries: Vec<HostStatsTableEntry> = sorted_hosts
.into_iter()
.map(|(hostname, stats)| {
let median_time = stats
.median_request_time()
.map_or_else(|| "N/A".to_string(), |d| format!("{:.0}ms", d.as_millis()));
HostStatsTableEntry {
host: hostname.clone(),
requests: stats.total_requests,
success_rate: format!("{:.1}%", stats.success_rate() * 100.0),
median_time,
cache_hit_rate: format!("{:.1}%", stats.cache_hit_rate() * 100.0),
}
})
.collect();
if entries.is_empty() {
return String::new();
}
let style = Style::markdown();
Table::new(entries)
.with(Modify::new(Segment::all()).with(Alignment::left()))
.with(style)
.to_string()
}
struct MarkdownHostStats(HashMap<String, HostStats>);
impl Display for MarkdownHostStats {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.0.is_empty() {
return Ok(());
}
writeln!(f, "\n## Per-host Statistics")?;
writeln!(f)?;
writeln!(f, "{}", host_stats_table(&self.0))?;
Ok(())
}
}
pub(crate) struct Markdown;
impl Markdown {
pub(crate) const fn new() -> Self {
Self {}
}
}
impl HostStatsFormatter for Markdown {
fn format(&self, host_stats: HashMap<String, HostStats>) -> Result<String> {
let markdown = MarkdownHostStats(host_stats);
Ok(markdown.to_string())
}
}