Changed layout again.

This commit is contained in:
ClementTsang 2019-09-11 23:15:25 -04:00
parent 86eb14f620
commit afe9349573
3 changed files with 64 additions and 37 deletions

View File

@ -52,7 +52,7 @@ fn vangelis_cpu_usage_calculation(prev_idle : &mut f64, prev_non_idle : &mut f64
let total_delta : f64 = total - prev_total; let total_delta : f64 = total - prev_total;
let idle_delta : f64 = idle - *prev_idle; let idle_delta : f64 = idle - *prev_idle;
debug!("Vangelis function: CPU PERCENT: {}", (total_delta - idle_delta) / total_delta * 100_f64); //debug!("Vangelis function: CPU PERCENT: {}", (total_delta - idle_delta) / total_delta * 100_f64);
*prev_idle = idle; *prev_idle = idle;
*prev_non_idle = non_idle; *prev_non_idle = non_idle;
@ -103,7 +103,7 @@ fn get_process_cpu_stats(pid : u32) -> std::io::Result<f64> {
let utime = val[13].parse::<f64>().unwrap_or(0_f64); let utime = val[13].parse::<f64>().unwrap_or(0_f64);
let stime = val[14].parse::<f64>().unwrap_or(0_f64); let stime = val[14].parse::<f64>().unwrap_or(0_f64);
debug!("PID: {}, utime: {}, stime: {}", pid, utime, stime); //debug!("PID: {}, utime: {}, stime: {}", pid, utime, stime);
Ok(utime + stime) // This seems to match top... Ok(utime + stime) // This seems to match top...
} }
@ -118,14 +118,14 @@ fn linux_cpu_usage(pid : u32, cpu_usage : f64, previous_pid_stats : &mut HashMap
}; };
let after_proc_val = get_process_cpu_stats(pid)?; let after_proc_val = get_process_cpu_stats(pid)?;
debug!( /*debug!(
"PID - {} - Before: {}, After: {}, CPU: {}, Percentage: {}", "PID - {} - Before: {}, After: {}, CPU: {}, Percentage: {}",
pid, pid,
before_proc_val, before_proc_val,
after_proc_val, after_proc_val,
cpu_usage, cpu_usage,
(after_proc_val - before_proc_val) / cpu_usage * 100_f64 (after_proc_val - before_proc_val) / cpu_usage * 100_f64
); );*/
let entry = previous_pid_stats.entry(pid.to_string()).or_insert(after_proc_val); let entry = previous_pid_stats.entry(pid.to_string()).or_insert(after_proc_val);
*entry = after_proc_val; *entry = after_proc_val;

View File

@ -7,7 +7,7 @@ use tui::{
}; };
const COLOUR_LIST : [Color; 6] = [Color::LightRed, Color::LightGreen, Color::LightYellow, Color::LightBlue, Color::LightCyan, Color::LightMagenta]; const COLOUR_LIST : [Color; 6] = [Color::LightRed, Color::LightGreen, Color::LightYellow, Color::LightBlue, Color::LightCyan, Color::LightMagenta];
const TEXT_COLOUR : Color = Color::Green; const TEXT_COLOUR : Color = Color::Gray;
#[derive(Default)] #[derive(Default)]
pub struct CanvasData { pub struct CanvasData {
@ -22,49 +22,64 @@ pub struct CanvasData {
// TODO: Change the error // TODO: Change the error
pub fn draw_data<B : tui::backend::Backend>(terminal : &mut Terminal<B>, canvas_data : &CanvasData) -> Result<(), io::Error> { pub fn draw_data<B : tui::backend::Backend>(terminal : &mut Terminal<B>, canvas_data : &CanvasData) -> Result<(), io::Error> {
let temperature_rows = canvas_data.temp_sensor_data.iter().map(|sensor| Row::StyledData(sensor.iter(), Style::default().fg(TEXT_COLOUR))); let temperature_rows = canvas_data.temp_sensor_data.iter().map(|sensor| Row::StyledData(sensor.iter(), Style::default().fg(TEXT_COLOUR)));
let disk_rows = canvas_data.disk_data.iter().map(|disk| Row::StyledData(disk.iter(), Style::default().fg(TEXT_COLOUR))); let disk_rows = canvas_data.disk_data.iter().map(|disk| Row::StyledData(disk.iter(), Style::default().fg(TEXT_COLOUR)));
let process_rows = canvas_data.process_data.iter().map(|process| Row::StyledData(process.iter(), Style::default().fg(TEXT_COLOUR))); let process_rows = canvas_data.process_data.iter().map(|process| Row::StyledData(process.iter(), Style::default().fg(TEXT_COLOUR)));
// TODO: Convert this into a separate func! // TODO: Convert this into a separate func!
terminal.draw(|mut f| { terminal.draw(|mut f| {
debug!("Drawing!");
let vertical_chunks = Layout::default() let vertical_chunks = Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.margin(1) .margin(1)
.constraints([Constraint::Percentage(35), Constraint::Percentage(30), Constraint::Percentage(35)].as_ref()) .constraints([Constraint::Percentage(34), Constraint::Percentage(34), Constraint::Percentage(32)].as_ref())
.split(f.size()); .split(f.size());
let top_chunks = Layout::default() let _top_chunks = Layout::default()
.direction(Direction::Horizontal) .direction(Direction::Horizontal)
.margin(0) .margin(0)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref()) .constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
.split(vertical_chunks[0]); .split(vertical_chunks[0]);
let middle_chunks = Layout::default() let middle_chunks = Layout::default()
.direction(Direction::Horizontal) .direction(Direction::Horizontal)
.margin(0) .margin(0)
.constraints([Constraint::Percentage(40), Constraint::Percentage(60)].as_ref()) .constraints([Constraint::Percentage(70), Constraint::Percentage(30)].as_ref())
.split(vertical_chunks[1]); .split(vertical_chunks[1]);
let middle_divided_chunk_1 = Layout::default() let _middle_divided_chunk_1 = Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.margin(0) .margin(0)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref()) .constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
.split(middle_chunks[0]); .split(middle_chunks[0]);
let middle_divided_chunk_2 = Layout::default() let _middle_divided_chunk_2 = Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.margin(0) .margin(0)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref()) .constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
.split(middle_chunks[1]); .split(middle_chunks[1]);
let bottom_chunks = Layout::default() let bottom_chunks = Layout::default()
.direction(Direction::Horizontal) .direction(Direction::Horizontal)
.margin(0) .margin(0)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref()) .constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
.split(vertical_chunks[2]); .split(vertical_chunks[2]);
let bottom_divided_chunk_1 = Layout::default()
.direction(Direction::Vertical)
.margin(0)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
.split(bottom_chunks[0]);
let bottom_divided_chunk_1_1 = Layout::default()
.direction(Direction::Horizontal)
.margin(0)
.constraints([Constraint::Percentage(40), Constraint::Percentage(60)].as_ref())
.split(bottom_divided_chunk_1[0]);
let bottom_divided_chunk_1_2 = Layout::default()
.direction(Direction::Horizontal)
.margin(0)
.constraints([Constraint::Percentage(40), Constraint::Percentage(60)].as_ref())
.split(bottom_divided_chunk_1[1]);
// Set up blocks and their components // Set up blocks and their components
// CPU usage graph // CPU usage graph
{ {
debug!("Drawing CPU...");
let x_axis : Axis<String> = Axis::default().style(Style::default().fg(Color::White)).bounds([0.0, 60.0]); let x_axis : Axis<String> = Axis::default().style(Style::default().fg(Color::White)).bounds([0.0, 60.0]);
let y_axis = Axis::default().style(Style::default().fg(Color::White)).bounds([0.0, 100.0]).labels(&["0.0", "50.0", "100.0"]); let y_axis = Axis::default().style(Style::default().fg(Color::White)).bounds([0.0, 100.0]).labels(&["0.0", "50.0", "100.0"]);
@ -84,7 +99,7 @@ pub fn draw_data<B : tui::backend::Backend>(terminal : &mut Terminal<B>, canvas_
.x_axis(x_axis) .x_axis(x_axis)
.y_axis(y_axis) .y_axis(y_axis)
.datasets(&dataset_vector) .datasets(&dataset_vector)
.render(&mut f, top_chunks[0]); .render(&mut f, vertical_chunks[0]);
} }
//Memory usage graph //Memory usage graph
@ -99,7 +114,7 @@ pub fn draw_data<B : tui::backend::Backend>(terminal : &mut Terminal<B>, canvas_
Dataset::default() Dataset::default()
.name(&("MEM :".to_string() + &format!("{:3}%", (canvas_data.mem_data.last().unwrap_or(&(0_f64, 0_f64)).1.round() as u64)))) .name(&("MEM :".to_string() + &format!("{:3}%", (canvas_data.mem_data.last().unwrap_or(&(0_f64, 0_f64)).1.round() as u64))))
.marker(Marker::Braille) .marker(Marker::Braille)
.style(Style::default().fg(Color::Cyan)) .style(Style::default().fg(Color::LightRed))
.data(&canvas_data.mem_data), .data(&canvas_data.mem_data),
Dataset::default() Dataset::default()
.name(&("SWAP:".to_string() + &format!("{:3}%", (canvas_data.swap_data.last().unwrap_or(&(0_f64, 0_f64)).1.round() as u64)))) .name(&("SWAP:".to_string() + &format!("{:3}%", (canvas_data.swap_data.last().unwrap_or(&(0_f64, 0_f64)).1.round() as u64))))
@ -107,31 +122,31 @@ pub fn draw_data<B : tui::backend::Backend>(terminal : &mut Terminal<B>, canvas_
.style(Style::default().fg(Color::LightGreen)) .style(Style::default().fg(Color::LightGreen))
.data(&canvas_data.swap_data), .data(&canvas_data.swap_data),
]) ])
.render(&mut f, top_chunks[1]); .render(&mut f, middle_chunks[0]);
} }
// Network graph
Block::default().title("Network").borders(Borders::ALL).render(&mut f, middle_chunks[1]);
// Temperature table // Temperature table
Table::new(["Sensor", "Temperature"].iter(), temperature_rows) Table::new(["Sensor", "Temperature"].iter(), temperature_rows)
.block(Block::default().title("Temperatures").borders(Borders::ALL)) .block(Block::default().title("Temperatures").borders(Borders::ALL))
.header_style(Style::default().fg(Color::LightBlue)) .header_style(Style::default().fg(Color::LightBlue))
.widths(&[15, 5]) .widths(&[15, 5])
.render(&mut f, middle_divided_chunk_1[0]); .render(&mut f, bottom_divided_chunk_1_1[0]);
// Disk usage table // Disk usage table
Table::new(["Disk", "Mount", "Used", "Total", "Free"].iter(), disk_rows) Table::new(["Disk", "Mount", "Used", "Total", "Free"].iter(), disk_rows)
.block(Block::default().title("Disk Usage").borders(Borders::ALL)) .block(Block::default().title("Disk Usage").borders(Borders::ALL))
.header_style(Style::default().fg(Color::LightBlue)) .header_style(Style::default().fg(Color::LightBlue))
.widths(&[15, 10, 5, 5, 5]) .widths(&[15, 10, 5, 5, 5])
.render(&mut f, middle_divided_chunk_1[1]); .render(&mut f, bottom_divided_chunk_1_2[0]);
// Temp graph // Temp graph
Block::default().title("Temperatures").borders(Borders::ALL).render(&mut f, middle_divided_chunk_2[0]); Block::default().title("Temperatures").borders(Borders::ALL).render(&mut f, bottom_divided_chunk_1_1[1]);
// IO graph // IO graph
Block::default().title("IO Usage").borders(Borders::ALL).render(&mut f, middle_divided_chunk_2[1]); Block::default().title("IO Usage").borders(Borders::ALL).render(&mut f, bottom_divided_chunk_1_2[1]);
// Network graph
Block::default().title("Network").borders(Borders::ALL).render(&mut f, bottom_chunks[0]);
// Processes table // Processes table
Table::new(["PID", "Name", "CPU%", "Mem%"].iter(), process_rows) Table::new(["PID", "Name", "CPU%", "Mem%"].iter(), process_rows)

View File

@ -88,17 +88,15 @@ async fn main() -> Result<(), io::Error> {
if app.to_be_resorted { if app.to_be_resorted {
data_collection::processes::sort_processes(&mut app_data.list_of_processes, &app.process_sorting_type, app.process_sorting_reverse); data_collection::processes::sort_processes(&mut app_data.list_of_processes, &app.process_sorting_type, app.process_sorting_reverse);
canvas_data.process_data = update_process_row(&app_data);
app.to_be_resorted = false; app.to_be_resorted = false;
} }
try_debug(&log, "Input event complete."); try_debug(&log, "Input event complete.");
// Only update processes
} }
Event::Update(data) => { Event::Update(data) => {
try_debug(&log, "Update event fired!"); try_debug(&log, "Update event fired!");
app_data = *data; app_data = *data;
data_collection::processes::sort_processes(&mut app_data.list_of_processes, &app.process_sorting_type, app.process_sorting_reverse); data_collection::processes::sort_processes(&mut app_data.list_of_processes, &app.process_sorting_type, app.process_sorting_reverse);
try_debug(&log, "Update event complete.");
// Convert all data into tui components // Convert all data into tui components
canvas_data.disk_data = update_disk_row(&app_data); canvas_data.disk_data = update_disk_row(&app_data);
@ -107,15 +105,15 @@ async fn main() -> Result<(), io::Error> {
canvas_data.mem_data = update_mem_data_points(&app_data); canvas_data.mem_data = update_mem_data_points(&app_data);
canvas_data.swap_data = update_swap_data_points(&app_data); canvas_data.swap_data = update_swap_data_points(&app_data);
canvas_data.cpu_data = update_cpu_data_points(&app_data); canvas_data.cpu_data = update_cpu_data_points(&app_data);
try_debug(&log, "Update event complete.");
} }
} }
if app.should_quit { if app.should_quit {
break; break;
} }
} }
// Draw! // Draw!
// TODO: We should change this btw! It should not redraw everything on every tick!
canvas::draw_data(&mut terminal, &canvas_data)?; canvas::draw_data(&mut terminal, &canvas_data)?;
} }
@ -187,30 +185,44 @@ fn update_process_row(app_data : &app::Data) -> Vec<Vec<String>> {
} }
fn update_cpu_data_points(app_data : &app::Data) -> Vec<(String, Vec<(f64, f64)>)> { fn update_cpu_data_points(app_data : &app::Data) -> Vec<(String, Vec<(f64, f64)>)> {
let mut cpu_vector : Vec<(String, Vec<(f64, f64)>)> = Vec::new(); let mut cpu_data_vector : Vec<(String, Vec<(f64, f64)>)> = Vec::new();
let mut data_vector : Vec<Vec<(f64, f64)>> = Vec::new(); let mut cpu_collection : Vec<Vec<(f64, f64)>> = Vec::new();
if !app_data.list_of_cpu_packages.is_empty() { if !app_data.list_of_cpu_packages.is_empty() {
for cpu_num in 0..app_data.list_of_cpu_packages.last().unwrap().cpu_vec.len() { // Initially, populate the cpu_collection. We want to inject elements in between if possible.
let current_time = std::time::Instant::now();
for cpu_num in 1..app_data.list_of_cpu_packages.last().unwrap().cpu_vec.len() {
// 1 to skip total cpu
let mut this_cpu_data : Vec<(f64, f64)> = Vec::new(); let mut this_cpu_data : Vec<(f64, f64)> = Vec::new();
let current_time = std::time::Instant::now();
for cpu in &app_data.list_of_cpu_packages { for cpu in &app_data.list_of_cpu_packages {
this_cpu_data.push((STALE_MAX_SECONDS as f64 - current_time.duration_since(cpu.instant).as_secs_f64(), cpu.cpu_vec[cpu_num].cpu_usage)); // To make it look better...
let current_cpu_usage = cpu.cpu_vec[cpu_num].cpu_usage;
if !this_cpu_data.is_empty() {
let previous_entry_cpu_usage = this_cpu_data.last().unwrap().1;
let middling_cpu_usage = (previous_entry_cpu_usage + current_cpu_usage) / 2_f64;
this_cpu_data.push((STALE_MAX_SECONDS as f64 - current_time.duration_since(cpu.instant).as_secs_f64().floor() - 0.5_f64, middling_cpu_usage));
}
this_cpu_data.push((STALE_MAX_SECONDS as f64 - current_time.duration_since(cpu.instant).as_secs_f64().floor(), current_cpu_usage));
} }
data_vector.push(this_cpu_data); cpu_collection.push(this_cpu_data);
} }
for (i, data) in data_vector.iter().enumerate() { // Finally, add it all onto the end
cpu_vector.push(( for (i, data) in cpu_collection.iter().enumerate() {
cpu_data_vector.push((
(&*(app_data.list_of_cpu_packages.last().unwrap().cpu_vec[i].cpu_name)).to_string() + " " + &format!("{:.2}", data.last().unwrap_or(&(0_f64, 0_f64)).1.to_string()), (&*(app_data.list_of_cpu_packages.last().unwrap().cpu_vec[i].cpu_name)).to_string() + " " + &format!("{:.2}", data.last().unwrap_or(&(0_f64, 0_f64)).1.to_string()),
data.clone(), data.clone(),
)) ))
} }
} }
cpu_vector cpu_data_vector
} }
fn update_mem_data_points(app_data : &app::Data) -> Vec<(f64, f64)> { fn update_mem_data_points(app_data : &app::Data) -> Vec<(f64, f64)> {