From 0ed7464fd326088be0f09a045e22dbc1adced178 Mon Sep 17 00:00:00 2001 From: Alexey Velikiy Date: Mon, 25 Oct 2021 21:01:21 +0300 Subject: [PATCH] 0.0.5 --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/cli.rs | 2 +- src/datasources/grafana/prometheus.rs | 45 ++------------------------- src/datasources/prometheus.rs | 3 +- src/metric.rs | 2 ++ 6 files changed, 8 insertions(+), 48 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0055b96..84f7e45 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -690,7 +690,7 @@ checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" [[package]] name = "subbeat" -version = "0.0.4" +version = "0.0.5" dependencies = [ "anyhow", "async-trait", diff --git a/Cargo.toml b/Cargo.toml index e6e138b..2816eb3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "subbeat" -version = "0.0.4" +version = "0.0.5" edition = "2018" license = "MIT OR Apache-2.0" diff --git a/src/cli.rs b/src/cli.rs index c679e02..2d7a968 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -8,7 +8,7 @@ pub struct CLI { impl CLI { pub fn new() -> CLI { let matches = App::new("subbeat") - .version("0.0.4") + .version("0.0.5") .about("Timeseries toolkit") .subcommand( SubCommand::with_name("grafana") diff --git a/src/datasources/grafana/prometheus.rs b/src/datasources/grafana/prometheus.rs index fcd59bb..f59e19f 100644 --- a/src/datasources/grafana/prometheus.rs +++ b/src/datasources/grafana/prometheus.rs @@ -1,8 +1,8 @@ use async_trait::async_trait; use hyper::StatusCode; -use serde_json::Value; use crate::{ + datasources::prometheus, metric::{Metric, MetricResult}, types, }; @@ -37,39 +37,6 @@ impl<'a> Prometheus<'a> { } } -fn parse_result(value: Value) -> types::Result { - let metric = &value["data"]["result"][0]["metric"]; - let metric_name = metric - .as_object() - .unwrap() - .iter() - .map(|(k, v)| format!("{}=\"{}\"", k, v.as_str().unwrap())) - .collect::>() - .join(","); - - let metric_name = format!("{{{}}}", metric_name); - - let values = &value["data"]["result"][0]["values"] - .as_array() - .unwrap() - .iter() - .map(|e| { - let r = e.as_array().unwrap(); - return ( - r[0].as_u64().unwrap(), - r[1].as_str().unwrap().to_string().parse::().unwrap(), - ); - }) - .collect::>(); - - let mut result: MetricResult = Default::default(); - result.data.insert(metric_name, values.to_owned()); - - // println!("{:?}", result); - - return Ok(result); -} - #[async_trait] impl Metric for Prometheus<'_> { async fn query_chunk(&self, from: u64, to: u64, step: u64) -> types::Result { @@ -88,18 +55,10 @@ impl Metric for Prometheus<'_> { let (status_code, value) = self.grafana_service.post_form(&self.url, &rq).await?; if status_code != StatusCode::OK { - // println!("Error: status code {:?}", status_code); let error = &value["error"].as_str().unwrap(); - return Err(anyhow::anyhow!("Can`t query: {}", error)); } - // println!("{:?}", value); - - // return Ok(Default::default()); - - // println!("{:?}", value); - - return parse_result(value); + return prometheus::parse_result(value); } } diff --git a/src/datasources/prometheus.rs b/src/datasources/prometheus.rs index 3ada29a..6eae3c9 100644 --- a/src/datasources/prometheus.rs +++ b/src/datasources/prometheus.rs @@ -37,8 +37,7 @@ impl Prometheus { } } -// code duplication... -fn parse_result(value: Value) -> types::Result { +pub fn parse_result(value: Value) -> types::Result { let metric = &value["data"]["result"][0]["metric"]; let metric_name = metric .as_object() diff --git a/src/metric.rs b/src/metric.rs index e3ef233..ced8553 100644 --- a/src/metric.rs +++ b/src/metric.rs @@ -4,6 +4,7 @@ use std::{collections::HashMap, result}; use crate::types; pub type MetricId = String; +use serde_derive::Serialize; const CHUNK_SIZE: u64 = 10_000; @@ -28,6 +29,7 @@ struct MetricQuery { headers: Option>, } +#[derive(Serialize)] pub struct MetricResult { pub data: HashMap>, }