feat: specify the trace label in graph config.

This commit is contained in:
Jeremy Wall 2024-02-10 14:19:19 -06:00
parent 576043e78d
commit dfa8196af4
4 changed files with 28 additions and 16 deletions

View File

@ -4,14 +4,14 @@
- title: Node cpu - title: Node cpu
source: http://heimdall:9001 source: http://heimdall:9001
query: 'sum by (instance)(irate(node_cpu_seconds_total{mode="system",job="nodestats"}[5m])) * 100' query: 'sum by (instance)(irate(node_cpu_seconds_total{mode="system",job="nodestats"}[5m])) * 100'
- title: Node memory name_label: instance
source: http://heimdall:9001
query: 'node_memory_MemFree_bytes{job="nodestats"}'
- title: Test Dasbboard 2 - title: Test Dasbboard 2
graphs: graphs:
- title: Node cpu - title: Node cpu
source: http://heimdall:9001 source: http://heimdall:9001
query: 'sum by (instance)(irate(node_cpu_seconds_total{mode="system",job="nodestats"}[5m])) * 100' query: 'sum by (instance)(irate(node_cpu_seconds_total{mode="system",job="nodestats"}[5m])) * 100'
name_label: instance
- title: Node memory - title: Node memory
source: http://heimdall:9001 source: http://heimdall:9001
query: 'node_memory_MemFree_bytes{,job="nodestats"}' query: 'node_memory_MemFree_bytes{job="nodestats"}'
name_label: instance

View File

@ -30,6 +30,7 @@ pub struct Graph {
pub title: String, pub title: String,
pub source: String, pub source: String,
pub query: String, pub query: String,
pub name_label: String,
} }
impl Graph { impl Graph {

View File

@ -60,14 +60,13 @@ pub fn mk_api_routes(config: Arc<Vec<Dashboard>>) -> Router<Config> {
) )
} }
// TODO(jwall): This should probably be encapsulated in a web component?
pub fn graph_component(dash_idx: usize, graph_idx: usize, graph: &Graph) -> Markup { pub fn graph_component(dash_idx: usize, graph_idx: usize, graph: &Graph) -> Markup {
let graph_id = format!("graph-{}-{}", dash_idx, graph_idx); let graph_id = format!("graph-{}-{}", dash_idx, graph_idx);
let graph_data_uri = format!("/api/dash/{}/graph/{}", dash_idx, graph_idx); let graph_data_uri = format!("/api/dash/{}/graph/{}", dash_idx, graph_idx);
html!( html!(
div { div {
h2 { (graph.title) } h2 { (graph.title) }
timeseries-graph uri=(graph_data_uri) id=(graph_id) { } timeseries-graph uri=(graph_data_uri) id=(graph_id) label=(graph.name_label) { }
} }
) )
} }
@ -125,6 +124,7 @@ pub async fn index(State(config): State<Config>) -> Markup {
script src="/js/lib.js" { } script src="/js/lib.js" { }
template id="timeseries_template" { template id="timeseries_template" {
div; div;
div;
} }
(app(State(config.clone())).await) (app(State(config.clone())).await)
} }

View File

@ -4,13 +4,14 @@ class TimeseriesGraph extends HTMLElement {
#height; #height;
#intervalId; #intervalId;
#pollSeconds; #pollSeconds;
#label;
constructor() { constructor() {
super(); super();
const root = this.attachShadow({ mode: "open" }); const root = this.attachShadow({ mode: "open" });
var template = document.getElementById("timeseries_template"); var template = document.getElementById("timeseries_template");
this.#width = 1000; this.#width = 800;
this.#height = 500; this.#height = 600;
this.#pollSeconds = 5; this.#pollSeconds = 30;
root.appendChild(template.content.cloneNode(true)); root.appendChild(template.content.cloneNode(true));
} }
@ -30,19 +31,22 @@ class TimeseriesGraph extends HTMLElement {
case 'poll-seconds': case 'poll-seconds':
this.#pollSeconds = newValue; this.#pollSeconds = newValue;
break; break;
case 'label':
this.#label = newValue;
break;
default: // do nothing; default: // do nothing;
break; break;
} }
this.updateGraph(); this.resetInterval();
// TODO(zaphar): reset update timer as well
} }
connectedCallback() { connectedCallback() {
// TODO(zaphar): Set up the timer loop to update graph data. // TODO(zaphar): Set up the timer loop to update graph data.
this.#uri = this.getAttribute('uri'); this.#uri = this.getAttribute('uri');
if (this.#uri) { this.#width = this.getAttribute('width');
this.updateGraph(); this.#height = this.getAttribute('height');
} this.#pollSeconds = this.getAttribute('poll-seconds');
this.#label = this.getAttribute('label');
this.resetInterval() this.resetInterval()
} }
@ -67,6 +71,9 @@ class TimeseriesGraph extends HTMLElement {
} }
resetInterval() { resetInterval() {
if (this.#uri) {
this.updateGraph();
}
this.stopInterval() this.stopInterval()
this.#intervalId = setInterval(() => this.updateGraph(), 1000 * this.#pollSeconds); this.#intervalId = setInterval(() => this.updateGraph(), 1000 * this.#pollSeconds);
} }
@ -88,14 +95,18 @@ class TimeseriesGraph extends HTMLElement {
if (data.Series) { if (data.Series) {
var traces = []; var traces = [];
for (const pair of data.Series) { for (const pair of data.Series) {
const series = pair[1];
const labels = pair[0];
var trace = { var trace = {
type: "scatter", type: "scatter",
mode: "lines", mode: "lines",
x: [], x: [],
y: [] y: []
}; };
//const labels = pair[0]; console.log("labels: ", labels, this.#label);
const series = pair[1]; if (labels[this.#label]) {
trace.name = labels[this.#label];
};
for (const point of series) { for (const point of series) {
trace.x.push(point.timestamp); trace.x.push(point.timestamp);
trace.y.push(point.value); trace.y.push(point.value);