Modified errors in data_collection portion to use the newer error type added earlier on.

This commit is contained in:
ClementTsang 2019-09-16 19:05:44 -04:00
parent a5306c6692
commit 266c281024
11 changed files with 43 additions and 19 deletions

View File

@ -12,12 +12,14 @@
* ~~FIX PROCESSES AHHHHHH~~
* Scrolling in at least processes
~~* Scrolling in at least processes~~
* Keybindings
## After making public
* Tests
* Mouse + key events conflict? Make it so that some events don't clog up the loop if they are not valid keys!
* Header should be clear on current sorting direction!
@ -28,7 +30,7 @@
* ~~Add custom error because it's really messy~~ Done, but need to implement across rest of app!
* Remove any ``unwrap()``, ensure no crashing!
* Remove any ``unwrap()``, ensure no crashing! Might have to use this: <https://doc.rust-lang.org/std/panic/fn.catch_unwind.html>
* Scrolling event in lists

View File

@ -1,6 +1,8 @@
pub mod data_collection;
use data_collection::{processes, temperature};
mod process_killer;
#[allow(dead_code)]
// Probably only use the list elements...
pub enum ApplicationPosition {

View File

@ -10,13 +10,13 @@ pub mod network;
pub mod processes;
pub mod temperature;
fn set_if_valid<T : std::clone::Clone>(result : &Result<T, heim::Error>, value_to_set : &mut T) {
fn set_if_valid<T : std::clone::Clone>(result : &Result<T, crate::utils::error::RustopError>, value_to_set : &mut T) {
if let Ok(result) = result {
*value_to_set = (*result).clone();
}
}
fn push_if_valid<T : std::clone::Clone>(result : &Result<T, heim::Error>, vector_to_push : &mut Vec<T>) {
fn push_if_valid<T : std::clone::Clone>(result : &Result<T, crate::utils::error::RustopError>, vector_to_push : &mut Vec<T>) {
if let Ok(result) = result {
vector_to_push.push(result.clone());
}

View File

@ -13,7 +13,7 @@ pub struct CPUPackage {
pub instant : Instant,
}
pub fn get_cpu_data_list(sys : &System) -> Result<CPUPackage, heim::Error> {
pub fn get_cpu_data_list(sys : &System) -> crate::utils::error::Result<CPUPackage> {
let cpu_data = sys.get_processor_list();
let mut cpu_vec = Vec::new();

View File

@ -23,8 +23,7 @@ pub struct IOPackage {
pub instant : Instant,
}
// TODO: This is total --- we have to change the calculation to PER SECOND!
pub async fn get_io_usage_list(get_physical : bool) -> Result<IOPackage, heim::Error> {
pub async fn get_io_usage_list(get_physical : bool) -> crate::utils::error::Result<IOPackage> {
let mut io_hash : std::collections::HashMap<String, IOData> = std::collections::HashMap::new();
if get_physical {
let mut physical_counter_stream = heim::disk::io_counters_physical();
@ -63,7 +62,7 @@ pub async fn get_io_usage_list(get_physical : bool) -> Result<IOPackage, heim::E
})
}
pub async fn get_disk_usage_list() -> Result<Vec<DiskData>, heim::Error> {
pub async fn get_disk_usage_list() -> crate::utils::error::Result<Vec<DiskData>> {
let mut vec_disks : Vec<DiskData> = Vec::new();
let mut partitions_stream = heim::disk::partitions_physical();

View File

@ -8,7 +8,7 @@ pub struct MemData {
pub instant : Instant,
}
pub async fn get_mem_data_list() -> Result<MemData, heim::Error> {
pub async fn get_mem_data_list() -> crate::utils::error::Result<MemData> {
let memory = heim::memory::memory().await?;
Ok(MemData {
@ -18,7 +18,7 @@ pub async fn get_mem_data_list() -> Result<MemData, heim::Error> {
})
}
pub async fn get_swap_data_list() -> Result<MemData, heim::Error> {
pub async fn get_swap_data_list() -> crate::utils::error::Result<MemData> {
let memory = heim::memory::swap().await?;
Ok(MemData {

View File

@ -11,7 +11,7 @@ pub struct NetworkData {
pub instant : Instant,
}
pub fn get_network_data(sys : &System) -> Result<NetworkData, heim::Error> {
pub fn get_network_data(sys : &System) -> crate::utils::error::Result<NetworkData> {
let network_data = sys.get_network();
Ok(NetworkData {
rx : network_data.get_income(),

View File

@ -171,16 +171,13 @@ fn convert_ps(process : &str, cpu_usage_percentage : f64, prev_pid_stats : &mut
pub async fn get_sorted_processes_list(
prev_idle : &mut f64, prev_non_idle : &mut f64, prev_pid_stats : &mut std::collections::HashMap<String, f64>,
) -> Result<Vec<ProcessData>, heim::Error> {
) -> crate::utils::error::Result<Vec<ProcessData>> {
let mut process_vector : Vec<ProcessData> = Vec::new();
if cfg!(target_os = "linux") {
// Linux specific - this is a massive pain... ugh.
let ps_result = Command::new("ps")
.args(&["-axo", "pid:10,comm:50,%mem:5", "--noheader"])
.output()
.expect("Failed to execute.");
let ps_result = Command::new("ps").args(&["-axo", "pid:10,comm:50,%mem:5", "--noheader"]).output()?;
let ps_stdout = String::from_utf8_lossy(&ps_result.stdout);
let split_string = ps_stdout.split('\n');
let cpu_usage = vangelis_cpu_usage_calculation(prev_idle, prev_non_idle).unwrap(); // TODO: FIX THIS ERROR CHECKING

View File

@ -19,7 +19,7 @@ impl Default for TemperatureType {
}
}
pub async fn get_temperature_data(temp_type : &TemperatureType) -> Result<Vec<TempData>, heim::Error> {
pub async fn get_temperature_data(temp_type : &TemperatureType) -> crate::utils::error::Result<Vec<TempData>> {
let mut temperature_vec : Vec<TempData> = Vec::new();
let mut sensor_data = heim::sensors::temperatures();

24
src/app/process_killer.rs Normal file
View File

@ -0,0 +1,24 @@
/// This file is meant to house (OS specific) implementations on how to kill processes.
use std::process::Command;
/// Kills a process, given a PID.
pub fn kill_process_given_pid(pid : i64) -> crate::utils::error::Result<()> {
if cfg!(target_os = "linux") {
// Linux
Command::new("kill").arg(pid.to_string()).output()?;
}
else if cfg!(target_os = "windows") {
// Windows
debug!("Sorry, Windows support is not implemented yet!");
}
else if cfg!(target_os = "macos") {
// TODO: macOS
debug!("Sorry, macOS support is not implemented yet!");
}
else {
// TODO: Others?
debug!("Sorry, other support this is not implemented yet!");
}
Ok(())
}

View File

@ -226,10 +226,10 @@ pub fn draw_data<B : tui::backend::Backend>(terminal : &mut Terminal<B>, app_sta
}
};
debug!(
/*debug!(
"START POSN: {}, PREV POSN: {}, CURRENT SELECTED POSN: {}, NUM ROWS: {}",
start_position, app_state.previous_process_position, app_state.currently_selected_process_position, num_rows
);
);*/
let sliced_vec : Vec<Vec<String>> = (&canvas_data.process_data[start_position as usize..]).to_vec();