Add rendering hint to snap y axis

This commit is contained in:
Olivier 'reivilibre' 2022-01-08 16:52:34 +00:00
parent 9cc141e227
commit badfae12e7
3 changed files with 51 additions and 17 deletions

View File

@ -21,6 +21,8 @@ pub struct GraphConfig {
pub kind: MetricKind,
#[serde(default)]
pub transform: Option<MetricTransform>,
#[serde(default)]
pub rendering: GraphRenderingHints,
}
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
@ -29,3 +31,9 @@ pub struct GraphRequest {
pub metric_labels: BTreeMap<String, String>,
pub derivation: Option<MetricTransform>,
}
#[derive(Clone, Debug, Default, Deserialize)]
pub struct GraphRenderingHints {
/// The view will be forced to include this range.
pub include_y_range: Option<(f64, f64)>,
}

View File

@ -1,5 +1,5 @@
use crate::background_loader::MetricsLogReadingRequester;
use crate::config::GraphRequest;
use crate::config::{GraphRenderingHints, GraphRequest};
use bare_metrics_core::structures::UnixTimestampMilliseconds;
use eframe::egui::{
Color32, Frame as EguiFrame, PointerButton, Pos2, Rect, Sense, Stroke, TextStyle, Ui, Vec2,
@ -64,7 +64,12 @@ impl Mul<(f64, f64)> for GraphTransform {
}
impl Graph {
pub fn draw(ui: &mut Ui, graph_request: &GraphRequest, reader: &MetricsLogReadingRequester) {
pub fn draw(
ui: &mut Ui,
graph_request: &GraphRequest,
render_hint: &GraphRenderingHints,
reader: &MetricsLogReadingRequester,
) {
let context_menu_id = ui.id().with("context menu");
EguiFrame::dark_canvas(ui.style()).show(ui, |ui| {
@ -101,13 +106,25 @@ impl Graph {
// This range is reversed because screen coordinates go down, but we'd like them to go
// up since this is more of a mathematical graph.
let y_axis = if let Some(histogram) = current_window.histograms.get(graph_request) {
let mut y_axis = if let Some(histogram) = current_window.histograms.get(graph_request) {
histogram.y_axis.clone().into_inner()
} else if let Some(scalar) = current_window.scalars.get(graph_request) {
scalar.y_axis.clone().into_inner()
} else {
(0.0, 10.0)
};
if let Some((a, b)) = render_hint.include_y_range {
// Force the y-axis to contain a range. Useful if you like 0 to be anchored on a
// rate graph or something like that.
if y_axis.0 > a {
y_axis.0 = a;
}
if y_axis.1 < b {
y_axis.1 = b;
}
}
let y_axis = (y_axis.1)..=(y_axis.0);
// let display_transform =

View File

@ -1,7 +1,7 @@
use crate::background_loader::{
MetricsLogReaderMessage, MetricsLogReaderNotification, MetricsLogReadingRequester,
};
use crate::config::{DashboardConfig, GraphRequest};
use crate::config::{DashboardConfig, GraphRenderingHints, GraphRequest};
use crate::graph::Graph;
use bare_metrics_reader::MetricsLogReader;
@ -23,7 +23,8 @@ pub struct MetricsGui {
requester: MetricsLogReadingRequester,
dashboard: Option<DashboardConfig>,
graph_requests: Vec<GraphRequest>,
// TODO we may want multiple graphs on the same pane, somehow.
graph_requests: Vec<(GraphRequest, GraphRenderingHints)>,
}
impl App for MetricsGui {
@ -39,13 +40,13 @@ impl App for MetricsGui {
egui::ScrollArea::new([false, true]).show(ui, |ui| {
if let Some(_dashboard) = dashboard {
let window = requester.shared.current_window.read().unwrap();
for graph in graph_requests.iter() {
ui.label(&graph.metric_name);
for (request, render_hint) in graph_requests.iter() {
ui.label(&request.metric_name);
if window.scalars.contains_key(graph)
|| window.histograms.contains_key(graph)
if window.scalars.contains_key(request)
|| window.histograms.contains_key(request)
{
Graph::draw(ui, graph, requester);
Graph::draw(ui, request, render_hint, requester);
} else {
// TODO clarify
ui.label("(loading or missing ...)");
@ -104,12 +105,15 @@ impl MetricsGui {
if let Some(dashboard) = self.dashboard.as_ref() {
for graph in dashboard.graphs.iter() {
// TODO frankly we should maybe be using metric IDs in the graph requests
self.graph_requests.push(GraphRequest {
metric_name: graph.name.clone(),
// TODO we need something better about the metric labels
metric_labels: BTreeMap::new(),
derivation: graph.transform.clone(),
});
self.graph_requests.push((
GraphRequest {
metric_name: graph.name.clone(),
// TODO we need something better about the metric labels
metric_labels: BTreeMap::new(),
derivation: graph.transform.clone(),
},
graph.rendering.clone(),
));
}
} else {
// TODO Build some stuff automatically based on alphabetical ordering
@ -120,7 +124,12 @@ impl MetricsGui {
.send(MetricsLogReaderMessage::LoadNewWindow {
new_time_range: f64::NEG_INFINITY..=f64::INFINITY,
new_wanted_time_points: 512,
requests: self.graph_requests.clone(),
requests: self
.graph_requests
.iter()
.map(|(a, _)| a)
.cloned()
.collect(),
})
.unwrap();
}