From 5aff24e1f764c7ae2f5deb8232987d42a5817f89 Mon Sep 17 00:00:00 2001 From: Alexey Velikiy Date: Mon, 25 Oct 2021 08:40:42 +0300 Subject: [PATCH] prom continue --- src/datasources.rs | 20 ++++++++++++++++++++ src/datasources/grafana.rs | 12 ------------ src/datasources/prometheus.rs | 4 +++- src/types.rs | 1 + 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/datasources.rs b/src/datasources.rs index db2dbb4..c2caf0b 100644 --- a/src/datasources.rs +++ b/src/datasources.rs @@ -1,2 +1,22 @@ +use crate::{ + metric::Metric, + types::{DatasourceType, QueryConfig}, +}; + pub mod grafana; pub mod prometheus; + +pub fn resolve(query_config: &QueryConfig) -> Box { + if query_config.datasource_type == DatasourceType::Grafana { + let gs = grafana::Grafana::new( + query_config.url.to_string(), + query_config.key.to_string(), + query_config.datasource_url.to_string(), + query_config.query.to_string(), + ); + return Box::new(gs); + } else { + let pm = prometheus::Prometheus::new(&query_config.url, &query_config.query); + return Box::new(pm); + } +} diff --git a/src/datasources/grafana.rs b/src/datasources/grafana.rs index 98c0be1..fbdf25c 100644 --- a/src/datasources/grafana.rs +++ b/src/datasources/grafana.rs @@ -40,18 +40,6 @@ impl Grafana { Ok(()) } - // pub async fn extract_metrics( - // &self, - // from: u64, - // to: u64, - // step: u64, - // ) -> types::Result { - // let pm = prometheus::Prometheus::new(self, &self.datasource_url, &self.query); - // // TODO: split big query to chunks - // let r = pm.query(from, to, step).await?; - // Ok(r) - // } - async fn get(&self, suburl: &str) -> types::Result<(StatusCode, serde_json::Value)> { let req = Request::builder() .method(Method::GET) diff --git a/src/datasources/prometheus.rs b/src/datasources/prometheus.rs index 1dbc6d1..b3ee628 100644 --- a/src/datasources/prometheus.rs +++ b/src/datasources/prometheus.rs @@ -8,7 +8,7 @@ use crate::{ use bytes::Buf as _; -struct Prometheus { +pub struct Prometheus { url: String, query: String, } @@ -44,6 +44,8 @@ impl Prometheus { #[async_trait] impl Metric for Prometheus { async fn query_chunk(&self, from: u64, to: u64, step: u64) -> types::Result { + // TODO: query + // TODO: parse return Ok(Default::default()); } } diff --git a/src/types.rs b/src/types.rs index 488f116..1ee76ba 100644 --- a/src/types.rs +++ b/src/types.rs @@ -4,6 +4,7 @@ use anyhow; pub type Result = anyhow::Result; +#[derive(PartialEq)] pub enum DatasourceType { Grafana, Prometheus,