cognite/dto/core/
asset.rs

1mod aggregate;
2mod filter;
3
4pub use self::aggregate::*;
5pub use self::filter::*;
6
7use crate::dto::identity::Identity;
8use crate::Items;
9use crate::UpsertOptions;
10use crate::{CogniteExternalId, CogniteId, EqIdentity, IntoPatch, IntoPatchItem, UpdateList};
11use crate::{Patch, UpdateMap, UpdateSet, UpdateSetNull};
12use serde::{Deserialize, Serialize};
13use serde_with::skip_serializing_none;
14use std::collections::HashMap;
15
16use super::common::GeoLocation;
17
18#[skip_serializing_none]
19#[derive(Serialize, Deserialize, Debug, Clone)]
20#[serde(rename_all = "camelCase")]
21/// Aggregated asset properties.
22pub struct AssetAggregate {
23    /// Number of direct descendants of the asset.
24    pub child_count: Option<i32>,
25    /// Asset path depth (number of levels below root node)
26    pub depth: Option<i32>,
27    /// IDs of assets on the path to the asset.
28    pub path: Option<Vec<CogniteId>>,
29}
30
31#[skip_serializing_none]
32#[derive(Serialize, Deserialize, Debug, Default, Clone)]
33#[serde(rename_all = "camelCase")]
34/// A CDF asset, representing some entity.
35pub struct Asset {
36    /// Server-generated ID of the asset.
37    pub id: i64,
38    /// Name of the asset.
39    pub name: String,
40    /// Unique user-provided external ID.
41    pub external_id: Option<String>,
42    /// ID of the parent node.
43    pub parent_id: Option<i64>,
44    /// External ID of the parent node.
45    pub parent_external_id: Option<String>,
46    /// Description of the asset.
47    pub description: Option<String>,
48    /// Custom, application specific metadata. String key -> String value.
49    /// Limits: Maximum length of key is 128 bytes,
50    /// value 10240 bytes, up to 256 key-value pairs, of total size at most 10240.
51    pub metadata: Option<HashMap<String, String>>,
52    /// Source of the asset.
53    pub source: Option<String>,
54    /// Time this asset was created, in milliseconds since epoch.
55    pub created_time: i64,
56    /// Time this assset was last updated, in milliseconds since epoch.
57    pub last_updated_time: i64,
58    /// ID of the root asset.
59    pub root_id: Option<i64>,
60    /// Aggregated metrics computed on this asset.
61    pub aggregates: Option<AssetAggregate>,
62    /// ID of the data set this asset belongs to.
63    pub data_set_id: Option<i64>,
64    /// List of the labels associated with this asset.
65    pub labels: Option<Vec<CogniteExternalId>>,
66    /// Geographic metadata.
67    pub geo_location: Option<GeoLocation>,
68}
69
70impl Asset {
71    /// Create an asset
72    ///
73    /// # Arguments
74    ///
75    /// * `name` - Name of the asset.
76    /// * `description` - Description of the asset.
77    /// * `external_id` - External ID of the asset, must be unique.
78    /// * `parent_id` - ID of the parent of this asset.
79    /// * `metadata` - Optional application specific metadata.
80    /// * `source` - Source of this asset.
81    pub fn new(
82        name: &str,
83        description: &str,
84        external_id: Option<String>,
85        parent_id: Option<i64>,
86        metadata: Option<HashMap<String, String>>,
87        source: Option<String>,
88    ) -> Asset {
89        Asset {
90            name: String::from(name),
91            id: 0,
92            external_id,
93            parent_id,
94            description: Some(String::from(description)),
95            metadata,
96            source,
97            created_time: 0,
98            last_updated_time: 0,
99            root_id: None,
100            aggregates: None,
101            data_set_id: None,
102            parent_external_id: None,
103            labels: None,
104            geo_location: None,
105        }
106    }
107}
108
109#[skip_serializing_none]
110#[derive(Serialize, Deserialize, Debug, Default, Clone)]
111#[serde(rename_all = "camelCase")]
112/// Create an asset.
113pub struct AddAsset {
114    /// Name of the asset.
115    pub name: String,
116    /// Unique external ID of the asset.
117    pub external_id: Option<String>,
118    /// Parent node ID used to specify a parent-child relationship. Do not use
119    /// in combination with `parent_external_id`
120    pub parent_id: Option<i64>,
121    /// Description of the asset.
122    pub description: Option<String>,
123    /// Custom, application specific metadata. String key -> String value.
124    /// Limits: Maximum length of key is 128 bytes, value 10240 bytes,
125    /// up to 256 key-value pairs, of total size at most 10240.
126    pub metadata: Option<HashMap<String, String>>,
127    /// Source of the asset.
128    pub source: Option<String>,
129    /// External ID of the parent asset.
130    ///  When specifying this field, the API will resolve the external ID
131    /// into an internal ID and use the internal ID to store the
132    /// parent-child relation. As a result, a later change to update the parent's
133    /// external ID will not affect this parent-child relationship as
134    /// it is based on internal ID.
135    ///
136    /// Do not use in combination with `parent_id`
137    pub parent_external_id: Option<String>,
138    /// ID of the dataset this asset belongs to.
139    pub data_set_id: Option<i64>,
140    /// A list of labels associated with this asset.
141    pub labels: Option<Vec<CogniteExternalId>>,
142    /// Geographic metadata.
143    pub geo_location: Option<GeoLocation>,
144}
145
146impl From<Asset> for AddAsset {
147    fn from(asset: Asset) -> AddAsset {
148        AddAsset {
149            name: asset.name,
150            external_id: asset.external_id,
151            parent_id: asset.parent_id,
152            description: asset.description,
153            metadata: asset.metadata,
154            source: asset.source,
155            parent_external_id: if asset.parent_id.is_none() {
156                asset.parent_external_id
157            } else {
158                None
159            },
160            data_set_id: asset.data_set_id,
161            labels: asset.labels,
162            geo_location: asset.geo_location,
163        }
164    }
165}
166
167impl EqIdentity for AddAsset {
168    fn eq(&self, id: &Identity) -> bool {
169        match id {
170            Identity::Id { id: _ } => false,
171            Identity::ExternalId { external_id } => self.external_id.as_ref() == Some(external_id),
172        }
173    }
174}
175
176#[skip_serializing_none]
177#[derive(Serialize, Deserialize, Debug, Default, Clone)]
178#[serde(rename_all = "camelCase")]
179/// Update an asset.
180pub struct PatchAsset {
181    /// Unique external ID of the asset.
182    pub external_id: Option<UpdateSetNull<String>>,
183    /// Name of the asset.
184    pub name: Option<UpdateSet<String>>,
185    /// Description of the asset.
186    pub description: Option<UpdateSetNull<String>>,
187    /// ID of the dataset this asset belongs to.
188    pub data_set_id: Option<UpdateSetNull<i64>>,
189    /// Custom, application specific metadata. String key -> String value.
190    /// Limits: Maximum length of key is 128 bytes, value 10240 bytes,
191    /// up to 256 key-value pairs, of total size at most 10240.
192    pub metadata: Option<UpdateMap<String, String>>,
193    /// Source of the asset.
194    pub source: Option<UpdateSetNull<String>>,
195    /// Parent node ID used to specify a parent-child relationship. Do not use
196    /// in combination with `parent_external_id`
197    pub parent_id: Option<UpdateSet<i64>>,
198    /// External ID of the parent asset.
199    ///  When specifying this field, the API will resolve the external ID
200    /// into an internal ID and use the internal ID to store the
201    /// parent-child relation. As a result, a later change to update the parent's
202    /// external ID will not affect this parent-child relationship as
203    /// it is based on internal ID.
204    ///
205    /// Do not use in combination with `parent_id`
206    pub parent_external_id: Option<UpdateSet<String>>,
207    /// A list of labels associated with this asset.
208    pub labels: Option<UpdateList<CogniteExternalId, CogniteExternalId>>,
209    /// Geographic metadata.
210    pub geo_location: Option<UpdateSetNull<GeoLocation>>,
211}
212
213impl IntoPatch<Patch<PatchAsset>> for Asset {
214    fn patch(self, options: &UpsertOptions) -> Patch<PatchAsset> {
215        Patch::<PatchAsset> {
216            id: to_idt!(self),
217            update: PatchAsset {
218                external_id: self.external_id.patch(options),
219                name: self.name.patch(options),
220                description: self.description.patch(options),
221                data_set_id: self.data_set_id.patch(options),
222                metadata: self.metadata.patch(options),
223                source: self.source.patch(options),
224                parent_id: self.parent_id.and_then(|p| p.patch(options)),
225                parent_external_id: None,
226                labels: self.labels.patch(options),
227                geo_location: self.geo_location.patch(options),
228            },
229        }
230    }
231}
232
233impl IntoPatch<PatchAsset> for AddAsset {
234    fn patch(self, options: &UpsertOptions) -> PatchAsset {
235        let mut parent_id = None;
236        let mut parent_external_id = None;
237        if let Some(p_id) = self.parent_id {
238            parent_id = p_id.patch(options);
239        } else if let Some(p_extid) = self.parent_external_id {
240            parent_external_id = p_extid.patch(options);
241        }
242        PatchAsset {
243            external_id: self.external_id.patch(options),
244            name: self.name.patch(options),
245            description: self.description.patch(options),
246            data_set_id: self.data_set_id.patch(options),
247            metadata: self.metadata.patch(options),
248            source: self.source.patch(options),
249            parent_id,
250            parent_external_id,
251            labels: self.labels.patch(options),
252            geo_location: self.geo_location.patch(options),
253        }
254    }
255}
256
257impl From<Asset> for Patch<PatchAsset> {
258    fn from(value: Asset) -> Self {
259        IntoPatch::<Patch<PatchAsset>>::patch(value, &Default::default())
260    }
261}
262
263#[derive(Serialize, Deserialize, Debug, Default)]
264#[serde(rename_all = "camelCase")]
265/// Extra data for retrieve assets request.
266pub struct RetrieveAssetsRequestData {
267    /// If `true`, ignore any IDs that do not exist in CDF.
268    pub ignore_unknown_ids: bool,
269    /// List of aggregated properties to include in response.
270    pub aggregated_properties: Option<Vec<AssetAggregatedProperty>>,
271}
272
273/// Request for retrieving assets.
274pub type RetrieveAssetsRequest<T> = Items<T, RetrieveAssetsRequestData>;
275
276#[derive(Serialize, Deserialize, Debug, Default)]
277#[serde(rename_all = "camelCase")]
278/// Extra data for delete assets request.
279pub struct DeleteAssetsRequestData {
280    /// If `true`, ignore any IDs that do not exist in CDF.
281    pub ignore_unknown_ids: bool,
282    /// If `true`, recursively delete any children of the deleted assets.
283    pub recursive: bool,
284}
285
286/// Request for deleting assets.
287pub type DeleteAssetsRequest<R> = Items<R, DeleteAssetsRequestData>;