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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
use alfred::Item;
use chrono::{DateTime, Utc};
use failure::{format_err, Error};
use reqwest::Client;
use rusqlite::{types::ToSql, Connection, NO_PARAMS};
use serde::Deserialize;
use std::str;

const APPLICATION_KEY: &str = "application_key";
const API_KEY: &str = "api_key";

pub struct DatadogWorkflow {
    conn: Connection,
}

impl DatadogWorkflow {
    pub fn create() -> Result<Self, Error> {
        let conn =
            alfred_workflow::open_database_or_else("datadog", DatadogWorkflow::create_tables)?;
        Ok(DatadogWorkflow { conn })
    }

    fn create_tables(conn: &Connection) -> Result<(), Error> {
        conn.execute(
            "CREATE TABLE IF NOT EXISTS config (
                key   TEXT NOT NULL PRIMARY KEY,
                value TEXT NOT NULL
            );",
            NO_PARAMS,
        )?;
        conn.execute(
            "CREATE TABLE IF NOT EXISTS timeboards (
                id          TEXT    NOT NULL PRIMARY KEY,
                title       TEXT    NOT NULL,
                description TEXT    NOT NULL,
                url         TEXT    NOT NULL,
                modified    INTEGER NOT NULL
            );",
            NO_PARAMS,
        )?;
        conn.execute(
            "CREATE INDEX IF NOT EXISTS idx_timeboards_title_modified ON timeboards (title, modified);",
            NO_PARAMS,
        )?;
        conn.execute(
            "CREATE TABLE IF NOT EXISTS screenboards (
                id          INTEGER NOT NULL PRIMARY KEY,
                title       TEXT    NOT NULL,
                description TEXT    NOT NULL,
                url         TEXT    NOT NULL,
                modified    INTEGER NOT NULL
            );",
            NO_PARAMS,
        )?;
        conn.execute(
            "CREATE INDEX IF NOT EXISTS idx_screenboards_title_modified ON screenboards (title, modified);",
            NO_PARAMS,
        )?;
        conn.execute(
            "CREATE TABLE IF NOT EXISTS monitors (
                id          INTEGER NOT NULL PRIMARY KEY,
                name        TEXT    NOT NULL,
                url         TEXT    NOT NULL,
                modified    INTEGER NOT NULL
            );",
            NO_PARAMS,
        )?;
        conn.execute(
            "CREATE INDEX IF NOT EXISTS idx_monitors_name_modified ON monitors (name, modified);",
            NO_PARAMS,
        )?;
        conn.execute(
            "CREATE TABLE IF NOT EXISTS monitor_tags (
                id          INTEGER NOT NULL,
                name        TEXT    NOT NULL,
                CONSTRAINT fk_monitors
                FOREIGN KEY (id)
                REFERENCES monitors(id)
                ON DELETE CASCADE
            );",
            NO_PARAMS,
        )?;
        conn.execute(
            "CREATE INDEX IF NOT EXISTS idx_monitor_tags_id ON monitor_tags (id);",
            NO_PARAMS,
        )?;
        conn.execute(
            "CREATE INDEX IF NOT EXISTS idx_monitor_tags_name ON monitor_tags (name);",
            NO_PARAMS,
        )?;
        Ok(())
    }

    pub fn set_application_key(&self, key: &str) -> Result<(), Error> {
        self.set_key(APPLICATION_KEY, key)
    }

    pub fn set_api_key(&self, key: &str) -> Result<(), Error> {
        self.set_key(API_KEY, key)
    }

    fn set_key(&self, name: &str, key: &str) -> Result<(), Error> {
        self.conn
            .execute(
                "INSERT INTO config (key, value) VALUES (?1, ?2) ON CONFLICT(key) DO UPDATE SET value=excluded.value",
                &[name, key],
            )
            .map(|_|Ok(()))
            .map_err(|e| format_err!("failed to insert application key: {}", e))?
    }

    pub fn refresh_cache(&mut self) -> Result<(), Error> {
        let mut stmt = self.conn.prepare("SELECT value FROM config WHERE key=?1")?;
        let application_key: String = stmt.query_row(&[APPLICATION_KEY], |row| row.get(0))?;
        let api_key: String = stmt.query_row(&[API_KEY], |row| row.get(0))?;
        stmt.finalize()?;

        let client = reqwest::Client::new();

        self.refresh_timeboards(&client, &application_key, &api_key)?;
        self.refresh_screenboards(&client, &application_key, &api_key)?;
        self.refresh_monitors(&client, &application_key, &api_key)?;

        // since this workflow is READ heavy, let's optimize the SQLite indexes and DB
        self.conn
            .execute("VACUUM;", NO_PARAMS)
            .map(|_| Ok(()))
            .map_err(|e| format_err!("failed to VACCUM database: {}", e))?
    }

    fn refresh_timeboards(
        &mut self,
        client: &Client,
        app_key: &str,
        api_key: &str,
    ) -> Result<(), Error> {
        self.conn
            .execute("DELETE FROM timeboards;", NO_PARAMS)
            .map_err(|e| format_err!("failed to delete timeboards: {}", e))?;

        #[derive(Debug, Deserialize)]
        struct Dashboards {
            #[serde(rename = "dashes")]
            boards: Vec<Dashboard>,
        }

        #[derive(Debug, Deserialize)]
        struct Dashboard {
            id: String,
            title: String,
            description: Option<String>,
            modified: DateTime<Utc>,
        }

        let tx = self.conn.transaction()?;
        let mut stmt = tx.prepare("INSERT INTO timeboards (id, title, description, url, modified) VALUES (?1, ?2, ?3, ?4, ?5)")?;

        for board in client
            .get("https://api.datadoghq.com/api/v1/dash")
            .query(&[(APPLICATION_KEY, app_key), (API_KEY, api_key)])
            .send()?
            .json::<Dashboards>()?
            .boards
        {
            let url = format!("https://segment.datadoghq.com/dash/{}", board.id);
            stmt.execute(&[
                &board.id as &ToSql,
                &board.title,
                &board.description.unwrap_or_default(),
                &url,
                &board.modified.timestamp(),
            ])?;
        }
        stmt.finalize()?;
        tx.commit()
            .map_err(|e| format_err!("failed to commit timeboards transaction: {}", e))?;
        Ok(())
    }

    fn refresh_screenboards(
        &mut self,
        client: &Client,
        app_key: &str,
        api_key: &str,
    ) -> Result<(), Error> {
        self.conn
            .execute("DELETE FROM screenboards;", NO_PARAMS)
            .map_err(|e| format_err!("failed to delete screenboards: {}", e))?;

        #[derive(Debug, Deserialize)]
        struct ScreenBoards {
            #[serde(rename = "screenboards")]
            boards: Vec<ScreenBoard>,
        }

        #[derive(Debug, Deserialize)]
        struct ScreenBoard {
            id: i32,
            title: String,
            description: Option<String>,
            modified: DateTime<Utc>,
        }

        let tx = self.conn.transaction()?;
        let mut stmt = tx.prepare("INSERT INTO screenboards (id, title, description, url, modified) VALUES (?1, ?2, ?3, ?4, ?5)")?;

        for board in client
            .get("https://api.datadoghq.com/api/v1/screen")
            .query(&[(APPLICATION_KEY, app_key), (API_KEY, api_key)])
            .send()?
            .json::<ScreenBoards>()?
            .boards
        {
            let url = format!("https://segment.datadoghq.com/screen/{}", board.id);
            stmt.execute(&[
                &board.id as &ToSql,
                &board.title,
                &board.description.unwrap_or_default(),
                &url,
                &board.modified.timestamp(),
            ])?;
        }
        stmt.finalize()?;
        tx.commit()
            .map_err(|e| format_err!("failed to commit  screenboards transaction: {}", e))?;
        Ok(())
    }

    fn refresh_monitors(
        &mut self,
        client: &Client,
        app_key: &str,
        api_key: &str,
    ) -> Result<(), Error> {
        self.conn
            .execute("DELETE FROM monitors;", NO_PARAMS)
            .map_err(|e| format_err!("failed to delete monitors: {}", e))?;

        #[derive(Debug, Deserialize)]
        struct Monitor {
            id: i32,
            name: String,
            tags: Vec<String>,
            modified: DateTime<Utc>,
        }

        let tx = self.conn.transaction()?;
        let mut stmt_monitor =
            tx.prepare("INSERT INTO monitors (id, name, url, modified) VALUES (?1, ?2, ?3, ?4)")?;
        let mut stmt_tags = tx.prepare("INSERT INTO monitor_tags (id, name) VALUES (?1, ?2)")?;

        for monitor in client
            .get("https://api.datadoghq.com/api/v1/monitor")
            .query(&[(APPLICATION_KEY, app_key), (API_KEY, api_key)])
            .send()?
            .json::<Vec<Monitor>>()?
        {
            let url = format!("https://segment.datadoghq.com/monitors/{}", monitor.id);
            stmt_monitor.execute(&[
                &monitor.id as &ToSql,
                &monitor.name,
                &url,
                &monitor.modified.timestamp(),
            ])?;
            for tag in monitor.tags {
                stmt_tags.execute(&[&monitor.id as &ToSql, &tag])?;
            }
        }
        stmt_monitor.finalize()?;
        stmt_tags.finalize()?;
        tx.commit()
            .map_err(|e| format_err!("failed to commit  screenboards transaction: {}", e))?;
        Ok(())
    }

    pub fn query_timeboards<'items>(&self, title: &str) -> Result<Vec<Item<'items>>, Error> {
        let query = format!("%{}%", title);
        self.conn.prepare(
            "SELECT title, description, url FROM timeboards WHERE title LIKE ? ORDER BY modified DESC LIMIT 10",
        )?.query_map(&[&query], |row| {
            let title: String = row.get(0);
            let description: String = row.get(1);
            let url: String = row.get(2);
            alfred::ItemBuilder::new(title.clone())
                .subtitle(description)
                .autocomplete(title)
                .arg(format!("open {}", url))
                .into_item()
        })?
        .collect::<Result<Vec<_>, _>>()
        .map_err(|e| format_err!("failed querying timeboards: {}", e))
    }

    pub fn query_screenboards<'items>(&self, title: &str) -> Result<Vec<Item<'items>>, Error> {
        let query = format!("%{}%", title);
        self.conn.prepare(
            "SELECT title, description, url FROM screenboards WHERE title LIKE ? ORDER BY modified DESC LIMIT 10",
        )?.query_map(&[&query], |row| {
            let title: String = row.get(0);
            let description: String = row.get(1);
            let url: String = row.get(2);
            alfred::ItemBuilder::new(title.clone())
                .subtitle(description)
                .autocomplete(title)
                .arg(format!("open {}", url))
                .into_item()
        })?
        .collect::<Result<Vec<_>, _>>()
        .map_err(|e| format_err!("failed querying screenboards: {}", e))
    }

    pub fn query_dashboards<'items>(&self, title: &str) -> Result<Vec<Item<'items>>, Error> {
        let query = format!("%{}%", title);
        self.conn
            .prepare(
                "SELECT title, description, url, modified FROM timeboards WHERE title LIKE ?1
                 UNION ALL
                 SELECT title, description, url, modified FROM screenboards WHERE title LIKE ?1
                 ORDER BY modified
                 LIMIT 10",
            )?
            .query_map(&[&query], |row| {
                let title: String = row.get(0);
                let description: String = row.get(1);
                let url: String = row.get(2);
                alfred::ItemBuilder::new(title.clone())
                    .subtitle(description)
                    .autocomplete(title)
                    .arg(format!("open {}", url))
                    .into_item()
            })?
            .collect::<Result<Vec<_>, _>>()
            .map_err(|e| format_err!("failed querying dashboards: {}", e))
    }

    pub fn query_monitors<'items>(
        &self,
        name: &str,
        tag: Option<&str>,
    ) -> Result<Vec<Item<'items>>, Error> {
        let query = format!("%{}%", name);
        let tag_query: String;
        let mut params: Vec<&ToSql> = vec![&query];
        let mut select = "SELECT m.name, m.url FROM monitors m ".to_owned();
        match tag {
            Some(ref t) => {
                select += "LEFT JOIN monitor_tags t ON t.id = m.id WHERE m.name LIKE ? AND t.name LIKE ? ";
                tag_query = format!("{}%", t);
                params.push(&tag_query);
            }
            _ => select += "WHERE m.name LIKE ? ",
        }
        select += "ORDER BY m.modified DESC LIMIT 10";

        self.conn
            .prepare(&select)?
            .query_map(&params, |row| {
                let name: String = row.get(0);
                let url: String = row.get(1);
                alfred::ItemBuilder::new(name.clone())
                    .subtitle(name.clone())
                    .autocomplete(name)
                    .arg(format!("open {}", url))
                    .into_item()
            })?
            .collect::<Result<Vec<_>, _>>()
            .map_err(|e| format_err!("failed querying monitors: {}", e))
    }
}