subbeat
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.5 KiB

3 years ago
use async_trait::async_trait;
3 years ago
use hyper::StatusCode;
3 years ago
use crate::{
3 years ago
datasources::prometheus,
3 years ago
metric::{Metric, MetricResult},
types,
};
use serde_derive::{Deserialize, Serialize};
use serde_qs as qs;
use super::Grafana;
3 years ago
3 years ago
#[derive(Clone)]
pub struct Prometheus {
3 years ago
url: String,
3 years ago
query: String,
3 years ago
grafana_service: Grafana,
3 years ago
}
#[derive(Deserialize, Serialize)]
struct Query {
query: String,
start: u64,
end: u64,
3 years ago
step: u64,
3 years ago
}
3 years ago
impl Prometheus {
pub fn new(grafana_service: Grafana, url: &str, query: &str) -> Prometheus {
3 years ago
Prometheus {
3 years ago
url: url.to_owned(),
3 years ago
grafana_service,
query: query.to_string(),
}
}
}
#[async_trait]
3 years ago
impl Metric for Prometheus {
3 years ago
async fn query_chunk(&self, from: u64, to: u64, step: u64) -> types::Result<MetricResult> {
if from >= to {
panic!("from >= to");
}
3 years ago
let q = Query {
query: self.query.to_owned(),
3 years ago
step: step,
3 years ago
start: from,
end: to,
};
3 years ago
let rq = qs::to_string(&q)?;
3 years ago
let (status_code, value) = self.grafana_service.post_form(&self.url, &rq).await?;
3 years ago
if status_code != StatusCode::OK {
let error = &value["error"].as_str().unwrap();
return Err(anyhow::anyhow!("Can`t query: {}", error));
}
3 years ago
return prometheus::parse_result(value);
3 years ago
}
3 years ago
3 years ago
fn boxed_clone(&self) -> Box<dyn Metric + Sync> {
3 years ago
return Box::new(self.clone());
}
3 years ago
}