cognite/dto/core/
time_series.rs

1mod filter;
2mod synthetic;
3
4pub use self::filter::*;
5pub use self::synthetic::*;
6
7use crate::models::instances::CogniteTimeseries;
8use crate::models::instances::InstanceId;
9use crate::IntoPatch;
10use crate::IntoPatchItem;
11use crate::UpsertOptions;
12use crate::{EqIdentity, Identity, Patch, UpdateList, UpdateMap, UpdateSet, UpdateSetNull};
13
14use serde::{Deserialize, Serialize};
15use serde_with::skip_serializing_none;
16use std::collections::HashMap;
17
18#[derive(Serialize, Deserialize, Debug, Default, Clone)]
19#[serde(rename_all = "camelCase")]
20/// A CDF time series.
21pub struct TimeSeries {
22    /// Time series internal ID.
23    pub id: i64,
24    /// Time series external ID. Must be unique for all time series in the project.
25    pub external_id: Option<String>,
26    /// Time series ID in data models. Only present if this time series was created through
27    /// data modeling.
28    pub instance_id: Option<InstanceId>,
29    /// Time series name.
30    pub name: Option<String>,
31    /// Whether this is a time series for string or double data points.
32    pub is_string: bool,
33    /// Custom, application specific metadata. String key -> String value.
34    /// Maximum length of key is 128 bytes, up to 256 key-value pairs,
35    /// of total size of at most 10000 bytes across all keys and values.
36    pub metadata: Option<HashMap<String, String>>,
37    /// The physical unit of the time series (free-text field).
38    pub unit: Option<String>,
39    /// The physical unit of the time series as represented in the unit catalog.
40    pub unit_external_id: Option<String>,
41    /// Asset ID of equipment linked to this time series.
42    pub asset_id: Option<i64>,
43    /// Whether this is a step time series or not.
44    pub is_step: bool,
45    /// Description of the time series.
46    pub description: Option<String>,
47    /// The required security categories to access this time series.
48    pub security_categories: Option<Vec<i64>>,
49    /// Time this time series was created, in milliseconds since epoch.
50    pub created_time: i64,
51    /// Time this time series was last updated, in milliseconds since epoch.
52    pub last_updated_time: i64,
53    /// Data set this time series belongs to.
54    pub data_set_id: Option<i64>,
55}
56
57#[skip_serializing_none]
58#[derive(Serialize, Deserialize, Debug, Default, Clone)]
59#[serde(rename_all = "camelCase")]
60/// Create a new time series.
61pub struct AddTimeSeries {
62    /// Time series external ID. Must be unique accross all time series in the project.
63    pub external_id: Option<String>,
64    /// Time series name.
65    pub name: Option<String>,
66    /// Whether this is a time series for string or double data points.
67    pub is_string: bool,
68    /// Custom, application specific metadata. String key -> String value.
69    /// Maximum length of key is 128 bytes, up to 256 key-value pairs,
70    /// of total size of at most 10000 bytes across all keys and values.
71    pub metadata: Option<HashMap<String, String>>,
72    /// The physical unit of the time series (free-text field).
73    pub unit: Option<String>,
74    /// The physical unit of the time series as represented in the unit catalog.
75    pub unit_external_id: Option<String>,
76    /// ID of the asset this time series belongs to.
77    pub asset_id: Option<i64>,
78    /// Whether this is a step time series or not.
79    pub is_step: bool,
80    /// Description of the time series.
81    pub description: Option<String>,
82    /// The required security categories to access this time series.
83    pub security_categories: Option<Vec<i64>>,
84    /// Data set this time series belongs to.
85    pub data_set_id: Option<i64>,
86}
87
88/// Add Core DM or classic time series.
89pub enum AddDmOrTimeSeries {
90    /// Classic time series.
91    TimeSeries(Box<AddTimeSeries>),
92    /// Core DM timeseries
93    Cdm(Box<CogniteTimeseries>),
94}
95
96impl From<TimeSeries> for AddTimeSeries {
97    fn from(time_serie: TimeSeries) -> AddTimeSeries {
98        AddTimeSeries {
99            name: time_serie.name,
100            external_id: time_serie.external_id,
101            is_string: time_serie.is_string,
102            metadata: time_serie.metadata,
103            unit: time_serie.unit,
104            unit_external_id: time_serie.unit_external_id,
105            asset_id: time_serie.asset_id,
106            is_step: time_serie.is_step,
107            description: time_serie.description,
108            security_categories: time_serie.security_categories,
109            data_set_id: time_serie.data_set_id,
110        }
111    }
112}
113
114impl EqIdentity for AddTimeSeries {
115    fn eq(&self, id: &Identity) -> bool {
116        match id {
117            Identity::Id { id: _ } => false,
118            Identity::ExternalId { external_id } => self.external_id.as_ref() == Some(external_id),
119        }
120    }
121}
122
123#[skip_serializing_none]
124#[derive(Serialize, Deserialize, Debug, Default, Clone)]
125#[serde(rename_all = "camelCase")]
126/// Update a time series.
127pub struct PatchTimeSeries {
128    /// Time series name.
129    pub name: Option<UpdateSetNull<String>>,
130    /// Time series external ID. Must be unique accross all time series in the project.
131    pub external_id: Option<UpdateSetNull<String>>,
132    /// Custom, application specific metadata. String key -> String value.
133    /// Maximum length of key is 128 bytes, up to 256 key-value pairs,
134    /// of total size of at most 10000 bytes across all keys and values.
135    pub metadata: Option<UpdateMap<String, String>>,
136    /// The physical unit of the time series (free-text field).
137    pub unit: Option<UpdateSetNull<String>>,
138    /// The physical unit of the time series as represented in the unit catalog.
139    pub unit_external_id: Option<UpdateSetNull<String>>,
140    /// ID of the asset this time series belongs to.
141    pub asset_id: Option<UpdateSetNull<i64>>,
142    /// Description of the time series.
143    pub description: Option<UpdateSetNull<String>>,
144    /// The required security categories to access this time series.
145    pub security_categories: Option<UpdateList<i64, i64>>,
146    /// Data set this time series belongs to.
147    pub data_set_id: Option<UpdateSetNull<i64>>,
148    /// Whether this is a step time series or not.
149    pub is_step: Option<UpdateSet<bool>>,
150}
151
152impl IntoPatch<Patch<PatchTimeSeries>> for TimeSeries {
153    fn patch(self, options: &UpsertOptions) -> Patch<PatchTimeSeries> {
154        Patch::<PatchTimeSeries> {
155            id: to_idt!(self),
156            update: PatchTimeSeries {
157                name: self.name.patch(options),
158                external_id: self.external_id.patch(options),
159                metadata: self.metadata.patch(options),
160                unit: self.unit.patch(options),
161                unit_external_id: self.unit_external_id.patch(options),
162                asset_id: self.asset_id.patch(options),
163                description: self.description.patch(options),
164                security_categories: self.security_categories.patch(options),
165                data_set_id: self.data_set_id.patch(options),
166                is_step: self.is_step.patch(options),
167            },
168        }
169    }
170}
171
172impl IntoPatch<PatchTimeSeries> for AddTimeSeries {
173    fn patch(self, options: &UpsertOptions) -> PatchTimeSeries {
174        PatchTimeSeries {
175            name: self.name.patch(options),
176            external_id: self.external_id.patch(options),
177            metadata: self.metadata.patch(options),
178            unit: self.unit.patch(options),
179            unit_external_id: self.unit_external_id.patch(options),
180            asset_id: self.asset_id.patch(options),
181            description: self.description.patch(options),
182            security_categories: self.security_categories.patch(options),
183            data_set_id: self.data_set_id.patch(options),
184            is_step: self.is_step.patch(options),
185        }
186    }
187}
188
189impl From<TimeSeries> for Patch<PatchTimeSeries> {
190    fn from(time_serie: TimeSeries) -> Patch<PatchTimeSeries> {
191        IntoPatch::<Patch<PatchTimeSeries>>::patch(time_serie, &Default::default())
192    }
193}