feat!: allow to toggle averaging when reducing

This commit is contained in:
əlemi 2022-06-25 02:33:52 +02:00
parent 78989daaae
commit 2c0504c170
No known key found for this signature in database
GPG key ID: BBCBFE5D7244634E
3 changed files with 11 additions and 14 deletions

View file

@ -4,7 +4,7 @@ use eframe::egui::plot::Value;
use eframe::epaint::Color32;
use std::sync::RwLock;
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Panel {
pub(crate) id: i32,
pub name: String,
@ -18,6 +18,7 @@ pub struct Panel {
pub limit: bool,
pub reduce: bool,
pub shift: bool,
pub average: bool,
}
impl Default for Panel {
@ -35,6 +36,7 @@ impl Default for Panel {
limit: false,
reduce: false,
shift: false,
average: false,
}
}
}
@ -138,6 +140,7 @@ impl Metric {
min_x: Option<f64>,
max_x: Option<f64>,
chunk_size: Option<u32>,
average: bool,
) -> Vec<Value> {
let mut values = self.data.read().expect("Values RwLock poisoned").clone();
if let Some(min_x) = min_x {
@ -150,7 +153,7 @@ impl Metric {
if chunk_size > 0 {
// TODO make this nested if prettier
let iter = values.chunks(chunk_size as usize);
values = iter.map(|x| avg_value(x)).collect();
values = iter.map(|x| if average { avg_value(x) } else { if x.len() > 0 { x[x.len()-1] } else { Value {x: 0.0, y:0.0 }} }).collect();
}
}
values

View file

@ -36,7 +36,8 @@ impl SQLiteDataStore {
reduce_view BOOL NOT NULL,
view_chunks INT NOT NULL,
shift_view BOOL NOT NULL,
view_offset INT NOT NULL
view_offset INT NOT NULL,
average_view BOOL NOT NULL
);",
[],
)?;
@ -67,15 +68,6 @@ impl SQLiteDataStore {
[],
)?;
// BEGIN TRANSACTION;
// CREATE TEMPORARY TABLE t1_backup(a,b);
// INSERT INTO t1_backup SELECT a,b FROM t1;
// DROP TABLE t1;
// CREATE TABLE t1(a,b);
// INSERT INTO t1 SELECT a,b FROM t1_backup;
// DROP TABLE t1_backup;
// COMMIT;
conn.execute(
"CREATE TABLE IF NOT EXISTS points (
id INTEGER PRIMARY KEY,
@ -308,6 +300,7 @@ impl SQLiteDataStore {
view_chunks: row.get(10)?,
shift: row.get(11)?,
view_offset: row.get(12)?,
average: row.get(13)?,
})
})?;
@ -356,6 +349,7 @@ impl SQLiteDataStore {
view_chunks: row.get(10)?,
shift: row.get(11)?,
view_offset: row.get(12)?,
average: row.get(13)?,
})
})? {
if let Ok(p) = panel {

View file

@ -134,7 +134,7 @@ pub fn panel_title_ui(ui: &mut Ui, panel: &mut Panel, edit: bool) { // TODO make
.prefix("x")
.clamp_range(1..=1000), // TODO allow to average larger spans maybe?
);
ui.checkbox(&mut panel.average, "avg");
ui.toggle_value(&mut panel.average, "avg");
}
ui.toggle_value(&mut panel.reduce, "reduce");
});
@ -216,7 +216,7 @@ pub fn panel_body_ui(ui: &mut Ui, panel: &mut Panel, metrics: &Vec<Metric>) {
let chunk_size = if panel.reduce { Some(panel.view_chunks) } else { None };
for metric in metrics {
if metric.panel_id == panel.id {
let values = metric.values(min_x, max_x, chunk_size);
let values = metric.values(min_x, max_x, chunk_size, panel.average);
// if !panel.timeserie && panel.view_scroll && values.len() > 0 {
// let l = values.len() - 1;
// p = p.include_x(values[0].x)