From c3f2af59c4ec6caf2284a41c15ccea2d35dfb562 Mon Sep 17 00:00:00 2001 From: Alexey Velikiy Date: Thu, 4 Nov 2021 06:08:09 +0300 Subject: [PATCH] box_clone --- src/datasources/grafana.rs | 7 ++++++- src/datasources/grafana/prometheus.rs | 15 ++++++++++----- src/datasources/influx.rs | 4 ++++ src/datasources/prometheus.rs | 4 ++++ src/metric.rs | 3 +++ 5 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/datasources/grafana.rs b/src/datasources/grafana.rs index f1a35bd..c61171b 100644 --- a/src/datasources/grafana.rs +++ b/src/datasources/grafana.rs @@ -11,6 +11,7 @@ mod prometheus; use serde_json; +#[derive(Clone)] pub struct Grafana { url: String, api_key: String, @@ -112,8 +113,12 @@ impl Grafana { #[async_trait] impl Metric for Grafana { async fn query_chunk(&self, from: u64, to: u64, step: u64) -> types::Result { - 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?; Ok(r) } + + fn boxed_clone(&self) -> Box { + return Box::new(self.clone()); + } } diff --git a/src/datasources/grafana/prometheus.rs b/src/datasources/grafana/prometheus.rs index f59e19f..8417e70 100644 --- a/src/datasources/grafana/prometheus.rs +++ b/src/datasources/grafana/prometheus.rs @@ -13,10 +13,11 @@ use serde_qs as qs; use super::Grafana; -pub struct Prometheus<'a> { +#[derive(Clone)] +pub struct Prometheus { url: String, query: String, - grafana_service: &'a Grafana, + grafana_service: Grafana, } #[derive(Deserialize, Serialize)] @@ -27,8 +28,8 @@ struct Query { step: u64, } -impl<'a> Prometheus<'a> { - pub fn new(grafana_service: &'a Grafana, url: &str, query: &str) -> Prometheus<'a> { +impl Prometheus { + pub fn new(grafana_service: Grafana, url: &str, query: &str) -> Prometheus { Prometheus { url: url.to_owned(), grafana_service, @@ -38,7 +39,7 @@ impl<'a> Prometheus<'a> { } #[async_trait] -impl Metric for Prometheus<'_> { +impl Metric for Prometheus { async fn query_chunk(&self, from: u64, to: u64, step: u64) -> types::Result { if from >= to { panic!("from >= to"); @@ -61,4 +62,8 @@ impl Metric for Prometheus<'_> { return prometheus::parse_result(value); } + + fn boxed_clone(&self) -> Box { + return Box::new(self.clone()); + } } diff --git a/src/datasources/influx.rs b/src/datasources/influx.rs index 0c89ff2..86dc830 100644 --- a/src/datasources/influx.rs +++ b/src/datasources/influx.rs @@ -89,6 +89,10 @@ impl Metric for Influx { return parse_result(value); } + + fn boxed_clone(&self) -> Box { + return Box::new(self.clone()); + } } // curl -XPOST "localhost:8086/api/v2/query?orgID=5abe4759f7360f1c" -sS \ diff --git a/src/datasources/prometheus.rs b/src/datasources/prometheus.rs index 5bc85b4..e4bebdc 100644 --- a/src/datasources/prometheus.rs +++ b/src/datasources/prometheus.rs @@ -97,4 +97,8 @@ impl Metric for Prometheus { return parse_result(value); } + + fn boxed_clone(&self) -> Box { + return Box::new(self.clone()); + } } diff --git a/src/metric.rs b/src/metric.rs index 7b9e5bf..edc4765 100644 --- a/src/metric.rs +++ b/src/metric.rs @@ -58,6 +58,9 @@ impl Default for MetricResult { #[async_trait] pub trait Metric { + + fn boxed_clone(&self) -> Box; + // (to - from) / step < 10000 async fn query_chunk(&self, from: u64, to: u64, step: u64) -> types::Result;