Added stale data filtering.

This commit is contained in:
ClementTsang 2019-09-11 00:08:55 -04:00
parent ac26ac7f5a
commit 0d76c49973
5 changed files with 88 additions and 27 deletions

View File

@ -238,27 +238,25 @@ fn draw_data<B : tui::backend::Backend>(terminal : &mut Terminal<B>, app_data :
}
fn init_logger() -> Result<(), fern::InitError> {
if cfg!(debug_assertions) {
fern::Dispatch::new()
.format(|out, message, record| {
out.finish(format_args!(
"{}[{}][{}] {}",
chrono::Local::now().format("[%Y-%m-%d][%H:%M:%S]"),
record.target(),
record.level(),
message
))
})
.level(log::LevelFilter::Debug)
.chain(fern::log_file("debug.log")?)
.apply()?;
}
fern::Dispatch::new()
.format(|out, message, record| {
out.finish(format_args!(
"{}[{}][{}] {}",
chrono::Local::now().format("[%Y-%m-%d][%H:%M:%S]"),
record.target(),
record.level(),
message
))
})
.level(if cfg!(debug_assertions) { log::LevelFilter::Debug } else { log::LevelFilter::Info })
.chain(fern::log_file("debug.log")?)
.apply()?;
Ok(())
}
fn try_debug(result_log : &Result<(), fern::InitError>, message : &str) {
if cfg!(debug_assertions) && result_log.is_ok() {
if result_log.is_ok() {
debug!("{}", message);
}
}

View File

@ -5,10 +5,15 @@ use sysinfo::{ProcessorExt, System, SystemExt};
pub struct CPUData {
pub cpu_name : Box<str>,
pub cpu_usage : u32,
}
#[derive(Clone)]
pub struct CPUPackage {
pub cpu_vec : Vec<CPUData>,
pub instant : Instant,
}
pub fn get_cpu_data_list(sys : &System) -> Result<Vec<CPUData>, heim::Error> {
pub fn get_cpu_data_list(sys : &System) -> Result<CPUPackage, heim::Error> {
let cpu_data = sys.get_processor_list();
let mut cpu_vec = Vec::new();
@ -16,9 +21,8 @@ pub fn get_cpu_data_list(sys : &System) -> Result<Vec<CPUData>, heim::Error> {
cpu_vec.push(CPUData {
cpu_name : Box::from(cpu.get_name()),
cpu_usage : (cpu.get_cpu_usage() * 100_f32).ceil() as u32,
instant : Instant::now(),
})
}
Ok(cpu_vec)
Ok(CPUPackage { cpu_vec, instant : Instant::now() })
}

View File

@ -15,10 +15,15 @@ pub struct IOData {
pub mount_point : Box<str>,
pub read_bytes : u64,
pub write_bytes : u64,
}
#[derive(Clone)]
pub struct IOPackage {
pub io_list : Vec<IOData>,
pub instant : Instant,
}
pub async fn get_io_usage_list(get_physical : bool) -> Result<Vec<IOData>, heim::Error> {
pub async fn get_io_usage_list(get_physical : bool) -> Result<IOPackage, heim::Error> {
let mut io_list : Vec<IOData> = Vec::new();
if get_physical {
let mut physical_counter_stream = heim::disk::io_counters_physical();
@ -28,7 +33,6 @@ pub async fn get_io_usage_list(get_physical : bool) -> Result<Vec<IOData>, heim:
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>(),
instant : Instant::now(),
})
}
}
@ -40,12 +44,11 @@ pub async fn get_io_usage_list(get_physical : bool) -> Result<Vec<IOData>, heim:
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>(),
instant : Instant::now(),
})
}
}
Ok(io_list)
Ok(IOPackage { io_list, instant : Instant::now() })
}
pub async fn get_disk_usage_list() -> Result<Vec<DiskData>, heim::Error> {

View File

@ -30,9 +30,9 @@ fn push_if_valid<T : std::clone::Clone>(result : &Result<T, heim::Error>, vector
#[derive(Default, Clone)]
pub struct Data {
pub list_of_cpu_packages : Vec<Vec<cpu::CPUData>>,
pub list_of_io : Vec<Vec<disks::IOData>>,
pub list_of_physical_io : Vec<Vec<disks::IOData>>,
pub list_of_cpu_packages : Vec<cpu::CPUPackage>,
pub list_of_io : Vec<disks::IOPackage>,
pub list_of_physical_io : Vec<disks::IOPackage>,
pub memory : Vec<mem::MemData>,
pub swap : Vec<mem::MemData>,
pub list_of_temperature : Vec<temperature::TempData>,
@ -61,6 +61,8 @@ impl DataState {
self.sys.refresh_system();
self.sys.refresh_network();
const STALE_MAX_SECONDS : u64 = 60;
// What we want to do: For timed data, if there is an error, just do not add. For other data, just don't update!
push_if_valid(&network::get_network_data(&self.sys), &mut self.data.network);
push_if_valid(&cpu::get_cpu_data_list(&self.sys), &mut self.data.list_of_cpu_packages);
@ -74,6 +76,57 @@ impl DataState {
push_if_valid(&disks::get_io_usage_list(false).await, &mut self.data.list_of_io);
push_if_valid(&disks::get_io_usage_list(true).await, &mut self.data.list_of_physical_io);
set_if_valid(&temperature::get_temperature_data().await, &mut self.data.list_of_temperature);
// Filter out stale timed entries...
let current_instant = std::time::Instant::now();
self.data.list_of_cpu_packages = self
.data
.list_of_cpu_packages
.iter()
.cloned()
.filter(|entry| current_instant.duration_since(entry.instant).as_secs() <= STALE_MAX_SECONDS)
.collect::<Vec<_>>();
self.data.memory = self
.data
.memory
.iter()
.cloned()
.filter(|entry| current_instant.duration_since(entry.instant).as_secs() <= STALE_MAX_SECONDS)
.collect::<Vec<_>>();
self.data.swap = self
.data
.swap
.iter()
.cloned()
.filter(|entry| current_instant.duration_since(entry.instant).as_secs() <= STALE_MAX_SECONDS)
.collect::<Vec<_>>();
self.data.network = self
.data
.network
.iter()
.cloned()
.filter(|entry| current_instant.duration_since(entry.instant).as_secs() <= STALE_MAX_SECONDS)
.collect::<Vec<_>>();
self.data.list_of_io = self
.data
.list_of_io
.iter()
.cloned()
.filter(|entry| current_instant.duration_since(entry.instant).as_secs() <= STALE_MAX_SECONDS)
.collect::<Vec<_>>();
self.data.list_of_physical_io = self
.data
.list_of_physical_io
.iter()
.cloned()
.filter(|entry| current_instant.duration_since(entry.instant).as_secs() <= STALE_MAX_SECONDS)
.collect::<Vec<_>>();
debug!("End updating...");
}
}

View File

@ -1,9 +1,11 @@
use std::time::Instant;
use sysinfo::{NetworkExt, System, SystemExt};
#[derive(Clone, Default)]
#[derive(Clone)]
pub struct NetworkData {
pub rx : u64,
pub tx : u64,
pub instant : Instant,
}
pub fn get_network_data(sys : &System) -> Result<NetworkData, heim::Error> {
@ -11,5 +13,6 @@ pub fn get_network_data(sys : &System) -> Result<NetworkData, heim::Error> {
Ok(NetworkData {
rx : network_data.get_income(),
tx : network_data.get_outcome(),
instant : Instant::now(),
})
}