Skip to content

Commit 5c41781

Browse files
committed
feat(reader): add check tester
1 parent 07cbf41 commit 5c41781

File tree

3 files changed

+88
-13
lines changed

3 files changed

+88
-13
lines changed

src/bins/daemon.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ pub(crate) fn daemon() {
3535
eprintln!("error in the wakeup turn: {err}");
3636
}
3737
}
38-
println!("done! sleeping...");
3938
std::thread::sleep(Duration::from_secs(1));
4039
}
4140
}
@@ -48,6 +47,8 @@ fn wakeup(store: &mut Store) -> Result<(), StoreError> {
4847
if let Err(err) = store.save() {
4948
eprintln!("error while saving to file: {err:}");
5049
}
50+
51+
println!("done!");
5152
Ok(())
5253
}
5354

src/bins/netpulse.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,45 @@
1-
use netpulse::records::Check;
1+
use getopts::Options;
2+
use netpulse::records::{Check, CheckType};
23
use netpulse::store::Store;
34

45
fn main() {
6+
let args: Vec<String> = std::env::args().collect();
7+
let program = &args[0];
8+
let mut opts = Options::new();
9+
opts.optflag("h", "help", "print this help menu");
10+
opts.optflag("t", "test", "test run all checks");
11+
let matches = match opts.parse(&args[1..]) {
12+
Ok(m) => m,
13+
Err(f) => {
14+
panic!("{}", f.to_string())
15+
}
16+
};
17+
18+
if matches.opt_present("help") {
19+
print_usage(program, opts);
20+
} else if matches.opt_present("test") {
21+
test_checks();
22+
} else {
23+
analyze();
24+
}
25+
}
26+
27+
fn print_usage(program: &str, opts: Options) {
28+
let brief = format!("Usage: {} [options]", program);
29+
print!("{}", opts.usage(&brief));
30+
}
31+
32+
fn test_checks() {
33+
for check_type in CheckType::all() {
34+
let check = check_type.make();
35+
println!("{check}");
36+
}
37+
}
38+
39+
fn analyze() {
540
let store = Store::load().expect("store file not found");
641
let checks = store.checks();
7-
let successes: Vec<&Check> = checks.iter().filter(|c| c.is_ok()).collect();
42+
let successes: Vec<&Check> = checks.iter().filter(|c| c.is_success()).collect();
843
println!("store contains {:09} checks.", checks.len());
944
println!("store contains {:09} successful checks.", successes.len());
1045
println!(

src/records.rs

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::fmt::Display;
12
use std::net::IpAddr;
23
use std::str::FromStr;
34
use std::time;
@@ -39,6 +40,7 @@ pub enum CheckType {
3940
Http,
4041
IcmpV4,
4142
IcmpV6,
43+
Unknown,
4244
}
4345
impl CheckType {
4446
/// Make a new [Check] of this type.
@@ -100,14 +102,19 @@ impl CheckType {
100102
}
101103
}
102104

103-
impl From<CheckType> for CheckFlag {
104-
fn from(value: CheckType) -> Self {
105-
match value {
106-
CheckType::Dns => CheckFlag::TypeDns,
107-
CheckType::Http => CheckFlag::TypeHTTP,
108-
CheckType::IcmpV4 => CheckFlag::TypeIcmp,
109-
CheckType::IcmpV6 => CheckFlag::TypeIcmp,
110-
}
105+
impl Display for CheckType {
106+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
107+
write!(
108+
f,
109+
"{}",
110+
match self {
111+
Self::Dns => "DNS",
112+
Self::Http => "HTTP(S)",
113+
Self::IcmpV4 => "ICMPv4",
114+
Self::IcmpV6 => "ICMPv6",
115+
Self::Unknown => "Unknown",
116+
}
117+
)
111118
}
112119
}
113120

@@ -149,13 +156,13 @@ impl Check {
149156
///
150157
/// Ok means, it's a [Success](CheckFlag::Success), and has no weird anomalies (that this
151158
/// checks for).
152-
pub fn is_ok(&self) -> bool {
159+
pub fn is_success(&self) -> bool {
153160
self.flags.contains(CheckFlag::Success)
154161
}
155162

156163
/// Returns the latency of this [`Check`].
157164
pub fn latency(&self) -> Option<u16> {
158-
if !self.is_ok() {
165+
if !self.is_success() {
159166
None
160167
} else {
161168
self.latency
@@ -181,6 +188,38 @@ impl Check {
181188
pub fn add_flag(&mut self, flag: CheckFlag) {
182189
self.flags |= flag
183190
}
191+
192+
/// Check the flags and infer the [CheckType]
193+
pub fn calc_type(&self) -> CheckType {
194+
if self.flags.contains(CheckFlag::TypeHTTP) {
195+
CheckType::Http
196+
} else if self.flags.contains(CheckFlag::TypeDns) {
197+
CheckType::Dns
198+
} else if self.flags.contains(CheckFlag::TypeIcmp) {
199+
if self.flags.contains(CheckFlag::IPv4) {
200+
CheckType::IcmpV4
201+
} else if self.flags.contains(CheckFlag::IPv6) {
202+
CheckType::IcmpV6
203+
} else {
204+
eprintln!("flag for ICMP is set, but not if ipv4 or ipv6 was used");
205+
CheckType::Unknown
206+
}
207+
} else {
208+
CheckType::Unknown
209+
}
210+
}
211+
}
212+
213+
impl Display for Check {
214+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
215+
writeln!(f, "Type: {}\nOk: {}", self.calc_type(), self.is_success())?;
216+
writeln!(f, "Latency: {}", {
217+
match self.latency() {
218+
Some(l) => format!("{l} ms"),
219+
None => "(Error)".to_string(),
220+
}
221+
})
222+
}
184223
}
185224

186225
#[cfg(test)]

0 commit comments

Comments
 (0)