From 92427a4a11bf0f4f2e542eb640851037c246b8ee Mon Sep 17 00:00:00 2001 From: Alexey Velikiy Date: Thu, 14 Oct 2021 03:07:12 +0300 Subject: [PATCH] some commit --- Cargo.lock | 25 +++++++++++++++++++++++++ Cargo.toml | 2 ++ src/grafana_service.rs | 34 +++++++++++++++++++++++++++++++--- 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ca98de9..1ff80b1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -544,6 +544,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "ryu" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" + [[package]] name = "schannel" version = "0.1.19" @@ -583,6 +589,23 @@ dependencies = [ "libc", ] +[[package]] +name = "serde" +version = "1.0.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f12d06de37cf59146fbdecab66aa99f9fe4f78722e3607577a5375d66bd0c913" + +[[package]] +name = "serde_json" +version = "1.0.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f690853975602e1bfe1ccbf50504d67174e3bcf340f23b5ea9992e0587a52d8" +dependencies = [ + "itoa", + "ryu", + "serde", +] + [[package]] name = "signal-hook-registry" version = "1.4.0" @@ -624,9 +647,11 @@ checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" name = "subbeat" version = "0.0.2" dependencies = [ + "bytes", "clap", "hyper", "hyper-tls", + "serde_json", "tokio", ] diff --git a/Cargo.toml b/Cargo.toml index df92fa9..5a62780 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,9 @@ repository = "https://github.com/subbeat/subbeat" readme = "README.md" [dependencies] +bytes = "1" tokio = { version = "1", features = ["full"] } clap = "2.33.3" hyper = { version = "0.14.13", features = ["full"] } hyper-tls = "0.5.0" +serde_json = "1.0.59" diff --git a/src/grafana_service.rs b/src/grafana_service.rs index f0cb6a7..9588e5b 100644 --- a/src/grafana_service.rs +++ b/src/grafana_service.rs @@ -1,7 +1,11 @@ +use std::io::BufRead; + use hyper::{Body, Client, Method, Request}; use tokio::io::{stdout, AsyncWriteExt as _}; +use serde_json::json; use crate::types; +use bytes::Buf as _; pub struct GrafanaService { url: String, @@ -33,12 +37,36 @@ impl GrafanaService { .uri(self.url.clone() + "/api/datasources/proxy/1/api/v1/query_range") .header("content-type", "application/json") .header("Authorization", format!("Bearer {}", self.api_key)) - .body(Body::from(r#"{"query":"go_memstats_alloc_bytes_total", "start": "1634163645", "end": "1634163945", "step": "15"}"#)) + .body(Body::from(json!({ + "query":"go_memstats_alloc_bytes_total", + "start": "1634163645", + "end": "1634163945", + "step": "15" + }).to_string()) + ) + .unwrap(); let client = Client::new(); - let resp = client.request(req).await?; - println!("Response: {}", resp.status()); + let res = client.request(req).await?; + println!("Response: {}", res.status()); + + // while let Some(next) = resp.body().data().await { + // let chunk = next?; + // io::stdout().write_all(&chunk).await?; + // } + + let body = hyper::body::aggregate(res).await?; + let mut reader = body.reader(); + let mut line = String::new(); + loop { + match reader.read_line(&mut line) { + Ok(_) => println!("{}", line), + Err(_) => break + } + } + + Ok(()) } }