From c3a1c4877a3466e9ad252419248dffcfca163128 Mon Sep 17 00:00:00 2001 From: Alexey Velikiy Date: Thu, 21 Oct 2021 02:52:53 +0300 Subject: [PATCH] query url params --- src/grafana_service.rs | 11 +++++------ src/grafana_service/prometheus.rs | 9 +++------ src/main.rs | 3 ++- src/metric.rs | 2 +- 4 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/grafana_service.rs b/src/grafana_service.rs index 81fbda2..b42e764 100644 --- a/src/grafana_service.rs +++ b/src/grafana_service.rs @@ -34,15 +34,14 @@ impl GrafanaService { Ok(()) } - pub async fn extract_metrics(&self, panel_url: &str) -> types::Result { + pub async fn extract_metrics(&self, datasource_url: &str, query: &str, from: u64, to: u64, step: u64) -> types::Result { let pm = prometheus::Prometheus::new( self, - "/api/datasources/proxy/1/api/v1/query_range", - "rate(go_memstats_alloc_bytes_total[5m])", - 15, + datasource_url, + query ); - let r = pm.query(1634672070, 1634672970).await?; - + // TODO: split big query to chunks + let r = pm.query(from, to, step).await?; Ok(r) } diff --git a/src/grafana_service/prometheus.rs b/src/grafana_service/prometheus.rs index 06d4b82..529ca82 100644 --- a/src/grafana_service/prometheus.rs +++ b/src/grafana_service/prometheus.rs @@ -15,16 +15,15 @@ use super::GrafanaService; pub struct Prometheus<'a> { url: String, query: String, - step: u32, grafana_service: &'a GrafanaService, } #[derive(Deserialize, Serialize)] struct Query { query: String, - step: u32, start: u64, end: u64, + step: u64 } impl<'a> Prometheus<'a> { @@ -32,13 +31,11 @@ impl<'a> Prometheus<'a> { grafana_service: &'a GrafanaService, url: &str, query: &str, - step: u32, ) -> Prometheus<'a> { Prometheus { url: url.to_owned(), grafana_service, query: query.to_string(), - step, } } } @@ -78,10 +75,10 @@ fn parse_result(value: Value) -> types::Result { #[async_trait] impl Metric for Prometheus<'_> { - async fn query(&self, from: u64, to: u64) -> types::Result { + async fn query(&self, from: u64, to: u64, step: u64) -> types::Result { let q = Query { query: self.query.to_owned(), - step: self.step, + step: step, start: from, end: to, }; diff --git a/src/main.rs b/src/main.rs index 8f1d86d..7024baa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,7 +29,8 @@ async fn main() -> types::Result<()> { // gs.test_connection().await?; // gs.get_datasources().await?; - let r = gs.extract_metrics("http://localhost:3000/d/YeBxHjzWz/starter-app-stats?editPanel=2&orgId=1") + // "http://localhost:3000/d/YeBxHjzWz/starter-app-stats?editPanel=2&orgId=1" + let r = gs.extract_metrics("/api/datasources/proxy/1/api/v1/query_range", "rate(go_memstats_alloc_bytes_total[5m])", 1634672070, 1634672970, 15) .await?; let key = r.keys().nth(0).unwrap(); diff --git a/src/metric.rs b/src/metric.rs index e4c6816..dadf930 100644 --- a/src/metric.rs +++ b/src/metric.rs @@ -30,5 +30,5 @@ pub type MetricResult = HashMap>; #[async_trait] pub trait Metric { - async fn query(&self, from: u64, to: u64) -> types::Result; + async fn query(&self, from: u64, to: u64, step: u64) -> types::Result; }