mirror of
https://github.com/zaphar/Heracles.git
synced 2025-07-23 20:49:50 -04:00
fix: spans should parse and get applied correctly
This commit is contained in:
parent
bec6f69645
commit
964a5e10e3
@ -6,15 +6,15 @@
|
|||||||
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'
|
||||||
query_type: Range
|
query_type: Range
|
||||||
span:
|
span:
|
||||||
end: 2024-02-10T00:00:00.00Z
|
end: now
|
||||||
duration: 1d
|
duration: 1d
|
||||||
step_duration: 1min
|
step_duration: 1h
|
||||||
name_label: instance
|
name_label: instance
|
||||||
- title: Test Dasbboard 2
|
- title: Test Dasbboard 2
|
||||||
span:
|
span:
|
||||||
end: 2024-02-10T00:00:00.00Z
|
end: 2024-02-10T00:00:00.00Z
|
||||||
duration: 2d
|
duration: 2 days
|
||||||
step_duration: 1min
|
step_duration: 1 minute
|
||||||
graphs:
|
graphs:
|
||||||
- title: Node cpu
|
- title: Node cpu
|
||||||
source: http://heimdall:9001
|
source: http://heimdall:9001
|
||||||
|
@ -21,7 +21,7 @@ use tracing::{debug, error};
|
|||||||
|
|
||||||
use crate::query::{QueryConn, QueryType};
|
use crate::query::{QueryConn, QueryType};
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize, Debug)]
|
||||||
pub struct GraphSpan {
|
pub struct GraphSpan {
|
||||||
pub end: String,
|
pub end: String,
|
||||||
pub duration: String,
|
pub duration: String,
|
||||||
@ -46,8 +46,8 @@ pub struct Graph {
|
|||||||
pub query_type: QueryType,
|
pub query_type: QueryType,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn duration_from_string(duration: &str) -> Option<Duration> {
|
fn duration_from_string(duration_string: &str) -> Option<Duration> {
|
||||||
match parse_duration::parse(duration) {
|
match parse_duration::parse(duration_string) {
|
||||||
Ok(d) => match Duration::from_std(d) {
|
Ok(d) => match Duration::from_std(d) {
|
||||||
Ok(d) => Some(d),
|
Ok(d) => Some(d),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
@ -96,14 +96,18 @@ fn graph_span_to_tuple(span: &Option<GraphSpan>) -> Option<(DateTime<Utc>, Durat
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Graph {
|
impl Graph {
|
||||||
pub fn get_query_connection<'conn, 'graph: 'conn>(&'graph self, graph_span: &'graph Option<GraphSpan>) -> QueryConn<'conn> {
|
pub fn get_query_connection<'conn, 'graph: 'conn>(&'graph self, graph_span: &'graph Option<GraphSpan>, query_span: &'graph Option<GraphSpan>) -> QueryConn<'conn> {
|
||||||
debug!(
|
debug!(
|
||||||
query = self.query,
|
query = self.query,
|
||||||
source = self.source,
|
source = self.source,
|
||||||
"Getting query connection for graph"
|
"Getting query connection for graph"
|
||||||
);
|
);
|
||||||
let mut conn = QueryConn::new(&self.source, &self.query, self.query_type.clone());
|
let mut conn = QueryConn::new(&self.source, &self.query, self.query_type.clone());
|
||||||
if let Some((end, duration, step_duration)) = graph_span_to_tuple(&self.span) {
|
// Query params take precendence over all other settings. Then graph settings take
|
||||||
|
// precedences and finally the dashboard settings take precendence
|
||||||
|
if let Some((end, duration, step_duration)) = graph_span_to_tuple(query_span) {
|
||||||
|
conn = conn.with_span(end, duration, step_duration);
|
||||||
|
} else if let Some((end, duration, step_duration)) = graph_span_to_tuple(&self.span) {
|
||||||
conn = conn.with_span(end, duration, step_duration);
|
conn = conn.with_span(end, duration, step_duration);
|
||||||
} else if let Some((end, duration, step_duration)) = graph_span_to_tuple(graph_span) {
|
} else if let Some((end, duration, step_duration)) = graph_span_to_tuple(graph_span) {
|
||||||
conn = conn.with_span(end, duration, step_duration);
|
conn = conn.with_span(end, duration, step_duration);
|
||||||
|
25
src/query.rs
25
src/query.rs
@ -59,29 +59,36 @@ impl<'conn> QueryConn<'conn> {
|
|||||||
debug!("Getting results for query");
|
debug!("Getting results for query");
|
||||||
let client = Client::try_from(self.source)?;
|
let client = Client::try_from(self.source)?;
|
||||||
let (start, end, step_resolution) = if let Some(TimeSpan {
|
let (start, end, step_resolution) = if let Some(TimeSpan {
|
||||||
end: e,
|
end,
|
||||||
duration: du,
|
duration: du,
|
||||||
step_seconds,
|
step_seconds,
|
||||||
}) = self.span
|
}) = self.span
|
||||||
{
|
{
|
||||||
((e -du).timestamp(), e.timestamp(), step_seconds as f64)
|
let start = end - du;
|
||||||
|
debug!(?start, ?end, step_seconds, "Running Query with range values");
|
||||||
|
(start.timestamp(), end.timestamp(), step_seconds as f64)
|
||||||
} else {
|
} else {
|
||||||
let end = Utc::now().timestamp();
|
let end = Utc::now();
|
||||||
let start = end - (60 * 10);
|
let start = end - chrono::Duration::minutes(10);
|
||||||
(start, end, 30 as f64)
|
debug!(?start, ?end, step_seconds=30, "Running Query with range values");
|
||||||
|
(start.timestamp(), end.timestamp(), 30 as f64)
|
||||||
};
|
};
|
||||||
debug!(start, end, step_resolution, "Running Query with range values");
|
//debug!(start, end, step_resolution, "Running Query with range values");
|
||||||
match self.query_type {
|
match self.query_type {
|
||||||
QueryType::Range => Ok(client
|
QueryType::Range => {
|
||||||
|
let results = client
|
||||||
.query_range(self.query, start, end, step_resolution)
|
.query_range(self.query, start, end, step_resolution)
|
||||||
.get()
|
.get()
|
||||||
.await?),
|
.await?;
|
||||||
|
//debug!(?results, "range results");
|
||||||
|
Ok(results)
|
||||||
|
},
|
||||||
QueryType::Scalar => Ok(client.query(self.query).get().await?),
|
QueryType::Scalar => Ok(client.query(self.query).get().await?),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct DataPoint {
|
pub struct DataPoint {
|
||||||
timestamp: f64,
|
timestamp: f64,
|
||||||
value: f64,
|
value: f64,
|
||||||
|
@ -22,7 +22,7 @@ use axum::{
|
|||||||
|
|
||||||
// https://maud.lambda.xyz/getting-started.html
|
// https://maud.lambda.xyz/getting-started.html
|
||||||
use maud::{html, Markup};
|
use maud::{html, Markup};
|
||||||
use tracing::{debug, error};
|
use tracing::debug;
|
||||||
|
|
||||||
use crate::dashboard::{Dashboard, Graph, GraphSpan};
|
use crate::dashboard::{Dashboard, Graph, GraphSpan};
|
||||||
use crate::query::{to_samples, QueryResult};
|
use crate::query::{to_samples, QueryResult};
|
||||||
@ -57,11 +57,7 @@ pub async fn graph_query(
|
|||||||
};
|
};
|
||||||
let data = to_samples(
|
let data = to_samples(
|
||||||
graph
|
graph
|
||||||
.get_query_connection(if query_span.is_some() {
|
.get_query_connection(&dash.span, &query_span)
|
||||||
&query_span
|
|
||||||
} else {
|
|
||||||
&dash.span
|
|
||||||
})
|
|
||||||
.get_results()
|
.get_results()
|
||||||
.await
|
.await
|
||||||
.expect("Unable to get query results")
|
.expect("Unable to get query results")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user