Got the basics of process sorting done.

This commit is contained in:
ClementTsang 2019-09-05 18:28:54 -04:00
parent c8bbf5850c
commit 243742de2a
10 changed files with 120 additions and 106 deletions

View File

@ -6,7 +6,7 @@ A gotop clone, written in Rust. Mostly done in an effort to learn Rust, while a
* CPU usage monitor
* Disk usage (both total and I guess active?)
* Total disk usage
* Memory usage
@ -18,7 +18,7 @@ A gotop clone, written in Rust. Mostly done in an effort to learn Rust, while a
## Other possible features
* Potentially package managing?
* Potentially process managing?
* Theming
@ -28,6 +28,8 @@ A gotop clone, written in Rust. Mostly done in an effort to learn Rust, while a
* Filtering in processing
* See if current disk activity is possible to do/graph?
## Thanks
* As mentioned, this project is most definitely inspired by [gotop](https://github.com/cjbassi/gotop)

View File

@ -1,109 +1,10 @@
use sysinfo::{ProcessExt, ProcessStatus, RefreshKind, System, SystemExt};
// TODO: Fix this - CPU Up, and CPU Down!
enum ProcessSorting {
CPU,
MEM,
PID,
Status,
}
use sysinfo::{System, SystemExt};
mod widgets;
use widgets::{cpu, disks, mem, network, processes, temperature};
fn main() {
let mut system = System::new();
system.refresh_all();
draw_sorted_processes(ProcessSorting::CPU, true, &system);
//processes::draw_sorted_processes(processes::ProcessSorting::NAME, true, &system);
disks::draw_disk_usage_data(&system);
}
// Possible process info struct?
#[derive(Debug)]
struct ProcessInfo<'a> {
pid: u32,
cpu_usage: f32,
mem_usage: u64,
uptime: u64,
command: &'a str,
//status: &'a str,
// TODO: Env?
}
fn get_status(status: ProcessStatus) -> &'static str {
match status {
ProcessStatus::Idle => "Idle",
ProcessStatus::Run => "Run",
ProcessStatus::Sleep => "Sleep",
ProcessStatus::Zombie => "Zombie",
ProcessStatus::Tracing => "Tracing",
ProcessStatus::Dead => "Dead",
_ => "Unknown",
}
}
fn draw_sorted_processes(sorting_method: ProcessSorting, reverse_order: bool, sys: &System) {
let process_hashmap = sys.get_process_list();
// Read into a btreemap, based on sorting type.
// TODO: Evaluate whether this is too slow!
let mut process_vector: Vec<sysinfo::Process> = process_hashmap.iter().map(|(_, process)| process.clone()).collect();
match sorting_method {
ProcessSorting::CPU => process_vector.sort_by(|a, b| {
let a_usage = a.cpu_usage();
let b_usage = b.cpu_usage();
if a_usage > b_usage {
if reverse_order {
std::cmp::Ordering::Less
} else {
std::cmp::Ordering::Greater
}
} else if a_usage < b_usage {
if reverse_order {
std::cmp::Ordering::Greater
} else {
std::cmp::Ordering::Less
}
} else {
std::cmp::Ordering::Equal
}
}),
ProcessSorting::MEM => {}
ProcessSorting::PID => {}
ProcessSorting::Status => {}
}
let mut formatted_vector: Vec<ProcessInfo> = Vec::new();
for process in &mut process_vector {
formatted_vector.push(ProcessInfo {
cpu_usage: process.cpu_usage(),
command: process.name(),
mem_usage: process.memory(),
uptime: process.start_time(),
pid: process.pid() as u32,
});
}
for process in formatted_vector {
println!("{:?}", process);
}
}
fn get_timestamped_temperature() {}
fn draw_temperatures() {}
fn get_timestamped_cpu_data() {}
fn draw_cpu_data() {}
fn get_timestamped_ram_data() {}
fn draw_ram_data() {}
fn get_timestamped_network_data() {}
fn draw_network_data() {}
fn get_timestamped_drive_data() {}
fn draw_drive_usage_data() {}

View File

@ -0,0 +1,3 @@
fn get_timestamped_cpu_data() {}
fn draw_cpu_data() {}

View File

@ -0,0 +1,11 @@
use sysinfo::{System, SystemExt, DiskExt};
fn get_timestamped_disk_data() {}
pub fn draw_disk_usage_data(sys: &System) {
let list_of_disks = sys.get_disks();
for disk in list_of_disks {
println!("Disk: Total size: {}, used: {}, disk: {}, mount: {}", disk.get_total_space(), disk.get_total_space() - disk.get_available_space(), disk.get_name().to_str().unwrap(), disk.get_mount_point().to_str().unwrap());
}
}

View File

@ -0,0 +1,3 @@
fn get_timestamped_ram_data() {}
fn draw_ram_data() {}

6
src/widgets/mod.rs Normal file
View File

@ -0,0 +1,6 @@
pub mod disks;
pub mod temperature;
pub mod network;
pub mod processes;
pub mod mem;
pub mod cpu;

View File

@ -0,0 +1,3 @@
fn get_timestamped_network_data() {}
fn draw_network_data() {}

View File

@ -0,0 +1,82 @@
use sysinfo::{ProcessExt, ProcessStatus, System, SystemExt};
// TODO: Fix this - CPU Up, and CPU Down!
pub enum ProcessSorting {
CPU,
MEM,
PID,
NAME,
}
// Possible process info struct?
#[derive(Debug)]
pub struct ProcessInfo<'a> {
pid: u32,
cpu_usage: f32,
mem_usage: u64,
uptime: u64,
command: &'a str,
//status: &'a str,
// TODO: Env?
}
fn get_status(status: ProcessStatus) -> &'static str {
match status {
ProcessStatus::Idle => "Idle",
ProcessStatus::Run => "Run",
ProcessStatus::Sleep => "Sleep",
ProcessStatus::Zombie => "Zombie",
ProcessStatus::Tracing => "Tracing",
ProcessStatus::Dead => "Dead",
_ => "Unknown",
}
}
fn get_ordering<T: std::cmp::PartialOrd>(a_val: T, b_val: T, reverse_order: bool) -> std::cmp::Ordering {
if a_val > b_val {
if reverse_order {
std::cmp::Ordering::Less
} else {
std::cmp::Ordering::Greater
}
} else if a_val < b_val {
if reverse_order {
std::cmp::Ordering::Greater
} else {
std::cmp::Ordering::Less
}
} else {
std::cmp::Ordering::Equal
}
}
pub fn draw_sorted_processes(sorting_method: ProcessSorting, reverse_order: bool, sys: &System) {
let process_hashmap = sys.get_process_list();
// TODO: Evaluate whether this is too slow!
// TODO: Should I filter out blank command names?
let mut process_vector: Vec<sysinfo::Process> = process_hashmap.iter().map(|(_, process)| process.clone()).collect();
match sorting_method {
ProcessSorting::CPU => process_vector.sort_by(|a, b| get_ordering(a.cpu_usage(), b.cpu_usage(), reverse_order)),
ProcessSorting::MEM => process_vector.sort_by(|a, b| get_ordering(a.memory(), b.memory(), reverse_order)),
ProcessSorting::PID => process_vector.sort_by(|a, b| get_ordering(a.pid(), b.pid(), reverse_order)),
ProcessSorting::NAME => process_vector.sort_by(|a, b| get_ordering(a.name(), b.name(), reverse_order)),
}
let mut formatted_vector: Vec<ProcessInfo> = Vec::new();
for process in &mut process_vector {
formatted_vector.push(ProcessInfo {
cpu_usage: process.cpu_usage(),
command: process.name(),
mem_usage: process.memory(),
uptime: process.start_time(),
pid: process.pid() as u32,
});
}
// TODO: For debugging, remove.
for process in formatted_vector {
println!("{:?}", process);
}
}

View File

View File

@ -0,0 +1,3 @@
fn get_timestamped_temperature() {}
fn draw_temperatures() {}