|
|
@ -1,10 +1,9 @@ |
|
|
|
use std::{io::BufRead}; |
|
|
|
use crate::types; |
|
|
|
|
|
|
|
|
|
|
|
use hyper::{Body, Client, Method, Request}; |
|
|
|
use hyper::{Body, Client, Method, Request, StatusCode}; |
|
|
|
use tokio::io::{stdout, AsyncWriteExt as _}; |
|
|
|
|
|
|
|
use serde_json::json; |
|
|
|
use serde_json::json; |
|
|
|
|
|
|
|
use tokio::io::{stdout, AsyncWriteExt as _}; |
|
|
|
|
|
|
|
|
|
|
|
use crate::types; |
|
|
|
|
|
|
|
use bytes::Buf as _; |
|
|
|
use bytes::Buf as _; |
|
|
|
|
|
|
|
|
|
|
|
pub struct GrafanaService { |
|
|
|
pub struct GrafanaService { |
|
|
@ -19,35 +18,50 @@ impl GrafanaService { |
|
|
|
|
|
|
|
|
|
|
|
pub async fn test_connection(&self) -> types::Result<()> { |
|
|
|
pub async fn test_connection(&self) -> types::Result<()> { |
|
|
|
println!("Test connection response"); |
|
|
|
println!("Test connection response"); |
|
|
|
self.get("/api").await?; |
|
|
|
let (s, p) = self.get("/api").await?; |
|
|
|
|
|
|
|
println!("{}", p.to_string()); |
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub async fn get_datasources(&self) -> types::Result<()> { |
|
|
|
pub async fn get_datasources(&self) -> types::Result<()> { |
|
|
|
self.get("/api/datasources").await?; |
|
|
|
let (s, p) = self.get("/api/datasources").await?; |
|
|
|
|
|
|
|
println!("{}", p); |
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub async fn extract_metrics(&self) -> types::Result<()> { |
|
|
|
pub async fn extract_metrics(&self) -> types::Result<()> { |
|
|
|
let req = Request::builder() |
|
|
|
let (s, p) = self |
|
|
|
.uri(self.url.clone() + "/api/datasources/proxy/1/api/v1/query_range") |
|
|
|
.post( |
|
|
|
.header("Authorization", format!("Bearer {}", self.api_key)) |
|
|
|
"/api/datasources/proxy/1/api/v1/query_range", |
|
|
|
.method(Method::POST) |
|
|
|
// serde_json::json!({
|
|
|
|
.header("content-type", "application/json") |
|
|
|
// "from": "1634237655",
|
|
|
|
.body(Body::from(json!({
|
|
|
|
// "to": "1634238555",
|
|
|
|
"query":"go_memstats_alloc_bytes_total",
|
|
|
|
// "queries": [
|
|
|
|
"from": "1634163645", |
|
|
|
// {
|
|
|
|
"to": "1634163945", |
|
|
|
// "datasourceId": 1,
|
|
|
|
"step": "15" |
|
|
|
// "refId": "A",
|
|
|
|
}).to_string()) |
|
|
|
// "expr": "rate(go_memstats_alloc_bytes_total[5m])",
|
|
|
|
) |
|
|
|
// "format": "time_series",
|
|
|
|
|
|
|
|
// "step": "15",
|
|
|
|
|
|
|
|
// "start": "1634329050",
|
|
|
|
|
|
|
|
// "end": "1634329950"
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// ]
|
|
|
|
|
|
|
|
// "query": "rate(go_memstats_alloc_bytes_total[5m])",
|
|
|
|
|
|
|
|
// "start": 1634672070,
|
|
|
|
|
|
|
|
// "end": 1634672970,
|
|
|
|
|
|
|
|
// "step": "15"
|
|
|
|
|
|
|
|
// }),
|
|
|
|
|
|
|
|
serde_json::json!({}) |
|
|
|
|
|
|
|
|
|
|
|
.unwrap(); |
|
|
|
) |
|
|
|
|
|
|
|
.await?; |
|
|
|
|
|
|
|
println!("{}", p.to_string()); |
|
|
|
|
|
|
|
|
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async fn get(&self, suburl:&str) -> types::Result<serde_json::Value> { |
|
|
|
async fn get(&self, suburl: &str) -> types::Result<(StatusCode, serde_json::Value)> { |
|
|
|
let req = Request::builder() |
|
|
|
let req = Request::builder() |
|
|
|
.method(Method::GET) |
|
|
|
.method(Method::GET) |
|
|
|
.uri(self.url.to_owned() + suburl) |
|
|
|
.uri(self.url.to_owned() + suburl) |
|
|
@ -58,25 +72,37 @@ impl GrafanaService { |
|
|
|
|
|
|
|
|
|
|
|
let client = Client::new(); |
|
|
|
let client = Client::new(); |
|
|
|
let res = client.request(req).await?; |
|
|
|
let res = client.request(req).await?; |
|
|
|
println!("Response: {}", res.status()); |
|
|
|
let status = res.status(); |
|
|
|
println!(""); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let body = hyper::body::aggregate(res).await?; |
|
|
|
let body = hyper::body::aggregate(res).await?; |
|
|
|
let reader = body.reader(); |
|
|
|
let reader = body.reader(); |
|
|
|
let result: serde_json::Value = serde_json::from_reader(reader)?; |
|
|
|
let result: serde_json::Value = serde_json::from_reader(reader)?; |
|
|
|
// let mut line = String::new();
|
|
|
|
Ok((status, result)) |
|
|
|
// loop {
|
|
|
|
} |
|
|
|
// match reader.read_line(&mut line) {
|
|
|
|
|
|
|
|
// Ok(s) => {
|
|
|
|
async fn post( |
|
|
|
// if s == 0 {
|
|
|
|
&self, |
|
|
|
// break;
|
|
|
|
suburl: &str, |
|
|
|
// }
|
|
|
|
value: serde_json::Value, |
|
|
|
// println!("{}", line);
|
|
|
|
) -> types::Result<(StatusCode, serde_json::Value)> { |
|
|
|
// line.clear();
|
|
|
|
let req = Request::builder() |
|
|
|
// },
|
|
|
|
.method(Method::POST) |
|
|
|
// Err(_) => break
|
|
|
|
.uri(self.url.to_owned() + suburl) |
|
|
|
// }
|
|
|
|
.header("Accept", "application/json") |
|
|
|
// }
|
|
|
|
// .header("Content-Type", "application/json")
|
|
|
|
Ok(result) |
|
|
|
.header("Content-Type", "application/x-www-form-urlencoded") |
|
|
|
|
|
|
|
.header("Authorization", format!("Bearer {}", self.api_key)) |
|
|
|
|
|
|
|
// .body(Body::from(value.to_string()))
|
|
|
|
|
|
|
|
.body(Body::from("query=rate%28go_memstats_alloc_bytes_total%5B5m%5D%29&start=1634672070&end=1634672970&step=15")) |
|
|
|
|
|
|
|
.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)) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|