diff options
-rw-r--r-- | src/app/data_collection/network.rs | 6 | ||||
-rw-r--r-- | src/app/data_collection/temperature.rs | 2 | ||||
-rw-r--r-- | src/canvas.rs | 25 | ||||
-rw-r--r-- | src/data_conversion.rs | 100 | ||||
-rw-r--r-- | src/main.rs | 1 | ||||
-rw-r--r-- | src/utils.rs | 1 | ||||
-rw-r--r-- | src/utils/gen_util.rs | 47 |
7 files changed, 99 insertions, 83 deletions
diff --git a/src/app/data_collection/network.rs b/src/app/data_collection/network.rs index 99ce8504..39bf4ad9 100644 --- a/src/app/data_collection/network.rs +++ b/src/app/data_collection/network.rs @@ -40,10 +40,10 @@ pub async fn get_network_data( } } let cur_time = Instant::now(); - let elapsed_time = cur_time.duration_since(*prev_net_access_time).as_millis(); + let elapsed_time = cur_time.duration_since(*prev_net_access_time).as_secs_f64(); - let rx = (((net_rx - *prev_net_rx_bytes) * 1000) as u128 / elapsed_time) as u64; - let tx = (((net_tx - *prev_net_tx_bytes) * 1000) as u128 / elapsed_time) as u64; + let rx = ((net_rx - *prev_net_rx_bytes) as f64 / elapsed_time) as u64; + let tx = ((net_tx - *prev_net_tx_bytes) as f64 / elapsed_time) as u64; *prev_net_rx_bytes = net_rx; *prev_net_tx_bytes = net_tx; diff --git a/src/app/data_collection/temperature.rs b/src/app/data_collection/temperature.rs index 18b4652a..4328fb78 100644 --- a/src/app/data_collection/temperature.rs +++ b/src/app/data_collection/temperature.rs @@ -39,7 +39,7 @@ pub async fn get_temperature_data(sys: &System, temp_type: &TemperatureType) -> }); } } - } else if cfg!(target_os = "windows") { + } else { let sensor_data = sys.get_components_list(); for component in sensor_data { temperature_vec.push(TempData { diff --git a/src/canvas.rs b/src/canvas.rs index d71301e2..9801d398 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -1,5 +1,4 @@ -use crate::{app, constants, utils::error}; -use std::cmp::Ordering; +use crate::{app, constants, utils::error, utils::gen_util::*}; use tui::{ backend, layout::{Alignment, Constraint, Direction, Layout}, @@ -12,7 +11,7 @@ const TEXT_COLOUR: Color = Color::Gray; const GRAPH_COLOUR: Color = Color::Gray; const BORDER_STYLE_COLOUR: Color = Color::Gray; const HIGHLIGHTED_BORDER_STYLE_COLOUR: Color = Color::LightBlue; -const GOLDEN_RATIO: f32 = 0.618_034; +const GOLDEN_RATIO: f32 = 0.618_034; // Approx, good enough for use (also Clippy gets mad if it's too long) lazy_static! { static ref HELP_TEXT: [Text<'static>; 14] = [ @@ -60,26 +59,6 @@ fn gen_n_colours(num_to_gen: i32) -> Vec<Color> { new_val } } - fn float_min(a: f32, b: f32) -> f32 { - match a.partial_cmp(&b) { - Some(x) => match x { - Ordering::Greater => b, - Ordering::Less => a, - Ordering::Equal => a, - }, - None => a, - } - } - fn float_max(a: f32, b: f32) -> f32 { - match a.partial_cmp(&b) { - Some(x) => match x { - Ordering::Greater => a, - Ordering::Less => b, - Ordering::Equal => a, - }, - None => a, - } - } /// This takes in an h, s, and v value of range [0, 1] /// For explanation of what this does, see /// https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB_alternative diff --git a/src/data_conversion.rs b/src/data_conversion.rs index 244c2356..6d3d40ed 100644 --- a/src/data_conversion.rs +++ b/src/data_conversion.rs @@ -1,4 +1,8 @@ -use crate::{app::data_collection, constants}; +use crate::{ + app::data_collection, + constants, + utils::gen_util::{get_exact_byte_values, get_simple_byte_values}, +}; use constants::*; pub fn update_temp_row(app_data: &data_collection::Data, temp_type: &data_collection::temperature::TemperatureType) -> Vec<Vec<String>> { @@ -40,48 +44,33 @@ pub fn update_disk_row(app_data: &data_collection::Data) -> Vec<Vec<String>> { let prev = &prev_io_hashmap[trimmed_mount]; let read_bytes_per_sec = ((ele.read_bytes - prev.read_bytes) as f64 / time_difference) as u64; let write_bytes_per_sec = ((ele.write_bytes - prev.write_bytes) as f64 / time_difference) as u64; + let converted_read = get_simple_byte_values(read_bytes_per_sec); + let converted_write = get_simple_byte_values(write_bytes_per_sec); ( - if read_bytes_per_sec < 1000 { - format!("{}B", read_bytes_per_sec) - } else if read_bytes_per_sec < 1000 * 1000 { - format!("{}KB", read_bytes_per_sec / 1000) - } else { - format!("{}MB", read_bytes_per_sec / 1000 / 1000) - }, - if write_bytes_per_sec < 1000 { - format!("{}B", write_bytes_per_sec) - } else if write_bytes_per_sec < 1000 * 1000 { - format!("{}KB", write_bytes_per_sec / 1000) - } else { - format!("{}MB", write_bytes_per_sec / 1000 / 1000) - }, + format!("{:.*}{}/s", 0, converted_read.0, converted_read.1), + format!("{:.*}{}/s", 0, converted_write.0, converted_write.1), ) } else { - ("0B".to_string(), "0B".to_string()) + ("0B/s".to_string(), "0B/s".to_string()) } } else { - ("0B".to_string(), "0B".to_string()) + ("0B/s".to_string(), "0B/s".to_string()) } } else { - ("0B".to_string(), "0B".to_string()) + ("0B/s".to_string(), "0B/s".to_string()) } } else { - ("0B".to_string(), "0B".to_string()) + ("0B/s".to_string(), "0B/s".to_string()) }; + + let converted_free_space = get_simple_byte_values(disk.free_space); + let converted_total_space = get_simple_byte_values(disk.total_space); disk_vector.push(vec![ disk.name.to_string(), disk.mount_point.to_string(), format!("{:.0}%", disk.used_space as f64 / disk.total_space as f64 * 100_f64), - if disk.free_space < 1000 { - disk.free_space.to_string() + "MB" - } else { - (disk.free_space / 1000).to_string() + "GB" - }, - if disk.total_space < 1000 { - disk.total_space.to_string() + "MB" - } else { - (disk.total_space / 1000).to_string() + "GB" - }, + format!("{:4.*}{}", 0, converted_free_space.0, converted_free_space.1), + format!("{:4.*}{}", 0, converted_total_space.0, converted_total_space.1), io_activity.0, io_activity.1, ]); @@ -237,6 +226,8 @@ pub struct ConvertedNetworkData { pub tx: Vec<(f64, f64)>, pub rx_display: String, pub tx_display: String, + pub total_rx_display: String, + pub total_tx_display: String, } pub fn update_network_data_points(app_data: &data_collection::Data) -> ConvertedNetworkData { @@ -286,40 +277,37 @@ pub fn convert_network_data_points(network_data: &[data_collection::network::Net tx.push(tx_data); } - let rx_display = if let Some(last_num_bytes_entry) = network_data.last() { - let num_bytes = last_num_bytes_entry.rx; - if num_bytes < 1024 { - format!("RX: {:5.*} B/s", 1, num_bytes as f64) - } else if num_bytes < (1024 * 1024) { - format!("RX: {:5.*}KiB/s", 1, num_bytes as f64 / 1024.0) - } else if num_bytes < (1024 * 1024 * 1024) { - format!("RX: {:5.*}MiB/s", 1, num_bytes as f64 / 1024.0 / 1024.0) - } else { - format!("RX: {:5.*}GiB/s", 1, num_bytes as f64 / 1024.0 / 1024.0 / 1024.0) - } + let total_rx_converted_result: (f64, String); + let rx_converted_result: (f64, String); + let total_tx_converted_result: (f64, String); + let tx_converted_result: (f64, String); + + if let Some(last_num_bytes_entry) = network_data.last() { + rx_converted_result = get_exact_byte_values(last_num_bytes_entry.rx); + total_rx_converted_result = get_exact_byte_values(last_num_bytes_entry.total_rx); } else { - "0.0B/s".to_string() - }; - - let tx_display = if let Some(last_num_bytes_entry) = network_data.last() { - let num_bytes = last_num_bytes_entry.tx; - if num_bytes < 1024 { - format!("TX: {:5.*} B/s", 1, num_bytes as f64) - } else if num_bytes < (1024 * 1024) { - format!("TX: {:5.*}KiB/s", 1, num_bytes as f64 / 1024.0) - } else if num_bytes < (1024 * 1024 * 1024) { - format!("TX: {:5.*}MiB/s", 1, num_bytes as f64 / 1024.0 / 1024.0) - } else { - format!("TX: {:5.*}GiB/s", 1, num_bytes as f64 / 1024.0 / 1024.0 / 1024.0) - } + rx_converted_result = get_exact_byte_values(0); + total_rx_converted_result = get_exact_byte_values(0); + } + let rx_display = format!("RX: {:5.*}{}", 1, rx_converted_result.0, rx_converted_result.1); + let total_rx_display = format!("Total RX: {:5.*}{}", 1, total_rx_converted_result.0, total_rx_converted_result.1); + + if let Some(last_num_bytes_entry) = network_data.last() { + tx_converted_result = get_exact_byte_values(last_num_bytes_entry.tx); + total_tx_converted_result = get_exact_byte_values(last_num_bytes_entry.total_tx); } else { - "0.0B/s".to_string() - }; + tx_converted_result = get_exact_byte_values(0); + total_tx_converted_result = get_exact_byte_values(0); + } + let tx_display = format!("TX: {:5.*}{}", 1, tx_converted_result.0, tx_converted_result.1); + let total_tx_display = format!("Total TX: {:5.*}{}", 1, total_tx_converted_result.0, total_tx_converted_result.1); ConvertedNetworkData { rx, tx, rx_display, tx_display, + total_rx_display, + total_tx_display, } } diff --git a/src/main.rs b/src/main.rs index 9602d285..0d6ca958 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,6 +25,7 @@ use tui::{backend::CrosstermBackend, Terminal}; pub mod app; mod utils { pub mod error; + pub mod gen_util; pub mod logging; } mod canvas; diff --git a/src/utils.rs b/src/utils.rs index 19b5087f..5aa52d90 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,2 +1,3 @@ pub mod error; +pub mod general_utility; pub mod logging; diff --git a/src/utils/gen_util.rs b/src/utils/gen_util.rs new file mode 100644 index 00000000..30a257bc --- /dev/null +++ b/src/utils/gen_util.rs @@ -0,0 +1,47 @@ +use std::cmp::Ordering; + +pub fn float_min(a: f32, b: f32) -> f32 { + match a.partial_cmp(&b) { + Some(x) => match x { + Ordering::Greater => b, + Ordering::Less => a, + Ordering::Equal => a, + }, + None => a, + } +} + +pub fn float_max(a: f32, b: f32) -> f32 { + match a.partial_cmp(&b) { + Some(x) => match x { + Ordering::Greater => a, + Ordering::Less => b, + Ordering::Equal => a, + }, + None => a, + } +} + +/// Returns a tuple containing the value and the unit. In units of 1024. +/// This only supports up to a tebibyte. +pub fn get_exact_byte_values(bytes: u64) -> (f64, String) { + match bytes { + b if b < 1024 => (bytes as f64, "B".to_string()), + b if b < 1_048_576 => (bytes as f64 / 1024.0, "KiB".to_string()), + b if b < 1_073_741_824 => (bytes as f64 / 1_048_576.0, "MiB".to_string()), + b if b < 1_099_511_627_776 => (bytes as f64 / 1_073_741_824.0, "GiB".to_string()), + _ => (bytes as f64 / 1_099_511_627_776.0, "TiB".to_string()), + } +} + +/// Returns a tuple containing the value and the unit. In units of 1000. +/// This only supports up to a terabyte. Note the "byte" unit will have a space appended to match the others. +pub fn get_simple_byte_values(bytes: u64) -> (f64, String) { + match bytes { + b if b < 1000 => (bytes as f64, "B".to_string()), + b if b < 1_000_000 => (bytes as f64 / 1000.0, "KB".to_string()), + b if b < 1_000_000_000 => (bytes as f64 / 1_000_000.0, "MB".to_string()), + b if b < 1_000_000_000_000 => (bytes as f64 / 1_000_000_000.0, "GB".to_string()), + _ => (bytes as f64 / 1_000_000_000_000.0, "TB".to_string()), + } +} |