Skip to content

Commit 6748110

Browse files
author
puetzp
committed
Implement Deserialize on Vector, Matrix, Sample
1 parent 8de714e commit 6748110

File tree

3 files changed

+12
-26
lines changed

3 files changed

+12
-26
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ description = "Prometheus HTTP API client"
1111

1212
[dependencies]
1313
reqwest = { version = "0.11", features = ["json"] }
14+
serde = { version = "1", features = ["derive"] }
1415
serde_json = "1"
1516

1617
[dev-dependencies]

src/client.rs

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -703,23 +703,15 @@ fn parse_query_response(response: HashMap<String, serde_json::Value>) -> Result<
703703
"success" => {
704704
let data_obj = response["data"].as_object().unwrap();
705705
let data_type = data_obj["resultType"].as_str().unwrap();
706-
let data = data_obj["result"].as_array().unwrap();
706+
let data = data_obj["result"].as_array().unwrap().to_owned();
707707

708708
match data_type {
709709
"vector" => {
710710
let mut result: Vec<Vector> = vec![];
711711

712712
for datum in data {
713-
let metric = parse_metric(datum["metric"].as_object().unwrap());
714-
715-
let raw_value = datum["value"].as_array().unwrap();
716-
717-
let sample = Sample {
718-
timestamp: raw_value[0].as_f64().unwrap(),
719-
value: raw_value[1].as_str().unwrap().to_string(),
720-
};
721-
722-
result.push(Vector { metric, sample });
713+
let vector: Vector = serde_json::from_value(datum).unwrap();
714+
result.push(vector);
723715
}
724716

725717
Ok(Response::Vector(result))
@@ -728,18 +720,8 @@ fn parse_query_response(response: HashMap<String, serde_json::Value>) -> Result<
728720
let mut result: Vec<Matrix> = vec![];
729721

730722
for datum in data {
731-
let metric = parse_metric(datum["metric"].as_object().unwrap());
732-
733-
let mut samples: Vec<Sample> = vec![];
734-
735-
for sample in datum["values"].as_array().unwrap() {
736-
samples.push(Sample {
737-
timestamp: sample[0].as_f64().unwrap(),
738-
value: sample[1].as_str().unwrap().to_string(),
739-
});
740-
}
741-
742-
result.push(Matrix { metric, samples });
723+
let matrix: Matrix = serde_json::from_value(datum).unwrap();
724+
result.push(matrix);
743725
}
744726

745727
Ok(Response::Matrix(result))

src/response.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Collection of response types, most importantly the [Response] enum
22
use crate::util::TargetHealth;
3+
use serde::Deserialize;
34
use std::collections::HashMap;
45

56
/// A wrapper for any kind of response the API returns.
@@ -14,9 +15,10 @@ pub enum Response {
1415
}
1516

1617
/// A single time series containing a single data point ([Sample]).
17-
#[derive(Debug, PartialEq)]
18+
#[derive(Debug, PartialEq, Deserialize)]
1819
pub struct Vector {
1920
pub(crate) metric: HashMap<String, String>,
21+
#[serde(alias = "value")]
2022
pub(crate) sample: Sample,
2123
}
2224

@@ -34,9 +36,10 @@ impl Vector {
3436
}
3537

3638
/// A single time series containing a range of data points ([Sample]s).
37-
#[derive(Debug, PartialEq)]
39+
#[derive(Debug, PartialEq, Deserialize)]
3840
pub struct Matrix {
3941
pub(crate) metric: HashMap<String, String>,
42+
#[serde(alias = "values")]
4043
pub(crate) samples: Vec<Sample>,
4144
}
4245

@@ -54,7 +57,7 @@ impl Matrix {
5457
}
5558

5659
/// A single data point.
57-
#[derive(Debug, PartialEq)]
60+
#[derive(Debug, PartialEq, Deserialize)]
5861
pub struct Sample {
5962
pub(crate) timestamp: f64,
6063
pub(crate) value: String,

0 commit comments

Comments
 (0)