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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/// Format:
/// _e{<TITLE>.length,<TEXT>.length}:<TITLE>|<TEXT>|d:<TIMESTAMP>|h:<HOSTNAME>|p:<PRIORITY>|t:<ALERT_TYPE>|#<TAG_KEY_1>:<TAG_VALUE_1>,<TAG_2>
/// ex:
/// ## Send an exception
/// _e{21,36}:An exception occurred|Cannot parse CSV file from 10.0.0.17|t:warning|#err_type:bad_file
///
/// ## Send an event with a newline in the text
/// _e{21,42}:An exception occurred|Cannot parse JSON request:\\n{"foo: "bar"}|p:low|#err_type:bad_request
use std::time::SystemTime;

use crate::DatagramFormat;

#[derive(Clone, PartialEq)]
pub enum Priority {
    Low,
    Normal,
}

impl DatagramFormat for Priority {
    fn format(&self) -> String {
        let prefix = "p:";
        let p = match &*self {
            Priority::Low => "low",
            Priority::Normal => "normal",
        };
        let mut buf = String::with_capacity(prefix.len() + p.len());
        buf.push_str(prefix);
        buf.push_str(p);
        buf
    }
}

impl DatagramFormat for Option<Priority> {
    fn format(&self) -> String {
        match &*self {
            None => "".to_string(),
            Some(priority) => priority.format(),
        }
    }
}

#[derive(Clone, PartialEq)]
pub enum AlertType {
    Error,
    Info,
    Success,
    Warning,
}

impl DatagramFormat for AlertType {
    fn format(&self) -> String {
        match &*self {
            AlertType::Error => "error".to_string(),
            AlertType::Info => "info".to_string(),
            AlertType::Success => "success".to_string(),
            AlertType::Warning => "warning".to_string(),
        }
    }
}

impl DatagramFormat for Option<AlertType> {
    fn format(&self) -> String {
        match &*self {
            None => "".to_string(),
            Some(alert_type) => alert_type.format(),
        }
    }
}

/// Rust representation of the DogStatsD Datagram Event.
#[derive(Default)]
pub struct Event {
    /// The event title.
    title: String,
    /// The text associated with an event.
    text: String,
    /// Add a timestamp to the event.
    /// The default is a the current Unix Epoch timestamp.
    timestamp: Option<SystemTime>,
    /// Add a hostname to the event.
    /// There is no default.
    hostname: Option<String>,
    /// Add an aggregation key to group the event.
    /// There is no default.
    agg_key: Option<String>,
    /// Event priority.
    /// Default is Normal.
    priority: Option<Priority>,
    /// Event source.
    /// There is no default.
    source_type_name: Option<String>,
    /// Even alert type.
    /// Defaults to info.
    alert_type: Option<AlertType>,
    // Associated tags
    // tags: Option<T>,
}

impl Event {
    /// Creates a new Event with default options..
    ///
    /// You probably don't want this by itself.
    pub fn new() -> Self {
        Event::default()
    }

    /// Set the event Title...
    pub fn title(&mut self, title: &str) -> &mut Self {
        self.title = title.to_string();
        self
    }

    pub fn text(&mut self, text: &str) -> &mut Self {
        self.text = text.to_string();
        self
    }

    pub fn timestamp(&mut self, ts: SystemTime) -> &mut Self {
        self.timestamp = Some(ts);
        self
    }

    pub fn hostname(&mut self, host: &str) -> &mut Self {
        self.hostname = Some(host.to_string());
        self
    }

    pub fn agg_key(&mut self, agg_key: &str) -> &mut Self {
        self.agg_key = Some(agg_key.to_string());
        self
    }

    pub fn priority(&mut self, priority: Priority) -> &mut Self {
        self.priority = Some(priority);
        self
    }

    pub fn source_type_name(&mut self, name: &str) -> &mut Self {
        self.source_type_name = Some(name.to_string());
        self
    }

    pub fn alert_type(&mut self, alert_type: AlertType) -> &mut Self {
        self.alert_type = Some(alert_type);
        self
    }

    pub fn build(&mut self) -> Result<Event, &'static str> {
        Ok(Event {
            title: self.title.to_string(),
            text: self.text.to_string(),
            timestamp: self.timestamp,
            hostname: self.hostname.clone(),
            agg_key: self.agg_key.clone(),
            priority: self.priority.clone(),
            source_type_name: self.source_type_name.clone(),
            alert_type: self.alert_type.clone(),
        })
    }
}

impl DatagramFormat for Event {
    fn format(&self) -> String {
        // Add this to Datadog format trait.
        let (title, title_size) = {
            let title = &self.title;
            (title, title.len())
        };
        let (text, text_size) = {
            let text = &self.text;
            (text, text.len())
        };
        /*
        let (timestamp, timestamp_size) = match self.timestamp {
            Some(ts) => {
                match ts.duration_since(SystemTime::UNIX_EPOCH) {
                    Ok(elapsed) => {
                        let time = elapsed.to_string();
                        (time, time.len())
                    },
                    Err(_) => ("", 0),
                }
            }
            None => ("", 0),
        };:
        */
        let capacity = title_size + text_size;
        let mut msg = String::with_capacity(capacity);
        msg.push_str("_e{");
        msg.push_str(&title_size.to_string());
        msg.push_str(",");
        msg.push_str(&text_size.to_string());
        msg.push_str("}:");
        msg.push_str(title);
        msg.push_str("|");
        msg.push_str(text);
        msg
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_event_creation() {
        // let event: Event = Event::new()
        //     .title("Chungus")
        //     .text("Big Chungus")
        //     .priority(Priority::Low)
        //     .timestamp(std::time::SystemTime::UNIX_EPOCH)
        //     .hostname("kevin")
        //     .agg_key("something_cool")
        //     .source_type_name("your_app")
        //     .alert_type(AlertType::Error)
        //     .build()
        //     .expect("Failed to build");
    }

    #[test]
    fn test_simple_event() {
        assert_eq!(
            Event::new().title("Chungus").text("Big Chungus").format(),
            "_e{7,11}:Chungus|Big Chungus"
        );
    }

    #[test]
    #[ignore]
    fn test_event_with_timestamp() {
        assert_eq!(
            Event::new()
                .title("Chungus")
                .text("Big Chungus")
                .priority(Priority::Low)
                .format(),
            "_e{7,11}:Chungus|Big Chungus|p:low"
        );
    }

    #[test]
    #[ignore]
    fn test_event_with_all_stoppers() {
        assert_eq!(
            Event::new()
                .title("Chungus")
                .text("Big Chungus")
                .priority(Priority::Low)
                .timestamp(std::time::SystemTime::UNIX_EPOCH)
                .hostname("kevin")
                .agg_key("something_cool")
                .source_type_name("your_app")
                .alert_type(AlertType::Error)
                .format(),
            "_e{7,11}:Chungus|Big Chungus|d:0|h:kevin|a:something_cool|p:low|s:your_app|t:error"
        );
    }
}