diff options
author | Clement Tsang <34804052+ClementTsang@users.noreply.github.com> | 2020-12-03 23:45:54 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-03 23:45:54 -0500 |
commit | e56367fcbb933850f82140962542fb3f31627517 (patch) | |
tree | 72a6be765b490107fbfd9a4aa876e8bbcd77134f /src/app | |
parent | 86169871cb51f0524300878b8c9195eff33a91fb (diff) |
refactor: Clean up some parts of data harvesting (#336)
Diffstat (limited to 'src/app')
-rw-r--r-- | src/app/data_harvester.rs | 24 | ||||
-rw-r--r-- | src/app/data_harvester/battery_harvester.rs | 32 | ||||
-rw-r--r-- | src/app/data_harvester/disks.rs | 1 | ||||
-rw-r--r-- | src/app/data_harvester/mem.rs | 44 | ||||
-rw-r--r-- | src/app/data_harvester/network.rs | 1 | ||||
-rw-r--r-- | src/app/data_harvester/processes.rs | 5 | ||||
-rw-r--r-- | src/app/data_harvester/temperature.rs | 13 |
7 files changed, 62 insertions, 58 deletions
diff --git a/src/app/data_harvester.rs b/src/app/data_harvester.rs index 0f59b54f..9cf8de62 100644 --- a/src/app/data_harvester.rs +++ b/src/app/data_harvester.rs @@ -272,7 +272,7 @@ impl DataCollector { } } - // Async if Heim + // I am *well* aware that the sysinfo part w/ blocking code is... not great. let network_data_fut = { #[cfg(any(target_os = "windows", target_arch = "aarch64", target_arch = "arm"))] { @@ -307,17 +307,6 @@ impl DataCollector { mem::get_mem_data(self.widgets_to_harvest.use_mem) } }; - let swap_data_fut = { - #[cfg(any(target_arch = "aarch64", target_arch = "arm"))] - { - mem::get_swap_data(&self.sys, self.widgets_to_harvest.use_mem) - } - - #[cfg(not(any(target_arch = "aarch64", target_arch = "arm")))] - { - mem::get_swap_data(self.widgets_to_harvest.use_mem) - } - }; let disk_data_fut = { #[cfg(any(target_arch = "aarch64", target_arch = "arm"))] { @@ -343,7 +332,7 @@ impl DataCollector { let temp_data_fut = { #[cfg(any(not(target_os = "linux"), target_arch = "aarch64", target_arch = "arm"))] { - temperature::arm_and_non_linux_temperature_data( + temperature::get_temperature_data( &self.sys, &self.temperature_type, self.widgets_to_harvest.use_temp, @@ -356,17 +345,16 @@ impl DataCollector { target_arch = "arm" )))] { - temperature::linux_temperature_data( + temperature::get_temperature_data( &self.temperature_type, self.widgets_to_harvest.use_temp, ) } }; - let (net_data, mem_res, swap_res, disk_res, io_res, temp_res) = join!( + let (net_data, mem_res, disk_res, io_res, temp_res) = join!( network_data_fut, mem_data_fut, - swap_data_fut, disk_data_fut, disk_io_usage_fut, temp_data_fut @@ -388,14 +376,14 @@ impl DataCollector { } } - if let Ok(memory) = mem_res { + if let Ok(memory) = mem_res.0 { self.data.memory = memory; if log_enabled!(log::Level::Trace) { trace!("mem: {:?} results", self.data.memory); } } - if let Ok(swap) = swap_res { + if let Ok(swap) = mem_res.1 { self.data.swap = swap; if log_enabled!(log::Level::Trace) { trace!("swap: {:?} results", self.data.swap); diff --git a/src/app/data_harvester/battery_harvester.rs b/src/app/data_harvester/battery_harvester.rs index c4623cad..66e3c76b 100644 --- a/src/app/data_harvester/battery_harvester.rs +++ b/src/app/data_harvester/battery_harvester.rs @@ -1,5 +1,5 @@ use battery::{ - units::{power::watt, ratio::percent, time::second, Time}, + units::{power::watt, ratio::percent, time::second}, Battery, Manager, }; @@ -12,26 +12,28 @@ pub struct BatteryHarvest { pub health_percent: f64, } -fn convert_optional_time_to_optional_seconds(optional_time: Option<Time>) -> Option<i64> { - if let Some(time) = optional_time { - Some(f64::from(time.get::<second>()) as i64) - } else { - None - } -} - pub fn refresh_batteries(manager: &Manager, batteries: &mut [Battery]) -> Vec<BatteryHarvest> { batteries .iter_mut() .filter_map(|battery| { if manager.refresh(battery).is_ok() { Some(BatteryHarvest { - secs_until_full: convert_optional_time_to_optional_seconds( - battery.time_to_full(), - ), - secs_until_empty: convert_optional_time_to_optional_seconds( - battery.time_to_empty(), - ), + secs_until_full: { + let optional_time = battery.time_to_full(); + if let Some(time) = optional_time { + Some(f64::from(time.get::<second>()) as i64) + } else { + None + } + }, + secs_until_empty: { + let optional_time = battery.time_to_empty(); + if let Some(time) = optional_time { + Some(f64::from(time.get::<second>()) as i64) + } else { + None + } + }, charge_percent: f64::from(battery.state_of_charge().get::<percent>()), power_consumption_rate_watts: f64::from(battery.energy_rate().get::<watt>()), health_percent: f64::from(battery.state_of_health().get::<percent>()), diff --git a/src/app/data_harvester/disks.rs b/src/app/data_harvester/disks.rs index cb7ca0f4..2d606c14 100644 --- a/src/app/data_harvester/disks.rs +++ b/src/app/data_harvester/disks.rs @@ -36,6 +36,7 @@ pub async fn get_disk_usage( sys: &sysinfo::System, actually_get: bool, ) -> crate::utils::error::Result<Option<Vec<DiskHarvest>>> { use sysinfo::{DiskExt, SystemExt}; + if !actually_get { return Ok(None); } diff --git a/src/app/data_harvester/mem.rs b/src/app/data_harvester/mem.rs index 5f91b40e..2a3730b9 100644 --- a/src/app/data_harvester/mem.rs +++ b/src/app/data_harvester/mem.rs @@ -1,3 +1,5 @@ +use futures::join; + #[derive(Debug, Clone)] pub struct MemHarvest { pub mem_total_in_mb: u64, @@ -16,11 +18,20 @@ impl Default for MemHarvest { #[cfg(any(target_arch = "aarch64", target_arch = "arm"))] pub async fn get_mem_data( sys: &sysinfo::System, actually_get: bool, -) -> crate::utils::error::Result<Option<MemHarvest>> { - use sysinfo::SystemExt; +) -> ( + crate::utils::error::Result<Option<MemHarvest>>, + crate::utils::error::Result<Option<MemHarvest>>, +) { if !actually_get { - return Ok(None); + (Ok(None), Ok(None)) + } else { + (get_ram_data(sys), get_swap_data(sys)) } +} + +#[cfg(any(target_arch = "aarch64", target_arch = "arm"))] +pub fn get_ram_data(sys: &sysinfo::System) -> crate::utils::error::Result<Option<MemHarvest>> { + use sysinfo::SystemExt; Ok(Some(MemHarvest { mem_total_in_mb: sys.get_total_memory() / 1024, @@ -29,13 +40,8 @@ pub async fn get_mem_data( } #[cfg(any(target_arch = "aarch64", target_arch = "arm"))] -pub async fn get_swap_data( - sys: &sysinfo::System, actually_get: bool, -) -> crate::utils::error::Result<Option<MemHarvest>> { +pub fn get_swap_data(sys: &sysinfo::System) -> crate::utils::error::Result<Option<MemHarvest>> { use sysinfo::SystemExt; - if !actually_get { - return Ok(None); - } Ok(Some(MemHarvest { mem_total_in_mb: sys.get_total_swap() / 1024, @@ -44,11 +50,21 @@ pub async fn get_swap_data( } #[cfg(not(any(target_arch = "aarch64", target_arch = "arm")))] -pub async fn get_mem_data(actually_get: bool) -> crate::utils::error::Result<Option<MemHarvest>> { +pub async fn get_mem_data( + actually_get: bool, +) -> ( + crate::utils::error::Result<Option<MemHarvest>>, + crate::utils::error::Result<Option<MemHarvest>>, +) { if !actually_get { - return Ok(None); + (Ok(None), Ok(None)) + } else { + join!(get_ram_data(), get_swap_data()) } +} +#[cfg(not(any(target_arch = "aarch64", target_arch = "arm")))] +pub async fn get_ram_data() -> crate::utils::error::Result<Option<MemHarvest>> { let memory = heim::memory::memory().await?; Ok(Some(MemHarvest { @@ -61,11 +77,7 @@ pub async fn get_mem_data(actually_get: bool) -> crate::utils::error::Result<Opt } #[cfg(not(any(target_arch = "aarch64", target_arch = "arm")))] -pub async fn get_swap_data(actually_get: bool) -> crate::utils::error::Result<Option<MemHarvest>> { - if !actually_get { - return Ok(None); - } - +pub async fn get_swap_data() -> crate::utils::error::Result<Option<MemHarvest>> { let memory = heim::memory::swap().await?; Ok(Some(MemHarvest { diff --git a/src/app/data_harvester/network.rs b/src/app/data_harvester/network.rs index 9b37fc7a..9836cfb0 100644 --- a/src/app/data_harvester/network.rs +++ b/src/app/data_harvester/network.rs @@ -15,7 +15,6 @@ impl NetworkHarvest { } } -/// Meant for Windows and ARM use. #[cfg(any(target_os = "windows", target_arch = "aarch64", target_arch = "arm"))] pub async fn get_network_data( sys: &sysinfo::System, prev_net_access_time: Instant, prev_net_rx: &mut u64, diff --git a/src/app/data_harvester/processes.rs b/src/app/data_harvester/processes.rs index a79f8585..df7ae032 100644 --- a/src/app/data_harvester/processes.rs +++ b/src/app/data_harvester/processes.rs @@ -404,7 +404,10 @@ pub fn get_process_data( Ok(process_vector) } else { - Ok(Vec::new()) + trace!("Could not calculate CPU usage."); + Err(BottomError::GenericError( + "Could not calculate CPU usage.".to_string(), + )) } } diff --git a/src/app/data_harvester/temperature.rs b/src/app/data_harvester/temperature.rs index 59fad22a..c3925ceb 100644 --- a/src/app/data_harvester/temperature.rs +++ b/src/app/data_harvester/temperature.rs @@ -20,13 +20,16 @@ impl Default for TemperatureType { } } -/// Meant for ARM and non-Linux usage. #[cfg(any(not(target_os = "linux"), target_arch = "aarch64", target_arch = "arm"))] -pub async fn arm_and_non_linux_temperature_data( +pub async fn get_temperature_data( sys: &sysinfo::System, temp_type: &TemperatureType, actually_get: bool, ) -> crate::utils::error::Result<Option<Vec<TempHarvest>>> { use sysinfo::{ComponentExt, SystemExt}; + if !actually_get { + return Ok(None); + } + fn convert_celsius_to_kelvin(celsius: f32) -> f32 { celsius + 273.15 } @@ -35,10 +38,6 @@ pub async fn arm_and_non_linux_temperature_data( (celsius * (9.0 / 5.0)) + 32.0 } - if !actually_get { - return Ok(None); - } - let mut temperature_vec: Vec<TempHarvest> = Vec::new(); let sensor_data = sys.get_components(); @@ -61,7 +60,7 @@ pub async fn arm_and_non_linux_temperature_data( } #[cfg(not(any(not(target_os = "linux"), target_arch = "aarch64", target_arch = "arm")))] -pub async fn linux_temperature_data( +pub async fn get_temperature_data( temp_type: &TemperatureType, actually_get: bool, ) -> crate::utils::error::Result<Option<Vec<TempHarvest>>> { use futures::StreamExt; |