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] [dependencies]
anyhow = "1.0.79" anyhow = "1.0.79"
async-io = "2.3.1" 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" axum-macros = "0.4.1"
chrono = { version = "0.4.33", features = ["alloc", "std", "now", "serde"] } chrono = { version = "0.4.33", features = ["alloc", "std", "now", "serde"] }
clap = { version = "4.4.18", features = ["derive"] } clap = { version = "4.4.18", features = ["derive"] }

View File

@ -11,10 +11,10 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use std::sync::Arc; use std::{sync::Arc, collections::HashMap};
use axum::{ use axum::{
extract::{Path, State}, extract::{Path, Query, State},
response::Response, response::Response,
routing::get, routing::get,
Json, Router, Json, Router,
@ -22,9 +22,10 @@ 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; use tracing::{debug, error};
use chrono::prelude::*;
use crate::dashboard::{Dashboard, Graph}; use crate::dashboard::{Dashboard, Graph, GraphSpan};
use crate::query::{to_samples, QueryResult}; use crate::query::{to_samples, QueryResult};
type Config = State<Arc<Vec<Dashboard>>>; type Config = State<Arc<Vec<Dashboard>>>;
@ -32,17 +33,34 @@ type Config = State<Arc<Vec<Dashboard>>>;
pub async fn graph_query( pub async fn graph_query(
State(config): Config, State(config): Config,
Path((dash_idx, graph_idx)): Path<(usize, usize)>, Path((dash_idx, graph_idx)): Path<(usize, usize)>,
Query(query): Query<HashMap<String, String>>,
) -> Json<QueryResult> { ) -> Json<QueryResult> {
debug!("Getting data for query"); debug!("Getting data for query");
let dash = config let dash = config.get(dash_idx).expect("No such dashboard index");
.get(dash_idx) let graph = dash
.expect("No such dashboard index"); .graphs
let graph = dash.graphs
.get(graph_idx) .get(graph_idx)
.expect(&format!("No such graph in dasboard {}", dash_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( let data = to_samples(
graph graph
.get_query_connection(&dash.span) .get_query_connection(if query_span.is_some() { &query_span } else { &dash.span })
.get_results() .get_results()
.await .await
.expect("Unable to get query results") .expect("Unable to get query results")