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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
use chrono::{DateTime, Duration, Utc};
use http::header::HeaderValue;
use num_enum::{IntoPrimitive, TryFromPrimitive};
use serde::{
    de::{self, Error as SerdeError, Unexpected, Visitor},
    Deserialize, Deserializer, Serialize, Serializer,
};
use serde_json::value::Value as JsonValue;
use std::{
    collections::HashMap,
    convert::TryFrom,
    fmt::{self, Display},
    ops::Add,
    str::FromStr,
};
use thiserror::Error;

use crate::serde::{deserialize_null_default, empty_string_as_none};

/// The default field the server looks for a time to use as
/// ingestion time. If not present, the server will set the ingestion time by
/// itself.
pub static TIMESTAMP_FIELD: &str = "_time";

/// All supported content types.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ContentType {
    /// JSON treats the data as JSON array.
    Json,
    /// NDJSON treats the data as newline delimited JSON objects. Preferred
    /// format.
    NdJson,
    /// CSV treats the data as CSV content.
    Csv,
}

impl ContentType {
    pub fn as_str(&self) -> &'static str {
        match self {
            ContentType::Json => "application/json",
            ContentType::NdJson => "application/x-ndjson",
            ContentType::Csv => "text/csv",
        }
    }
}

impl Display for ContentType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

impl FromStr for ContentType {
    type Err = crate::error::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "application/json" => Ok(ContentType::Json),
            "application/x-ndjson" => Ok(ContentType::NdJson),
            "text/csv" => Ok(ContentType::Csv),
            _ => Err(crate::error::Error::InvalidContentType(s.to_string())),
        }
    }
}

impl From<ContentType> for HeaderValue {
    fn from(content_type: ContentType) -> Self {
        HeaderValue::from_static(content_type.as_str())
    }
}

/// All supported content encoding
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ContentEncoding {
    /// Identity marks the data as not being encoded.
    Identity,
    /// GZIP marks the data as being gzip encoded.
    Gzip,
    /// Zstd marks the data as being zstd encoded.
    Zstd,
}

impl ContentEncoding {
    pub fn as_str(&self) -> &'static str {
        match self {
            ContentEncoding::Identity => "",
            ContentEncoding::Gzip => "gzip",
            ContentEncoding::Zstd => "zstd",
        }
    }
}

impl Display for ContentEncoding {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

impl FromStr for ContentEncoding {
    type Err = crate::error::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "" => Ok(ContentEncoding::Identity),
            "gzip" => Ok(ContentEncoding::Gzip),
            "zstd" => Ok(ContentEncoding::Zstd),
            _ => Err(crate::error::Error::InvalidContentEncoding(s.to_string())),
        }
    }
}

impl From<ContentEncoding> for HeaderValue {
    fn from(content_encoding: ContentEncoding) -> Self {
        HeaderValue::from_static(content_encoding.as_str())
    }
}

/// An Axiom dataset.
#[derive(Serialize, Deserialize, Debug)]
pub struct Dataset {
    /// The name of the dataset.
    pub name: String,
    /// The description of the dataset.
    pub description: String,
    /// The ID of the user who created the dataset.
    #[serde(rename = "who")]
    pub created_by: String,
    /// The time the dataset was created at.
    #[serde(rename = "created")]
    pub created_at: DateTime<Utc>,
    // ignored: integrationConfigs, integrationFilters, quickQueries
}

/// A field of an Axiom dataset.
#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct Field {
    /// Name is the unique name of the field.
    pub name: String,
    /// Description is the description of the field.
    pub description: String,
    /// Type is the datatype of the field.
    #[serde(rename = "type")]
    pub typ: String,
    /// Unit is the unit of the field.
    pub unit: String,
    /// Hidden describes if the field is hidden or not.
    pub hidden: bool,
}

/// Details of the information stored in a dataset.
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Stat {
    /// The unique name of the dataset.
    pub name: String,
    /// The number of blocks of the dataset.
    pub num_blocks: u64,
    /// The number of events of the dataset.
    pub num_events: u64,
    /// The number of fields of the dataset.
    pub num_fields: u32,
    /// The amount of data stored in the dataset.
    pub input_bytes: u64,
    /// The amount of data stored in the dataset formatted in a human
    /// readable format.
    pub input_bytes_human: String,
    /// The amount of compressed data stored in the dataset.
    pub compressed_bytes: u64,
    /// The amount of compressed data stored in the
    /// dataset formatted in a human readable format.
    pub compressed_bytes_human: String,
    /// The time of the oldest event stored in the dataset.
    pub min_time: Option<DateTime<Utc>>,
    /// The time of the newest event stored in the dataset.
    pub max_time: Option<DateTime<Utc>>,
    /// The ID of the user who created the dataset.
    #[serde(rename = "who")]
    pub created_by: Option<String>,
    /// The time the dataset was created at.
    #[serde(rename = "created")]
    pub created_at: DateTime<Utc>,
}

/// Details of the information stored inside a dataset including the fields.
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Info {
    /// The stats of the dataset.
    #[serde(flatten)]
    pub stat: Stat,
    /// The fields of the dataset.
    pub fields: Vec<Field>,
}

#[derive(Serialize, Debug)]
pub(crate) struct TrimRequest {
    #[serde(rename = "maxDuration")]
    max_duration: String,
}

impl TrimRequest {
    pub(crate) fn new(duration: Duration) -> Self {
        TrimRequest {
            max_duration: format!("{}s", duration.num_seconds()),
        }
    }
}

/// The result of a trim operation.
#[derive(Deserialize, Debug)]
pub struct TrimResult {
    /// The amount of blocks deleted by the trim operation.
    #[serde(rename = "numDeleted")]
    pub blocks_deleted: u64,
}

/// Returned on event ingestion operation.
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub struct IngestStatus {
    /// Amount of events that have been ingested.
    pub ingested: u64,
    /// Amount of events that failed to ingest.
    pub failed: u64,
    /// Ingestion failures, if any.
    pub failures: Vec<IngestFailure>,
    /// Number of bytes processed.
    pub processed_bytes: u64,
    /// Amount of blocks created.
    pub blocks_created: u32,
    /// The length of the Write-Ahead Log.
    pub wal_length: u32,
}

impl Add for IngestStatus {
    type Output = Self;

    fn add(self, other: Self) -> Self {
        let mut failures = self.failures;
        failures.extend(other.failures);

        Self {
            ingested: self.ingested + other.ingested,
            failed: self.failed + other.failed,
            failures,
            processed_bytes: self.processed_bytes + other.processed_bytes,
            blocks_created: self.blocks_created + other.blocks_created,
            wal_length: other.wal_length,
        }
    }
}

/// Ingestion failure of a single event.
#[derive(Serialize, Deserialize, Debug)]
pub struct IngestFailure {
    /// Timestamp of the event that failed to ingest.
    pub timestamp: DateTime<Utc>,
    /// Error that made the event fail to ingest.
    pub error: String,
}

/// Used to create a dataset.
#[derive(Serialize, Debug)]
pub struct DatasetCreateRequest {
    /// Restricted to 128 bytes of [a-zA-Z0-9] and special characters "-", "_"
    /// and ".". Special characters cannot be a prefix or suffix. The prefix
    /// cannot be "axiom-".
    pub name: String,
    /// Description of the dataset.
    pub description: String,
}

/// Used to update a dataset.
#[derive(Serialize, Deserialize, Debug)]
pub struct DatasetUpdateRequest {
    /// Description of the dataset to update.
    pub description: String,
}

/// A query that gets executed on a dataset.
/// If you're looking for the analytics, check out [`Query`].
#[derive(Serialize, Deserialize, Debug, Default, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct AplQuery {
    pub apl: String,
    pub start_time: Option<DateTime<Utc>>,
    pub end_time: Option<DateTime<Utc>>,
}

// AplQueryParams is the part of `AplOptions` that is added to the request url.
#[derive(Serialize, Debug, Default)]
pub(crate) struct AplQueryParams {
    #[serde(rename = "nocache")]
    pub no_cache: bool,
    #[serde(rename = "saveAsKind")]
    pub save: bool,
    pub format: AplResultFormat,
}

/// The optional parameters to APL query methods.
#[derive(Debug)]
pub struct AplOptions {
    /// The start time of the query.
    pub start_time: Option<DateTime<Utc>>,
    // The end time of the query.
    pub end_time: Option<DateTime<Utc>>,

    // Omits the query cache.
    pub no_cache: bool,
    /// Save the query on the server, if set to `true`. The ID of the saved query
    /// is returned with the query result as part of the response.
    // NOTE: The server automatically sets the query kind to "apl" for queries
    // going // to the "/_apl" query endpoint. This allows us to set any value
    // for the // `saveAsKind` query param. For user experience, we use a bool
    // here instead of forcing the user to set the value to `query.APL`.
    pub save: bool,
    // Format specifies the format of the APL query. Defaults to Legacy.
    pub format: AplResultFormat,
}

impl Default for AplOptions {
    fn default() -> Self {
        AplOptions {
            start_time: None,
            end_time: None,
            no_cache: false,
            save: false,
            format: AplResultFormat::Legacy,
        }
    }
}

/// The result format of an APL query.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum AplResultFormat {
    Legacy,
}

impl Serialize for AplResultFormat {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match self {
            AplResultFormat::Legacy => serializer.serialize_str("legacy"),
        }
    }
}

impl Default for AplResultFormat {
    fn default() -> Self {
        AplResultFormat::Legacy
    }
}

/// The kind of a query.
#[derive(Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum QueryKind {
    Analytics,
    Stream,
    Apl, // Read-only, don't use this for requests.
}

impl Serialize for QueryKind {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match self {
            QueryKind::Analytics => serializer.serialize_str("analytics"),
            QueryKind::Stream => serializer.serialize_str("stream"),
            QueryKind::Apl => serializer.serialize_str("apl"),
        }
    }
}

impl<'de> Deserialize<'de> for QueryKind {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        match String::deserialize(deserializer)?.as_str() {
            "analytics" => Ok(QueryKind::Analytics),
            "stream" => Ok(QueryKind::Stream),
            "apl" => Ok(QueryKind::Apl),
            _ => Err(D::Error::custom("unknown query kind")),
        }
    }
}

impl Default for QueryKind {
    fn default() -> Self {
        QueryKind::Analytics
    }
}

/// A query that gets executed on a dataset.
/// If you're looking for the APL query, check out [`AplQuery`].
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct Query {
    /// Start time of the query.
    #[serde(deserialize_with = "empty_string_as_none")]
    pub start_time: Option<DateTime<Utc>>,
    /// End time of the query.
    #[serde(deserialize_with = "empty_string_as_none")]
    pub end_time: Option<DateTime<Utc>>,
    /// Resolution of the queries graph. Valid values are the queries time
    /// range / 100 at maximum and / 1000 at minimum. Use zero value for
    /// serve-side auto-detection.
    #[serde(default)]
    pub resolution: String, // TODO: Implement custom type to {de,}serialize to/from go string
    /// Aggregations performed as part of the query.
    #[serde(default, deserialize_with = "deserialize_null_default")]
    pub aggregations: Vec<Aggregation>,
    /// Filter applied on the queried results.
    pub filter: Option<Filter>,
    /// Field names to group the query results by.
    #[serde(default, deserialize_with = "deserialize_null_default")]
    pub group_by: Vec<String>,
    /// Order rules that specify the order of the query result.
    #[serde(default, deserialize_with = "deserialize_null_default")]
    pub order: Vec<Order>,
    /// Number of results returned from the query.
    #[serde(default)]
    pub limit: u32,
    /// Virtual fields that can be referenced by aggregations, filters and
    /// orders.
    #[serde(default, deserialize_with = "deserialize_null_default")]
    pub virtual_fields: Vec<VirtualField>,
    #[serde(default, deserialize_with = "deserialize_null_default")]
    pub projections: Vec<Projection>,
    /// The query cursor. Should be set to the cursor returned with a previous
    /// query result, if it was parital.
    #[serde(default)]
    pub cursor: String,
    /// Return the Cursor as part of the query result.
    #[serde(default)]
    pub include_cursor: bool,
    /// Used to get more results of a previous query. It is not valid for starred
    /// queries or otherwise stored queries.
    #[serde(default)]
    pub continuation_token: String,
}

impl Default for Query {
    fn default() -> Self {
        Query {
            start_time: None,
            end_time: None,
            resolution: "".to_string(),
            aggregations: vec![],
            filter: None,
            group_by: vec![],
            order: vec![],
            limit: 0,
            virtual_fields: vec![],
            projections: vec![],
            cursor: "".to_string(),
            include_cursor: false,
            continuation_token: "".to_string(),
        }
    }
}

/// A field that is projected to the query result.
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
pub struct Projection {
    /// The name of the field to project.
    pub field: String,
    /// The alias to reference the projected field by.
    pub alias: Option<String>,
}

/// Supported aggregation operations.
#[derive(Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum AggregationOp {
    Count,
    CountDistinct,

    // Only works for numbers.
    Sum,
    Avg,
    Min,
    Max,
    Topk,
    Percentiles,
    Histogram,
}

impl Serialize for AggregationOp {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(match self {
            AggregationOp::Count => "count",
            AggregationOp::CountDistinct => "distinct",
            AggregationOp::Sum => "sum",
            AggregationOp::Avg => "avg",
            AggregationOp::Min => "min",
            AggregationOp::Max => "max",
            AggregationOp::Topk => "topk",
            AggregationOp::Percentiles => "percentiles",
            AggregationOp::Histogram => "histogram",
        })
    }
}

struct AggregationOpVisitor;

impl<'de> Visitor<'de> for AggregationOpVisitor {
    type Value = AggregationOp;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        write!(formatter, "a valid aggregation op string")
    }

    fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        match s {
            "count" => Ok(AggregationOp::Count),
            "distinct" => Ok(AggregationOp::CountDistinct),
            "sum" => Ok(AggregationOp::Sum),
            "avg" => Ok(AggregationOp::Avg),
            "min" => Ok(AggregationOp::Min),
            "max" => Ok(AggregationOp::Max),
            "topk" => Ok(AggregationOp::Topk),
            "percentiles" => Ok(AggregationOp::Percentiles),
            "histogram" => Ok(AggregationOp::Histogram),
            _ => Err(de::Error::invalid_value(Unexpected::Str(s), &self)),
        }
    }
}

impl<'de> Deserialize<'de> for AggregationOp {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_str(AggregationOpVisitor {})
    }
}

/// Aggregations are applied to a query.
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
pub struct Aggregation {
    /// The alias for the aggregation.
    pub alias: Option<String>,
    /// The operation of the aggregation.
    pub op: AggregationOp,
    /// The field to aggregate on.
    pub field: String,
    /// Argument to the aggregation.
    /// Only valid for `OpCountDistinctIf`, `OpTopk`, `OpPercentiles` and
    /// `OpHistogram` aggregations.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub argument: Option<JsonValue>,
}

/// Supported filter operations.
#[derive(Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum FilterOp {
    And,
    Or,
    Not,

    // Works for strings and numbers.
    Equal,
    NotEqual,
    Exists,
    NotExists,

    // Only works for numbers.
    GreaterThan,
    GreaterThanEqual,
    LessThan,
    LessThanEqual,

    // Only works for strings.
    StartsWith,
    NotStartsWith,
    EndsWith,
    NotEndsWith,
    Regexp,
    NotRegexp,

    // Works for strings and arrays.
    Contains,
    NotContains,
}

impl Serialize for FilterOp {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(match self {
            FilterOp::And => "and",
            FilterOp::Or => "or",
            FilterOp::Not => "not",
            FilterOp::Equal => "==",
            FilterOp::NotEqual => "!=",
            FilterOp::Exists => "exists",
            FilterOp::NotExists => "not-exists",
            FilterOp::GreaterThan => ">",
            FilterOp::GreaterThanEqual => ">=",
            FilterOp::LessThan => "<",
            FilterOp::LessThanEqual => "<=",
            FilterOp::StartsWith => "starts-with",
            FilterOp::NotStartsWith => "not-starts-with",
            FilterOp::EndsWith => "ends-with",
            FilterOp::NotEndsWith => "not-ends-with",
            FilterOp::Regexp => "regexp",
            FilterOp::NotRegexp => "not-regexp",
            FilterOp::Contains => "contains",
            FilterOp::NotContains => "not-contains",
        })
    }
}

struct FilterOpVisitor;

impl<'de> Visitor<'de> for FilterOpVisitor {
    type Value = FilterOp;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        write!(formatter, "a valid filter op string")
    }

    fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        match s {
            "and" => Ok(FilterOp::And),
            "or" => Ok(FilterOp::Or),
            "not" => Ok(FilterOp::Not),
            "==" => Ok(FilterOp::Equal),
            "!=" => Ok(FilterOp::NotEqual),
            "exists" => Ok(FilterOp::Exists),
            "not-exists" => Ok(FilterOp::NotExists),
            ">" => Ok(FilterOp::GreaterThan),
            ">=" => Ok(FilterOp::GreaterThanEqual),
            "<" => Ok(FilterOp::LessThan),
            "<=" => Ok(FilterOp::LessThanEqual),
            "starts-with" => Ok(FilterOp::StartsWith),
            "not-starts-with" => Ok(FilterOp::NotStartsWith),
            "ends-with" => Ok(FilterOp::EndsWith),
            "not-ends-with" => Ok(FilterOp::NotEndsWith),
            "regexp" => Ok(FilterOp::Regexp),
            "not-regexp" => Ok(FilterOp::NotRegexp),
            "contains" => Ok(FilterOp::Contains),
            "not-contains" => Ok(FilterOp::NotContains),
            _ => Err(de::Error::invalid_value(Unexpected::Str(s), &self)),
        }
    }
}

impl<'de> Deserialize<'de> for FilterOp {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_str(FilterOpVisitor {})
    }
}

/// A filter is applied to a query.
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct Filter {
    pub op: FilterOp,
    pub field: String,
    pub value: JsonValue,
    #[serde(default)]
    pub case_insensitive: bool,
    #[serde(default, deserialize_with = "deserialize_null_default")]
    pub children: Vec<Filter>,
}

impl Default for Filter {
    fn default() -> Self {
        Filter {
            op: FilterOp::Equal,
            field: "".to_string(),
            value: JsonValue::Null,
            case_insensitive: false,
            children: vec![],
        }
    }
}

/// Specifies the order a queries result will be in.
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
pub struct Order {
    /// Field to order on.
    pub field: String,
    /// If the field is ordered desending.
    pub desc: bool,
}

/// A VirtualField is not part of a dataset and its value is derived from an
/// expression. Aggregations, filters and orders can reference this field like
/// any other field.
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
pub struct VirtualField {
    /// Alias the virtual field is referenced by.
    pub alias: String,
    /// Expression which specifies the virtual fields value.
    pub expr: String,
}

/// The parameters for a query.
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct QueryOptions {
    #[serde(rename = "streaming-duration")]
    pub streaming_duration: Option<String>, // TODO: Implement custom type to {de,}serialize to/from go string
    #[serde(rename = "no-cache")]
    pub no_cache: bool,
    #[serde(rename = "saveAsKind")]
    pub save_as_kind: QueryKind,
}

/// The result of an APL query. It embeds the APL request in the result it
/// created.
#[derive(Serialize, Deserialize, Debug)]
pub struct AplQueryResult {
    /// The query request.
    pub request: Query,
    // NOTE: The following is copied from QueryResult. Maybe we should have a macro?
    /// The status of the query result.
    pub status: QueryStatus,
    /// The events that matched the query.
    #[serde(default, deserialize_with = "deserialize_null_default")]
    pub matches: Vec<Entry>,
    /// The time series buckets.
    pub buckets: Timeseries,
    /// The ID of the query that generated this result when it was saved on the
    /// server. This is only set when the query was send with the `SaveKind`
    /// option specified.
    #[serde(skip)]
    pub saved_query_id: Option<String>,
}

/// The result of a query.
#[derive(Serialize, Deserialize, Debug)]
pub struct QueryResult {
    /// The status of the query result.
    pub status: QueryStatus,
    /// The events that matched the query.
    pub matches: Vec<Entry>,
    /// The time series buckets.
    pub buckets: Timeseries,
    /// The ID of the query that generated this result when it was saved on the
    /// server. This is only set when the query was send with the `SaveKind`
    /// option specified.
    #[serde(skip)]
    pub saved_query_id: Option<String>,
}

/// The status of a query result.
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct QueryStatus {
    /// The duration it took the query to execute.
    pub elapsed_time: u64,
    /// The amount of blocks that have been examined by the query.
    pub blocks_examined: u64,
    /// The amount of rows that have been examined by the query.
    pub rows_examined: u64,
    /// The amount of rows that matched the query.
    pub rows_matched: u64,
    /// The amount of groups returned by the query.
    pub num_groups: u32,
    /// True if the query result is a partial result.
    pub is_partial: bool,
    /// Populated when IsPartial is true, must be passed to the next query
    /// request to retrieve the next result set.
    pub continuation_token: Option<String>,
    /// True if the query result is estimated.
    #[serde(default)]
    pub is_estimate: bool,
    /// The status of the cache.
    pub cache_status: CacheStatus,
    /// The timestamp of the oldest block examined.
    pub min_block_time: DateTime<Utc>,
    /// The timestamp of the newest block examined.
    pub max_block_time: DateTime<Utc>,
    /// Messages associated with the query.
    #[serde(default, deserialize_with = "deserialize_null_default")]
    pub messages: Vec<QueryMessage>,
}

/// The cache status of the query.
#[derive(IntoPrimitive, TryFromPrimitive, Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
#[non_exhaustive]
pub enum CacheStatus {
    Miss = 1,
    Materialized = 2, // Filtered rows
    Results = 4,      // Aggregated and grouped records
}

impl Serialize for CacheStatus {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_u8((*self).into())
    }
}

impl<'de> Deserialize<'de> for CacheStatus {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value: u8 = Deserialize::deserialize(deserializer)?;
        Self::try_from(value).map_err(serde::de::Error::custom)
    }
}

/// A message that is returned in the status of a query.
#[derive(Serialize, Deserialize, Debug)]
pub struct QueryMessage {
    priority: QueryMessagePriority,
    count: u32,
    code: QueryMessageCode,
    text: Option<String>,
}

/// The priority of a query message.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum QueryMessagePriority {
    Trace,
    Debug,
    Info,
    Warn,
    Error,
    Fatal,
}

impl std::fmt::Display for QueryMessagePriority {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            QueryMessagePriority::Trace => "trace",
            QueryMessagePriority::Debug => "debug",
            QueryMessagePriority::Info => "info",
            QueryMessagePriority::Warn => "warn",
            QueryMessagePriority::Error => "error",
            QueryMessagePriority::Fatal => "fatal",
        })
    }
}

#[derive(Error, Debug)]
pub enum ParseQueryMessagePriorityError {
    #[error("Unknown item: {0}")]
    UnknownItem(String),
}

impl TryFrom<&str> for QueryMessagePriority {
    type Error = ParseQueryMessagePriorityError;

    fn try_from(s: &str) -> Result<Self, <QueryMessagePriority as TryFrom<&str>>::Error> {
        match s {
            "trace" => Ok(QueryMessagePriority::Trace),
            "debug" => Ok(QueryMessagePriority::Debug),
            "info" => Ok(QueryMessagePriority::Info),
            "warn" => Ok(QueryMessagePriority::Warn),
            "error" => Ok(QueryMessagePriority::Error),
            "fatal" => Ok(QueryMessagePriority::Fatal),
            item => Err(ParseQueryMessagePriorityError::UnknownItem(
                item.to_string(),
            )),
        }
    }
}

impl Serialize for QueryMessagePriority {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(self.to_string().as_str())
    }
}

impl<'de> Deserialize<'de> for QueryMessagePriority {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value: &str = Deserialize::deserialize(deserializer)?;
        Self::try_from(value).map_err(serde::de::Error::custom)
    }
}

/// The code of a message that is returned in the status of a query.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum QueryMessageCode {
    Unknown,
    VirtualFieldFinalizeError,
    MissingColumn,
    DefaultLimitWarning,
    LicenseLimitForQueryWarning,
}

impl std::fmt::Display for QueryMessageCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            QueryMessageCode::Unknown => "unknown",
            QueryMessageCode::VirtualFieldFinalizeError => "virtual_field_finalize_error",
            QueryMessageCode::MissingColumn => "missing_column",
            QueryMessageCode::DefaultLimitWarning => "default_limit_warning",
            QueryMessageCode::LicenseLimitForQueryWarning => "license_limit_for_query_warning",
        })
    }
}

impl From<&str> for QueryMessageCode {
    fn from(s: &str) -> Self {
        match s {
            "virtual_field_finalize_error" => QueryMessageCode::VirtualFieldFinalizeError,
            "missing_column" => QueryMessageCode::MissingColumn,
            "default_limit_warning" => QueryMessageCode::DefaultLimitWarning,
            "license_limit_for_query_warning" => QueryMessageCode::LicenseLimitForQueryWarning,
            _ => QueryMessageCode::Unknown,
        }
    }
}

impl Serialize for QueryMessageCode {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(self.to_string().as_str())
    }
}

impl<'de> Deserialize<'de> for QueryMessageCode {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value: &str = Deserialize::deserialize(deserializer)?;
        Ok(Self::from(value))
    }
}

/// An event that matched a query and is thus part of the result set.
#[derive(Serialize, Deserialize, Debug)]
pub struct Entry {
    /// The time the event occurred. Matches SysTime if not specified during
    /// ingestion.
    #[serde(rename = "_time")]
    pub time: DateTime<Utc>,
    /// The time the event was recorded on the server.
    #[serde(rename = "_sysTime")]
    pub sys_time: DateTime<Utc>,
    /// The unique ID of the event row.
    #[serde(rename = "_rowId")]
    pub row_id: String,
    /// Contains the raw data of the event (with filters and aggregations
    /// applied).
    pub data: HashMap<String, JsonValue>,
}

/// A queried time series.
#[derive(Serialize, Deserialize, Debug)]
pub struct Timeseries {
    /// The intervals that build a time series.
    pub series: Vec<Interval>,
    /// The totals of the time series.
    pub totals: Vec<EntryGroup>,
}

/// The interval of queried time series.
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Interval {
    pub start_time: DateTime<Utc>,
    pub end_time: DateTime<Utc>,
    #[serde(default, deserialize_with = "deserialize_null_default")]
    pub groups: Vec<EntryGroup>,
}

/// A group of queried event.
#[derive(Serialize, Deserialize, Debug)]
pub struct EntryGroup {
    pub id: u64,
    pub group: HashMap<String, JsonValue>,
    pub aggregations: Vec<EntryGroupAgg>,
}

/// An aggregation which is part of a group of queried events.
#[derive(Serialize, Deserialize, Debug)]
pub struct EntryGroupAgg {
    #[serde(rename = "op")]
    pub alias: String,
    pub value: JsonValue,
}

#[cfg(test)]
mod test {
    use super::*;
    use serde_test::{assert_de_tokens, assert_tokens, Token};

    #[test]
    fn test_aggregation_op() {
        let count = AggregationOp::Count;
        assert_tokens(&count, &[Token::Str("count")]);
        assert_de_tokens(&count, &[Token::Str("count")]);
    }

    #[test]
    fn test_filter_op() {
        let and = FilterOp::And;
        assert_tokens(&and, &[Token::Str("and")]);
        assert_de_tokens(&and, &[Token::Str("and")]);
    }
}