2024-02-12 15:50:35 -06:00
|
|
|
// Copyright 2023 Jeremy Wall
|
2024-02-03 16:31:41 -06:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
use std::path::Path;
|
|
|
|
|
2024-02-13 14:22:26 -06:00
|
|
|
use chrono::prelude::*;
|
2024-02-13 16:15:19 -06:00
|
|
|
use chrono::Duration;
|
2024-02-03 16:31:41 -06:00
|
|
|
use serde::Deserialize;
|
|
|
|
use serde_yaml;
|
2024-02-13 14:22:26 -06:00
|
|
|
use tracing::{debug, error};
|
2024-02-04 16:39:25 -06:00
|
|
|
|
2024-02-11 19:08:15 -06:00
|
|
|
use crate::query::{QueryConn, QueryType};
|
2024-02-03 16:31:41 -06:00
|
|
|
|
2024-02-13 14:22:26 -06:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct GraphSpan {
|
2024-02-14 19:45:28 -06:00
|
|
|
pub end: String,
|
2024-02-13 14:22:26 -06:00
|
|
|
pub duration: String,
|
|
|
|
pub step_duration: String,
|
|
|
|
}
|
|
|
|
|
2024-02-13 16:15:19 -06:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct Dashboard {
|
|
|
|
pub title: String,
|
|
|
|
pub graphs: Vec<Graph>,
|
|
|
|
pub span: Option<GraphSpan>
|
|
|
|
}
|
|
|
|
|
2024-02-03 16:31:41 -06:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct Graph {
|
2024-02-03 19:06:38 -06:00
|
|
|
pub title: String,
|
2024-02-04 16:39:25 -06:00
|
|
|
pub source: String,
|
2024-02-03 19:06:38 -06:00
|
|
|
pub query: String,
|
2024-02-13 14:22:26 -06:00
|
|
|
// serialized with https://datatracker.ietf.org/doc/html/rfc3339
|
|
|
|
pub span: Option<GraphSpan>,
|
2024-02-10 14:19:19 -06:00
|
|
|
pub name_label: String,
|
2024-02-11 19:08:15 -06:00
|
|
|
pub query_type: QueryType,
|
2024-02-03 16:31:41 -06:00
|
|
|
}
|
|
|
|
|
2024-02-13 16:15:19 -06:00
|
|
|
fn duration_from_string(duration: &str) -> Option<Duration> {
|
2024-02-13 14:22:26 -06:00
|
|
|
match parse_duration::parse(duration) {
|
2024-02-13 16:15:19 -06:00
|
|
|
Ok(d) => match Duration::from_std(d) {
|
2024-02-13 14:22:26 -06:00
|
|
|
Ok(d) => Some(d),
|
|
|
|
Err(e) => {
|
|
|
|
error!(err = ?e, "specified Duration is out of bounds");
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
error!(
|
|
|
|
err = ?e,
|
|
|
|
"Failed to parse duration"
|
|
|
|
);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-13 16:15:19 -06:00
|
|
|
fn graph_span_to_tuple(span: &Option<GraphSpan>) -> Option<(DateTime<Utc>, Duration, Duration)> {
|
|
|
|
if span.is_none() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let span = span.as_ref().unwrap();
|
|
|
|
let duration = match duration_from_string(&span.duration) {
|
|
|
|
Some(d) => d,
|
|
|
|
None => {
|
|
|
|
error!("Invalid query duration not assigning span to to graph query");
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let step_duration = match duration_from_string(&span.step_duration) {
|
|
|
|
Some(d) => d,
|
|
|
|
None => {
|
|
|
|
error!("Invalid query step resolution not assigning span to to graph query");
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
2024-02-14 19:45:28 -06:00
|
|
|
let end = if span.end == "now" {
|
|
|
|
Utc::now()
|
|
|
|
} else if let Ok(end) = DateTime::parse_from_rfc3339(&span.end) {
|
|
|
|
end.to_utc()
|
|
|
|
} else {
|
|
|
|
error!(?span.end, "Invalid DateTime using current time.");
|
|
|
|
Utc::now()
|
|
|
|
};
|
|
|
|
Some((end, duration, step_duration))
|
2024-02-13 16:15:19 -06:00
|
|
|
}
|
|
|
|
|
2024-02-04 16:39:25 -06:00
|
|
|
impl Graph {
|
2024-02-13 16:15:19 -06:00
|
|
|
pub fn get_query_connection<'conn, 'graph: 'conn>(&'graph self, graph_span: &'graph Option<GraphSpan>) -> QueryConn<'conn> {
|
2024-02-13 14:22:26 -06:00
|
|
|
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());
|
2024-02-14 19:45:28 -06:00
|
|
|
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);
|
2024-02-13 14:22:26 -06:00
|
|
|
}
|
|
|
|
conn
|
2024-02-04 16:39:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-03 16:31:41 -06:00
|
|
|
pub fn read_dashboard_list(path: &Path) -> anyhow::Result<Vec<Dashboard>> {
|
|
|
|
let f = std::fs::File::open(path)?;
|
|
|
|
Ok(serde_yaml::from_reader(f)?)
|
|
|
|
}
|