-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoint.rs
244 lines (215 loc) · 6.35 KB
/
point.rs
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
use super::{error::Error, escape, Field, Measurement, Precision, Tag, TagSet, Timestamp};
use std::{convert::TryInto, iter::FromIterator};
/// Represents a single data record
///
/// Each point:
/// - has a measurement, a tag set, a field key, a field value, and a timestamp;
/// - is uniquely identified by its series and timestamp.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Point {
measurment: Measurement,
tag_set: Vec<Tag>,
field_set: Vec<Field>,
timestamp: Timestamp,
}
impl Point {
pub fn builder(measurment: impl Into<String>) -> Result<PointBuilder, Error> {
PointBuilder::new(measurment)
}
pub fn precision(&self) -> Option<Precision> {
self.timestamp.precision()
}
pub(crate) fn to_text_with_precision(&self, precision: Option<Precision>) -> String {
let mut line = escape::measurement(&self.measurment);
for tag_set in &self.tag_set {
line += &format!(",{}", tag_set.to_text());
}
let mut first_iter = true;
for field_set in &self.field_set {
if first_iter {
first_iter = false;
line += &format!(" {}", field_set.to_text());
} else {
line += &format!(",{}", field_set.to_text());
}
}
let ts = precision
.map(|p| self.timestamp.timestamp_precision_lossy(p))
.unwrap_or(self.timestamp);
match ts {
Timestamp::Now => {}
Timestamp::Nanos(v)
| Timestamp::Micro(v)
| Timestamp::Milli(v)
| Timestamp::Secs(v) => {
line += " ";
line += &v.to_string();
}
}
line
}
}
/// Builder for [`Point`]
///
/// [`Point`]:Point
#[derive(Debug, Clone)]
pub struct PointBuilder {
point: Point,
errors: Vec<Error>,
}
impl PointBuilder {
pub fn new(measurment: impl Into<String>) -> Result<Self, Error> {
let measurment = Measurement::new(measurment)?;
let point = Point {
measurment,
tag_set: Default::default(),
field_set: Default::default(),
timestamp: Timestamp::Now,
};
Ok(Self {
point,
errors: vec![],
})
}
pub fn add_tag(mut self, tag_set: impl Into<Tag>) -> Self {
self.point.tag_set.push(tag_set.into());
self
}
pub fn add_tags<I>(mut self, tags: I) -> Self
where
I: IntoIterator,
I::Item: Into<Tag>,
{
self.point.tag_set = self
.point
.tag_set
.into_iter()
.chain(tags.into_iter().map(|x| x.into()))
.collect::<TagSet>();
self
}
pub fn try_add_tag<I>(mut self, tag: I) -> Self
where
I: TryInto<Tag>,
I::Error: Into<Error>,
{
match tag.try_into() {
Ok(tag) => self.add_tag(tag),
Err(err) => {
self.errors.push(err.into());
self
}
}
}
pub fn try_add_tags<I>(mut self, iter: I) -> Self
where
I: IntoIterator,
I::Item: TryInto<Tag>,
<I::Item as TryInto<Tag>>::Error: Into<Error>,
{
let tags_iter = iter.into_iter().map(|x| x.try_into());
let res_tags: Result<Vec<Tag>, _> =
Result::from_iter(tags_iter.collect::<Vec<Result<Tag, _>>>());
match res_tags {
Ok(v) => self.add_tags(v),
Err(err) => {
self.errors.push(err.into());
self
}
}
}
pub fn add_field(mut self, field: impl Into<Field>) -> Self {
self.point.field_set.push(field.into());
self
}
pub fn add_fields<I>(mut self, fields: I) -> Self
where
I: IntoIterator,
I::Item: Into<Field>,
{
self.point.field_set = self
.point
.field_set
.into_iter()
.chain(fields.into_iter().map(|x| x.into()))
.collect::<Vec<_>>();
self
}
pub fn try_add_field<V>(mut self, field: V) -> Self
where
V: TryInto<Field>,
V::Error: Into<Error>,
{
match field.try_into() {
Ok(field) => self.add_field(field),
Err(err) => {
self.errors.push(err.into());
self
}
}
}
pub fn try_add_fields<I>(mut self, iter: I) -> Self
where
I: IntoIterator,
I::Item: TryInto<Field>,
<I::Item as TryInto<Field>>::Error: Into<Error>,
{
let tags_iter = iter.into_iter().map(|x| x.try_into());
let res_tags: Result<Vec<Field>, _> =
Result::from_iter(tags_iter.collect::<Vec<Result<Field, _>>>());
match res_tags {
Ok(v) => self.add_fields(v),
Err(err) => {
self.errors.push(err.into());
self
}
}
}
pub fn timestamp(mut self, timestamp: impl Into<Timestamp>) -> Self {
self.point.timestamp = timestamp.into();
self
}
pub fn errors(&self) -> &Vec<Error> {
&self.errors
}
pub fn build(mut self) -> Result<Point, Error> {
if self.point.field_set.is_empty() {
panic!("At least one field value is required!");
}
// https://v2.docs.influxdata.com/v2.0/write-data/best-practices/optimize-writes/#sort-tags-by-key
// TODO make sure it match `https://golang.org/pkg/bytes/#Compare` function
self.point.tag_set.sort();
if let Some(err) = self.errors.drain(..).next() {
Err(err)
} else {
Ok(self.point)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn add_vec_of_fields_to_builder() {
let a = Field::new("a", "b").unwrap();
let b = Field::new("c", 6i64).unwrap();
let _point = Point::builder("test")
.unwrap()
.add_fields(vec![a, b])
.build()
.unwrap();
}
#[test]
fn try_add_tags_to_builder() {
let v = vec![("field1", "value1"), ("field2", "value2")];
let point = Point::builder("test")
.unwrap()
.try_add_fields(v)
.build()
.unwrap();
assert_eq!(
point.to_text_with_precision(None),
r#"test field1="value1",field2="value2""#
);
}
}