Browse Source

query url params

main
Alexey Velikiy 3 years ago
parent
commit
c3a1c4877a
  1. 11
      src/grafana_service.rs
  2. 9
      src/grafana_service/prometheus.rs
  3. 3
      src/main.rs
  4. 2
      src/metric.rs

11
src/grafana_service.rs

@ -34,15 +34,14 @@ impl GrafanaService {
Ok(()) Ok(())
} }
pub async fn extract_metrics(&self, panel_url: &str) -> types::Result<MetricResult> { pub async fn extract_metrics(&self, datasource_url: &str, query: &str, from: u64, to: u64, step: u64) -> types::Result<MetricResult> {
let pm = prometheus::Prometheus::new( let pm = prometheus::Prometheus::new(
self, self,
"/api/datasources/proxy/1/api/v1/query_range", datasource_url,
"rate(go_memstats_alloc_bytes_total[5m])", query
15,
); );
let r = pm.query(1634672070, 1634672970).await?; // TODO: split big query to chunks
let r = pm.query(from, to, step).await?;
Ok(r) Ok(r)
} }

9
src/grafana_service/prometheus.rs

@ -15,16 +15,15 @@ use super::GrafanaService;
pub struct Prometheus<'a> { pub struct Prometheus<'a> {
url: String, url: String,
query: String, query: String,
step: u32,
grafana_service: &'a GrafanaService, grafana_service: &'a GrafanaService,
} }
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
struct Query { struct Query {
query: String, query: String,
step: u32,
start: u64, start: u64,
end: u64, end: u64,
step: u64
} }
impl<'a> Prometheus<'a> { impl<'a> Prometheus<'a> {
@ -32,13 +31,11 @@ impl<'a> Prometheus<'a> {
grafana_service: &'a GrafanaService, grafana_service: &'a GrafanaService,
url: &str, url: &str,
query: &str, query: &str,
step: u32,
) -> Prometheus<'a> { ) -> Prometheus<'a> {
Prometheus { Prometheus {
url: url.to_owned(), url: url.to_owned(),
grafana_service, grafana_service,
query: query.to_string(), query: query.to_string(),
step,
} }
} }
} }
@ -78,10 +75,10 @@ fn parse_result(value: Value) -> types::Result<MetricResult> {
#[async_trait] #[async_trait]
impl Metric for Prometheus<'_> { impl Metric for Prometheus<'_> {
async fn query(&self, from: u64, to: u64) -> types::Result<MetricResult> { async fn query(&self, from: u64, to: u64, step: u64) -> types::Result<MetricResult> {
let q = Query { let q = Query {
query: self.query.to_owned(), query: self.query.to_owned(),
step: self.step, step: step,
start: from, start: from,
end: to, end: to,
}; };

3
src/main.rs

@ -29,7 +29,8 @@ async fn main() -> types::Result<()> {
// gs.test_connection().await?; // gs.test_connection().await?;
// gs.get_datasources().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?; .await?;
let key = r.keys().nth(0).unwrap(); let key = r.keys().nth(0).unwrap();

2
src/metric.rs

@ -30,5 +30,5 @@ pub type MetricResult = HashMap<String, Vec<(u64, f64)>>;
#[async_trait] #[async_trait]
pub trait Metric { pub trait Metric {
async fn query(&self, from: u64, to: u64) -> types::Result<MetricResult>; async fn query(&self, from: u64, to: u64, step: u64) -> types::Result<MetricResult>;
} }

Loading…
Cancel
Save