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
//! Write Query Builder returned by InfluxDbQuery::write_query
//!
//! Can only be instantiated by using InfluxDbQuery::write_query

use crate::error::InfluxDbError;
use crate::query::{InfluxDbQuery, QueryType, Timestamp, ValidQuery};
use itertools::Itertools;

// todo: batch write queries

/// Internal Representation of a Write query that has not yet been built
pub struct InfluxDbWriteQuery {
    fields: Vec<(String, String)>,
    tags: Vec<(String, String)>,
    measurement: String,
    timestamp: Timestamp,
}

impl InfluxDbWriteQuery {
    /// Creates a new [`InfluxDbWriteQuery`](crate::query::write_query::InfluxDbWriteQuery)
    pub fn new<S>(timestamp: Timestamp, measurement: S) -> Self
    where
        S: ToString,
    {
        InfluxDbWriteQuery {
            fields: vec![],
            tags: vec![],
            measurement: measurement.to_string(),
            timestamp,
        }
    }

    /// Adds a field to the [`InfluxDbWriteQuery`](crate::query::write_query::InfluxDbWriteQuery)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use influxdb::query::{InfluxDbQuery, Timestamp};
    ///
    /// InfluxDbQuery::write_query(Timestamp::NOW, "measurement").add_field("field1", 5).build();
    /// ```
    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
    }

    /// Adds a tag to the [`InfluxDbWriteQuery`](crate::query::write_query::InfluxDbWriteQuery)
    ///
    /// Please note that a [`InfluxDbWriteQuery`](crate::query::write_query::InfluxDbWriteQuery) requires at least one field. Composing a query with
    /// only tags will result in a failure building the query.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use influxdb::query::{InfluxDbQuery, Timestamp};
    ///
    /// InfluxDbQuery::write_query(Timestamp::NOW, "measurement")
    ///     .add_tag("field1", 5); // calling `.build()` now would result in a `Err(InfluxDbError::InvalidQueryError)`
    /// ```
    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 fn get_precision_modifier(&self) -> String {
        let modifier = match self.timestamp {
            Timestamp::NOW => return String::from(""),
            Timestamp::NANOSECONDS(_) => "ns",
            Timestamp::MICROSECONDS(_) => "u",
            Timestamp::MILLISECONDS(_) => "ms",
            Timestamp::SECONDS(_) => "s",
            Timestamp::MINUTES(_) => "m",
            Timestamp::HOURS(_) => "h",
        };

        format!("&precision={modifier}", modifier = modifier)
    }
}

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
            .iter()
            .map(|(tag, value)| format!("{tag}={value}", tag = tag, value = value))
            .join(",");
        if !tags.is_empty() {
            tags.insert_str(0, ",");
        }
        let fields = self
            .fields
            .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 = match self.timestamp {
                Timestamp::NOW => String::from(""),
                _ => format!(" {}", self.timestamp),
            }
        )))
    }

    fn get_type(&self) -> QueryType {
        QueryType::WriteQuery
    }
}

#[cfg(test)]
mod tests {
    use crate::query::{InfluxDbQuery, Timestamp};

    #[test]
    fn test_write_builder_empty_query() {
        let query = InfluxDbQuery::write_query(Timestamp::HOURS(5), "marina_3").build();

        assert!(query.is_err(), "Query was not empty");
    }

    #[test]
    fn test_write_builder_single_field() {
        let query = InfluxDbQuery::write_query(Timestamp::HOURS(11), "weather")
            .add_field("temperature", 82)
            .build();

        assert!(query.is_ok(), "Query was empty");
        assert_eq!(query.unwrap(), "weather temperature=82 11");
    }

    #[test]
    fn test_write_builder_multiple_fields() {
        let query = InfluxDbQuery::write_query(Timestamp::HOURS(11), "weather")
            .add_field("temperature", 82)
            .add_field("wind_strength", 3.7)
            .build();

        assert!(query.is_ok(), "Query was empty");
        assert_eq!(
            query.unwrap(),
            "weather temperature=82,wind_strength=3.7 11"
        );
    }

    #[test]
    fn test_write_builder_only_tags() {
        let query = InfluxDbQuery::write_query(Timestamp::HOURS(11), "weather")
            .add_tag("season", "summer")
            .build();

        assert!(query.is_err(), "Query missing one or more fields");
    }

    #[test]
    fn test_write_builder_full_query() {
        let query = InfluxDbQuery::write_query(Timestamp::HOURS(11), "weather")
            .add_field("temperature", 82)
            .add_tag("location", "us-midwest")
            .add_tag("season", "summer")
            .build();

        assert!(query.is_ok(), "Query was empty");
        assert_eq!(
            query.unwrap(),
            "weather,location=\"us-midwest\",season=\"summer\" temperature=82 11"
        );
    }

    #[test]
    fn test_correct_query_type() {
        use crate::query::QueryType;

        let query = InfluxDbQuery::write_query(Timestamp::HOURS(11), "weather")
            .add_field("temperature", 82)
            .add_tag("location", "us-midwest")
            .add_tag("season", "summer");

        assert_eq!(query.get_type(), QueryType::WriteQuery);
    }
}