feat: Query string support for the timespan

This commit is contained in:
Jeremy Wall 2024-02-13 17:09:06 -06:00
parent 710f73332d
commit a36a59b900
2 changed files with 28 additions and 10 deletions

View File

@ -9,7 +9,7 @@ license = "Apache-2.0"
[dependencies]
anyhow = "1.0.79"
async-io = "2.3.1"
axum = { version = "0.7.4", features = [ "http2" ] }
axum = { version = "0.7.4", features = ["http2", "query"] }
axum-macros = "0.4.1"
chrono = { version = "0.4.33", features = ["alloc", "std", "now", "serde"] }
clap = { version = "4.4.18", features = ["derive"] }

View File

@ -11,10 +11,10 @@
// 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::sync::Arc;
use std::{sync::Arc, collections::HashMap};
use axum::{
extract::{Path, State},
extract::{Path, Query, State},
response::Response,
routing::get,
Json, Router,
@ -22,9 +22,10 @@ use axum::{
// https://maud.lambda.xyz/getting-started.html
use maud::{html, Markup};
use tracing::debug;
use tracing::{debug, error};
use chrono::prelude::*;
use crate::dashboard::{Dashboard, Graph};
use crate::dashboard::{Dashboard, Graph, GraphSpan};
use crate::query::{to_samples, QueryResult};
type Config = State<Arc<Vec<Dashboard>>>;
@ -32,17 +33,34 @@ type Config = State<Arc<Vec<Dashboard>>>;
pub async fn graph_query(
State(config): Config,
Path((dash_idx, graph_idx)): Path<(usize, usize)>,
Query(query): Query<HashMap<String, String>>,
) -> Json<QueryResult> {
debug!("Getting data for query");
let dash = config
.get(dash_idx)
.expect("No such dashboard index");
let graph = dash.graphs
let dash = config.get(dash_idx).expect("No such dashboard index");
let graph = dash
.graphs
.get(graph_idx)
.expect(&format!("No such graph in dasboard {}", dash_idx));
let query_span = {
if query.contains_key("start") && query.contains_key("duration") && query.contains_key("step_duration")
{
if let Ok(start) = DateTime::parse_from_rfc3339(&query["start"]) {
Some(GraphSpan {
start: start.to_utc(),
duration: query["duration"].clone(),
step_duration: query["step_duration"].clone(),
})
} else {
error!(?query, "Invalid date time in start for query string");
None
}
} else {
None
}
};
let data = to_samples(
graph
.get_query_connection(&dash.span)
.get_query_connection(if query_span.is_some() { &query_span } else { &dash.span })
.get_results()
.await
.expect("Unable to get query results")