refactor: revert linear interpolation until tui fix comes

This commit is contained in:
Clement Tsang 2020-04-28 16:44:10 -04:00 committed by GitHub
parent de3a1fb7c0
commit 2faf3c6592
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 177 additions and 45 deletions

View File

@ -55,8 +55,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Switch to stateful widget style for tables.
- Switch to using tui-rs' new built in linear interpolation rather than doing it manually.
- Updated arg tests and added config testing.
- More refactoring.

6
Cargo.lock generated
View File

@ -136,7 +136,7 @@ dependencies = [
"serde 1.0.106 (registry+https://github.com/rust-lang/crates.io-index)",
"sysinfo 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)",
"tui 0.9.1 (git+https://github.com/ClementTsang/tui-rs)",
"tui 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)",
"typed-builder 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-segmentation 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1174,7 +1174,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "tui"
version = "0.9.1"
source = "git+https://github.com/ClementTsang/tui-rs#2be5ddd34d92a2f901f53eb17592eef7cb3cc403"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"cassowary 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1432,7 +1432,7 @@ dependencies = [
"checksum time 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)" = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438"
"checksum toml 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ffc92d160b1eef40665be3a05630d003936a3bc7da7421277846c2613e92c71a"
"checksum treeline 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a7f741b240f1a48843f9b8e0444fb55fb2a4ff67293b50a9179dfd5ea67f8d41"
"checksum tui 0.9.1 (git+https://github.com/ClementTsang/tui-rs)" = "<none>"
"checksum tui 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b7de74b91c6cb83119a2140e7c215d95d9e54db27b58a500a2cbdeec4987b0a2"
"checksum typed-builder 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "78cea224ddd4282dfc40d1edabbd0c020a12e946e3a48e2c2b8f6ff167ad29fe"
"checksum typenum 1.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33"
"checksum unicode-segmentation 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0"

View File

@ -42,9 +42,8 @@ serde = {version = "1.0", features = ["derive"] }
unicode-segmentation = "1.6.0"
unicode-width = "0.1.7"
# TODO: If tui-rs doesn't update by the time we want to push, we'll have to manually re-add linear interpolation again...
# tui = {version = "0.9", features = ["crossterm"], default-features = false }
tui = {git = "https://github.com/ClementTsang/tui-rs", features = ["crossterm"], default-features = false }
tui = {version = "0.9", features = ["crossterm"], default-features = false }
# tui = {git = "https://github.com/ClementTsang/tui-rs", features = ["crossterm"], default-features = false }
[target.'cfg(windows)'.dependencies]
winapi = "0.3.8"

View File

@ -21,14 +21,20 @@ use crate::data_harvester::{
pub type TimeOffset = f64;
pub type Value = f64;
pub type JoinedDataPoints = (Value, Vec<(TimeOffset, Value)>);
#[derive(Debug, Default)]
pub struct TimedData {
pub rx_data: Value,
pub tx_data: Value,
pub cpu_data: Vec<Value>,
pub mem_data: Value,
pub swap_data: Value,
// pub rx_data: Value,
// pub tx_data: Value,
// pub cpu_data: Vec<Value>,
// pub mem_data: Value,
// pub swap_data: Value,
pub rx_data: JoinedDataPoints,
pub tx_data: JoinedDataPoints,
pub cpu_data: Vec<JoinedDataPoints>,
pub mem_data: JoinedDataPoints,
pub swap_data: JoinedDataPoints,
}
/// AppCollection represents the pooled data stored within the main app
@ -117,19 +123,22 @@ impl DataCollection {
// Network
if let Some(network) = &harvested_data.network {
self.eat_network(network, &mut new_entry);
// self.eat_network(network, &mut new_entry);
self.eat_network(network, harvested_time, &mut new_entry);
}
// Memory and Swap
if let Some(memory) = &harvested_data.memory {
if let Some(swap) = &harvested_data.swap {
self.eat_memory_and_swap(memory, swap, &mut new_entry);
// self.eat_memory_and_swap(memory, swap, &mut new_entry);
self.eat_memory_and_swap(memory, swap, harvested_time, &mut new_entry);
}
}
// CPU
if let Some(cpu) = &harvested_data.cpu {
self.eat_cpu(cpu, &mut new_entry);
// self.eat_cpu(cpu, &mut new_entry);
self.eat_cpu(cpu, harvested_time, &mut new_entry);
}
// Temp
@ -160,7 +169,7 @@ impl DataCollection {
}
fn eat_memory_and_swap(
&mut self, memory: &mem::MemHarvest, swap: &mem::MemHarvest,
&mut self, memory: &mem::MemHarvest, swap: &mem::MemHarvest, harvested_time: Instant,
new_entry: &mut TimedData,
) {
// Memory
@ -168,7 +177,17 @@ impl DataCollection {
0 => 0f64,
total => (memory.mem_used_in_mb as f64) / (total as f64) * 100.0,
};
new_entry.mem_data = mem_percent;
// new_entry.mem_data = mem_percent;
// Can delete this block
let mem_joining_pts = if let Some((time, last_pt)) = self.timed_data_vec.last() {
generate_joining_points(*time, last_pt.mem_data.0, harvested_time, mem_percent)
} else {
Vec::new()
};
let mem_pt = (mem_percent, mem_joining_pts);
new_entry.mem_data = mem_pt;
// To here
// Swap
if swap.mem_total_in_mb > 0 {
@ -176,7 +195,17 @@ impl DataCollection {
0 => 0f64,
total => (swap.mem_used_in_mb as f64) / (total as f64) * 100.0,
};
new_entry.swap_data = swap_percent;
// new_entry.swap_data = swap_percent;
// Can delete to
let swap_joining_pt = if let Some((time, last_pt)) = self.timed_data_vec.last() {
generate_joining_points(*time, last_pt.swap_data.0, harvested_time, swap_percent)
} else {
Vec::new()
};
let swap_pt = (swap_percent, swap_joining_pt);
new_entry.swap_data = swap_pt;
// here
}
// In addition copy over latest data for easy reference
@ -185,7 +214,7 @@ impl DataCollection {
}
fn eat_network(
&mut self, network: &network::NetworkHarvest,
&mut self, network: &network::NetworkHarvest, harvested_time: Instant,
new_entry: &mut TimedData,
) {
// RX
@ -194,7 +223,17 @@ impl DataCollection {
} else {
0.0
};
new_entry.rx_data = logged_rx_val;
// new_entry.rx_data = logged_rx_val;
// Can delete
let rx_joining_pts = if let Some((time, last_pt)) = self.timed_data_vec.last() {
generate_joining_points(*time, last_pt.rx_data.0, harvested_time, logged_rx_val)
} else {
Vec::new()
};
let rx_pt = (logged_rx_val, rx_joining_pts);
new_entry.rx_data = rx_pt;
// to here
// TX
let logged_tx_val = if network.tx as f64 > 0.0 {
@ -202,20 +241,46 @@ impl DataCollection {
} else {
0.0
};
new_entry.tx_data = logged_tx_val;
// new_entry.tx_data = logged_tx_val;
// Can delete
let tx_joining_pts = if let Some((time, last_pt)) = self.timed_data_vec.last() {
generate_joining_points(*time, last_pt.tx_data.0, harvested_time, logged_tx_val)
} else {
Vec::new()
};
let tx_pt = (logged_tx_val, tx_joining_pts);
new_entry.tx_data = tx_pt;
// to here
// In addition copy over latest data for easy reference
self.network_harvest = network.clone();
}
fn eat_cpu(
&mut self, cpu: &[cpu::CPUData], new_entry: &mut TimedData,
&mut self, cpu: &[cpu::CPUData], harvested_time: Instant, new_entry: &mut TimedData,
) {
// Note this only pre-calculates the data points - the names will be
// within the local copy of cpu_harvest. Since it's all sequential
// it probably doesn't matter anyways.
cpu.iter()
.for_each(|cpu| new_entry.cpu_data.push(cpu.cpu_usage));
// cpu.iter()
// .for_each(|cpu| new_entry.cpu_data.push(cpu.cpu_usage));
// Can delete
if let Some((time, last_pt)) = self.timed_data_vec.last() {
for (cpu, last_pt_data) in cpu.iter().zip(&last_pt.cpu_data) {
let cpu_joining_pts =
generate_joining_points(*time, last_pt_data.0, harvested_time, cpu.cpu_usage);
let cpu_pt = (cpu.cpu_usage, cpu_joining_pts);
new_entry.cpu_data.push(cpu_pt);
}
} else {
for cpu in cpu.iter() {
let cpu_pt = (cpu.cpu_usage, Vec::new());
new_entry.cpu_data.push(cpu_pt);
}
}
// to here
self.cpu_harvest = cpu.to_vec();
}
@ -268,3 +333,37 @@ impl DataCollection {
self.battery_harvest = list_of_batteries.to_vec();
}
}
// Delete later
fn generate_joining_points(
start_x: Instant, start_y: f64, end_x: Instant, end_y: f64,
) -> Vec<(TimeOffset, Value)> {
let mut points: Vec<(TimeOffset, Value)> = Vec::new();
// Convert time floats first:
let tmp_time_diff = (end_x).duration_since(start_x).as_millis() as f64;
let time_difference = if tmp_time_diff == 0.0 {
0.001
} else {
tmp_time_diff
};
let value_difference = end_y - start_y;
// Let's generate... about this many points!
let num_points = std::cmp::min(
std::cmp::max(
(value_difference.abs() / time_difference * 2000.0) as u64,
50,
),
2000,
);
for itx in (0..num_points).step_by(2) {
points.push((
time_difference - (itx as f64 / num_points as f64 * time_difference),
start_y + (itx as f64 / num_points as f64 * value_difference),
));
}
points
}

View File

@ -0,0 +1 @@

View File

@ -161,7 +161,7 @@ impl CpuGraphWidget for Painter {
[itx % self.colours.cpu_colour_styles.len()]
})
.data(&cpu.cpu_data[..])
.graph_type(tui::widgets::GraphType::Line),
// .graph_type(tui::widgets::GraphType::Line),
)
} else {
None

View File

@ -73,8 +73,7 @@ impl MemGraphWidget for Painter {
Marker::Braille
})
.style(self.colours.ram_style)
.data(&mem_data)
.graph_type(tui::widgets::GraphType::Line),
.data(&mem_data), // .graph_type(tui::widgets::GraphType::Line),
);
}
@ -88,8 +87,7 @@ impl MemGraphWidget for Painter {
Marker::Braille
})
.style(self.colours.swap_style)
.data(&swap_data)
.graph_type(tui::widgets::GraphType::Line),
.data(&swap_data), // .graph_type(tui::widgets::GraphType::Line),
);
}

View File

@ -144,8 +144,7 @@ impl NetworkGraphWidget for Painter {
Marker::Braille
})
.style(self.colours.rx_style)
.data(&network_data_rx)
.graph_type(tui::widgets::GraphType::Line),
.data(&network_data_rx), // .graph_type(tui::widgets::GraphType::Line),
);
}
@ -159,8 +158,7 @@ impl NetworkGraphWidget for Painter {
Marker::Braille
})
.style(self.colours.tx_style)
.data(&network_data_tx)
.graph_type(tui::widgets::GraphType::Line),
.data(&network_data_tx), // .graph_type(tui::widgets::GraphType::Line),
);
ret_val.push(
Dataset::default()
@ -195,8 +193,7 @@ impl NetworkGraphWidget for Painter {
Marker::Braille
})
.style(self.colours.rx_style)
.data(&network_data_rx)
.graph_type(tui::widgets::GraphType::Line),
.data(&network_data_rx), // .graph_type(tui::widgets::GraphType::Line),
);
}
@ -210,8 +207,7 @@ impl NetworkGraphWidget for Painter {
Marker::Braille
})
.style(self.colours.tx_style)
.data(&network_data_tx)
.graph_type(tui::widgets::GraphType::Line),
.data(&network_data_tx), // .graph_type(tui::widgets::GraphType::Line),
);
}

View File

@ -161,8 +161,17 @@ pub fn convert_cpu_data_points(
}
if let Some(cpu_data) = cpu_data_vector.get_mut(itx) {
cpu_data.legend_value = format!("{:.0}%", cpu.round());
cpu_data.cpu_data.push((-time_from_start, *cpu));
// cpu_data.legend_value = format!("{:.0}%", cpu.round());
// cpu_data.cpu_data.push((-time_from_start, *cpu));
// Delete
cpu_data.legend_value = format!("{:.0}%", cpu.0.round());
for &(joiner_offset, joiner_val) in &cpu.1 {
let offset_time = time_from_start + joiner_offset as f64;
cpu_data.cpu_data.push((-offset_time, joiner_val));
}
cpu_data.cpu_data.push((-time_from_start, cpu.0));
// to here
}
}
@ -191,7 +200,15 @@ pub fn convert_mem_data_points(
for (time, data) in &current_data.timed_data_vec {
let time_from_start: f64 = (current_time.duration_since(*time).as_millis() as f64).floor();
result.push((-time_from_start, data.mem_data));
// result.push((-time_from_start, data.mem_data));
// Delete
for &(joiner_offset, joiner_val) in &data.mem_data.1 {
let offset_time = time_from_start + joiner_offset as f64;
result.push((-offset_time, joiner_val));
}
result.push((-time_from_start, data.mem_data.0));
// to here
if *time == current_time {
break;
@ -217,7 +234,16 @@ pub fn convert_swap_data_points(
for (time, data) in &current_data.timed_data_vec {
let time_from_start: f64 = (current_time.duration_since(*time).as_millis() as f64).floor();
result.push((-time_from_start, data.swap_data));
// result.push((-time_from_start, data.swap_data));
// Delete
for &(joiner_offset, joiner_val) in &data.swap_data.1 {
let offset_time = time_from_start + joiner_offset as f64;
result.push((-offset_time, joiner_val));
}
result.push((-time_from_start, data.swap_data.0));
// to here
if *time == current_time {
break;
@ -281,8 +307,23 @@ pub fn get_rx_tx_data_points(
for (time, data) in &current_data.timed_data_vec {
let time_from_start: f64 = (current_time.duration_since(*time).as_millis() as f64).floor();
rx.push((-time_from_start, data.rx_data));
tx.push((-time_from_start, data.tx_data));
// rx.push((-time_from_start, data.rx_data));
// tx.push((-time_from_start, data.tx_data));
// Delete
for &(joiner_offset, joiner_val) in &data.rx_data.1 {
let offset_time = time_from_start + joiner_offset as f64;
rx.push((-offset_time, joiner_val));
}
for &(joiner_offset, joiner_val) in &data.tx_data.1 {
let offset_time = time_from_start + joiner_offset as f64;
tx.push((-offset_time, joiner_val));
}
rx.push((-time_from_start, data.rx_data.0));
tx.push((-time_from_start, data.tx_data.0));
// to here
if *time == current_time {
break;
@ -463,7 +504,7 @@ pub fn convert_battery_harvest(
} else {
None
},
health: format!("{:.2}%", battery_harvest.health_percent)
health: format!("{:.2}%", battery_harvest.health_percent),
})
.collect()
}