From d7856a921e85ee5ea3d94f2098f5201372a51aaf Mon Sep 17 00:00:00 2001 From: alemi Date: Mon, 7 Nov 2022 00:48:34 +0100 Subject: [PATCH] feat: metrics now have only 1 query field X is always current timestamp, and everything is a timeserie by default --- src/data/entities/metrics.rs | 19 ++----- src/data/entities/panels.rs | 2 - src/gui/metric.rs | 30 ++--------- src/gui/panel.rs | 96 ++++++++++++++++++------------------ src/gui/scaffold.rs | 18 ++++--- src/util.rs | 5 +- src/worker/visualizer.rs | 1 - 7 files changed, 68 insertions(+), 103 deletions(-) diff --git a/src/data/entities/metrics.rs b/src/data/entities/metrics.rs index 9925972..5315cbf 100644 --- a/src/data/entities/metrics.rs +++ b/src/data/entities/metrics.rs @@ -13,8 +13,7 @@ pub struct Model { pub id: i64, pub name: String, pub source_id: i64, - pub query_x: String, - pub query_y: String, + pub query: String, pub color: i32, pub position: i32, } @@ -54,17 +53,10 @@ impl ActiveModelBehavior for ActiveModel {} impl Model { pub fn extract(&self, value: &serde_json::Value) -> Result { - let x: f64; - if self.query_x.len() > 0 { - x = jql::walker(value, self.query_x.as_str())? - .as_f64() - .ok_or(FetchError::JQLError("X query is null".to_string()))?; // TODO what if it's given to us as a string? - } else { - x = Utc::now().timestamp() as f64; - } - let y = jql::walker(value, self.query_y.as_str())? + let x = Utc::now().timestamp() as f64; + let y = jql::walker(value, self.query.as_str())? .as_f64() - .ok_or(FetchError::JQLError("Y query is null".to_string()))?; + .ok_or(FetchError::JQLError("query result is null".to_string()))?; Ok(PlotPoint { x, y }) } } @@ -75,8 +67,7 @@ impl Default for Model { id: 0, name: "".into(), source_id: 0, - query_x: "".into(), - query_y: "".into(), + query: "".into(), color: 0, position: 0, } diff --git a/src/data/entities/panels.rs b/src/data/entities/panels.rs index dc9bd68..a60ed97 100644 --- a/src/data/entities/panels.rs +++ b/src/data/entities/panels.rs @@ -10,7 +10,6 @@ pub struct Model { pub name: String, pub view_scroll: bool, pub view_size: i32, - pub timeserie: bool, pub height: i32, pub position: i32, pub reduce_view: bool, @@ -41,7 +40,6 @@ impl Default for Model { name: "".into(), view_scroll: true, view_size: 1000, - timeserie: true, height: 100, position: 0, reduce_view: false, diff --git a/src/gui/metric.rs b/src/gui/metric.rs index 47130fb..a58bb86 100644 --- a/src/gui/metric.rs +++ b/src/gui/metric.rs @@ -1,4 +1,4 @@ -use eframe::{egui::{Ui, Layout, Sense, color_picker::show_color_at, TextEdit}, emath::Align, epaint::Color32}; +use eframe::{egui::{Ui, Sense, color_picker::show_color_at, TextEdit}, epaint::Color32}; use crate::{data::entities, util::unpack_color}; @@ -17,26 +17,9 @@ fn color_square(ui: &mut Ui, color:Color32) { } } -pub fn _metric_display_ui(ui: &mut Ui, metric: &entities::metrics::Model, _width: f32) { - ui.horizontal(|ui| { - color_square(ui, unpack_color(metric.color)); - ui.label(&metric.name); - ui.with_layout(Layout::top_down(Align::RIGHT), |ui| { - ui.horizontal(|ui| { - ui.label("panel: ???"); - ui.label(format!("y: {}", metric.query_y)); - // if let Some(query_x) = metric.query_x { - // ui.label(format!("x: {}", query_x)); - // } - }) - }); - }); -} - pub fn metric_edit_ui(ui: &mut Ui, metric: &entities::metrics::Model) { let mut name = metric.name.clone(); - let mut query_x = metric.query_x.clone(); - let mut query_y = metric.query_y.clone(); + let mut query = metric.query.clone(); ui.horizontal(|ui| { // ui.color_edit_button_srgba(&mut unpack_color(metric.color)); color_square(ui, unpack_color(metric.color)); @@ -47,13 +30,8 @@ pub fn metric_edit_ui(ui: &mut Ui, metric: &entities::metrics::Model) { .hint_text("name") .show(ui); ui.separator(); - TextEdit::singleline(&mut query_x) - .desired_width(available / 4.0) - .interactive(false) - .hint_text("x") - .show(ui); - TextEdit::singleline(&mut query_y) - .desired_width(available / 4.0) + TextEdit::singleline(&mut query) + .desired_width(available / 2.0) .interactive(false) .hint_text("y") .show(ui); diff --git a/src/gui/panel.rs b/src/gui/panel.rs index c3b54dd..d780859 100644 --- a/src/gui/panel.rs +++ b/src/gui/panel.rs @@ -159,56 +159,54 @@ pub fn panel_body_ui( } - if panel.timeserie { - if panel.view_scroll { - let now = (Utc::now().timestamp() as f64) - (60.0 * panel.view_offset as f64); - p = p.include_x(now) - .include_x(now + (panel.view_size as f64 * 3.0)) - .include_x(now - (panel.view_size as f64 * 60.0)); // ??? TODO - } - p = p - .x_axis_formatter(|x, _range| timestamp_to_str(x as i64, true, false)) - .label_formatter(|name, value| { - if !name.is_empty() { - return format!( - "{}\nx = {}\ny = {:.1}", - name, - timestamp_to_str(value.x as i64, false, true), - value.y - ); - } else { - return format!( - "x = {}\ny = {:.1}", - timestamp_to_str(value.x as i64, false, true), - value.y - ); - } - }) - .x_grid_spacer(|grid| { - let offset = Local::now().offset().local_minus_utc() as i64; - let (start, end) = grid.bounds; - let mut counter = (start as i64) - ((start as i64) % 3600); - let mut out: Vec = Vec::new(); - loop { - counter += 3600; - if counter > end as i64 { - break; - } - if (counter + offset) % 86400 == 0 { - out.push(GridMark { - value: counter as f64, - step_size: 86400 as f64, - }) - } else if counter % 3600 == 0 { - out.push(GridMark { - value: counter as f64, - step_size: 3600 as f64, - }); - } - } - return out; - }); + if panel.view_scroll { + let now = (Utc::now().timestamp() as f64) - (60.0 * panel.view_offset as f64); + p = p.include_x(now) + .include_x(now + (panel.view_size as f64 * 3.0)) + .include_x(now - (panel.view_size as f64 * 60.0)); // ??? TODO } + p = p + .x_axis_formatter(|x, _range| timestamp_to_str(x as i64, true, false)) + .label_formatter(|name, value| { + if !name.is_empty() { + return format!( + "{}\nx = {}\ny = {:.1}", + name, + timestamp_to_str(value.x as i64, false, true), + value.y + ); + } else { + return format!( + "x = {}\ny = {:.1}", + timestamp_to_str(value.x as i64, false, true), + value.y + ); + } + }) + .x_grid_spacer(|grid| { + let offset = Local::now().offset().local_minus_utc() as i64; + let (start, end) = grid.bounds; + let mut counter = (start as i64) - ((start as i64) % 3600); + let mut out: Vec = Vec::new(); + loop { + counter += 3600; + if counter > end as i64 { + break; + } + if (counter + offset) % 86400 == 0 { + out.push(GridMark { + value: counter as f64, + step_size: 86400 as f64, + }) + } else if counter % 3600 == 0 { + out.push(GridMark { + value: counter as f64, + step_size: 3600 as f64, + }); + } + } + return out; + }); let mut lines : Vec = Vec::new(); let now = Utc::now().timestamp() as f64; diff --git a/src/gui/scaffold.rs b/src/gui/scaffold.rs index ff2e4a8..bb20364 100644 --- a/src/gui/scaffold.rs +++ b/src/gui/scaffold.rs @@ -113,7 +113,6 @@ impl EditingModel { name: Set(panel.name.clone()), view_scroll: Set(panel.view_scroll), view_size: Set(panel.view_size), - timeserie: Set(panel.timeserie), height: Set(panel.height), position: Set(panel.position), reduce_view: Set(panel.reduce_view), @@ -150,8 +149,7 @@ impl EditingModel { name: Set(metric.name.clone()), source_id: Set(metric.source_id), color: Set(metric.color), - query_x: Set(metric.query_x.clone()), - query_y: Set(metric.query_y.clone()), + query: Set(metric.query.clone()), position: Set(metric.position), } }, @@ -251,11 +249,11 @@ pub fn popup_edit_ui( ui.selectable_value(&mut metric.source_id, s.id, s.name.as_str()); } }); - TextEdit::singleline(&mut metric.query_x) - .hint_text("x") - .show(ui); - TextEdit::singleline(&mut metric.query_y) - .hint_text("y") + // TextEdit::singleline(&mut metric.query_x) + // .hint_text("x") + // .show(ui); + TextEdit::singleline(&mut metric.query) + .hint_text("query") .show(ui); }, } @@ -292,8 +290,12 @@ pub fn header(app: &mut App, ui: &mut Ui, frame: &mut Frame) { app.refresh_data(); } ui.separator(); + let last_edit = app.edit; // replace panels when going into edit mode ui.checkbox(&mut app.edit, "edit"); if app.edit { + if !last_edit { // TODO kinda cheap fix having it down here + app.panels = app.view.panels.borrow().clone(); + } if ui.button("reset").clicked() { app.panels = app.view.panels.borrow().clone(); } diff --git a/src/util.rs b/src/util.rs index 7e49001..d42bda3 100644 --- a/src/util.rs +++ b/src/util.rs @@ -14,9 +14,8 @@ pub fn _serialize_values(values: &Vec, metric: &entities::metrics::Mo let mut wtr = csv::Writer::from_writer(std::fs::File::create(path)?); // DAMN! VVVVV let name = metric.name.as_str(); - let q_x = metric.query_x.as_str(); - let q_y = metric.query_y.as_str(); - wtr.write_record(&[name, q_x, q_y])?; + let q = metric.query.as_str(); + wtr.write_record(&[name, q, "1"])?; // DAMN! AAAAA for v in values { wtr.serialize(("", v.x, v.y))?; diff --git a/src/worker/visualizer.rs b/src/worker/visualizer.rs index 11a464c..b556f69 100644 --- a/src/worker/visualizer.rs +++ b/src/worker/visualizer.rs @@ -171,7 +171,6 @@ impl AppState { name: Set(v.name.clone()), view_scroll: Set(v.view_scroll), view_size: Set(v.view_size), - timeserie: Set(v.timeserie), height: Set(v.height), position: Set(v.position), reduce_view: Set(v.reduce_view),