Browse Source

some commit

main
Alexey Velikiy 3 years ago
parent
commit
851d7221c2
  1. 38
      src/grafana_service.rs

38
src/grafana_service.rs

@ -37,20 +37,21 @@ impl GrafanaService {
.body(Body::from(json!({ .body(Body::from(json!({
"query":"go_memstats_alloc_bytes_total", "query":"go_memstats_alloc_bytes_total",
"from": "1634163645", "from": "1634163645",
"to": "1634163945", "to": "1634163945",
"step": "15" "step": "15"
}).to_string()) }).to_string())
) )
.unwrap(); .unwrap();
Ok(()) Ok(())
} }
async fn get(&self, suburl:&str) -> types::Result<()> { async fn get(&self, suburl:&str) -> types::Result<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)
.header("Accept", "application/json")
.header("Authorization", format!("Bearer {}", self.api_key)) .header("Authorization", format!("Bearer {}", self.api_key))
.body(Body::empty()) .body(Body::empty())
.unwrap(); .unwrap();
@ -61,20 +62,21 @@ impl GrafanaService {
println!(""); println!("");
let body = hyper::body::aggregate(res).await?; let body = hyper::body::aggregate(res).await?;
let mut reader = body.reader(); let reader = body.reader();
let mut line = String::new(); let result: serde_json::Value = serde_json::from_reader(reader)?;
loop { // let mut line = String::new();
match reader.read_line(&mut line) { // loop {
Ok(s) => { // match reader.read_line(&mut line) {
if s == 0 { // Ok(s) => {
break; // if s == 0 {
} // break;
println!("{}", line); // }
line.clear(); // println!("{}", line);
}, // line.clear();
Err(_) => break // },
} // Err(_) => break
} // }
Ok(()) // }
Ok(result)
} }
} }

Loading…
Cancel
Save