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
use std::collections::BTreeMap;

#[derive(Debug)]
/// Measurement's field value.
pub enum Value<'a> {
    /// String.
    String(&'a str),
    /// Floating point number.
    Float(f64),
    /// Integer number.
    Integer(i64),
    /// Boolean value.
    Boolean(bool)
}

/// Measurement model.
#[derive(Debug)]
pub struct Measurement<'a> {
    /// Key.
    pub key: &'a str,

    /// Timestamp.
    pub timestamp: Option<i64>,

    /// Map of fields.
    pub fields: BTreeMap<&'a str, Value<'a>>,
    
    /// Map of tags.
    pub tags: BTreeMap<&'a str, &'a str>
}

impl<'a> Measurement<'a> {
    /// Constructs a new `Measurement`.
    ///
    /// # Examples
    /// 
    /// ```
    /// use influent::measurement::Measurement;
    ///
    /// let measurement = Measurement::new("key");
    /// ```
    pub fn new(key: &str) -> Measurement {
        Measurement {
            key: key,
            timestamp: None,
            fields: BTreeMap::new(),
            tags: BTreeMap::new()
        }
    }

    /// Adds field to the measurement.
    ///
    /// # Examples
    ///
    /// ```
    /// use influent::measurement::{Measurement, Value};
    ///
    /// let mut measurement = Measurement::new("key");
    ///
    /// measurement.add_field("field", Value::String("hello"));
    /// ```
    pub fn add_field(&mut self, field: &'a str, value: Value<'a>) {
        self.fields.insert(field, value);
    }

    /// Adds tag to the measurement.
    ///
    /// # Examples
    ///
    /// ```
    /// use influent::measurement::{Measurement, Value};
    ///
    /// let mut measurement = Measurement::new("key");
    ///
    /// measurement.add_tag("tag", "value");
    /// ```
    pub fn add_tag(&mut self, tag: &'a str, value: &'a str) {
        self.tags.insert(tag, value);
    }

    /// Sets the timestamp of the measurement. It should be unix timestamp in nanosecond
    ///
    /// # Examples
    ///
    /// ```
    /// use influent::measurement::{Measurement, Value};
    ///
    /// let mut measurement = Measurement::new("key");
    ///
    /// measurement.set_timestamp(1434055562000000000)
    /// ```
    pub fn set_timestamp(&mut self, timestamp: i64) {
        self.timestamp = Some(timestamp);
    }
}