-
-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathstats.rs
185 lines (155 loc) · 6.24 KB
/
stats.rs
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Disable lint, clippy thinks that InputSource has inner mutability, but this seems like a flase positive
#![allow(clippy::mutable_key_type)]
use std::collections::{HashMap, HashSet};
use crate::archive::Suggestion;
use lychee_lib::{CacheStatus, InputSource, Response, ResponseBody, Status};
use serde::Serialize;
#[derive(Default, Serialize, Debug)]
pub(crate) struct ResponseStats {
pub(crate) total: usize,
pub(crate) successful: usize,
pub(crate) unknown: usize,
pub(crate) unsupported: usize,
pub(crate) timeouts: usize,
pub(crate) redirects: usize,
pub(crate) excludes: usize,
pub(crate) errors: usize,
pub(crate) cached: usize,
pub(crate) success_map: HashMap<InputSource, HashSet<ResponseBody>>,
pub(crate) fail_map: HashMap<InputSource, HashSet<ResponseBody>>,
pub(crate) suggestion_map: HashMap<InputSource, HashSet<Suggestion>>,
pub(crate) excluded_map: HashMap<InputSource, HashSet<ResponseBody>>,
pub(crate) duration_secs: u64,
pub(crate) detailed_stats: bool,
}
impl ResponseStats {
#[inline]
pub(crate) fn extended() -> Self {
Self {
detailed_stats: true,
..Default::default()
}
}
pub(crate) fn increment_status_counters(&mut self, status: &Status) {
match status {
Status::Ok(_) => self.successful += 1,
Status::Error(_) => self.errors += 1,
Status::UnknownStatusCode(_) => self.unknown += 1,
Status::Timeout(_) => self.timeouts += 1,
Status::Redirected(_) => self.redirects += 1,
Status::Excluded => self.excludes += 1,
Status::Unsupported(_) => self.unsupported += 1,
Status::Cached(cache_status) => {
self.cached += 1;
match cache_status {
CacheStatus::Ok(_) => self.successful += 1,
CacheStatus::Error(_) => self.errors += 1,
CacheStatus::Excluded => self.excludes += 1,
CacheStatus::Unsupported => self.unsupported += 1,
}
}
}
}
pub(crate) fn add(&mut self, response: Response) {
self.total += 1;
let Response(source, ResponseBody { ref status, .. }) = response;
self.increment_status_counters(status);
match status {
_ if status.is_failure() => {
let fail = self.fail_map.entry(source).or_default();
fail.insert(response.1);
}
Status::Ok(_) if self.detailed_stats => {
let success = self.success_map.entry(source).or_default();
success.insert(response.1);
}
Status::Excluded if self.detailed_stats => {
let excluded = self.excluded_map.entry(source).or_default();
excluded.insert(response.1);
}
_ => (),
}
}
#[inline]
pub(crate) const fn is_success(&self) -> bool {
self.total == self.successful + self.excludes + self.unsupported
}
#[inline]
pub(crate) const fn is_empty(&self) -> bool {
self.total == 0
}
}
#[cfg(test)]
mod tests {
use pretty_assertions::assert_eq;
use std::collections::{HashMap, HashSet};
use http::StatusCode;
use lychee_lib::{ErrorKind, InputSource, Response, ResponseBody, Status, Uri};
use reqwest::Url;
use super::ResponseStats;
fn website(url: &str) -> Uri {
Uri::from(Url::parse(url).expect("Expected valid Website URI"))
}
// Generate a fake response with a given status code
// Don't use a mock server for this, as it's not necessary
// and it's a lot faster to just generate a fake response
fn mock_response(status: Status) -> Response {
let uri = website("https://some-url.com/ok");
let response_body = ResponseBody { uri, status };
Response(InputSource::Stdin, response_body)
}
fn dummy_ok() -> Response {
mock_response(Status::Ok(StatusCode::OK))
}
fn dummy_error() -> Response {
mock_response(Status::Error(ErrorKind::InvalidStatusCode(1000)))
}
fn dummy_excluded() -> Response {
mock_response(Status::Excluded)
}
#[tokio::test]
async fn test_stats_is_empty() {
let mut stats = ResponseStats::default();
assert!(stats.is_empty());
stats.add(dummy_error());
assert!(!stats.is_empty());
}
#[tokio::test]
async fn test_stats() {
let mut stats = ResponseStats::default();
assert!(stats.success_map.is_empty());
assert!(stats.excluded_map.is_empty());
stats.add(dummy_error());
stats.add(dummy_ok());
let Response(source, body) = dummy_error();
let expected_fail_map: HashMap<InputSource, HashSet<ResponseBody>> =
HashMap::from_iter([(source, HashSet::from_iter([body]))]);
assert_eq!(stats.fail_map, expected_fail_map);
assert!(stats.success_map.is_empty());
}
#[tokio::test]
async fn test_detailed_stats() {
let mut stats = ResponseStats::extended();
assert!(stats.success_map.is_empty());
assert!(stats.fail_map.is_empty());
assert!(stats.excluded_map.is_empty());
stats.add(dummy_error());
stats.add(dummy_excluded());
stats.add(dummy_ok());
let mut expected_fail_map: HashMap<InputSource, HashSet<ResponseBody>> = HashMap::new();
let Response(source, response_body) = dummy_error();
let entry = expected_fail_map.entry(source).or_default();
entry.insert(response_body);
assert_eq!(stats.fail_map, expected_fail_map);
let mut expected_success_map: HashMap<InputSource, HashSet<ResponseBody>> = HashMap::new();
let Response(source, response_body) = dummy_ok();
let entry = expected_success_map.entry(source).or_default();
entry.insert(response_body);
assert_eq!(stats.success_map, expected_success_map);
let mut expected_excluded_map: HashMap<InputSource, HashSet<ResponseBody>> = HashMap::new();
let Response(source, response_body) = dummy_excluded();
let entry = expected_excluded_map.entry(source).or_default();
entry.insert(response_body);
assert_eq!(stats.excluded_map, expected_excluded_map);
}
}