1use super::*;
2
3#[derive(Clone, Debug, Default)]
4pub struct Traffic {
5 pub inbound: usize,
6 pub outbound: usize,
7}
8
9impl Traffic {
10 pub fn new() -> Self {
11 Self {
12 inbound: 0,
13 outbound: 0,
14 }
15 }
16}
17
18pub struct TrafficWatcher {
19 timer: Timer,
20 start_value: Traffic,
21 last_delta: Traffic,
22}
23
24impl std::fmt::Display for TrafficWatcher {
25 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
26 write!(
27 f,
28 "in: {}, out: {}",
29 self.last_delta.inbound, self.last_delta.outbound
30 )
31 }
32}
33
34impl TrafficWatcher {
35 #[allow(clippy::new_without_default)]
36 pub fn new() -> Self {
37 Self {
38 timer: Timer::new(),
39 start_value: Traffic::new(),
40 last_delta: Traffic::new(),
41 }
42 }
43 pub fn update(&mut self, traffic: &Traffic) {
44 if self.timer.elapsed().as_secs_f64() > 1.0 {
45 self.timer = Timer::new();
46 self.last_delta = Traffic {
47 inbound: traffic.inbound - self.start_value.inbound,
48 outbound: traffic.outbound - self.start_value.outbound,
49 };
50 self.start_value = traffic.clone();
51 }
52 }
53}