From 2d054bb9a19b5bca9d251b773a66a970e97dfdca Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Sat, 21 Sep 2019 21:20:01 -0700 Subject: [PATCH] Add history to TX/RX status. --- Cargo.lock | 7 +++++++ src/bin/home.rs | 2 +- src/bin/work.rs | 2 +- src/widgets/network.rs | 36 +++++++++++++++++++++++++++++++----- 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c0e63a9..3a1f6b3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -149,6 +149,7 @@ dependencies = [ "chrono-tz 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "i3monkit 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 1.10.1 (registry+https://github.com/rust-lang/crates.io-index)", + "spark 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "structopt 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -361,6 +362,11 @@ dependencies = [ "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "spark" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "structopt" version = "0.2.18" @@ -515,6 +521,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum serde_derive 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)" = "4b133a43a1ecd55d4086bd5b4dc6c1751c68b1bfbeba7a5040442022c7e7c02e" "checksum serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)" = "051c49229f282f7c6f3813f8286cc1e3323e8051823fce42c7ea80fe13521704" "checksum socket2 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)" = "e8b74de517221a2cb01a53349cf54182acdc31a074727d3079068448c0676d85" +"checksum spark 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cdfdbc4ce5316afaa9023d28ed77019bc0860e68e6e5857643a14071520347cb" "checksum structopt 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "16c2cdbf9cc375f15d1b4141bc48aeef444806655cd0e904207edc8d68d86ed7" "checksum structopt-derive 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "53010261a84b37689f9ed7d395165029f9cc7abb9f56bbfe86bee2597ed25107" "checksum syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)" = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5" diff --git a/src/bin/home.rs b/src/bin/home.rs index c139f73..eb7e774 100644 --- a/src/bin/home.rs +++ b/src/bin/home.rs @@ -26,7 +26,7 @@ fn main() { } // Realtime upload/download rate for a interface - bar.push(NetworkSpeedWidget::new(&opts.nic)); + bar.push(NetworkSpeedWidget::new(&opts.nic, 6)); bar.push(DateTimeWidget::new("%m/%d %H:%M".to_string())); diff --git a/src/bin/work.rs b/src/bin/work.rs index e329d4b..23672ea 100644 --- a/src/bin/work.rs +++ b/src/bin/work.rs @@ -26,7 +26,7 @@ fn main() { } // Realtime upload/download rate for a interface - bar.push(NetworkSpeedWidget::new(&opts.nic)); + bar.push(NetworkSpeedWidget::new(&opts.nic, 6)); bar.push(DateTimeWidget::tz( "%H:%M %Z".to_string(), diff --git a/src/widgets/network.rs b/src/widgets/network.rs index 96b74e6..128d816 100644 --- a/src/widgets/network.rs +++ b/src/widgets/network.rs @@ -1,12 +1,14 @@ use i3monkit::Block; use i3monkit::{Widget, WidgetUpdate}; -use std::path::PathBuf; - +use std::collections::vec_deque::VecDeque; use std::fs::File; use std::io::{BufRead, BufReader, Error, ErrorKind, Result}; +use std::path::PathBuf; use std::time::SystemTime; +use spark; + const NETWORK_PATH_PREFIX: &'static str = "/sys/class/net"; const NETWORK_STAT_SUFFIX: &'static str = "statistics/dummy"; @@ -72,18 +74,24 @@ impl TransferStat { pub struct NetworkSpeedWidget { interface: String, last_stat: TransferStat, + rx_history: VecDeque, + tx_history: VecDeque, } impl NetworkSpeedWidget { /// Create the widget, for given interface. /// /// **interface** The interface to monitor - pub fn new(interface: &str) -> Self { + pub fn new(interface: &str, num_samples: usize) -> Self { let last_stat = TransferStat::read_stat(interface).unwrap(); let interface = interface.to_string(); + let rx_history: VecDeque = (0..num_samples).map(|_| 0.).collect(); + let tx_history: VecDeque = (0..num_samples).map(|_| 0.).collect(); Self { last_stat, interface, + rx_history, + tx_history, } } @@ -116,6 +124,12 @@ impl NetworkSpeedWidget { let rx_rate = cur_stat.rx_rate(&self.last_stat); let tx_rate = cur_stat.tx_rate(&self.last_stat); + dbg!(&self.rx_history); + dbg!(&self.tx_history); + self.rx_history.push_back(rx_rate as f32); + self.rx_history.pop_front(); + self.tx_history.push_back(tx_rate as f32); + self.tx_history.pop_front(); self.last_stat = cur_stat; return Ok((Self::format_rate(rx_rate), Self::format_rate(tx_rate))); @@ -126,10 +140,22 @@ impl Widget for NetworkSpeedWidget { fn update(&mut self) -> Option { if let Ok((rx, tx)) = self.get_human_readable_stat() { let mut data = Block::new(); + let (rx_history, tx_history) = if self.rx_history.len() > 1 { + ( + spark::graph(&self.rx_history.iter().cloned().collect::>()), + spark::graph(&self.tx_history.iter().cloned().collect::>()), + ) + } else { + ("".to_string(), "".to_string()) + }; + eprintln!("rx_history {} tx_history {}", &rx_history, &tx_history); data.use_pango(); - data.append_full_text(&format!("Rx:{} Tx:{}", rx, tx)); + data.append_full_text(&format!( + "Rx:{}{} Tx:{}{}", + rx, rx_history, tx, tx_history + )); return Some(WidgetUpdate { - refresh_interval: std::time::Duration::new(1, 0), + refresh_interval: std::time::Duration::new(10, 0), data: Some(data), }); }