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
use bytes::Bytes;
use flate2::{write::GzEncoder, Compression};
use futures::{Stream, StreamExt};
use reqwest::{header, Body};
use serde::Serialize;
use std::{
    convert::{TryFrom, TryInto},
    io::Write,
    result::Result as StdResult,
    time::Duration as StdDuration,
};

use crate::{
    datasets::model::*,
    error::{Error, Result},
    http::{self, Response},
};

/// Provides methods to work with Axiom datasets, including ingesting and
/// querying.
pub struct Client {
    http_client: http::Client,
}

impl Client {
    pub(crate) fn new(http_client: http::Client) -> Self {
        Self { http_client }
    }

    /// Executes the given query specified using the Axiom Processing Language (APL).
    pub async fn apl_query<S, O>(&self, apl: S, opts: O) -> Result<AplQueryResult>
    where
        S: Into<String>,
        O: Into<Option<AplOptions>>,
    {
        let (req, query_params) = match opts.into() {
            Some(opts) => {
                let req = AplQuery {
                    apl: apl.into(),
                    start_time: opts.start_time,
                    end_time: opts.end_time,
                };

                let query_params = AplQueryParams {
                    no_cache: opts.no_cache,
                    save: opts.save,
                    format: opts.format,
                };

                (req, query_params)
            }
            None => (
                AplQuery {
                    apl: apl.into(),
                    ..Default::default()
                },
                AplQueryParams::default(),
            ),
        };

        let query_params = serde_qs::to_string(&query_params)?;
        let path = format!("/datasets/_apl?{}", query_params);
        let res = self.http_client.post(path, &req).await?;

        let saved_query_id = res
            .headers()
            .get("X-Axiom-History-Query-Id")
            .map(|s| s.to_str())
            .transpose()
            .map_err(|_e| Error::InvalidQueryId)?
            .map(|s| s.to_string());

        let mut result = res.json::<AplQueryResult>().await?;
        result.saved_query_id = saved_query_id;

        Ok(result)
    }

    /// Create a dataset with the given name and description.
    pub async fn create<N, D>(&self, dataset_name: N, description: D) -> Result<Dataset>
    where
        N: Into<String>,
        D: Into<String>,
    {
        let req = DatasetCreateRequest {
            name: dataset_name.into(),
            description: description.into(),
        };
        self.http_client.post("/datasets", &req).await?.json().await
    }

    /// Delete the dataset with the given ID.
    pub async fn delete<N: Into<String>>(&self, dataset_name: N) -> Result<()> {
        self.http_client
            .delete(format!("/datasets/{}", dataset_name.into()))
            .await
    }

    /// Get a dataset by its id.
    pub async fn get<N: Into<String>>(&self, dataset_name: N) -> Result<Dataset> {
        self.http_client
            .get(format!("/datasets/{}", dataset_name.into()))
            .await?
            .json()
            .await
    }

    /// Retrieve the query stored inside the query history dataset identified by its id.
    pub async fn history<I: Into<String>>(&self, query_id: I) -> Result<HistoryQuery> {
        self.http_client
            .get(format!("/datasets/_history/{}", query_id.into()))
            .await?
            .json()
            .await
    }

    /// Retrieve the information of the dataset identified by its id.
    pub async fn info<N: Into<String>>(&self, dataset_name: N) -> Result<Info> {
        self.http_client
            .get(format!("/datasets/{}/info", dataset_name.into()))
            .await?
            .json()
            .await
    }

    /// Ingest events into the dataset identified by its id.
    /// Restrictions for field names (JSON object keys) can be reviewed here:
    /// <https://www.axiom.co/docs/usage/field-restrictions>.
    pub async fn ingest<N, I, E>(&self, dataset_name: N, events: I) -> Result<IngestStatus>
    where
        N: Into<String>,
        I: IntoIterator<Item = E>,
        E: Serialize,
    {
        let events: Vec<E> = events.into_iter().collect();
        let json_payload = serde_json::to_vec(&events)?;
        let mut gzip_payload = GzEncoder::new(Vec::new(), Compression::default());
        gzip_payload
            .write_all(&json_payload)
            .map_err(Error::Encoding)?;
        let payload = gzip_payload.finish().map_err(Error::Encoding)?;

        self.ingest_raw(
            dataset_name,
            payload,
            ContentType::Json,
            ContentEncoding::Gzip,
        )
        .await
    }

    /// Ingest data into the dataset identified by its id.
    /// Restrictions for field names (JSON object keys) can be reviewed here:
    /// <https://www.axiom.co/docs/usage/field-restrictions>.
    pub async fn ingest_raw<N, P>(
        &self,
        dataset_name: N,
        payload: P,
        content_type: ContentType,
        content_encoding: ContentEncoding,
    ) -> Result<IngestStatus>
    where
        N: Into<String>,
        P: Into<Bytes>,
    {
        let mut request = self
            .http_client
            .post_builder(format!("/datasets/{}/ingest", dataset_name.into()))
            .header(header::CONTENT_TYPE, content_type);

        // Add Content-Encoding header if necessary
        request = match content_encoding {
            ContentEncoding::Identity => request,
            _ => request.header(header::CONTENT_ENCODING, content_encoding),
        };

        request
            .body(payload.into())
            .send()
            .await
            .map(Response::new)
            .map_err(Error::Http)?
            .json()
            .await
    }

    // Ingest a stream of events into a dataset.
    /// Restrictions for field names (JSON object keys) can be reviewed here:
    /// <https://www.axiom.co/docs/usage/field-restrictions>.
    pub async fn ingest_stream<N, S, E>(&self, dataset_name: N, stream: S) -> Result<IngestStatus>
    where
        N: Into<String>,
        S: Stream<Item = E> + Send + Sync + 'static,
        E: Serialize,
    {
        let bytes_stream = stream.map(|item| {
            serde_json::to_vec(&item).map(|mut v| {
                v.push(b'\n');
                Bytes::from(v)
            })
        });
        let body = Body::wrap_stream(bytes_stream);

        self.http_client
            .post_builder(format!("/datasets/{}/ingest", dataset_name.into()))
            .header(header::CONTENT_TYPE, ContentType::NdJson)
            .body(body)
            .send()
            .await
            .map(Response::new)
            .map_err(Error::Http)?
            .json()
            .await
    }

    /// List all available datasets.
    pub async fn list(&self) -> Result<Vec<Dataset>> {
        self.http_client.get("/datasets").await?.json().await
    }

    /// Execute the given query on the dataset identified by its id.
    pub async fn query<N, O>(&self, dataset_name: N, query: Query, opts: O) -> Result<QueryResult>
    where
        N: Into<String>,
        O: Into<Option<QueryOptions>>,
    {
        let path = format!(
            "/datasets/{}/query?{}",
            dataset_name.into(),
            &opts
                .into()
                .map(|opts| { serde_qs::to_string(&opts) })
                .unwrap_or_else(|| Ok(String::new()))?
        );
        let res = self.http_client.post(path, &query).await?;

        let saved_query_id = res
            .headers()
            .get("X-Axiom-History-Query-Id")
            .map(|s| s.to_str())
            .transpose()
            .map_err(|_e| Error::InvalidQueryId)?
            .map(|s| s.to_string());
        let mut result = res.json::<QueryResult>().await?;
        result.saved_query_id = saved_query_id;

        Ok(result)
    }

    /// Trim the dataset identified by its id to a given length.
    /// The max duration given will mark the oldest timestamp an event can have.
    /// Older ones will be deleted from the dataset.
    /// The duration can either be a [`std::time::Duration`] or a
    /// [`chrono::Duration`].
    pub async fn trim<N, D>(&self, dataset_name: N, duration: D) -> Result<TrimResult>
    where
        N: Into<String>,
        D: TryInto<Duration, Error = Error>,
    {
        let duration = duration.try_into()?;
        let req = TrimRequest::new(duration.into());
        self.http_client
            .post(format!("/datasets/{}/trim", dataset_name.into()), &req)
            .await?
            .json()
            .await
    }

    /// Update a dataset.
    pub async fn update<N: Into<String>>(
        &self,
        dataset_name: N,
        req: DatasetUpdateRequest,
    ) -> Result<Dataset> {
        self.http_client
            .put(format!("/datasets/{}", dataset_name.into()), &req)
            .await?
            .json()
            .await
    }
}

pub struct Duration {
    inner: chrono::Duration,
}

impl From<Duration> for chrono::Duration {
    fn from(duration: Duration) -> Self {
        duration.inner
    }
}

impl TryFrom<chrono::Duration> for Duration {
    type Error = Error;

    fn try_from(duration: chrono::Duration) -> StdResult<Self, Self::Error> {
        Ok(Duration { inner: duration })
    }
}

impl TryFrom<StdDuration> for Duration {
    type Error = Error;

    fn try_from(duration: StdDuration) -> StdResult<Self, Self::Error> {
        Ok(Duration {
            inner: chrono::Duration::from_std(duration).map_err(|_| Error::DurationOutOfRange)?,
        })
    }
}