From 974f252c507848c5509ba22c24380a9ee1c864df Mon Sep 17 00:00:00 2001 From: Alexey Velikiy Date: Thu, 21 Oct 2021 03:02:20 +0300 Subject: [PATCH] args --- README.md | 7 ++++++ src/grafana_service.rs | 16 +++++++------ src/grafana_service/prometheus.rs | 8 ++----- src/main.rs | 38 ++++++++++++++++++++++++++++++- 4 files changed, 55 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index e601662..e1fccb7 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,9 @@ # subbeat subbeat + +## Example + +``` +subbeat http://localhost:3000 eyJrIjoiWnRRMTNmcGpvTHNPb3UzNzdUNUphRm53Rk9tMTNzOTQiLCJuIjoic3ViYmVhdC10ZXN0IiwiaWQiOjF9 "/api/datasources/proxy/1/api/v1/query_range" "rate(go_memstats_alloc_bytes_total[5m])" 1634672070 1634672970 + +``` \ No newline at end of file diff --git a/src/grafana_service.rs b/src/grafana_service.rs index b42e764..b9c38f3 100644 --- a/src/grafana_service.rs +++ b/src/grafana_service.rs @@ -1,7 +1,6 @@ use crate::{metric::MetricResult, types}; use hyper::{Body, Client, Method, Request, StatusCode}; -use tokio::io::{stdout, AsyncWriteExt as _}; use bytes::Buf as _; @@ -34,12 +33,15 @@ impl GrafanaService { Ok(()) } - 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, - datasource_url, - query - ); + 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, datasource_url, query); // 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 529ca82..160933e 100644 --- a/src/grafana_service/prometheus.rs +++ b/src/grafana_service/prometheus.rs @@ -23,15 +23,11 @@ struct Query { query: String, start: u64, end: u64, - step: u64 + step: u64, } impl<'a> Prometheus<'a> { - pub fn new( - grafana_service: &'a GrafanaService, - url: &str, - query: &str, - ) -> Prometheus<'a> { + pub fn new(grafana_service: &'a GrafanaService, url: &str, query: &str) -> Prometheus<'a> { Prometheus { url: url.to_owned(), grafana_service, diff --git a/src/main.rs b/src/main.rs index 7024baa..ef53d38 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,17 +20,53 @@ async fn main() -> types::Result<()> { .required(true) .index(2), ) + .arg( + Arg::with_name("datasource_url") + .help("relative path to datasource") + .required(true) + .index(3), + ) + .arg( + Arg::with_name("query") + .help("your query to datasource") + .required(true) + .index(4), + ) + .arg( + Arg::with_name("from") + .help("timestamp") + .required(true) + .index(5), + ) + .arg( + Arg::with_name("to") + .help("timestampt") + .required(true) + .index(6), + ) .get_matches(); let url = matches.value_of("GRAFANA_URL").unwrap(); let key = matches.value_of("GRAFANA_API_KEY").unwrap(); + let datasource_url = matches.value_of("datasource_url").unwrap(); + let query = matches.value_of("query").unwrap(); + let from = matches.value_of("from").unwrap().parse().unwrap(); + let to = matches.value_of("to").unwrap().parse().unwrap(); + let gs = grafana_service::GrafanaService::new(url.to_string(), key.to_string()); // gs.test_connection().await?; // gs.get_datasources().await?; // "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) + let r = gs + .extract_metrics( + datasource_url, + query, + from, + to, + 15, + ) .await?; let key = r.keys().nth(0).unwrap();