Browse Source

no data case

main
Alexey Velikiy 3 years ago
parent
commit
58f7fea43f
  1. 9
      src/datasources/prometheus.rs
  2. 7
      src/main.rs

9
src/datasources/prometheus.rs

@ -40,6 +40,15 @@ impl Prometheus {
pub fn parse_result(value: Value) -> types::Result<MetricResult> { pub fn parse_result(value: Value) -> types::Result<MetricResult> {
// TODO: check that metric exists // TODO: check that metric exists
// TODO: check status: "error" // TODO: check status: "error"
if value.get("data").is_none() {
return Err(anyhow::format_err!("no data in response"));
}
if value["data"].get("result").is_none() {
return Err(anyhow::format_err!("no result in response"));
}
if value["data"]["result"].as_array().unwrap().len() == 0 {
return Ok(Default::default());
}
let metric = &value["data"]["result"][0]["metric"]; let metric = &value["data"]["result"][0]["metric"];
let metric_name = metric let metric_name = metric
.as_object() .as_object()

7
src/main.rs

@ -20,13 +20,16 @@ async fn main() -> types::Result<()> {
) )
.await?; .await?;
if r.data.keys().len() > 0 {
let key = r.data.keys().nth(0).unwrap(); let key = r.data.keys().nth(0).unwrap();
println!("{}", key); println!("timestamp\t{}", key);
let vs = &r.data[key]; let vs = &r.data[key];
for (t, v) in vs.iter() { for (t, v) in vs.iter() {
println!("{}\t{}", t, v); println!("{}\t{}", t, v);
} }
} else {
println!("no_data");
}
Ok(()) Ok(())
} }

Loading…
Cancel
Save