Add history to TX/RX status.

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

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),
});
}