From 0050b77cafa2a33adf5275b75fbdf1f4a9adf132 Mon Sep 17 00:00:00 2001 From: ClementTsang Date: Sun, 8 Sep 2019 01:01:42 -0400 Subject: [PATCH] Removed the 'timing' aspect, it was irrelevant. --- src/main.rs | 85 ++++++++++++++------------------------ src/widgets/cpu.rs | 19 ++------- src/widgets/disks.rs | 21 ++++------ src/widgets/mem.rs | 9 +--- src/widgets/network.rs | 12 +++--- src/widgets/processes.rs | 23 ++++++++--- src/widgets/temperature.rs | 15 ++----- 7 files changed, 68 insertions(+), 116 deletions(-) diff --git a/src/main.rs b/src/main.rs index a9095c38..2d6715a6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,9 +5,9 @@ use widgets::{cpu, disks, mem, network, processes, temperature}; mod window; -fn push_if_valid(result : &Result, vector_to_push_to : &mut Vec) { +fn set_if_valid(result : &Result, value_to_set : &mut T) { if let Ok(result) = result { - vector_to_push_to.push(result.clone()); + *value_to_set = (*result).clone(); } } @@ -17,13 +17,13 @@ async fn main() { let refresh_interval = 1; // TODO: Make changing this possible! let mut sys = System::new(); - let mut list_of_timed_cpu_packages : Vec = Vec::new(); - let mut list_of_timed_io : Vec> = Vec::new(); - let mut list_of_timed_physical_io : Vec> = Vec::new(); - let mut list_of_timed_memory : Vec = Vec::new(); - let mut list_of_timed_swap : Vec = Vec::new(); - let mut list_of_timed_temperature : Vec = Vec::new(); - let mut list_of_timed_network : Vec = Vec::new(); + let mut list_of_cpu_packages : Vec = Vec::new(); + let mut list_of_io : Vec = Vec::new(); + let mut list_of_physical_io : Vec = Vec::new(); + let mut memory : mem::MemData = mem::MemData::default(); + let mut swap : mem::MemData = mem::MemData::default(); + let mut list_of_temperature : Vec = Vec::new(); + let mut network : network::NetworkData = network::NetworkData::default(); let mut list_of_processes = Vec::new(); let mut list_of_disks = Vec::new(); @@ -34,24 +34,19 @@ async fn main() { // What we want to do: For timed data, if there is an error, just do not add. For other data, just don't update! // TODO: Joining all would be better... - list_of_timed_network.push(network::get_network_data(&sys)); + set_if_valid(&network::get_network_data(&sys), &mut network); - if let Ok(process_vec) = processes::get_sorted_processes_list(processes::ProcessSorting::CPU, true).await { - list_of_processes = process_vec; - } + set_if_valid(&processes::get_sorted_processes_list(processes::ProcessSorting::NAME, false).await, &mut list_of_processes); + set_if_valid(&disks::get_disk_usage_list().await, &mut list_of_disks); - if let Ok(disks) = disks::get_disk_usage_list().await { - list_of_disks = disks; - } + set_if_valid(&disks::get_io_usage_list(false).await, &mut list_of_io); + set_if_valid(&disks::get_io_usage_list(true).await, &mut list_of_physical_io); - push_if_valid(&disks::get_io_usage_list(false).await, &mut list_of_timed_io); - push_if_valid(&disks::get_io_usage_list(true).await, &mut list_of_timed_physical_io); + set_if_valid(&mem::get_mem_data_list().await, &mut memory); + set_if_valid(&mem::get_swap_data_list().await, &mut swap); + set_if_valid(&temperature::get_temperature_data().await, &mut list_of_temperature); - push_if_valid(&mem::get_mem_data_list().await, &mut list_of_timed_memory); - push_if_valid(&mem::get_swap_data_list().await, &mut list_of_timed_swap); - push_if_valid(&temperature::get_temperature_data().await, &mut list_of_timed_temperature); - - push_if_valid(&cpu::get_cpu_data_list(&sys), &mut list_of_timed_cpu_packages); + set_if_valid(&cpu::get_cpu_data_list(&sys), &mut list_of_cpu_packages); println!("End data loop..."); @@ -67,45 +62,27 @@ async fn main() { // TODO: Check if this is valid } - if !list_of_timed_io.is_empty() { - for io in list_of_timed_io.last().unwrap() { - println!("IO counter for {} at {:?}: {} writes, {} reads.", &io.mount_point, io.time, io.write_bytes, io.read_bytes); - } - } - if !list_of_timed_physical_io.is_empty() { - for io in list_of_timed_physical_io.last().unwrap() { - println!("Physical IO counter for {} at {:?}: {} writes, {} reads.", &io.mount_point, io.time, io.write_bytes, io.read_bytes); - } + for io in &list_of_io { + println!("IO counter for {}: {} writes, {} reads.", &io.mount_point, io.write_bytes, io.read_bytes); } - if !list_of_timed_cpu_packages.is_empty() { - let current_cpu_time = list_of_timed_cpu_packages.last().unwrap().time; - for cpu in &list_of_timed_cpu_packages.last().unwrap().processor_list { - println!("CPU {} has {}% usage at timestamp {:?}!", &cpu.cpu_name, cpu.cpu_usage, current_cpu_time); - } + for io in &list_of_physical_io { + println!("Physical IO counter for {}: {} writes, {} reads.", &io.mount_point, io.write_bytes, io.read_bytes); } - if !list_of_timed_memory.is_empty() { - let current_mem = list_of_timed_memory.last().unwrap(); - println!("Memory usage: {} out of {} is used, at {:?}", current_mem.mem_used, current_mem.mem_total, current_mem.time); + for cpu in &list_of_cpu_packages { + println!("CPU {} has {}% usage!", &cpu.cpu_name, cpu.cpu_usage); } - if !list_of_timed_swap.is_empty() { - let current_mem = list_of_timed_swap.last().unwrap(); - println!("Memory usage: {} out of {} is used, at {:?}", current_mem.mem_used, current_mem.mem_total, current_mem.time); + println!("Memory usage: {} out of {} is used", memory.mem_used, memory.mem_total); + + println!("Memory usage: {} out of {} is used", swap.mem_used, swap.mem_total); + + for sensor in &list_of_temperature { + println!("Sensor for {} is at {} degrees Celsius", sensor.component_name, sensor.temperature); } - if !list_of_timed_temperature.is_empty() { - let current_time = list_of_timed_temperature.last().unwrap().time; - for sensor in &list_of_timed_temperature.last().unwrap().temperature_vec { - println!("Sensor for {} is at {} degrees Celsius at timestamp {:?}!", sensor.component_name, sensor.temperature, current_time); - } - } - - if !list_of_timed_network.is_empty() { - let current_network = list_of_timed_network.last().unwrap(); - println!("Network: {} rx, {} tx at {:?}", current_network.rx, current_network.tx, current_network.time); - } + println!("Network: {} rx, {} tx", network.rx, network.tx); // Send to drawing module window::draw_terminal(); diff --git a/src/widgets/cpu.rs b/src/widgets/cpu.rs index 48a419f3..b8a629e1 100644 --- a/src/widgets/cpu.rs +++ b/src/widgets/cpu.rs @@ -1,18 +1,12 @@ use sysinfo::{ProcessorExt, System, SystemExt}; -#[derive(Clone)] +#[derive(Clone, Default)] pub struct CPUData { pub cpu_name : Box, pub cpu_usage : u32, } -#[derive(Clone)] -pub struct TimedCPUPackages { - pub processor_list : Vec, - pub time : std::time::SystemTime, -} - -pub fn get_cpu_data_list(sys : &System) -> Result { +pub fn get_cpu_data_list(sys : &System) -> Result, heim::Error> { let cpu_data = sys.get_processor_list(); let mut cpu_vec = Vec::new(); @@ -23,12 +17,5 @@ pub fn get_cpu_data_list(sys : &System) -> Result }) } - Ok(TimedCPUPackages { - processor_list : cpu_vec, - time : std::time::SystemTime::now(), - }) -} - -pub fn is_cpu_data_old() -> bool { - true + Ok(cpu_vec) } diff --git a/src/widgets/disks.rs b/src/widgets/disks.rs index 0739ee4d..7a3ed212 100644 --- a/src/widgets/disks.rs +++ b/src/widgets/disks.rs @@ -1,6 +1,6 @@ use heim_common::prelude::StreamExt; -#[derive(Clone)] +#[derive(Clone, Default)] pub struct DiskInfo { pub name : Box, pub mount_point : Box, @@ -9,25 +9,23 @@ pub struct DiskInfo { pub total_space : u64, } -#[derive(Clone)] -pub struct TimedIOInfo { +#[derive(Clone, Default)] +pub struct IOInfo { pub mount_point : Box, pub read_bytes : u64, pub write_bytes : u64, - pub time : std::time::SystemTime, } -pub async fn get_io_usage_list(get_physical : bool) -> Result, heim::Error> { - let mut io_list : Vec = Vec::new(); +pub async fn get_io_usage_list(get_physical : bool) -> Result, heim::Error> { + let mut io_list : Vec = Vec::new(); if get_physical { let mut physical_counter_stream = heim::disk::io_counters_physical(); while let Some(io) = physical_counter_stream.next().await { let io = io?; - io_list.push(TimedIOInfo { + io_list.push(IOInfo { mount_point : Box::from(io.device_name().to_str().unwrap_or("Name Unavailable")), read_bytes : io.read_bytes().get::(), write_bytes : io.write_bytes().get::(), - time : std::time::SystemTime::now(), }) } } @@ -35,11 +33,10 @@ pub async fn get_io_usage_list(get_physical : bool) -> Result, let mut counter_stream = heim::disk::io_counters(); while let Some(io) = counter_stream.next().await { let io = io?; - io_list.push(TimedIOInfo { + io_list.push(IOInfo { mount_point : Box::from(io.device_name().to_str().unwrap_or("Name Unavailable")), read_bytes : io.read_bytes().get::(), write_bytes : io.write_bytes().get::(), - time : std::time::SystemTime::now(), }) } } @@ -47,10 +44,6 @@ pub async fn get_io_usage_list(get_physical : bool) -> Result, Ok(io_list) } -pub fn is_io_data_old() -> bool { - true -} - pub async fn get_disk_usage_list() -> Result, heim::Error> { let mut vec_disks : Vec = Vec::new(); let mut partitions_stream = heim::disk::partitions_physical(); diff --git a/src/widgets/mem.rs b/src/widgets/mem.rs index 25d3d944..c896351c 100644 --- a/src/widgets/mem.rs +++ b/src/widgets/mem.rs @@ -1,10 +1,9 @@ use heim_common::units::information; -#[derive(Clone)] +#[derive(Clone, Default)] pub struct MemData { pub mem_total : u64, pub mem_used : u64, - pub time : std::time::SystemTime, } pub async fn get_mem_data_list() -> Result { @@ -13,7 +12,6 @@ pub async fn get_mem_data_list() -> Result { Ok(MemData { mem_total : memory.total().get::(), mem_used : memory.total().get::() - memory.available().get::(), - time : std::time::SystemTime::now(), }) } @@ -23,10 +21,5 @@ pub async fn get_swap_data_list() -> Result { Ok(MemData { mem_total : memory.total().get::(), mem_used : memory.used().get::(), - time : std::time::SystemTime::now(), }) } - -pub fn is_mem_data_old() -> bool { - true -} diff --git a/src/widgets/network.rs b/src/widgets/network.rs index 1e8daad4..3094422b 100644 --- a/src/widgets/network.rs +++ b/src/widgets/network.rs @@ -1,17 +1,15 @@ use sysinfo::{NetworkExt, System, SystemExt}; -#[derive(Clone)] -pub struct TimedNetworkData { +#[derive(Clone, Default)] +pub struct NetworkData { pub rx : u64, pub tx : u64, - pub time : std::time::SystemTime, } -pub fn get_network_data(sys : &System) -> TimedNetworkData { +pub fn get_network_data(sys : &System) -> Result { let network_data = sys.get_network(); - TimedNetworkData { + Ok(NetworkData { rx : network_data.get_income(), tx : network_data.get_outcome(), - time : std::time::SystemTime::now(), - } + }) } diff --git a/src/widgets/processes.rs b/src/widgets/processes.rs index c8c3412b..b4e90d4d 100644 --- a/src/widgets/processes.rs +++ b/src/widgets/processes.rs @@ -3,6 +3,7 @@ use heim_common::{ units, }; +#[allow(dead_code)] pub enum ProcessSorting { CPU, MEM, @@ -11,7 +12,7 @@ pub enum ProcessSorting { } // Possible process info struct? -#[derive(Clone, Debug)] +#[derive(Clone, Default)] pub struct ProcessInfo { pub pid : u32, pub cpu_usage_percent : f32, @@ -52,14 +53,22 @@ async fn cpu_usage(process : heim::process::Process) -> heim::process::ProcessRe pub async fn get_sorted_processes_list(sorting_method : ProcessSorting, reverse_order : bool) -> Result, heim::Error> { let mut process_stream = heim::process::processes().map_ok(cpu_usage).try_buffer_unordered(std::usize::MAX); - // TODO: Group together processes - let mut process_vector : Vec = Vec::new(); while let Some(process) = process_stream.next().await { if let Ok(process) = process { let (process, cpu_usage) = process; let mem_measurement = process.memory().await; if let Ok(mem_measurement) = mem_measurement { + /* + // Unsure whether I want to implement this by grouping together process names...? + let mut process_info = process_hashmap.entry(command_name.to_string()).or_insert(ProcessInfo { + command : command_name, + pid : process.pid() as u32, + cpu_usage_percent : cpu_usage.get::(), + mem_usage_in_mb : mem_measurement.rss().get::(), + }); + */ + process_vector.push(ProcessInfo { command : process.name().await.unwrap_or_else(|_| "".to_string()), pid : process.pid() as u32, @@ -69,12 +78,16 @@ pub async fn get_sorted_processes_list(sorting_method : ProcessSorting, reverse_ } } } + sort_processes(sorting_method, &mut process_vector, reverse_order); + + Ok(process_vector) +} + +pub fn sort_processes(sorting_method : ProcessSorting, process_vector : &mut Vec, reverse_order : bool) { match sorting_method { ProcessSorting::CPU => process_vector.sort_by(|a, b| get_ordering(a.cpu_usage_percent, b.cpu_usage_percent, reverse_order)), ProcessSorting::MEM => process_vector.sort_by(|a, b| get_ordering(a.mem_usage_in_mb, b.mem_usage_in_mb, reverse_order)), ProcessSorting::PID => process_vector.sort_by(|a, b| get_ordering(a.pid, b.pid, reverse_order)), ProcessSorting::NAME => process_vector.sort_by(|a, b| get_ordering(&a.command, &b.command, reverse_order)), } - - Ok(process_vector) } diff --git a/src/widgets/temperature.rs b/src/widgets/temperature.rs index 632d0ac6..6c153aef 100644 --- a/src/widgets/temperature.rs +++ b/src/widgets/temperature.rs @@ -1,18 +1,12 @@ use heim_common::{prelude::StreamExt, units::thermodynamic_temperature}; -#[derive(Clone)] +#[derive(Clone, Default)] pub struct TempData { pub component_name : Box, pub temperature : f32, } -#[derive(Clone)] -pub struct TimedTempData { - pub temperature_vec : Vec, - pub time : std::time::SystemTime, -} - -pub async fn get_temperature_data() -> Result { +pub async fn get_temperature_data() -> Result, heim::Error> { let mut temperature_vec : Vec = Vec::new(); let mut sensor_data = heim::sensors::temperatures(); @@ -52,8 +46,5 @@ pub async fn get_temperature_data() -> Result { } }); - Ok(TimedTempData { - temperature_vec, - time : std::time::SystemTime::now(), - }) + Ok(temperature_vec) }