bug: fix issues with macos and windows during refactor

This commit is contained in:
ClementTsang 2022-05-15 05:01:19 -04:00
parent 05e9cd4d4d
commit ba362f81c9
12 changed files with 84 additions and 29 deletions

View File

@ -11,7 +11,7 @@ use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};
use typed_builder::*; use typed_builder::*;
use data_farmer::*; use data_farmer::*;
use data_harvester::{processes, temperature}; use data_harvester::temperature;
use layout_manager::*; use layout_manager::*;
pub use states::*; pub use states::*;
@ -132,7 +132,7 @@ pub struct App {
#[cfg(target_family = "unix")] #[cfg(target_family = "unix")]
#[builder(default, setter(skip))] #[builder(default, setter(skip))]
pub user_table: processes::UserTable, pub user_table: data_harvester::processes::UserTable,
pub cpu_state: CpuState, pub cpu_state: CpuState,
pub mem_state: MemState, pub mem_state: MemState,

View File

@ -279,6 +279,17 @@ impl DataCollector {
) )
} }
#[cfg(not(target_os = "linux"))] #[cfg(not(target_os = "linux"))]
{
#[cfg(target_family = "unix")]
{
processes::get_process_data(
&self.sys,
self.use_current_cpu_total,
self.mem_total_kb,
&mut self.user_table,
)
}
#[cfg(not(target_family = "unix"))]
{ {
processes::get_process_data( processes::get_process_data(
&self.sys, &self.sys,
@ -286,6 +297,7 @@ impl DataCollector {
self.mem_total_kb, self.mem_total_kb,
) )
} }
}
} { } {
self.data.list_of_processes = Some(process_list); self.data.list_of_processes = Some(process_list);
} }

View File

@ -3,7 +3,7 @@
use super::NetworkHarvest; use super::NetworkHarvest;
use std::time::Instant; use std::time::Instant;
// FIXME: Eventually make it so that this thing also takes individual usage into account, so we can show per-interface! // TODO: Eventually make it so that this thing also takes individual usage into account, so we can show per-interface!
pub async fn get_network_data( pub async fn get_network_data(
prev_net_access_time: Instant, prev_net_rx: &mut u64, prev_net_tx: &mut u64, prev_net_access_time: Instant, prev_net_rx: &mut u64, prev_net_tx: &mut u64,
curr_time: Instant, actually_get: bool, filter: &Option<crate::app::Filter>, curr_time: Instant, actually_get: bool, filter: &Option<crate::app::Filter>,

View File

@ -3,6 +3,8 @@
use super::ProcessHarvest; use super::ProcessHarvest;
use sysinfo::{PidExt, ProcessExt, ProcessStatus, ProcessorExt, System, SystemExt}; use sysinfo::{PidExt, ProcessExt, ProcessStatus, ProcessorExt, System, SystemExt};
use crate::data_harvester::processes::UserTable;
fn get_macos_process_cpu_usage( fn get_macos_process_cpu_usage(
pids: &[i32], pids: &[i32],
) -> std::io::Result<std::collections::HashMap<i32, f64>> { ) -> std::io::Result<std::collections::HashMap<i32, f64>> {
@ -35,7 +37,7 @@ fn get_macos_process_cpu_usage(
} }
pub fn get_process_data( pub fn get_process_data(
sys: &System, use_current_cpu_total: bool, mem_total_kb: u64, sys: &System, use_current_cpu_total: bool, mem_total_kb: u64, user_table: &mut UserTable,
) -> crate::utils::error::Result<Vec<ProcessHarvest>> { ) -> crate::utils::error::Result<Vec<ProcessHarvest>> {
let mut process_vector: Vec<ProcessHarvest> = Vec::new(); let mut process_vector: Vec<ProcessHarvest> = Vec::new();
let process_hashmap = sys.processes(); let process_hashmap = sys.processes();
@ -86,6 +88,11 @@ pub fn get_process_data(
}; };
let disk_usage = process_val.disk_usage(); let disk_usage = process_val.disk_usage();
let process_state = {
let ps = process_val.status();
(ps.to_string(), convert_process_status_to_char(ps))
};
let uid = process_val.uid;
process_vector.push(ProcessHarvest { process_vector.push(ProcessHarvest {
pid: process_val.pid().as_u32() as _, pid: process_val.pid().as_u32() as _,
parent_pid: process_val.parent().map(|p| p.as_u32() as _), parent_pid: process_val.parent().map(|p| p.as_u32() as _),
@ -102,16 +109,19 @@ pub fn get_process_data(
write_bytes_per_sec: disk_usage.written_bytes, write_bytes_per_sec: disk_usage.written_bytes,
total_read_bytes: disk_usage.total_read_bytes, total_read_bytes: disk_usage.total_read_bytes,
total_write_bytes: disk_usage.total_written_bytes, total_write_bytes: disk_usage.total_written_bytes,
process_state: process_val.status().to_string(), process_state,
process_state_char: convert_process_status_to_char(process_val.status()), uid,
uid: process_val.uid, user: user_table
.get_uid_to_username_mapping(uid)
.map(Into::into)
.unwrap_or_else(|_| "N/A".into()),
}); });
} }
let unknown_state = ProcessStatus::Unknown(0).to_string(); let unknown_state = ProcessStatus::Unknown(0).to_string();
let cpu_usage_unknown_pids: Vec<i32> = process_vector let cpu_usage_unknown_pids: Vec<i32> = process_vector
.iter() .iter()
.filter(|process| process.process_state == unknown_state) .filter(|process| process.process_state.0 == unknown_state)
.map(|process| process.pid) .map(|process| process.pid)
.collect(); .collect();
let cpu_usages = get_macos_process_cpu_usage(&cpu_usage_unknown_pids)?; let cpu_usages = get_macos_process_cpu_usage(&cpu_usage_unknown_pids)?;

View File

@ -23,8 +23,6 @@ cfg_if::cfg_if! {
} }
} }
use std::borrow::Cow;
use crate::Pid; use crate::Pid;
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
@ -71,7 +69,7 @@ pub struct ProcessHarvest {
/// This is the process' user. This is only used on Unix platforms. /// This is the process' user. This is only used on Unix platforms.
#[cfg(target_family = "unix")] #[cfg(target_family = "unix")]
pub user: Cow<'static, str>, pub user: std::borrow::Cow<'static, str>,
// TODO: Additional fields // TODO: Additional fields
// pub rss_kb: u64, // pub rss_kb: u64,
// pub virt_kb: u64, // pub virt_kb: u64,

View File

@ -55,6 +55,7 @@ pub fn get_process_data(
}; };
let disk_usage = process_val.disk_usage(); let disk_usage = process_val.disk_usage();
let process_state = (process_val.status().to_string(), 'R');
process_vector.push(ProcessHarvest { process_vector.push(ProcessHarvest {
pid: process_val.pid().as_u32() as _, pid: process_val.pid().as_u32() as _,
parent_pid: process_val.parent().map(|p| p.as_u32() as _), parent_pid: process_val.parent().map(|p| p.as_u32() as _),
@ -71,8 +72,7 @@ pub fn get_process_data(
write_bytes_per_sec: disk_usage.written_bytes, write_bytes_per_sec: disk_usage.written_bytes,
total_read_bytes: disk_usage.total_read_bytes, total_read_bytes: disk_usage.total_read_bytes,
total_write_bytes: disk_usage.total_written_bytes, total_write_bytes: disk_usage.total_written_bytes,
process_state: process_val.status().to_string(), process_state,
process_state_char: 'R',
}); });
} }

View File

@ -626,5 +626,7 @@ mod test {
} }
#[test] #[test]
fn test_row_width_boundary_creation() {} fn test_row_width_boundary_creation() {
// FIXME: [TEST] finish this
}
} }

View File

@ -235,6 +235,8 @@ impl ProcWidgetColumn {
} }
} }
ProcWidgetColumn::User => { ProcWidgetColumn::User => {
#[cfg(target_family = "unix")]
{
data.sort_by_cached_key(|p| p.name.to_lowercase()); data.sort_by_cached_key(|p| p.name.to_lowercase());
if sort_descending { if sort_descending {
data.sort_by_cached_key(|p| Reverse(p.user.to_lowercase())); data.sort_by_cached_key(|p| Reverse(p.user.to_lowercase()));
@ -244,6 +246,7 @@ impl ProcWidgetColumn {
} }
} }
} }
}
/// Basically, anything "alphabetical" should sort in ascending order by default. This also includes something like /// Basically, anything "alphabetical" should sort in ascending order by default. This also includes something like
/// PID, as one would probably want PID to sort by default starting from 0 or 1. /// PID, as one would probably want PID to sort by default starting from 0 or 1.
@ -785,7 +788,16 @@ impl ProcWidget {
main: process.process_state.0.clone().into(), main: process.process_state.0.clone().into(),
alt: process.process_state.1.to_string().into(), alt: process.process_state.1.to_string().into(),
}, },
ProcWidgetColumn::User => process.user.clone().into(), ProcWidgetColumn::User => {
#[cfg(target_family = "unix")]
{
process.user.clone().into()
}
#[cfg(not(target_family = "unix"))]
{
"".into()
}
}
}; };
if let Some(curr) = col_widths.get_mut(itx) { if let Some(curr) = col_widths.get_mut(itx) {

View File

@ -68,9 +68,9 @@ pub struct Painter {
height: u16, height: u16,
width: u16, width: u16,
styled_help_text: Vec<Spans<'static>>, styled_help_text: Vec<Spans<'static>>,
is_mac_os: bool, // FIXME: This feels out of place... is_mac_os: bool, // TODO: This feels out of place...
// FIXME: Redo this entire thing. // TODO: Redo this entire thing.
row_constraints: Vec<Constraint>, row_constraints: Vec<Constraint>,
col_constraints: Vec<Vec<Constraint>>, col_constraints: Vec<Vec<Constraint>>,
col_row_constraints: Vec<Vec<Vec<Constraint>>>, col_row_constraints: Vec<Vec<Vec<Constraint>>>,

View File

@ -148,7 +148,7 @@ pub fn build_app() -> Command<'static> {
.help("Uses a dot marker for graphs.") .help("Uses a dot marker for graphs.")
.long_help("Uses a dot marker for graphs as opposed to the default braille marker."); .long_help("Uses a dot marker for graphs as opposed to the default braille marker.");
let group = Arg::new("group") // FIXME: Rename this to something like "group_process", would be "breaking" though. let group = Arg::new("group") // TODO: Rename this to something like "group_process", would be "breaking" though.
.short('g') .short('g')
.long("group") .long("group")
.help("Groups processes with the same name by default.") .help("Groups processes with the same name by default.")

View File

@ -316,7 +316,7 @@ pub fn handle_force_redraws(app: &mut App) {
app.cpu_state.force_update = None; app.cpu_state.force_update = None;
} }
// FIXME: [OPT] Prefer reassignment over new vectors? // TODO: [OPT] Prefer reassignment over new vectors?
if app.mem_state.force_update.is_some() { if app.mem_state.force_update.is_some() {
app.converted_data.mem_data = convert_mem_data_points(&app.data_collection); app.converted_data.mem_data = convert_mem_data_points(&app.data_collection);
app.converted_data.swap_data = convert_swap_data_points(&app.data_collection); app.converted_data.swap_data = convert_swap_data_points(&app.data_collection);

View File

@ -116,6 +116,7 @@ pub fn partial_ordering_rev<T: std::cmp::PartialOrd>(a: T, b: T) -> Ordering {
pub fn get_ordering<T: std::cmp::PartialOrd>( pub fn get_ordering<T: std::cmp::PartialOrd>(
a_val: T, b_val: T, reverse_order: bool, a_val: T, b_val: T, reverse_order: bool,
) -> std::cmp::Ordering { ) -> std::cmp::Ordering {
// FIXME: Maybe we can just delete this entirely and change references to use partial_ordering...
match a_val.partial_cmp(&b_val) { match a_val.partial_cmp(&b_val) {
Some(x) => match x { Some(x) => match x {
Ordering::Greater => { Ordering::Greater => {
@ -137,3 +138,23 @@ pub fn get_ordering<T: std::cmp::PartialOrd>(
None => Ordering::Equal, None => Ordering::Equal,
} }
} }
#[cfg(test)]
mod test {
// use super::*;
#[test]
fn test_sort_partial_fn() {
// FIXME: Do this
}
#[test]
fn test_partial_ordering() {
// FIXME: Do this
}
#[test]
fn test_reverse_partial_ordering() {
// FIXME: Do this
}
}