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.
116 lines
3.4 KiB
116 lines
3.4 KiB
3 years ago
|
use crate::metric::Metric;
|
||
3 years ago
|
use crate::{metric::MetricResult, types};
|
||
3 years ago
|
|
||
3 years ago
|
use hyper::{Body, Client, Method, Request, StatusCode};
|
||
3 years ago
|
|
||
3 years ago
|
use bytes::Buf as _;
|
||
3 years ago
|
|
||
3 years ago
|
mod prometheus;
|
||
|
|
||
|
use serde_json;
|
||
|
|
||
3 years ago
|
pub struct Grafana {
|
||
3 years ago
|
url: String,
|
||
|
api_key: String,
|
||
|
}
|
||
|
|
||
3 years ago
|
impl Grafana {
|
||
|
pub fn new(url: String, api_key: String) -> Grafana {
|
||
|
Grafana { api_key, url }
|
||
3 years ago
|
}
|
||
|
|
||
|
pub async fn test_connection(&self) -> types::Result<()> {
|
||
3 years ago
|
println!("Test connection response");
|
||
3 years ago
|
let (s, p) = self.get("/api").await?;
|
||
|
println!("{}", p.to_string());
|
||
3 years ago
|
Ok(())
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
pub async fn get_datasources(&self) -> types::Result<()> {
|
||
3 years ago
|
let (s, p) = self.get("/api/datasources").await?;
|
||
|
println!("{}", p);
|
||
3 years ago
|
Ok(())
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
pub async fn extract_metrics(
|
||
|
&self,
|
||
|
datasource_url: &str,
|
||
|
query: &str,
|
||
|
from: u64,
|
||
|
to: u64,
|
||
|
step: u64,
|
||
|
) -> types::Result<MetricResult> {
|
||
|
let pm = prometheus::Prometheus::new(self, datasource_url, query);
|
||
3 years ago
|
// TODO: split big query to chunks
|
||
|
let r = pm.query(from, to, step).await?;
|
||
3 years ago
|
Ok(r)
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
async fn get(&self, suburl: &str) -> types::Result<(StatusCode, serde_json::Value)> {
|
||
3 years ago
|
let req = Request::builder()
|
||
|
.method(Method::GET)
|
||
|
.uri(self.url.to_owned() + suburl)
|
||
3 years ago
|
.header("Accept", "application/json")
|
||
3 years ago
|
.header("Authorization", format!("Bearer {}", self.api_key))
|
||
|
.body(Body::empty())
|
||
|
.unwrap();
|
||
|
|
||
3 years ago
|
let client = Client::new();
|
||
3 years ago
|
let res = client.request(req).await?;
|
||
3 years ago
|
let status = res.status();
|
||
|
|
||
|
let body = hyper::body::aggregate(res).await?;
|
||
|
let reader = body.reader();
|
||
|
let result: serde_json::Value = serde_json::from_reader(reader)?;
|
||
|
Ok((status, result))
|
||
|
}
|
||
|
|
||
3 years ago
|
async fn post_form(
|
||
3 years ago
|
&self,
|
||
|
suburl: &str,
|
||
3 years ago
|
value: &str,
|
||
3 years ago
|
) -> types::Result<(StatusCode, serde_json::Value)> {
|
||
|
let req = Request::builder()
|
||
|
.method(Method::POST)
|
||
|
.uri(self.url.to_owned() + suburl)
|
||
|
.header("Accept", "application/json")
|
||
|
.header("Content-Type", "application/x-www-form-urlencoded")
|
||
|
.header("Authorization", format!("Bearer {}", self.api_key))
|
||
3 years ago
|
.body(Body::from(value.to_string()))
|
||
|
.unwrap();
|
||
|
|
||
|
let client = Client::new();
|
||
|
let res = client.request(req).await?;
|
||
|
let status = res.status();
|
||
|
|
||
|
let body = hyper::body::aggregate(res).await?;
|
||
|
let reader = body.reader();
|
||
|
let result: serde_json::Value = serde_json::from_reader(reader)?;
|
||
|
Ok((status, result))
|
||
|
}
|
||
|
|
||
|
async fn post_json(
|
||
|
&self,
|
||
|
suburl: &str,
|
||
|
value: serde_json::Value,
|
||
|
) -> types::Result<(StatusCode, serde_json::Value)> {
|
||
|
let req = Request::builder()
|
||
|
.method(Method::POST)
|
||
|
.uri(self.url.to_owned() + suburl)
|
||
|
.header("Accept", "application/json")
|
||
|
.header("Content-Type", "application/json")
|
||
|
.header("Authorization", format!("Bearer {}", self.api_key))
|
||
|
.body(Body::from(value.to_string()))
|
||
3 years ago
|
.unwrap();
|
||
|
|
||
|
let client = Client::new();
|
||
|
let res = client.request(req).await?;
|
||
|
let status = res.status();
|
||
3 years ago
|
|
||
3 years ago
|
let body = hyper::body::aggregate(res).await?;
|
||
3 years ago
|
let reader = body.reader();
|
||
|
let result: serde_json::Value = serde_json::from_reader(reader)?;
|
||
3 years ago
|
Ok((status, result))
|
||
3 years ago
|
}
|
||
|
}
|