Removed the 'timing' aspect, it was irrelevant.

This commit is contained in:
ClementTsang 2019-09-08 01:01:42 -04:00
parent 826bc701c1
commit 0050b77caf
7 changed files with 68 additions and 116 deletions

View file

@ -5,9 +5,9 @@ use widgets::{cpu, disks, mem, network, processes, temperature};
mod window;
fn push_if_valid<T : std::clone::Clone>(result : &Result<T, heim::Error>, vector_to_push_to : &mut Vec<T>) {
fn set_if_valid<T : std::clone::Clone>(result : &Result<T, heim::Error>, 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<cpu::TimedCPUPackages> = Vec::new();
let mut list_of_timed_io : Vec<Vec<disks::TimedIOInfo>> = Vec::new();
let mut list_of_timed_physical_io : Vec<Vec<disks::TimedIOInfo>> = Vec::new();
let mut list_of_timed_memory : Vec<mem::MemData> = Vec::new();
let mut list_of_timed_swap : Vec<mem::MemData> = Vec::new();
let mut list_of_timed_temperature : Vec<temperature::TimedTempData> = Vec::new();
let mut list_of_timed_network : Vec<network::TimedNetworkData> = Vec::new();
let mut list_of_cpu_packages : Vec<cpu::CPUData> = Vec::new();
let mut list_of_io : Vec<disks::IOInfo> = Vec::new();
let mut list_of_physical_io : Vec<disks::IOInfo> = Vec::new();
let mut memory : mem::MemData = mem::MemData::default();
let mut swap : mem::MemData = mem::MemData::default();
let mut list_of_temperature : Vec<temperature::TempData> = 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();

View file

@ -1,18 +1,12 @@
use sysinfo::{ProcessorExt, System, SystemExt};
#[derive(Clone)]
#[derive(Clone, Default)]
pub struct CPUData {
pub cpu_name : Box<str>,
pub cpu_usage : u32,
}
#[derive(Clone)]
pub struct TimedCPUPackages {
pub processor_list : Vec<CPUData>,
pub time : std::time::SystemTime,
}
pub fn get_cpu_data_list(sys : &System) -> Result<TimedCPUPackages, heim::Error> {
pub fn get_cpu_data_list(sys : &System) -> Result<Vec<CPUData>, 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<TimedCPUPackages, heim::Error>
})
}
Ok(TimedCPUPackages {
processor_list : cpu_vec,
time : std::time::SystemTime::now(),
})
}
pub fn is_cpu_data_old() -> bool {
true
Ok(cpu_vec)
}

View file

@ -1,6 +1,6 @@
use heim_common::prelude::StreamExt;
#[derive(Clone)]
#[derive(Clone, Default)]
pub struct DiskInfo {
pub name : Box<str>,
pub mount_point : Box<str>,
@ -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<str>,
pub read_bytes : u64,
pub write_bytes : u64,
pub time : std::time::SystemTime,
}
pub async fn get_io_usage_list(get_physical : bool) -> Result<Vec<TimedIOInfo>, heim::Error> {
let mut io_list : Vec<TimedIOInfo> = Vec::new();
pub async fn get_io_usage_list(get_physical : bool) -> Result<Vec<IOInfo>, heim::Error> {
let mut io_list : Vec<IOInfo> = 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::<heim_common::units::information::megabyte>(),
write_bytes : io.write_bytes().get::<heim_common::units::information::megabyte>(),
time : std::time::SystemTime::now(),
})
}
}
@ -35,11 +33,10 @@ pub async fn get_io_usage_list(get_physical : bool) -> Result<Vec<TimedIOInfo>,
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::<heim_common::units::information::megabyte>(),
write_bytes : io.write_bytes().get::<heim_common::units::information::megabyte>(),
time : std::time::SystemTime::now(),
})
}
}
@ -47,10 +44,6 @@ pub async fn get_io_usage_list(get_physical : bool) -> Result<Vec<TimedIOInfo>,
Ok(io_list)
}
pub fn is_io_data_old() -> bool {
true
}
pub async fn get_disk_usage_list() -> Result<Vec<DiskInfo>, heim::Error> {
let mut vec_disks : Vec<DiskInfo> = Vec::new();
let mut partitions_stream = heim::disk::partitions_physical();

View file

@ -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<MemData, heim::Error> {
@ -13,7 +12,6 @@ pub async fn get_mem_data_list() -> Result<MemData, heim::Error> {
Ok(MemData {
mem_total : memory.total().get::<information::megabyte>(),
mem_used : memory.total().get::<information::megabyte>() - memory.available().get::<information::megabyte>(),
time : std::time::SystemTime::now(),
})
}
@ -23,10 +21,5 @@ pub async fn get_swap_data_list() -> Result<MemData, heim::Error> {
Ok(MemData {
mem_total : memory.total().get::<information::megabyte>(),
mem_used : memory.used().get::<information::megabyte>(),
time : std::time::SystemTime::now(),
})
}
pub fn is_mem_data_old() -> bool {
true
}

View file

@ -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<NetworkData, heim::Error> {
let network_data = sys.get_network();
TimedNetworkData {
Ok(NetworkData {
rx : network_data.get_income(),
tx : network_data.get_outcome(),
time : std::time::SystemTime::now(),
}
})
}

View file

@ -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<Vec<ProcessInfo>, 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<ProcessInfo> = 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::<units::ratio::percent>(),
mem_usage_in_mb : mem_measurement.rss().get::<units::information::megabyte>(),
});
*/
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<ProcessInfo>, 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)
}

View file

@ -1,18 +1,12 @@
use heim_common::{prelude::StreamExt, units::thermodynamic_temperature};
#[derive(Clone)]
#[derive(Clone, Default)]
pub struct TempData {
pub component_name : Box<str>,
pub temperature : f32,
}
#[derive(Clone)]
pub struct TimedTempData {
pub temperature_vec : Vec<TempData>,
pub time : std::time::SystemTime,
}
pub async fn get_temperature_data() -> Result<TimedTempData, heim::Error> {
pub async fn get_temperature_data() -> Result<Vec<TempData>, heim::Error> {
let mut temperature_vec : Vec<TempData> = Vec::new();
let mut sensor_data = heim::sensors::temperatures();
@ -52,8 +46,5 @@ pub async fn get_temperature_data() -> Result<TimedTempData, heim::Error> {
}
});
Ok(TimedTempData {
temperature_vec,
time : std::time::SystemTime::now(),
})
Ok(temperature_vec)
}