Add history to TX/RX status.

This commit is contained in:
Bill Thiede 2019-09-21 21:20:01 -07:00
parent f09145eccd
commit 2d054bb9a1
4 changed files with 40 additions and 7 deletions

7
Cargo.lock generated
View File

@ -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"

View File

@ -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()));

View File

@ -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(),

View File

@ -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<f32>,
tx_history: VecDeque<f32>,
}
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<f32> = (0..num_samples).map(|_| 0.).collect();
let tx_history: VecDeque<f32> = (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<WidgetUpdate> {
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::<Vec<f32>>()),
spark::graph(&self.tx_history.iter().cloned().collect::<Vec<f32>>()),
)
} else {
("".to_string(), "".to_string())
};
eprintln!("rx_history {} tx_history {}", &rx_history, &tx_history);
data.use_pango();
data.append_full_text(&format!("Rx:<tt>{}</tt> Tx:<tt>{}</tt>", rx, tx));
data.append_full_text(&format!(
"Rx:<tt>{}</tt>{} Tx:<tt>{}</tt>{}",
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),
});
}