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
use crate::error::InfluxDbError;
use crate::query::{InfluxDbQuery, QueryType, ValidQuery};
use itertools::Itertools;
pub struct InfluxDbWriteQuery {
fields: Vec<(String, String)>,
tags: Vec<(String, String)>,
measurement: String,
}
impl InfluxDbWriteQuery {
pub fn new<S>(measurement: S) -> Self
where
S: ToString,
{
InfluxDbWriteQuery {
fields: vec![],
tags: vec![],
measurement: measurement.to_string(),
}
}
pub fn add_field<S, I>(mut self, tag: S, value: I) -> Self
where
S: ToString,
I: Into<InfluxDbType>,
{
let val: InfluxDbType = value.into();
self.fields.push((tag.to_string(), val.to_string()));
self
}
pub fn add_tag<S, I>(mut self, tag: S, value: I) -> Self
where
S: ToString,
I: Into<InfluxDbType>,
{
let val: InfluxDbType = value.into();
self.tags.push((tag.to_string(), val.to_string()));
self
}
}
pub enum InfluxDbType {
Boolean(bool),
Float(f64),
SignedInteger(i64),
UnsignedInteger(u64),
Text(String),
}
impl ToString for InfluxDbType {
fn to_string(&self) -> String {
use InfluxDbType::*;
match self {
Boolean(x) => x.to_string(),
Float(x) => x.to_string(),
SignedInteger(x) => x.to_string(),
UnsignedInteger(x) => x.to_string(),
Text(text) => format!("\"{text}\"", text = text),
}
}
}
macro_rules! from_impl {
( $variant:ident => $( $typ:ident ),+ ) => (
$(
impl From<$typ> for InfluxDbType {
fn from(b: $typ) -> Self {
InfluxDbType::$variant(b.into())
}
}
)+
)
}
from_impl! {Boolean => bool}
from_impl! {Float => f32, f64}
from_impl! {SignedInteger => i8, i16, i32, i64}
from_impl! {UnsignedInteger => u8, u16, u32, u64}
from_impl! {Text => String}
impl From<&str> for InfluxDbType {
fn from(b: &str) -> Self {
InfluxDbType::Text(b.into())
}
}
impl InfluxDbQuery for InfluxDbWriteQuery {
fn build(self) -> Result<ValidQuery, InfluxDbError> {
if self.fields.is_empty() {
return Err(InfluxDbError::InvalidQueryError {
error: "fields cannot be empty".to_string(),
});
}
let mut tags = self
.tags
.into_iter()
.map(|(tag, value)| format!("{tag}={value}", tag = tag, value = value))
.join(",");
if !tags.is_empty() {
tags.insert_str(0, ",");
}
let fields = self
.fields
.into_iter()
.map(|(field, value)| format!("{field}={value}", field = field, value = value))
.join(",");
Ok(ValidQuery(format!(
"{measurement}{tags} {fields}{time}",
measurement = self.measurement,
tags = tags,
fields = fields,
time = ""
)))
}
fn get_type(&self) -> QueryType {
QueryType::WriteQuery
}
}