datadog_api_client/datadogV2/model/
model_rum_metric_create_attributes.rs

1// Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
2// This product includes software developed at Datadog (https://www.datadoghq.com/).
3// Copyright 2019-Present Datadog, Inc.
4use serde::de::{Error, MapAccess, Visitor};
5use serde::{Deserialize, Deserializer, Serialize};
6use serde_with::skip_serializing_none;
7use std::fmt::{self, Formatter};
8
9/// The object describing the Datadog rum-based metric to create.
10#[non_exhaustive]
11#[skip_serializing_none]
12#[derive(Clone, Debug, PartialEq, Serialize)]
13pub struct RumMetricCreateAttributes {
14    /// The compute rule to compute the rum-based metric.
15    #[serde(rename = "compute")]
16    pub compute: crate::datadogV2::model::RumMetricCompute,
17    /// The type of RUM events to filter on.
18    #[serde(rename = "event_type")]
19    pub event_type: crate::datadogV2::model::RumMetricEventType,
20    /// The rum-based metric filter. Events matching this filter will be aggregated in this metric.
21    #[serde(rename = "filter")]
22    pub filter: Option<crate::datadogV2::model::RumMetricFilter>,
23    /// The rules for the group by.
24    #[serde(rename = "group_by")]
25    pub group_by: Option<Vec<crate::datadogV2::model::RumMetricGroupBy>>,
26    /// The rule to count updatable events. Is only set if `event_type` is `sessions` or `views`.
27    #[serde(rename = "uniqueness")]
28    pub uniqueness: Option<crate::datadogV2::model::RumMetricUniqueness>,
29    #[serde(flatten)]
30    pub additional_properties: std::collections::BTreeMap<String, serde_json::Value>,
31    #[serde(skip)]
32    #[serde(default)]
33    pub(crate) _unparsed: bool,
34}
35
36impl RumMetricCreateAttributes {
37    pub fn new(
38        compute: crate::datadogV2::model::RumMetricCompute,
39        event_type: crate::datadogV2::model::RumMetricEventType,
40    ) -> RumMetricCreateAttributes {
41        RumMetricCreateAttributes {
42            compute,
43            event_type,
44            filter: None,
45            group_by: None,
46            uniqueness: None,
47            additional_properties: std::collections::BTreeMap::new(),
48            _unparsed: false,
49        }
50    }
51
52    pub fn filter(mut self, value: crate::datadogV2::model::RumMetricFilter) -> Self {
53        self.filter = Some(value);
54        self
55    }
56
57    pub fn group_by(mut self, value: Vec<crate::datadogV2::model::RumMetricGroupBy>) -> Self {
58        self.group_by = Some(value);
59        self
60    }
61
62    pub fn uniqueness(mut self, value: crate::datadogV2::model::RumMetricUniqueness) -> Self {
63        self.uniqueness = Some(value);
64        self
65    }
66
67    pub fn additional_properties(
68        mut self,
69        value: std::collections::BTreeMap<String, serde_json::Value>,
70    ) -> Self {
71        self.additional_properties = value;
72        self
73    }
74}
75
76impl<'de> Deserialize<'de> for RumMetricCreateAttributes {
77    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
78    where
79        D: Deserializer<'de>,
80    {
81        struct RumMetricCreateAttributesVisitor;
82        impl<'a> Visitor<'a> for RumMetricCreateAttributesVisitor {
83            type Value = RumMetricCreateAttributes;
84
85            fn expecting(&self, f: &mut Formatter<'_>) -> fmt::Result {
86                f.write_str("a mapping")
87            }
88
89            fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
90            where
91                M: MapAccess<'a>,
92            {
93                let mut compute: Option<crate::datadogV2::model::RumMetricCompute> = None;
94                let mut event_type: Option<crate::datadogV2::model::RumMetricEventType> = None;
95                let mut filter: Option<crate::datadogV2::model::RumMetricFilter> = None;
96                let mut group_by: Option<Vec<crate::datadogV2::model::RumMetricGroupBy>> = None;
97                let mut uniqueness: Option<crate::datadogV2::model::RumMetricUniqueness> = None;
98                let mut additional_properties: std::collections::BTreeMap<
99                    String,
100                    serde_json::Value,
101                > = std::collections::BTreeMap::new();
102                let mut _unparsed = false;
103
104                while let Some((k, v)) = map.next_entry::<String, serde_json::Value>()? {
105                    match k.as_str() {
106                        "compute" => {
107                            compute = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
108                        }
109                        "event_type" => {
110                            event_type = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
111                            if let Some(ref _event_type) = event_type {
112                                match _event_type {
113                                    crate::datadogV2::model::RumMetricEventType::UnparsedObject(
114                                        _event_type,
115                                    ) => {
116                                        _unparsed = true;
117                                    }
118                                    _ => {}
119                                }
120                            }
121                        }
122                        "filter" => {
123                            if v.is_null() {
124                                continue;
125                            }
126                            filter = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
127                        }
128                        "group_by" => {
129                            if v.is_null() {
130                                continue;
131                            }
132                            group_by = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
133                        }
134                        "uniqueness" => {
135                            if v.is_null() {
136                                continue;
137                            }
138                            uniqueness = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
139                        }
140                        &_ => {
141                            if let Ok(value) = serde_json::from_value(v.clone()) {
142                                additional_properties.insert(k, value);
143                            }
144                        }
145                    }
146                }
147                let compute = compute.ok_or_else(|| M::Error::missing_field("compute"))?;
148                let event_type = event_type.ok_or_else(|| M::Error::missing_field("event_type"))?;
149
150                let content = RumMetricCreateAttributes {
151                    compute,
152                    event_type,
153                    filter,
154                    group_by,
155                    uniqueness,
156                    additional_properties,
157                    _unparsed,
158                };
159
160                Ok(content)
161            }
162        }
163
164        deserializer.deserialize_any(RumMetricCreateAttributesVisitor)
165    }
166}