Browse Source

box_clone

main
Alexey Velikiy 3 years ago
parent
commit
c3f2af59c4
  1. 7
      src/datasources/grafana.rs
  2. 15
      src/datasources/grafana/prometheus.rs
  3. 4
      src/datasources/influx.rs
  4. 4
      src/datasources/prometheus.rs
  5. 3
      src/metric.rs

7
src/datasources/grafana.rs

@ -11,6 +11,7 @@ mod prometheus;
use serde_json; use serde_json;
#[derive(Clone)]
pub struct Grafana { pub struct Grafana {
url: String, url: String,
api_key: String, api_key: String,
@ -112,8 +113,12 @@ impl Grafana {
#[async_trait] #[async_trait]
impl Metric for Grafana { impl Metric for Grafana {
async fn query_chunk(&self, from: u64, to: u64, step: u64) -> types::Result<MetricResult> { async fn query_chunk(&self, from: u64, to: u64, step: u64) -> types::Result<MetricResult> {
let pm = prometheus::Prometheus::new(self, &self.datasource_url, &self.query); let pm = prometheus::Prometheus::new(self.clone(), &self.datasource_url, &self.query);
let r = pm.query(from, to, step).await?; let r = pm.query(from, to, step).await?;
Ok(r) Ok(r)
} }
fn boxed_clone(&self) -> Box<dyn Metric> {
return Box::new(self.clone());
}
} }

15
src/datasources/grafana/prometheus.rs

@ -13,10 +13,11 @@ use serde_qs as qs;
use super::Grafana; use super::Grafana;
pub struct Prometheus<'a> { #[derive(Clone)]
pub struct Prometheus {
url: String, url: String,
query: String, query: String,
grafana_service: &'a Grafana, grafana_service: Grafana,
} }
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
@ -27,8 +28,8 @@ struct Query {
step: u64, step: u64,
} }
impl<'a> Prometheus<'a> { impl Prometheus {
pub fn new(grafana_service: &'a Grafana, url: &str, query: &str) -> Prometheus<'a> { pub fn new(grafana_service: Grafana, url: &str, query: &str) -> Prometheus {
Prometheus { Prometheus {
url: url.to_owned(), url: url.to_owned(),
grafana_service, grafana_service,
@ -38,7 +39,7 @@ impl<'a> Prometheus<'a> {
} }
#[async_trait] #[async_trait]
impl Metric for Prometheus<'_> { impl Metric for Prometheus {
async fn query_chunk(&self, from: u64, to: u64, step: u64) -> types::Result<MetricResult> { async fn query_chunk(&self, from: u64, to: u64, step: u64) -> types::Result<MetricResult> {
if from >= to { if from >= to {
panic!("from >= to"); panic!("from >= to");
@ -61,4 +62,8 @@ impl Metric for Prometheus<'_> {
return prometheus::parse_result(value); return prometheus::parse_result(value);
} }
fn boxed_clone(&self) -> Box<dyn Metric> {
return Box::new(self.clone());
}
} }

4
src/datasources/influx.rs

@ -89,6 +89,10 @@ impl Metric for Influx {
return parse_result(value); return parse_result(value);
} }
fn boxed_clone(&self) -> Box<dyn Metric> {
return Box::new(self.clone());
}
} }
// curl -XPOST "localhost:8086/api/v2/query?orgID=5abe4759f7360f1c" -sS \ // curl -XPOST "localhost:8086/api/v2/query?orgID=5abe4759f7360f1c" -sS \

4
src/datasources/prometheus.rs

@ -97,4 +97,8 @@ impl Metric for Prometheus {
return parse_result(value); return parse_result(value);
} }
fn boxed_clone(&self) -> Box<dyn Metric> {
return Box::new(self.clone());
}
} }

3
src/metric.rs

@ -58,6 +58,9 @@ impl Default for MetricResult {
#[async_trait] #[async_trait]
pub trait Metric { pub trait Metric {
fn boxed_clone(&self) -> Box<dyn Metric>;
// (to - from) / step < 10000 // (to - from) / step < 10000
async fn query_chunk(&self, from: u64, to: u64, step: u64) -> types::Result<MetricResult>; async fn query_chunk(&self, from: u64, to: u64, step: u64) -> types::Result<MetricResult>;

Loading…
Cancel
Save