Update sysinfo, add total network to windows/linux.

This commit is contained in:
ClementTsang 2020-02-09 22:11:03 -05:00
parent 5dd22c6c89
commit 1ac6cdde2f
7 changed files with 32 additions and 35 deletions

View file

@ -25,11 +25,11 @@ chrono = "0.4.10"
clap = "2.33.0"
fern = "0.5.9"
futures-timer = "3.0.1"
futures = "0.3.3"
futures = "0.3.4"
heim = "0.0.9"
log = "0.4.8"
regex = "1.3.4"
sysinfo = "0.10.5"
sysinfo = "0.11"
tokio = "0.2.11"
winapi = "0.3.8"
crossterm = "0.14"

View file

@ -75,7 +75,7 @@ impl Default for DataState {
fn default() -> Self {
DataState {
data: Data::default(),
sys: System::new(),
sys: System::new_all(),
prev_pid_stats: HashMap::new(),
prev_idle: 0_f64,
prev_non_idle: 0_f64,
@ -96,7 +96,6 @@ impl DataState {
}
pub fn init(&mut self) {
self.sys.refresh_all();
self.mem_total_kb = self.sys.get_total_memory();
futures::executor::block_on(self.update_data());
std::thread::sleep(std::time::Duration::from_millis(250));
@ -109,7 +108,7 @@ impl DataState {
if !cfg!(target_os = "linux") {
// For now, might be just windows tbh
self.sys.refresh_processes();
self.sys.refresh_network();
self.sys.refresh_networks();
}
let current_instant = std::time::Instant::now();

View file

@ -9,13 +9,13 @@ pub struct CPUData {
pub type CPUHarvest = Vec<CPUData>;
pub fn get_cpu_data_list(sys: &System) -> CPUHarvest {
let cpu_data = sys.get_processor_list();
let cpu_data = sys.get_processors();
let mut cpu_vec = Vec::new();
for cpu in cpu_data {
cpu_vec.push(CPUData {
cpu_name: cpu.get_name().to_string(),
cpu_usage: f64::from(cpu.get_cpu_usage()) * 100_f64,
cpu_usage: f64::from(cpu.get_cpu_usage()),
});
}

View file

@ -23,40 +23,38 @@ pub async fn get_network_data(
sys: &System, prev_net_access_time: &Instant, prev_net_rx: &mut u64, prev_net_tx: &mut u64,
curr_time: &Instant,
) -> NetworkHarvest {
// FIXME: [WIN] Track current total bytes... also is this accurate?
let mut io_data = net::io_counters();
let mut total_rx: u64 = 0;
let mut total_tx: u64 = 0;
if cfg!(target_os = "windows") {
let network_data = sys.get_network();
NetworkHarvest {
rx: network_data.get_income(),
tx: network_data.get_outcome(),
total_rx: 0,
total_tx: 0,
let networks = sys.get_networks();
for (_, network) in networks {
total_rx += network.get_total_income();
total_tx += network.get_total_outcome();
}
} else {
let mut io_data = net::io_counters();
let mut total_rx: u64 = 0;
let mut total_tx: u64 = 0;
while let Some(io) = io_data.next().await {
if let Ok(io) = io {
total_rx += io.bytes_recv().get::<byte>();
total_tx += io.bytes_sent().get::<byte>();
}
}
let elapsed_time = curr_time
.duration_since(*prev_net_access_time)
.as_secs_f64();
}
let rx = ((total_rx - *prev_net_rx) as f64 / elapsed_time) as u64;
let tx = ((total_tx - *prev_net_tx) as f64 / elapsed_time) as u64;
let elapsed_time = curr_time
.duration_since(*prev_net_access_time)
.as_secs_f64();
*prev_net_rx = total_rx;
*prev_net_tx = total_tx;
NetworkHarvest {
rx,
tx,
total_rx,
total_tx,
}
let rx = ((total_rx - *prev_net_rx) as f64 / elapsed_time) as u64;
let tx = ((total_tx - *prev_net_tx) as f64 / elapsed_time) as u64;
*prev_net_rx = total_rx;
*prev_net_tx = total_tx;
NetworkHarvest {
rx,
tx,
total_rx,
total_tx,
}
}

View file

@ -231,7 +231,7 @@ pub fn get_sorted_processes_list(
error!("Result: {:?}", cpu_calc.err());
}
} else {
let process_hashmap = sys.get_process_list();
let process_hashmap = sys.get_processes();
for process_val in process_hashmap.values() {
let name = if process_val.name().is_empty() {
let process_cmd = process_val.cmd();

View file

@ -50,7 +50,7 @@ pub async fn get_temperature_data(
}
}
} else {
let sensor_data = sys.get_components_list();
let sensor_data = sys.get_components();
for component in sensor_data {
temperature_vec.push(TempHarvest {
component_name: component.get_label().to_string(),

View file

@ -361,8 +361,8 @@ fn main() -> error::Result<()> {
}
}
futures::executor::block_on(data_state.update_data());
tx.send(Event::Update(Box::from(data_state.data.clone())))
.unwrap(); // TODO: [UNWRAP] Might be required, it's in a closure and idk how to deal with it
let event = Event::Update(Box::from(data_state.data.clone()));
tx.send(event).unwrap();
thread::sleep(Duration::from_millis(update_rate_in_milliseconds as u64));
}
});