summaryrefslogtreecommitdiffstats
path: root/src/api.rs
blob: e09b62e0e4ffa5085d5228027226d48da3499576 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// This file was copied from:
// https://raw.githubusercontent.com/nickbabcock/rrinlog/master/rrinlog-server/src/api.rs
// on the 10th December 2017


use chrono::prelude::*;
use serde_json;
use getset::{Getters, MutGetters};

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, Copy)]
pub struct Range {
    pub from: DateTime<Utc>,
    pub to: DateTime<Utc>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct Target {
    pub target: String,
    #[serde(rename = "refId")] pub ref_id: String,
    #[serde(rename = "type")] pub _type: String,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct Query {
    pub range: Range,
    #[serde(rename = "intervalMs")] pub interval_ms: i32,
    #[serde(rename = "maxDataPoints")] pub max_data_points: i32,
    pub format: Option<String>,
    pub targets: Vec<Target>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
#[serde(untagged)]
pub enum TargetData {
    Series(Series),
    Table(Table),
}

#[derive(Getters, MutGetters, Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct Series {
    target: String,

    #[getset(get = "pub", get_mut = "pub")]
    datapoints: Vec<[f64; 2]>,
}

impl Series {
    pub fn new(target: String) -> Self {
        Series { target, datapoints: Vec::new() }
    }
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct Column {
    text: String,
    #[serde(rename = "type")] _type: String,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct Table {
    columns: Vec<Column>,
    #[serde(rename = "type")] _type: String,
    rows: Vec<Vec<serde_json::Value>>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct Search {
    target: String,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct SearchResponse(Vec<String>);

impl From<Vec<String>> for SearchResponse {
    fn from(v: Vec<String>) -> Self {
        SearchResponse(v)
    }
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct QueryResponse(Vec<TargetData>);


impl From<Vec<TargetData>> for QueryResponse {
    fn from(v: Vec<TargetData>) -> Self {
        QueryResponse(v)
    }
}