mirror of
https://github.com/zaphar/Heracles.git
synced 2025-07-23 04:29:48 -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_type: Range
|
||||
span:
|
||||
end: 2024-02-10T00:00:00.00Z
|
||||
end: now
|
||||
duration: 1d
|
||||
step_duration: 1min
|
||||
step_duration: 1h
|
||||
name_label: instance
|
||||
- title: Test Dasbboard 2
|
||||
span:
|
||||
end: 2024-02-10T00:00:00.00Z
|
||||
duration: 2d
|
||||
step_duration: 1min
|
||||
duration: 2 days
|
||||
step_duration: 1 minute
|
||||
graphs:
|
||||
- title: Node cpu
|
||||
source: http://heimdall:9001
|
||||
|
@ -21,7 +21,7 @@ use tracing::{debug, error};
|
||||
|
||||
use crate::query::{QueryConn, QueryType};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct GraphSpan {
|
||||
pub end: String,
|
||||
pub duration: String,
|
||||
@ -46,8 +46,8 @@ pub struct Graph {
|
||||
pub query_type: QueryType,
|
||||
}
|
||||
|
||||
fn duration_from_string(duration: &str) -> Option<Duration> {
|
||||
match parse_duration::parse(duration) {
|
||||
fn duration_from_string(duration_string: &str) -> Option<Duration> {
|
||||
match parse_duration::parse(duration_string) {
|
||||
Ok(d) => match Duration::from_std(d) {
|
||||
Ok(d) => Some(d),
|
||||
Err(e) => {
|
||||
@ -96,14 +96,18 @@ fn graph_span_to_tuple(span: &Option<GraphSpan>) -> Option<(DateTime<Utc>, Durat
|
||||
}
|
||||
|
||||
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!(
|
||||
query = self.query,
|
||||
source = self.source,
|
||||
"Getting query connection for graph"
|
||||
);
|
||||
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);
|
||||
} else if let Some((end, duration, step_duration)) = graph_span_to_tuple(graph_span) {
|
||||
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");
|
||||
let client = Client::try_from(self.source)?;
|
||||
let (start, end, step_resolution) = if let Some(TimeSpan {
|
||||
end: e,
|
||||
end,
|
||||
duration: du,
|
||||
step_seconds,
|
||||
}) = 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 {
|
||||
let end = Utc::now().timestamp();
|
||||
let start = end - (60 * 10);
|
||||
(start, end, 30 as f64)
|
||||
let end = Utc::now();
|
||||
let start = end - chrono::Duration::minutes(10);
|
||||
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 {
|
||||
QueryType::Range => Ok(client
|
||||
QueryType::Range => {
|
||||
let results = client
|
||||
.query_range(self.query, start, end, step_resolution)
|
||||
.get()
|
||||
.await?),
|
||||
.await?;
|
||||
//debug!(?results, "range results");
|
||||
Ok(results)
|
||||
},
|
||||
QueryType::Scalar => Ok(client.query(self.query).get().await?),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct DataPoint {
|
||||
timestamp: f64,
|
||||
value: f64,
|
||||
|
@ -22,7 +22,7 @@ use axum::{
|
||||
|
||||
// https://maud.lambda.xyz/getting-started.html
|
||||
use maud::{html, Markup};
|
||||
use tracing::{debug, error};
|
||||
use tracing::debug;
|
||||
|
||||
use crate::dashboard::{Dashboard, Graph, GraphSpan};
|
||||
use crate::query::{to_samples, QueryResult};
|
||||
@ -57,11 +57,7 @@ pub async fn graph_query(
|
||||
};
|
||||
let data = to_samples(
|
||||
graph
|
||||
.get_query_connection(if query_span.is_some() {
|
||||
&query_span
|
||||
} else {
|
||||
&dash.span
|
||||
})
|
||||
.get_query_connection(&dash.span, &query_span)
|
||||
.get_results()
|
||||
.await
|
||||
.expect("Unable to get query results")
|
||||
|
Loading…
x
Reference in New Issue
Block a user