fix: warnings, style, non-timeserie lock (maybe)

This commit is contained in:
əlemi 2022-06-21 03:36:09 +02:00
parent f249ca372a
commit c636946137
Signed by: alemi
GPG key ID: A4895B84D311642C
4 changed files with 48 additions and 34 deletions

View file

@ -1,9 +1,8 @@
use super::FetchError;
use chrono::{DateTime, Utc};
use eframe::egui::plot::{Value, Values};
use eframe::egui::plot::Value;
use eframe::epaint::Color32;
use std::sync::RwLock;
use tracing::info;
#[derive(Debug)]
pub struct Panel {
@ -82,9 +81,9 @@ impl Source {
return (Utc::now() - *last_fetch).num_seconds() < self.interval as i64;
}
pub fn fetch(&self) -> Result<serde_json::Value, FetchError> {
fetch(self.url.as_str())
}
// pub fn fetch(&self) -> Result<serde_json::Value, FetchError> {
// fetch(self.url.as_str())
// }
}
pub fn fetch(url: &str) -> Result<serde_json::Value, FetchError> {
@ -139,7 +138,7 @@ impl Metric {
min_x: Option<f64>,
max_x: Option<f64>,
chunk_size: Option<u32>,
) -> Values {
) -> Vec<Value> {
let mut values = self.data.read().expect("Values RwLock poisoned").clone();
if let Some(min_x) = min_x {
values.retain(|x| x.x > min_x);
@ -154,6 +153,6 @@ impl Metric {
values = iter.map(|x| avg_value(x)).collect();
}
}
Values::from_values(values)
values
}
}

View file

@ -23,7 +23,9 @@ pub fn metric_display_ui(ui: &mut Ui, metric: &Metric, _width: f32) {
ui.label(&metric.name);
ui.with_layout(Layout::top_down(Align::RIGHT), |ui| {
ui.horizontal(|ui| {
ui.label(format!("panel: {}", metric.panel_id));
if metric.panel_id >= 0 {
ui.label(format!("panel: {}", metric.panel_id));
}
if metric.query_y.len() > 0 {
ui.label(format!("y: {}", metric.query_y));
}

View file

@ -1,6 +1,6 @@
use chrono::{Local, Utc};
use eframe::{egui::{
plot::{Corner, GridMark, Legend, Line, Plot},
plot::{Corner, GridMark, Legend, Line, Plot, Values},
DragValue, Layout, Ui, Slider, TextEdit,
}, emath::Vec2};
@ -73,17 +73,17 @@ pub fn panel_body_ui(ui: &mut Ui, panel: &mut Panel, metrics: &Vec<Metric>) {
p = p.set_margin_fraction(Vec2 { x: 0.0, y: 0.1 });
}
if panel.view_scroll {
let _now = (Utc::now().timestamp() as f64) - (60.0 * panel.view_offset as f64);
p = p.include_x(_now);
if panel.limit {
p = p
.include_x(_now + (panel.view_size as f64 * 3.0))
.include_x(_now - (panel.view_size as f64 * 60.0)); // ??? TODO
}
}
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);
if panel.limit {
p = p
.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| {
@ -128,21 +128,34 @@ pub fn panel_body_ui(ui: &mut Ui, panel: &mut Panel, metrics: &Vec<Metric>) {
});
}
p.show(ui, |plot_ui| {
let _now = Utc::now().timestamp() as f64;
let _off = (panel.view_offset as f64) * 60.0; // TODO multiplying x60 makes sense only for timeseries
let _size = (panel.view_size as f64) * 60.0; // TODO multiplying x60 makes sense only for timeseries
let min_x = if panel.limit { Some(_now - _size - _off) } else { None };
let max_x = if panel.shift { Some(_now - _off) } else { None };
let chunk_size = if panel.reduce { Some(panel.view_chunks) } else { None };
for metric in metrics {
if metric.panel_id == panel.id {
// let chunks = None;
let line = Line::new(metric.values(min_x, max_x, chunk_size))
.name(metric.name.as_str())
.color(metric.color);
plot_ui.line(line);
let mut lines : Vec<Line> = Vec::new();
let _now = Utc::now().timestamp() as f64;
let _off = (panel.view_offset as f64) * 60.0; // TODO multiplying x60 makes sense only for timeseries
let _size = (panel.view_size as f64) * 60.0; // TODO multiplying x60 makes sense only for timeseries
let min_x = if panel.limit { Some(_now - _size - _off) } else { None };
let max_x = if panel.shift { Some(_now - _off) } else { None };
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);
if !panel.timeserie && panel.view_scroll && values.len() > 0 {
let l = values.len() - 1;
p = p.include_x(values[0].x)
.include_x(values[l].x)
.include_y(values[0].y)
.include_y(values[l].y);
}
lines.push(
Line::new(Values::from_values(values))
.name(metric.name.as_str())
.color(metric.color)
);
}
}
p.show(ui, |plot_ui| {
for line in lines {
plot_ui.line(line);
}
});
}

View file

@ -130,7 +130,7 @@ impl eframe::App for App {
// let mut to_delete: Option<usize> = None;
SidePanel::left("sources-bar")
.width_range(280.0..=800.0)
.default_width(350.0)
.default_width(330.0)
.show(ctx, |ui| {
let panels = self.data.panels.read().expect("Panels RwLock poisoned");
let panel_width = ui.available_width();
@ -208,7 +208,6 @@ impl eframe::App for App {
}
}
if self.edit {
ui.add_space(20.0);
ui.separator();
ui.horizontal(|ui| {
ui.heading("new source");
@ -232,6 +231,7 @@ impl eframe::App for App {
&panels,
panel_width,
);
ui.add_space(5.0);
if self.padding {
ui.add_space(300.0);
}