cognite/dto/core/
event.rs

1mod aggregate;
2mod filter;
3
4pub use self::aggregate::*;
5pub use self::filter::*;
6
7use crate::UpsertOptions;
8use crate::{
9    EqIdentity, Identity, IntoPatch, IntoPatchItem, Patch, UpdateList, UpdateMap, UpdateSetNull,
10};
11use serde::{Deserialize, Serialize};
12use serde_with::skip_serializing_none;
13use std::collections::HashMap;
14
15#[skip_serializing_none]
16#[derive(Serialize, Deserialize, Debug, Default, Clone)]
17#[serde(rename_all = "camelCase")]
18/// A CDF event.
19pub struct Event {
20    /// Event internal ID.
21    pub id: i64,
22    /// Event external ID. Must be unique accross all events in the project.
23    pub external_id: Option<String>,
24    /// The ID of the dataset this event belongs to.
25    pub data_set_id: Option<i64>,
26    /// Start time in milliseconds since epoch.
27    pub start_time: Option<i64>,
28    /// End time in milliseconds since epoch.
29    pub end_time: Option<i64>,
30    /// Type of the event.
31    #[serde(rename = "type")]
32    pub r#type: Option<String>,
33    /// Subtype of the event.
34    pub subtype: Option<String>,
35    /// Textual description of the event.
36    pub description: Option<String>,
37    /// Custom, application specific metadata. String key -> String value.
38    /// Limits: Maximum length of key is 128 bytes, value 128000 bytes,
39    /// up to 256 key-value pairs, of total size at most 200000.
40    pub metadata: Option<HashMap<String, String>>,
41    /// IDs of assets this event belongs to.
42    pub asset_ids: Option<Vec<i64>>,
43    /// The source of this event.
44    pub source: Option<String>,
45    /// Time this event was created, in milliseconds since epoch.
46    pub created_time: i64,
47    /// Time this event was last updated, in milliseconds since epoch.
48    pub last_updated_time: i64,
49}
50
51#[skip_serializing_none]
52#[derive(Serialize, Deserialize, Debug, Default, Clone)]
53#[serde(rename_all = "camelCase")]
54/// Create a new event
55pub struct AddEvent {
56    /// Event external ID. Must be unique accross all events in the project.
57    pub external_id: Option<String>,
58    /// The ID of the dataset this event belongs to.
59    pub data_set_id: Option<i64>,
60    /// Start time in milliseconds since epoch.
61    pub start_time: Option<i64>,
62    /// End time in milliseconds since epoch.
63    pub end_time: Option<i64>,
64    /// Type of the event.
65    #[serde(rename = "type")]
66    pub r#type: Option<String>,
67    /// Subtype of the event.
68    pub subtype: Option<String>,
69    /// Textual description of the event.
70    pub description: Option<String>,
71    /// Custom, application specific metadata. String key -> String value.
72    /// Limits: Maximum length of key is 128 bytes, value 128000 bytes,
73    /// up to 256 key-value pairs, of total size at most 200000.
74    pub metadata: Option<HashMap<String, String>>,
75    /// IDs of assets this event belongs to.
76    pub asset_ids: Option<Vec<i64>>,
77    /// The source of this event.
78    pub source: Option<String>,
79}
80
81impl From<Event> for AddEvent {
82    fn from(event: Event) -> AddEvent {
83        AddEvent {
84            external_id: event.external_id,
85            data_set_id: event.data_set_id,
86            start_time: event.start_time,
87            end_time: event.end_time,
88            r#type: event.r#type,
89            subtype: event.subtype,
90            description: event.description,
91            metadata: event.metadata,
92            asset_ids: event.asset_ids,
93            source: event.source,
94        }
95    }
96}
97
98impl EqIdentity for AddEvent {
99    fn eq(&self, id: &Identity) -> bool {
100        match id {
101            Identity::Id { id: _ } => false,
102            Identity::ExternalId { external_id } => self.external_id.as_ref() == Some(external_id),
103        }
104    }
105}
106
107#[skip_serializing_none]
108#[derive(Serialize, Deserialize, Debug, Default, Clone)]
109#[serde(rename_all = "camelCase")]
110/// Update an event.
111pub struct PatchEvent {
112    /// Event external ID. Must be unique accross all events in the project.
113    pub external_id: Option<UpdateSetNull<String>>,
114    /// The ID of the dataset this event belongs to.
115    pub data_set_id: Option<UpdateSetNull<i64>>,
116    /// Start time in milliseconds since epoch.
117    pub start_time: Option<UpdateSetNull<i64>>,
118    /// End time in milliseconds since epoch.
119    pub end_time: Option<UpdateSetNull<i64>>,
120    /// Textual description of the event.
121    pub description: Option<UpdateSetNull<String>>,
122    /// Custom, application specific metadata. String key -> String value.
123    /// Limits: Maximum length of key is 128 bytes, value 128000 bytes,
124    /// up to 256 key-value pairs, of total size at most 200000.
125    pub metadata: Option<UpdateMap<String, String>>,
126    /// IDs of assets this event belongs to.
127    pub asset_ids: Option<UpdateList<i64, i64>>,
128    /// The source of this event.
129    pub source: Option<UpdateSetNull<String>>,
130    /// Type of the event.
131    pub r#type: Option<UpdateSetNull<String>>,
132    /// Subtype of the event.
133    pub subtype: Option<UpdateSetNull<String>>,
134}
135
136impl IntoPatch<Patch<PatchEvent>> for Event {
137    fn patch(self, options: &UpsertOptions) -> Patch<PatchEvent> {
138        Patch::<PatchEvent> {
139            id: to_idt!(self),
140            update: PatchEvent {
141                external_id: self.external_id.patch(options),
142                data_set_id: self.data_set_id.patch(options),
143                start_time: self.start_time.patch(options),
144                end_time: self.end_time.patch(options),
145                description: self.description.patch(options),
146                metadata: self.metadata.patch(options),
147                asset_ids: self.asset_ids.patch(options),
148                source: self.source.patch(options),
149                r#type: self.r#type.patch(options),
150                subtype: self.subtype.patch(options),
151            },
152        }
153    }
154}
155
156impl IntoPatch<PatchEvent> for AddEvent {
157    fn patch(self, options: &UpsertOptions) -> PatchEvent {
158        PatchEvent {
159            external_id: self.external_id.patch(options),
160            data_set_id: self.data_set_id.patch(options),
161            start_time: self.start_time.patch(options),
162            end_time: self.end_time.patch(options),
163            description: self.description.patch(options),
164            metadata: self.metadata.patch(options),
165            asset_ids: self.asset_ids.patch(options),
166            source: self.source.patch(options),
167            r#type: self.r#type.patch(options),
168            subtype: self.subtype.patch(options),
169        }
170    }
171}
172
173impl From<Event> for Patch<PatchEvent> {
174    fn from(value: Event) -> Self {
175        IntoPatch::<Patch<PatchEvent>>::patch(value, &Default::default())
176    }
177}