refactor: Clean up some parts of data harvesting (#336)

This commit is contained in:
Clement Tsang 2020-12-03 23:45:54 -05:00 committed by GitHub
parent 86169871cb
commit e56367fcbb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 64 additions and 300 deletions

View file

@ -4,8 +4,8 @@ set -e
echo "Running pre-push hook:"
echo "Executing: cargo clippy -- -D clippy::all"
cargo clippy -- -D clippy::all
echo "Executing: cargo clippy --all-targets --workspace -- -D warnings"
cargo clippy --all-targets --workspace -- -D warnings
# echo "Executing: cargo test"
# cargo test

239
Cargo.lock generated
View file

@ -52,111 +52,6 @@ dependencies = [
"wait-timeout",
]
[[package]]
name = "async-channel"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59740d83946db6a5af71ae25ddf9562c2b176b2ca42cf99a455f09f4a220d6b9"
dependencies = [
"concurrent-queue",
"event-listener",
"futures-core",
]
[[package]]
name = "async-executor"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb877970c7b440ead138f6321a3b5395d6061183af779340b65e20c0fede9146"
dependencies = [
"async-task",
"concurrent-queue",
"fastrand",
"futures-lite",
"once_cell",
"vec-arena",
]
[[package]]
name = "async-fs"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b3ca4f8ff117c37c278a2f7415ce9be55560b846b5bc4412aaa5d29c1c3dae2"
dependencies = [
"async-lock",
"blocking",
"futures-lite",
]
[[package]]
name = "async-io"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "458c8f66c246624e7cf87c01451f3392ab77d66a0f105a49d9353b30ea97ced8"
dependencies = [
"concurrent-queue",
"fastrand",
"futures-lite",
"libc",
"log",
"nb-connect",
"once_cell",
"parking",
"polling",
"vec-arena",
"waker-fn",
"winapi",
]
[[package]]
name = "async-lock"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1996609732bde4a9988bc42125f55f2af5f3c36370e27c778d5191a4a1b63bfb"
dependencies = [
"event-listener",
]
[[package]]
name = "async-net"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06de475c85affe184648202401d7622afb32f0f74e02192857d0201a16defbe5"
dependencies = [
"async-io",
"blocking",
"fastrand",
"futures-lite",
]
[[package]]
name = "async-process"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c8cea09c1fb10a317d1b5af8024eeba256d6554763e85ecd90ff8df31c7bbda"
dependencies = [
"async-io",
"blocking",
"cfg-if 0.1.10",
"event-listener",
"futures-lite",
"once_cell",
"signal-hook",
"winapi",
]
[[package]]
name = "async-task"
version = "4.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e91831deabf0d6d7ec49552e489aed63b7456a7a3c46cff62adad428110b0af0"
[[package]]
name = "atomic-waker"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "065374052e7df7ee4047b1160cca5e1467a12351a40b3da123c870ba0b8eda2a"
[[package]]
name = "atty"
version = "0.2.14"
@ -211,20 +106,6 @@ version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]]
name = "blocking"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c5e170dbede1f740736619b776d7251cb1b9095c435c34d8ca9f57fcd2f335e9"
dependencies = [
"async-channel",
"async-task",
"atomic-waker",
"fastrand",
"futures-lite",
"once_cell",
]
[[package]]
name = "bottom"
version = "0.5.3"
@ -250,7 +131,6 @@ dependencies = [
"predicates",
"regex",
"serde",
"smol",
"sysinfo",
"thiserror",
"toml",
@ -261,12 +141,6 @@ dependencies = [
"winapi",
]
[[package]]
name = "cache-padded"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "631ae5198c9be5e753e5cc215e1bd73c2b466a3565173db433f52bb9d3e66dba"
[[package]]
name = "cargo-husky"
version = "1.5.0"
@ -334,15 +208,6 @@ dependencies = [
"bitflags",
]
[[package]]
name = "concurrent-queue"
version = "1.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30ed07550be01594c6026cff2a1d7fe9c8f683caa798e12b68694ac9e88286a3"
dependencies = [
"cache-padded",
]
[[package]]
name = "const_fn"
version = "0.4.3"
@ -506,21 +371,6 @@ version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
[[package]]
name = "event-listener"
version = "2.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7531096570974c3a9dcf9e4b8e1cede1ec26cf5046219fb3b9d897503b9be59"
[[package]]
name = "fastrand"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca5faf057445ce5c9d4329e382b2ce7ca38550ef3b73a5348362d5f24e0c7fe3"
dependencies = [
"instant",
]
[[package]]
name = "fern"
version = "0.6.0"
@ -587,21 +437,6 @@ version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "611834ce18aaa1bd13c4b374f5d653e1027cf99b6b502584ff8c9a64413b30bb"
[[package]]
name = "futures-lite"
version = "1.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e6c079abfac3ab269e2927ec048dabc89d009ebfdda6b8ee86624f30c689658"
dependencies = [
"fastrand",
"futures-core",
"futures-io",
"memchr",
"parking",
"pin-project-lite",
"waker-fn",
]
[[package]]
name = "futures-macro"
version = "0.3.8"
@ -988,16 +823,6 @@ dependencies = [
"winapi",
]
[[package]]
name = "nb-connect"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8123a81538e457d44b933a02faf885d3fe8408806b23fa700e8f01c6c3a98998"
dependencies = [
"libc",
"winapi",
]
[[package]]
name = "nix"
version = "0.17.0"
@ -1111,12 +936,6 @@ dependencies = [
"num-traits",
]
[[package]]
name = "parking"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "427c3892f9e783d91cc128285287e70a59e206ca452770ece88a76f7a3eddd72"
[[package]]
name = "parking_lot"
version = "0.11.0"
@ -1163,12 +982,6 @@ dependencies = [
"syn",
]
[[package]]
name = "pin-project-lite"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c917123afa01924fc84bb20c4c03f004d9c38e5127e3c039bbf7f4b9c76a2f6b"
[[package]]
name = "pin-utils"
version = "0.1.0"
@ -1181,19 +994,6 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "feb3b2b1033b8a60b4da6ee470325f887758c95d5320f52f9ce0df055a55940e"
[[package]]
name = "polling"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2a7bc6b2a29e632e45451c941832803a18cce6781db04de8a04696cdca8bde4"
dependencies = [
"cfg-if 0.1.10",
"libc",
"log",
"wepoll-sys",
"winapi",
]
[[package]]
name = "predicates"
version = "1.0.5"
@ -1411,24 +1211,6 @@ version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fbee7696b84bbf3d89a1c2eccff0850e3047ed46bfcd2e92c29a2d074d57e252"
[[package]]
name = "smol"
version = "1.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85cf3b5351f3e783c1d79ab5fc604eeed8b8ae9abd36b166e8b87a089efd85e4"
dependencies = [
"async-channel",
"async-executor",
"async-fs",
"async-io",
"async-lock",
"async-net",
"async-process",
"blocking",
"futures-lite",
"once_cell",
]
[[package]]
name = "socket2"
version = "0.3.15"
@ -1605,12 +1387,6 @@ dependencies = [
"typenum",
]
[[package]]
name = "vec-arena"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eafc1b9b2dfc6f5529177b62cf806484db55b32dc7c9658a118e11bbeb33061d"
[[package]]
name = "vec_map"
version = "0.8.2"
@ -1632,12 +1408,6 @@ dependencies = [
"libc",
]
[[package]]
name = "waker-fn"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d5b2c62b4012a3e1eca5a7e077d13b3bf498c4073e33ccd58626607748ceeca"
[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"
@ -1650,15 +1420,6 @@ version = "0.10.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f"
[[package]]
name = "wepoll-sys"
version = "3.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fcb14dea929042224824779fbc82d9fab8d2e6d3cbc0ac404de8edf489e77ff"
dependencies = [
"cc",
]
[[package]]
name = "widestring"
version = "0.4.3"

View file

@ -39,7 +39,6 @@ libc = "0.2"
once_cell = "1.5.2"
regex = "1.4.2"
serde = {version = "1.0", features = ["derive"] }
smol = "1.2.5"
sysinfo = "0.15.3"
thiserror = "1.0.22"
toml = "0.5.7"

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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