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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use std::collections::HashMap;
use std::iter::FromIterator;
use serde_json;
use std::iter::Iterator;
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(untagged)]
pub enum Value {
String(String),
Integer(i64),
Float(f64),
Boolean(bool),
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Point {
pub measurement: String,
pub tags: HashMap<String, Value>,
pub fields: HashMap<String, Value>,
pub timestamp: Option<i64>,
}
impl Point {
pub fn new(measurement: &str) -> Point {
Point {
measurement: String::from(measurement),
tags: HashMap::new(),
fields: HashMap::new(),
timestamp: None,
}
}
pub fn add_tag<T: ToString>(&mut self, tag: T, value: Value) -> &mut Self {
self.tags.insert(tag.to_string(), value);
self
}
pub fn add_field<T: ToString>(&mut self, field: T, value: Value) -> &mut Self {
self.fields.insert(field.to_string(), value);
self
}
pub fn add_timestamp(&mut self, timestamp: i64) -> &mut Self {
self.timestamp = Some(timestamp);
self
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Points {
pub point: Vec<Point>,
}
impl Points {
pub fn new(point: Point) -> Points {
let mut points = Vec::new();
points.push(point);
Points { point: points }
}
pub fn push(&mut self, point: Point) -> &mut Self {
self.point.push(point);
self
}
pub fn create_new(points: Vec<Point>) -> Points {
Points { point: points }
}
}
impl FromIterator<Point> for Points {
fn from_iter<T: IntoIterator<Item = Point>>(iter: T) -> Self {
let mut points = Vec::new();
for point in iter {
points.push(point);
}
Points { point: points }
}
}
impl Iterator for Points {
type Item = Point;
fn next(&mut self) -> Option<Point> {
self.point.pop()
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Query {
pub results: Option<Vec<Node>>,
pub error: Option<String>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Node {
pub statement_id: Option<u64>,
pub series: Option<Vec<Series>>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Series {
pub name: String,
pub tags: Option<serde_json::Map<String, serde_json::Value>>,
pub columns: Vec<String>,
pub values: Vec<Vec<serde_json::Value>>,
}
#[derive(Debug)]
pub enum Precision {
Nanoseconds,
Microseconds,
Milliseconds,
Seconds,
Minutes,
Hours,
}
impl Precision {
pub fn to_str(&self) -> &str {
match *self {
Precision::Nanoseconds => "n",
Precision::Microseconds => "u",
Precision::Milliseconds => "ms",
Precision::Seconds => "s",
Precision::Minutes => "m",
Precision::Hours => "h",
}
}
}
#[macro_export]
macro_rules! points {
($($x:expr),+) => {
{
let mut temp_vec = Vec::new();
$(temp_vec.push($x);)*
Points { point: temp_vec }
}
};
}
#[macro_export]
macro_rules! point {
($x:expr) => {
{
Point::new($x)
}
};
($x:expr, $y:expr, $z:expr) => {
{
Point {
measurement: String::from($x),
tags: $y,
fields: $z,
timestamp: None
}
}
};
($x:expr, $y:expr, $z:expr, $a:expr) => {
{
Point {
measurement: String::from($x),
tags: $y,
fields: $z,
timestamp: Some($a)
}
}
};
}