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
//! Contains the `Field` struct, which holds actual data in the form of Arrow arrays, as well as column-specific metadata.
use std::{
    collections::{BTreeMap, HashMap},
    iter::FromIterator,
};

use arrow2::{
    array::Array,
    datatypes::{DataType, Field as ArrowField, TimeUnit},
};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use serde_with::skip_serializing_none;

use crate::data::{
    error,
    field_type::{FieldType, IntoFieldType},
    ConfFloat64,
};

/// A typed column within a [`Frame`][crate::data::Frame].
///
/// The underlying data for this field can be read using the [`Field::values`] method,
/// and updated using the [`Field::set_values`] and [`Field::set_values_opt`] methods.
#[derive(Debug)]
pub struct Field {
    /// The name of this field.
    ///
    /// Fields within a [`Frame`][crate::data::Frame] are not required to have unique names, but
    /// the combination of `name` and `labels` should be unique within a frame
    /// to ensure proper behaviour in all situations.
    pub name: String,
    /// An optional set of key-value pairs that, combined with the name, should uniquely identify a field within a [`Frame`][crate::data::Frame].
    pub labels: BTreeMap<String, String>,
    /// Optional display configuration used by Grafana.
    pub config: Option<FieldConfig>,

    /// The actual values of this field.
    ///
    /// The types of values contained within the `Array` MUST match the
    /// type information in `type_info` at all times. The various `into_field`-like
    /// functions and the `set_values` methods should ensure this.
    pub(crate) values: Box<dyn Array>,
    /// Type information for this field, as understood by Grafana.
    pub(crate) type_info: TypeInfo,
}

impl Field {
    pub(crate) fn to_arrow_field(&self) -> Result<ArrowField, serde_json::Error> {
        let metadata = match (self.labels.is_empty(), self.config.as_ref()) {
            (true, None) => Default::default(),
            (false, None) => [("labels".to_string(), serde_json::to_string(&self.labels)?)]
                .into_iter()
                .collect(),
            (false, Some(c)) => [("config".to_string(), serde_json::to_string(&c)?)]
                .into_iter()
                .collect(),
            (true, Some(c)) => [
                ("labels".to_string(), serde_json::to_string(&self.labels)?),
                ("config".to_string(), serde_json::to_string(&c)?),
            ]
            .into_iter()
            .collect(),
        };
        Ok(ArrowField {
            name: self.name.clone(),
            data_type: self.type_info.frame.into(),
            is_nullable: self.type_info.nullable.unwrap_or_default(),
            metadata,
        })
    }

    /// Return a new field with the given name.
    ///
    /// # Example
    ///
    /// ```rust
    /// use grafana_plugin_sdk::prelude::*;
    ///
    /// let field = ["a", "b", "c"]
    ///     .into_field("x")
    ///     .with_name("other name");
    /// assert_eq!(&field.name, "other name");
    /// ```
    #[must_use]
    pub fn with_name(mut self, name: impl Into<String>) -> Self {
        self.name = name.into();
        self
    }

    /// Return a new field with the given labels.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::collections::BTreeMap;
    /// use grafana_plugin_sdk::prelude::*;
    ///
    /// let mut labels = BTreeMap::default();
    /// labels.insert("some".to_string(), "value".to_string());
    /// let field = ["a", "b", "c"]
    ///     .into_field("x")
    ///     .with_labels(labels);
    /// assert_eq!(field.labels["some"], "value");
    /// ```
    #[must_use]
    pub fn with_labels(mut self, labels: BTreeMap<String, String>) -> Self {
        self.labels = labels;
        self
    }

    /// Return a new field with the given config.
    ///
    /// # Example
    ///
    /// ```rust
    /// use grafana_plugin_sdk::{data::FieldConfig, prelude::*};
    ///
    /// let mut config = FieldConfig::default();
    /// config.display_name_from_ds = Some("X".to_string());
    /// let field = ["a", "b", "c"]
    ///     .into_field("x")
    ///     .with_config(config);
    /// assert_eq!(&field.config.unwrap().display_name_from_ds.unwrap(), "X");
    /// ```
    #[must_use]
    pub fn with_config(mut self, config: impl Into<Option<FieldConfig>>) -> Self {
        self.config = config.into();
        self
    }

    /// Get the values of this field as a [`&dyn Array`].
    pub fn values(&self) -> &dyn Array {
        // This nightly clippy lint creates spurious suggestions for `&dyn Trait` return
        // types. This can be uncommented when https://github.com/rust-lang/rust-clippy/pull/9126
        // is released.
        #[allow(unknown_lints)]
        #[allow(clippy::explicit_auto_deref)]
        &*self.values
    }

    /// Set the values of this field using an iterator of values.
    ///
    /// # Errors
    ///
    /// Returns an [`Error::DataTypeMismatch`][error::Error::DataTypeMismatch] if the types of the new data
    /// do not match the types of the existing data.
    ///
    /// ```rust
    /// use arrow2::array::Utf8Array;
    /// use grafana_plugin_sdk::prelude::*;
    ///
    /// let mut field = ["a", "b", "c"]
    ///     .into_field("x");
    /// assert!(field.set_values(["d", "e", "f", "g"]).is_ok());
    /// assert_eq!(
    ///     field
    ///         .values()
    ///         .as_any()
    ///         .downcast_ref::<Utf8Array<i32>>()
    ///         .unwrap()
    ///         .iter()
    ///         .collect::<Vec<_>>(),
    ///     vec![Some("d"), Some("e"), Some("f"), Some("g")],
    /// );
    ///
    /// assert!(field.set_values([1u32, 2, 3]).is_err());
    /// ```
    pub fn set_values<T, U, V>(&mut self, values: T) -> Result<(), crate::data::error::Error>
    where
        T: IntoIterator<Item = U>,
        U: IntoFieldType<ElementType = V>,
        V: FieldType,
        V::Array: Array + FromIterator<Option<V>> + 'static,
    {
        let new_data_type: DataType = U::TYPE_INFO_TYPE.into();
        if self.values.data_type() != &new_data_type {
            return Err(crate::data::error::Error::DataTypeMismatch {
                existing: self.values.data_type().clone(),
                new: new_data_type,
                field: self.name.clone(),
            });
        }
        self.values = Box::new(V::convert_arrow_array(
            values
                .into_iter()
                .map(U::into_field_type)
                .collect::<V::Array>(),
            new_data_type,
        ));
        self.type_info.nullable = Some(false);
        Ok(())
    }

    /// Set the values of this field using an iterator of optional values.
    ///
    /// # Errors
    ///
    /// Returns an [`Error::DataTypeMismatch`][error::Error::DataTypeMismatch] if the types of the new data
    /// do not match the types of the existing data.
    ///
    /// ```rust
    /// use arrow2::array::Utf8Array;
    /// use grafana_plugin_sdk::prelude::*;
    ///
    /// let mut field = ["a", "b", "c"]
    ///     .into_field("x");
    /// assert!(field.set_values_opt([Some("d"), Some("e"), None, None]).is_ok());
    /// assert_eq!(
    ///     field
    ///         .values()
    ///         .as_any()
    ///         .downcast_ref::<Utf8Array<i32>>()
    ///         .unwrap()
    ///         .iter()
    ///         .collect::<Vec<_>>(),
    ///     vec![Some("d"), Some("e"), None, None],
    /// );
    ///
    /// assert!(field.set_values([Some(1u32), Some(2), None]).is_err());
    /// ```
    pub fn set_values_opt<T, U, V>(&mut self, values: T) -> Result<(), crate::data::error::Error>
    where
        T: IntoIterator<Item = Option<U>>,
        U: IntoFieldType<ElementType = V>,
        V: FieldType,
        V::Array: Array + FromIterator<Option<V>> + 'static,
    {
        let new_data_type: DataType = U::TYPE_INFO_TYPE.into();
        if self.values.data_type() != &new_data_type {
            return Err(crate::data::error::Error::DataTypeMismatch {
                existing: self.values.data_type().clone(),
                new: new_data_type,
                field: self.name.clone(),
            });
        }
        self.values = Box::new(V::convert_arrow_array(
            values
                .into_iter()
                .map(|x| x.and_then(U::into_field_type))
                .collect::<V::Array>(),
            new_data_type,
        ));
        self.type_info.nullable = Some(true);
        Ok(())
    }

    /// Set the values of this field using an [`Array`].
    ///
    /// # Errors
    ///
    /// Returns an [`Error::DataTypeMismatch`][error::Error::DataTypeMismatch] if the types of the new data
    /// do not match the types of the existing data.
    ///
    /// ```rust
    /// use arrow2::array::{PrimitiveArray, Utf8Array};
    /// use grafana_plugin_sdk::prelude::*;
    ///
    /// let mut field = ["a", "b", "c"]
    ///     .into_field("x");
    /// let new_values = Utf8Array::<i32>::from(["d", "e", "f"].map(Some));
    /// assert!(field.set_values_array(new_values).is_ok());
    /// assert_eq!(
    ///     field
    ///         .values()
    ///         .as_any()
    ///         .downcast_ref::<Utf8Array<i32>>()
    ///         .unwrap()
    ///         .iter()
    ///         .collect::<Vec<_>>(),
    ///     vec![Some("d"), Some("e"), Some("f")],
    /// );
    ///
    /// let bad_values = PrimitiveArray::<u32>::from([1, 2, 3].map(Some));
    /// assert!(field.set_values_array(bad_values).is_err());
    /// ```
    pub fn set_values_array<T>(&mut self, values: T) -> Result<(), crate::data::error::Error>
    where
        T: Array + 'static,
    {
        if self.values.data_type() != values.data_type() {
            return Err(crate::data::error::Error::DataTypeMismatch {
                existing: self.values.data_type().clone(),
                new: values.data_type().clone(),
                field: self.name.clone(),
            });
        }
        self.values = Box::new(values);
        Ok(())
    }
}

impl PartialEq for Field {
    fn eq(&self, other: &Self) -> bool {
        self.name == other.name
            && self.labels == other.labels
            && self.config == other.config
            && self.type_info == other.type_info
            && arrow2::array::equal(&*self.values, &*other.values)
    }
}

// Traits for creating a `Field` from various array-like things:
// iterators of both values and optional values, and arrays themselves.
// These need to be separate traits because otherwise the impls would conflict,
// as e.g. `Array` implements `IntoIterator`.

/// Indicates that a [`Field`] can be created from this type.
pub trait IntoField {
    /// Create a [`Field`] from `self`.
    ///
    /// The type of the `Field` will depend on the values in `self`.
    fn into_field(self, name: impl Into<String>) -> Field;
}

impl<T, U, V> IntoField for T
where
    T: IntoIterator<Item = U>,
    U: IntoFieldType<ElementType = V>,
    V: FieldType,
    V::Array: Array + FromIterator<Option<V>> + 'static,
{
    fn into_field(self, name: impl Into<String>) -> Field {
        Field {
            name: name.into(),
            labels: Default::default(),
            config: None,
            type_info: TypeInfo {
                frame: U::TYPE_INFO_TYPE,
                nullable: Some(false),
            },
            values: Box::new(V::convert_arrow_array(
                self.into_iter()
                    .map(U::into_field_type)
                    .collect::<V::Array>(),
                U::TYPE_INFO_TYPE.into(),
            )),
        }
    }
}

/// Indicates that a [`Field`] of optional values can be created from this type.
pub trait IntoOptField {
    /// Create a [`Field`] from `self`, with `None` values marked as null.
    fn into_opt_field(self, name: impl Into<String>) -> Field;
}

impl<T, U, V> IntoOptField for T
where
    T: IntoIterator<Item = Option<U>>,
    U: IntoFieldType<ElementType = V>,
    V: FieldType,
    V::Array: Array + FromIterator<Option<V>> + 'static,
{
    fn into_opt_field(self, name: impl Into<String>) -> Field {
        Field {
            name: name.into(),
            labels: Default::default(),
            config: None,
            type_info: TypeInfo {
                frame: U::TYPE_INFO_TYPE,
                nullable: Some(true),
            },
            values: Box::new(V::convert_arrow_array(
                self.into_iter()
                    .map(|x| x.and_then(U::into_field_type))
                    .collect::<V::Array>(),
                U::TYPE_INFO_TYPE.into(),
            )),
        }
    }
}

/// Helper trait for creating a [`Field`] from an [`Array`][arrow2::array::Array].
pub trait ArrayIntoField {
    /// Create a `Field` using `self` as the values.
    ///
    /// # Errors
    ///
    /// This returns an error if the values are not valid field types.
    fn try_into_field(self, name: impl Into<String>) -> Result<Field, error::Error>;
}

impl<T> ArrayIntoField for T
where
    T: Array + 'static,
{
    fn try_into_field(self, name: impl Into<String>) -> Result<Field, error::Error> {
        Ok(Field {
            name: name.into(),
            labels: Default::default(),
            config: None,
            type_info: TypeInfo {
                frame: self.data_type().try_into()?,
                nullable: Some(true),
            },
            values: Box::new(self),
        })
    }
}

/// Helper trait for creating a [`Field`] from an [`Array`][arrow_array::Array].
pub trait ArrayRefIntoField {
    /// Create a `Field` using `self` as the values.
    ///
    /// # Errors
    ///
    /// This returns an error if the values are not valid field types.
    fn try_into_field(self, name: impl Into<String>) -> Result<Field, error::Error>;
}

#[cfg(feature = "arrow")]
impl ArrayRefIntoField for &dyn ::arrow_array::Array {
    fn try_into_field(self, name: impl Into<String>) -> Result<Field, error::Error> {
        Ok(Field {
            name: name.into(),
            labels: Default::default(),
            config: None,
            type_info: TypeInfo {
                frame: self.data_type().try_into()?,
                nullable: Some(true),
            },
            values: self.into(),
        })
    }
}

/// The type information for a [`Frame`][crate::data::Frame] as understood by Grafana.
#[skip_serializing_none]
#[derive(Debug, Deserialize, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TypeInfo {
    /// The type, as understood by Grafana.
    pub(crate) frame: TypeInfoType,
    /// Is this type nullable?
    #[serde(default)]
    pub(crate) nullable: Option<bool>,
}

impl TypeInfo {
    /// Get the corresponding [`SimpleType`].
    #[must_use]
    pub const fn simple_type(&self) -> SimpleType {
        self.frame.simple_type()
    }
}

/// Valid types understood by Grafana.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum TypeInfoType {
    /// An 8 bit signed integer.
    Int8,
    /// A 16 bit signed integer.
    Int16,
    /// A 32 bit signed integer.
    Int32,
    /// A 64 bit signed integer.
    Int64,
    /// An 8 bit unsigned integer.
    UInt8,
    /// A 16 bit unsigned integer.
    UInt16,
    /// A 32 bit unsigned integer.
    UInt32,
    /// A 64 bit unsigned integer.
    UInt64,
    /// A 32 bit float.
    Float32,
    /// A 64 bit float.
    Float64,
    /// A string.
    String,
    /// A boolean.
    Bool,
    /// A timestamp, in UTC.
    // For compatibility with the Go SDK.
    #[serde(rename = "time.Time")]
    Time,
}

impl TryFrom<&DataType> for TypeInfoType {
    type Error = error::Error;
    fn try_from(other: &DataType) -> Result<Self, Self::Error> {
        Ok(match other {
            DataType::Int8 => Self::Int8,
            DataType::Int16 => Self::Int16,
            DataType::Int32 => Self::Int32,
            DataType::Int64 => Self::Int64,
            DataType::UInt8 => Self::UInt8,
            DataType::UInt16 => Self::UInt16,
            DataType::UInt32 => Self::UInt32,
            DataType::UInt64 => Self::UInt64,
            DataType::Float32 => Self::Float32,
            DataType::Float64 => Self::Float64,
            DataType::Utf8 => Self::String,
            DataType::Boolean => Self::Bool,
            DataType::Timestamp(..) => Self::Time,
            // TODO - handle time correctly.
            other => return Err(error::Error::UnsupportedArrowDataType(other.clone())),
        })
    }
}

impl From<TypeInfoType> for DataType {
    fn from(other: TypeInfoType) -> Self {
        match other {
            TypeInfoType::Int8 => Self::Int8,
            TypeInfoType::Int16 => Self::Int16,
            TypeInfoType::Int32 => Self::Int32,
            TypeInfoType::Int64 => Self::Int64,
            TypeInfoType::UInt8 => Self::UInt8,
            TypeInfoType::UInt16 => Self::UInt16,
            TypeInfoType::UInt32 => Self::UInt32,
            TypeInfoType::UInt64 => Self::UInt64,
            TypeInfoType::Float32 => Self::Float32,
            TypeInfoType::Float64 => Self::Float64,
            TypeInfoType::String => Self::Utf8,
            TypeInfoType::Bool => Self::Boolean,
            TypeInfoType::Time => Self::Timestamp(TimeUnit::Nanosecond, None),
        }
    }
}

#[cfg(feature = "arrow")]
impl TryFrom<&::arrow_schema::DataType> for TypeInfoType {
    type Error = error::Error;
    fn try_from(other: &::arrow_schema::DataType) -> Result<Self, Self::Error> {
        Ok(match other {
            ::arrow_schema::DataType::Int8 => Self::Int8,
            ::arrow_schema::DataType::Int16 => Self::Int16,
            ::arrow_schema::DataType::Int32 => Self::Int32,
            ::arrow_schema::DataType::Int64 => Self::Int64,
            ::arrow_schema::DataType::UInt8 => Self::UInt8,
            ::arrow_schema::DataType::UInt16 => Self::UInt16,
            ::arrow_schema::DataType::UInt32 => Self::UInt32,
            ::arrow_schema::DataType::UInt64 => Self::UInt64,
            ::arrow_schema::DataType::Float32 => Self::Float32,
            ::arrow_schema::DataType::Float64 => Self::Float64,
            ::arrow_schema::DataType::Utf8 => Self::String,
            ::arrow_schema::DataType::Boolean => Self::Bool,
            ::arrow_schema::DataType::Timestamp(..) => Self::Time,
            // TODO - handle time correctly.
            other => return Err(error::Error::UnsupportedArrowDataType(other.clone().into())),
        })
    }
}

#[cfg(feature = "arrow")]
impl From<TypeInfoType> for ::arrow_schema::DataType {
    fn from(other: TypeInfoType) -> Self {
        match other {
            TypeInfoType::Int8 => Self::Int8,
            TypeInfoType::Int16 => Self::Int16,
            TypeInfoType::Int32 => Self::Int32,
            TypeInfoType::Int64 => Self::Int64,
            TypeInfoType::UInt8 => Self::UInt8,
            TypeInfoType::UInt16 => Self::UInt16,
            TypeInfoType::UInt32 => Self::UInt32,
            TypeInfoType::UInt64 => Self::UInt64,
            TypeInfoType::Float32 => Self::Float32,
            TypeInfoType::Float64 => Self::Float64,
            TypeInfoType::String => Self::Utf8,
            TypeInfoType::Bool => Self::Boolean,
            TypeInfoType::Time => Self::Timestamp(::arrow_schema::TimeUnit::Nanosecond, None),
        }
    }
}

impl TypeInfoType {
    #[must_use]
    pub(crate) const fn simple_type(&self) -> SimpleType {
        match self {
            Self::Int8
            | Self::Int16
            | Self::Int32
            | Self::Int64
            | Self::UInt8
            | Self::UInt16
            | Self::UInt32
            | Self::UInt64
            | Self::Float32
            | Self::Float64 => SimpleType::Number,
            Self::String => SimpleType::String,
            Self::Bool => SimpleType::Boolean,
            Self::Time => SimpleType::Time,
        }
    }
}

/// The 'simple' type of this data.
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum SimpleType {
    /// A number.
    Number,
    /// A boolean.
    Boolean,
    /// A string.
    String,
    /// A timestamp.
    Time,
}

/// The display properties for a [`Field`].
///
/// These are used by the Grafana frontend to modify how the field is displayed.
///
/// Note that this struct, like most structs in this crate, is marked `#[non_exhaustive]` and
/// therefore cannot be constructed using a struct expression. Instead, create a default
/// value using `FieldConfig::default()` and modify any fields necessary.
// This struct needs to match the frontend component defined in:
// https://github.com/grafana/grafana/blob/master/packages/grafana-data/src/types/dataFrame.ts#L23
// All properties are optional should be omitted from JSON when empty or not set.
#[skip_serializing_none]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct FieldConfig {
    /// Overrides Grafana default naming.
    ///
    /// This should not be used from a datasource.
    #[serde(default)]
    pub display_name: Option<String>,

    /// Overrides Grafana default naming in a better way that allows users to further override it easily.
    ///
    /// This should be used instead of `display_name` when used from a datasource.
    #[serde(default, rename = "displayNameFromDS")]
    pub display_name_from_ds: Option<String>,

    /// An explicit path to the field in the datasource.
    ///
    /// When the frame meta includes a path, this will default to `${frame.meta.path}/${field.name}.
    ///
    /// When defined, this value can be used as an identifier within the datasource scope, and
    /// may be used as an identifier to update values in a subsequent request.
    #[serde(default)]
    pub path: Option<String>,

    /// Human readable field metadata.
    #[serde(default)]
    pub description: Option<String>,

    /// Indicates that the datasource knows how to update this value.
    #[serde(default)]
    pub writeable: Option<bool>,

    /// Indicates if the field's data can be filtered by additional calls.
    #[serde(default)]
    pub filterable: Option<bool>,

    /// The string to display to represent this field's unit, such as "Requests/sec".
    #[serde(default)]
    pub unit: Option<String>,
    /// The number of decimal places to display.
    #[serde(default)]
    pub decimals: Option<u16>,
    /// The minimum value of fields in the column.
    ///
    /// When present the frontend can skip the calculation.
    #[serde(default)]
    pub min: Option<f64>,
    /// The maximum value of fields in the column.
    ///
    /// When present the frontend can skip the calculation.
    #[serde(default)]
    pub max: Option<f64>,

    /// Mappings from input value to display string.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub mappings: Vec<ValueMapping>,
    /// Mappings from numeric values to states.
    #[serde(default)]
    pub thresholds: Option<ThresholdsConfig>,

    /// Links to use when clicking on a result.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub links: Vec<DataLink>,

    /// An alternative string to use when no value is present.
    ///
    /// The default is an empty string.
    #[serde(default)]
    pub no_value: Option<String>,

    /// Panel-specific values.
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub custom: HashMap<String, Value>,
}

/// Special values that can be mapped to new text and colour values.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged, rename_all = "camelCase")]
#[non_exhaustive]
pub enum SpecialValueMatch {
    /// Match `true`.
    True,
    /// Match `false`.
    False,
    /// Match `null`.
    Null,
    /// Match `NaN`.
    NaN,
    /// Match `null` and `NaN`.
    #[serde(rename = "null+nan")]
    NullAndNaN,
    /// Match empty values.
    Empty,
}

/// Allows input values to be mapped to text and colour.
#[skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", content = "options", rename_all = "camelCase")]
#[non_exhaustive]
pub enum ValueMapping {
    /// Map strings to new strings directly.
    ValueMapper(HashMap<String, ValueMappingResult>),
    /// Map special values to new values.
    SpecialValueMapper {
        /// The input value to match.
        #[serde(rename = "match")]
        match_: SpecialValueMatch,
        /// The result to be shown instead of the matched value.
        result: ValueMappingResult,
    },
    /// Map ranges of floats to new values.
    RangeValueMapper {
        /// The minimum value to match.
        from: ConfFloat64,
        /// The maximum value to match.
        to: ConfFloat64,
        /// The result to be shown instead of the matched values.
        result: ValueMappingResult,
    },
}

/// A new value to be displayed when a [`ValueMapping`] matches an input value.
#[skip_serializing_none]
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ValueMappingResult {
    /// The text to display.
    #[serde(default)]
    pub text: Option<String>,
    /// The colour to use when displaying the value.
    #[serde(default)]
    pub color: Option<String>,
    /// Used for ordering in the UI.
    #[serde(default)]
    pub index: Option<isize>,
}

/// Configuration for thresholds for a field.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ThresholdsConfig {
    /// How thresholds should be evaluated.
    pub mode: ThresholdsMode,
    /// The threshold steps.
    pub steps: Vec<Threshold>,
}

/// A single step on the threshold list.
#[skip_serializing_none]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Threshold {
    /// The upper bound of this threshold.
    pub value: Option<ConfFloat64>,
    /// The colour to use for this threshold.
    pub color: Option<String>,
    /// The state of the alert.
    pub state: Option<String>,
}

/// How thresholds should be evaluated.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub enum ThresholdsMode {
    /// Pick thresholds based on the absolute value.
    ///
    /// This is the default value.
    Absolute,
    /// Thresholds should be treated as relative to the min/max.
    Percentage,
}

impl Default for ThresholdsMode {
    fn default() -> Self {
        Self::Absolute
    }
}

/// Links to use when clicking on a result.
#[skip_serializing_none]
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct DataLink {
    /// The title text to display.
    pub title: Option<String>,
    /// Is the target blank?
    pub target_blank: Option<bool>,
    /// The URL to link to.
    pub url: Option<String>,
}

#[cfg(test)]
mod tests {

    use std::time::UNIX_EPOCH;

    use chrono::prelude::*;
    use paste::paste;

    use super::*;

    macro_rules! test_create_field_from_type {
        ($t:ty) => {
            paste! {
                #[test]
                #[allow(non_snake_case)]
                fn [< create_field_from_ $t >]() {
                    let field = [<$t>::default()].into_field("x".to_string());
                    assert_eq!(field.name, "x");
                    assert_eq!(field.values.len(), 1)
                }

                #[test]
                #[allow(non_snake_case)]
                fn [< create_field_from_opt_ $t >]() {
                    let field = [Some(<$t>::default()), None].into_opt_field("x".to_string());
                    assert_eq!(field.name, "x");
                    assert_eq!(field.values.len(), 2)
                }

                #[test]
                #[allow(non_snake_case)]
                fn [< create_field_from_iter_ $t >]() {
                    let field = std::iter::once(<$t>::default()).into_field("x".to_string());
                    assert_eq!(field.name, "x");
                    assert_eq!(field.values.len(), 1)
                }

                #[test]
                #[allow(non_snake_case)]
                fn [< create_field_from_opt_iter_ $t >]() {
                    let field = std::iter::once(Some(<$t>::default())).into_opt_field("x".to_string());
                    assert_eq!(field.name, "x");
                    assert_eq!(field.values.len(), 1)
                }

                #[test]
                #[allow(non_snake_case)]
                fn [< create_field_from_array_ $t >]() {
                    let array = <$t as FieldType>::Array::from_slice([<$t>::default()]);
                    let field = array.try_into_field("x".to_string()).unwrap();
                    assert_eq!(field.name, "x");
                    assert_eq!(field.values.len(), 1)
                }
            }
        };
        ($t:ty, $e: expr) => {
            paste! {
                #[test]
                #[allow(non_snake_case)]
                fn [< create_field_from_ $t >]() {
                    let field = [$e].into_field("x".to_string());
                    assert_eq!(field.name, "x");
                    assert_eq!(field.values.len(), 1)
                }

                #[test]
                #[allow(non_snake_case)]
                fn [< create_field_from_opt_ $t >]() {
                    let field = [Some($e), None].into_opt_field("x".to_string());
                    assert_eq!(field.name, "x");
                    assert_eq!(field.values.len(), 2)
                }

                #[test]
                #[allow(non_snake_case)]
                fn [< create_field_from_iter_ $t >]() {
                    let field = std::iter::once($e).into_field("x".to_string());
                    assert_eq!(field.name, "x");
                    assert_eq!(field.values.len(), 1)
                }

                #[test]
                #[allow(non_snake_case)]
                fn [< create_field_from_opt_iter_ $t >]() {
                    let field = std::iter::once(Some($e)).into_opt_field("x".to_string());
                    assert_eq!(field.name, "x");
                    assert_eq!(field.values.len(), 1)
                }
            }
        };
    }

    test_create_field_from_type!(bool);
    test_create_field_from_type!(i8);
    test_create_field_from_type!(i16);
    test_create_field_from_type!(i32);
    test_create_field_from_type!(i64);
    test_create_field_from_type!(u8);
    test_create_field_from_type!(u16);
    test_create_field_from_type!(u32);
    test_create_field_from_type!(u64);
    test_create_field_from_type!(f32);
    test_create_field_from_type!(f64);
    test_create_field_from_type!(String);
    test_create_field_from_type!(str, "");
    test_create_field_from_type!(SystemTime, UNIX_EPOCH);
    test_create_field_from_type!(
        NaiveDateTime,
        NaiveDate::from_ymd_opt(1970, 1, 1)
            .unwrap()
            .and_hms_opt(0, 0, 0)
            .unwrap()
    );
    test_create_field_from_type!(NaiveDate, NaiveDate::from_ymd_opt(1970, 1, 1).unwrap());
    test_create_field_from_type!(
        DateTime,
        Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).single().unwrap()
    );
    test_create_field_from_type!(
        Date,
        Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).single().unwrap()
    );

    #[test]
    fn set_values_from_iter_primitive() {
        let mut field = vec![1u32, 2, 3].into_field("yhat".to_string());
        assert!(field.set_values([4u32, 5, 6]).is_ok());
        assert!(field.set_values([4u64, 5, 6]).is_err());
    }

    // #[test]
    // fn set_values_from_array_primitive() {
    //     let array = PrimitiveArray::<u32>::from_slice([1u32, 2, 3]);
    //     let mut field = array.try_into_field("yhat".to_string()).unwrap();
    //     let array = PrimitiveArray::<u32>::from_slice([4u32, 5, 6]);
    //     assert!(field.set_values(array).is_ok());
    //     let array = PrimitiveArray::<u64>::from_slice([4u64, 5, 6]);
    //     assert!(field.set_values(array).is_err());
    // }
}