Alexey Velikiy 3 years ago
parent
commit
0ed7464fd3
  1. 2
      Cargo.lock
  2. 2
      Cargo.toml
  3. 2
      src/cli.rs
  4. 45
      src/datasources/grafana/prometheus.rs
  5. 3
      src/datasources/prometheus.rs
  6. 2
      src/metric.rs

2
Cargo.lock generated

@ -690,7 +690,7 @@ checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]] [[package]]
name = "subbeat" name = "subbeat"
version = "0.0.4" version = "0.0.5"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"async-trait", "async-trait",

2
Cargo.toml

@ -1,6 +1,6 @@
[package] [package]
name = "subbeat" name = "subbeat"
version = "0.0.4" version = "0.0.5"
edition = "2018" edition = "2018"
license = "MIT OR Apache-2.0" license = "MIT OR Apache-2.0"

2
src/cli.rs

@ -8,7 +8,7 @@ pub struct CLI {
impl CLI { impl CLI {
pub fn new() -> CLI { pub fn new() -> CLI {
let matches = App::new("subbeat") let matches = App::new("subbeat")
.version("0.0.4") .version("0.0.5")
.about("Timeseries toolkit") .about("Timeseries toolkit")
.subcommand( .subcommand(
SubCommand::with_name("grafana") SubCommand::with_name("grafana")

45
src/datasources/grafana/prometheus.rs

@ -1,8 +1,8 @@
use async_trait::async_trait; use async_trait::async_trait;
use hyper::StatusCode; use hyper::StatusCode;
use serde_json::Value;
use crate::{ use crate::{
datasources::prometheus,
metric::{Metric, MetricResult}, metric::{Metric, MetricResult},
types, types,
}; };
@ -37,39 +37,6 @@ impl<'a> Prometheus<'a> {
} }
} }
fn parse_result(value: Value) -> types::Result<MetricResult> {
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::<Vec<String>>()
.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::<f64>().unwrap(),
);
})
.collect::<Vec<(u64, f64)>>();
let mut result: MetricResult = Default::default();
result.data.insert(metric_name, values.to_owned());
// println!("{:?}", result);
return Ok(result);
}
#[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> {
@ -88,18 +55,10 @@ impl Metric for Prometheus<'_> {
let (status_code, value) = self.grafana_service.post_form(&self.url, &rq).await?; let (status_code, value) = self.grafana_service.post_form(&self.url, &rq).await?;
if status_code != StatusCode::OK { if status_code != StatusCode::OK {
// println!("Error: status code {:?}", status_code);
let error = &value["error"].as_str().unwrap(); let error = &value["error"].as_str().unwrap();
return Err(anyhow::anyhow!("Can`t query: {}", error)); return Err(anyhow::anyhow!("Can`t query: {}", error));
} }
// println!("{:?}", value); return prometheus::parse_result(value);
// return Ok(Default::default());
// println!("{:?}", value);
return parse_result(value);
} }
} }

3
src/datasources/prometheus.rs

@ -37,8 +37,7 @@ impl Prometheus {
} }
} }
// code duplication... pub fn parse_result(value: Value) -> types::Result<MetricResult> {
fn parse_result(value: Value) -> types::Result<MetricResult> {
let metric = &value["data"]["result"][0]["metric"]; let metric = &value["data"]["result"][0]["metric"];
let metric_name = metric let metric_name = metric
.as_object() .as_object()

2
src/metric.rs

@ -4,6 +4,7 @@ use std::{collections::HashMap, result};
use crate::types; use crate::types;
pub type MetricId = String; pub type MetricId = String;
use serde_derive::Serialize;
const CHUNK_SIZE: u64 = 10_000; const CHUNK_SIZE: u64 = 10_000;
@ -28,6 +29,7 @@ struct MetricQuery {
headers: Option<HashMap<String, String>>, headers: Option<HashMap<String, String>>,
} }
#[derive(Serialize)]
pub struct MetricResult { pub struct MetricResult {
pub data: HashMap<String, Vec<(u64, f64)>>, pub data: HashMap<String, Vec<(u64, f64)>>,
} }

Loading…
Cancel
Save