Added temperature support for data polling.

This commit is contained in:
ClementTsang 2019-09-07 22:30:15 -04:00
parent 521698a2bd
commit ac85c42ce9
4 changed files with 87 additions and 12 deletions

View file

@ -16,6 +16,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
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();
loop {
println!("Start data loop...");
@ -24,7 +25,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// TODO: Get data, potentially store? Use a result to check!
let list_of_processes = processes::get_sorted_processes_list(processes::ProcessSorting::CPU, true).await;
for process in list_of_processes {
println!("Process: {} with PID {}, CPU: {}, MEM: {}", process.command, process.pid, process.cpu_usage, process.mem_usage,);
println!(
"Process: {} with PID {}, CPU: {}%, MEM: {} MB",
process.command, process.pid, process.cpu_usage_percent, process.mem_usage_in_mb,
);
}
let list_of_disks = disks::get_disk_usage_list().await?;
@ -70,6 +74,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Memory usage: {} out of {} is used, at {:?}", current_mem.mem_used, current_mem.mem_total, current_mem.time);
}
list_of_timed_temperature.push(temperature::get_temperature_data().await?);
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);
}
}
// Send to drawing module
println!("End data loop...");
window::draw_terminal();

View file

@ -1,3 +1,13 @@
fn get_timestamped_network_data() {}
pub struct TimedNetworkData {
pub rx : u32,
pub tx : u32,
pub time : std::time::SystemTime,
}
fn get_network_data_list() {}
pub fn get_network_data() -> TimedNetworkData {
TimedNetworkData {
rx : 0,
tx : 0,
time : std::time::SystemTime::now(),
}
}

View file

@ -14,8 +14,8 @@ pub enum ProcessSorting {
#[derive(Debug)]
pub struct ProcessInfo {
pub pid : u32,
pub cpu_usage : f32,
pub mem_usage : u64,
pub cpu_usage_percent : f32,
pub mem_usage_in_mb : u64,
pub command : String,
}
@ -52,7 +52,6 @@ async fn cpu_usage(process : heim::process::Process) -> heim::process::ProcessRe
pub async fn get_sorted_processes_list(sorting_method : ProcessSorting, reverse_order : bool) -> Vec<ProcessInfo> {
let mut process_stream = heim::process::processes().map_ok(cpu_usage).try_buffer_unordered(std::usize::MAX);
// TODO: Evaluate whether this is too slow!
// TODO: Group together processes
let mut process_vector : Vec<ProcessInfo> = Vec::new();
@ -64,15 +63,15 @@ pub async fn get_sorted_processes_list(sorting_method : ProcessSorting, reverse_
process_vector.push(ProcessInfo {
command : process.name().await.unwrap_or_else(|_| "".to_string()),
pid : process.pid() as u32,
cpu_usage : cpu_usage.get::<units::ratio::percent>(),
mem_usage : mem_measurement.rss().get::<units::information::megabyte>(),
cpu_usage_percent : cpu_usage.get::<units::ratio::percent>(),
mem_usage_in_mb : mem_measurement.rss().get::<units::information::megabyte>(),
});
}
}
}
match sorting_method {
ProcessSorting::CPU => process_vector.sort_by(|a, b| get_ordering(a.cpu_usage, b.cpu_usage, reverse_order)),
ProcessSorting::MEM => process_vector.sort_by(|a, b| get_ordering(a.mem_usage, b.mem_usage, reverse_order)),
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)),
}

View file

@ -1,3 +1,57 @@
fn get_timestamped_temperature() {}
use heim_common::{prelude::StreamExt, units::thermodynamic_temperature};
fn get_temps_list() {}
pub struct TempData {
pub component_name : Box<str>,
pub temperature : f32,
}
pub struct TimedTempData {
pub temperature_vec : Vec<TempData>,
pub time : std::time::SystemTime,
}
pub async fn get_temperature_data() -> Result<TimedTempData, heim::Error> {
let mut temperature_vec : Vec<TempData> = Vec::new();
let mut sensor_data = heim::sensors::temperatures();
while let Some(sensor) = sensor_data.next().await {
if let Ok(sensor) = sensor {
temperature_vec.push(TempData {
component_name : Box::from(sensor.unit()),
temperature : sensor.current().get::<thermodynamic_temperature::degree_celsius>(),
});
}
}
// By default, sort temperature, then by alphabetically! Allow for configuring this...
// Note we sort in reverse here; we want greater temps to be higher priority.
temperature_vec.sort_by(|a, b| {
if a.temperature > b.temperature {
std::cmp::Ordering::Less
}
else if a.temperature < b.temperature {
std::cmp::Ordering::Greater
}
else {
std::cmp::Ordering::Equal
}
});
temperature_vec.sort_by(|a, b| {
if a.component_name > b.component_name {
std::cmp::Ordering::Greater
}
else if a.component_name < b.component_name {
std::cmp::Ordering::Less
}
else {
std::cmp::Ordering::Equal
}
});
Ok(TimedTempData {
temperature_vec,
time : std::time::SystemTime::now(),
})
}