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-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
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct Dashboard {
|
2024-02-03 19:06:38 -06:00
|
|
|
pub title: String,
|
|
|
|
pub graphs: Vec<Graph>,
|
2024-02-03 16:31:41 -06:00
|
|
|
}
|
|
|
|
|
2024-02-13 14:22:26 -06:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct GraphSpan {
|
|
|
|
pub start: DateTime<Utc>,
|
|
|
|
pub duration: String,
|
|
|
|
pub step_duration: String,
|
|
|
|
}
|
|
|
|
|
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 14:22:26 -06:00
|
|
|
fn duration_from_string(duration: &str) -> Option<chrono::Duration> {
|
|
|
|
match parse_duration::parse(duration) {
|
|
|
|
Ok(d) => match chrono::Duration::from_std(d) {
|
|
|
|
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-04 16:39:25 -06:00
|
|
|
impl Graph {
|
|
|
|
pub fn get_query_connection<'conn, 'graph: 'conn>(&'graph self) -> 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());
|
|
|
|
if let Some(span) = &self.span {
|
|
|
|
let duration = match duration_from_string(&span.duration) {
|
|
|
|
Some(d) => d,
|
|
|
|
None => {
|
|
|
|
error!("Invalid query duration not assigning span to to graph query");
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
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 conn;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
conn = conn.with_span(span.start.clone(), duration, step_duration);
|
|
|
|
}
|
|
|
|
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)?)
|
|
|
|
}
|