From bfdaa09e3a397df08ff45c44022537195bab0c2b Mon Sep 17 00:00:00 2001 From: Clement Tsang <34804052+ClementTsang@users.noreply.github.com> Date: Thu, 17 Dec 2020 15:42:08 -0800 Subject: uptick: 0.5.6 (#362) --- src/app/data_harvester.rs | 37 ++++++++++++++++++++++++++-------- src/app/data_harvester/network.rs | 42 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 8 deletions(-) (limited to 'src/app') diff --git a/src/app/data_harvester.rs b/src/app/data_harvester.rs index 3644654f..cff9bc2e 100644 --- a/src/app/data_harvester.rs +++ b/src/app/data_harvester.rs @@ -135,6 +135,11 @@ impl DataCollector { self.sys.refresh_components_list(); } + // Refresh network list once... + if cfg!(target_os = "windows") && self.widgets_to_harvest.use_net { + self.sys.refresh_networks_list(); + } + if self.widgets_to_harvest.use_battery { // trace!("First run battery vec creation."); if let Ok(battery_manager) = Manager::new() { @@ -188,6 +193,9 @@ impl DataCollector { self.sys.refresh_components(); } } + if cfg!(target_os = "windows") && self.widgets_to_harvest.use_net { + self.sys.refresh_networks(); + } let current_instant = std::time::Instant::now(); @@ -233,15 +241,28 @@ impl DataCollector { } } - // I am *well* aware that the sysinfo part w/ blocking code is... not great. let network_data_fut = { - network::get_network_data( - self.last_collection_time, - &mut self.total_rx, - &mut self.total_tx, - current_instant, - self.widgets_to_harvest.use_net, - ) + #[cfg(target_os = "windows")] + { + network::get_network_data( + &self.sys, + self.last_collection_time, + &mut self.total_rx, + &mut self.total_tx, + current_instant, + self.widgets_to_harvest.use_net, + ) + } + #[cfg(not(target_os = "windows"))] + { + network::get_network_data( + self.last_collection_time, + &mut self.total_rx, + &mut self.total_tx, + current_instant, + self.widgets_to_harvest.use_net, + ) + } }; let mem_data_fut = mem::get_mem_data(self.widgets_to_harvest.use_mem); let disk_data_fut = disks::get_disk_usage(self.widgets_to_harvest.use_disk); diff --git a/src/app/data_harvester/network.rs b/src/app/data_harvester/network.rs index e15409c5..d4ff3ca9 100644 --- a/src/app/data_harvester/network.rs +++ b/src/app/data_harvester/network.rs @@ -15,6 +15,48 @@ impl NetworkHarvest { } } +#[cfg(target_os = "windows")] +pub async fn get_network_data( + sys: &sysinfo::System, prev_net_access_time: Instant, prev_net_rx: &mut u64, + prev_net_tx: &mut u64, curr_time: Instant, actually_get: bool, +) -> crate::utils::error::Result> { + use sysinfo::{NetworkExt, SystemExt}; + + if !actually_get { + return Ok(None); + } + + let mut total_rx: u64 = 0; + let mut total_tx: u64 = 0; + + let networks = sys.get_networks(); + for (_, network) in networks { + total_rx += network.get_total_received(); + total_tx += network.get_total_transmitted(); + } + + let elapsed_time = curr_time.duration_since(prev_net_access_time).as_secs_f64(); + + let (rx, tx) = if elapsed_time == 0.0 { + (0, 0) + } else { + ( + ((total_rx.saturating_sub(*prev_net_rx)) as f64 / elapsed_time) as u64, + ((total_tx.saturating_sub(*prev_net_tx)) as f64 / elapsed_time) as u64, + ) + }; + + *prev_net_rx = total_rx; + *prev_net_tx = total_tx; + Ok(Some(NetworkHarvest { + rx, + tx, + total_rx, + total_tx, + })) +} + +#[cfg(not(target_os = "windows"))] pub async fn get_network_data( prev_net_access_time: Instant, prev_net_rx: &mut u64, prev_net_tx: &mut u64, curr_time: Instant, actually_get: bool, -- cgit v1.2.3