cognite/dto/core/
sequences.rs

1use crate::{
2    AdvancedFilter, EqIdentity, Identity, IntoPatch, IntoPatchItem, Partition, Patch, Range,
3    SetCursor, UpdateList, UpdateMap, UpdateSet, UpdateSetNull, UpsertOptions, WithPartition,
4};
5
6use serde::{Deserialize, Serialize};
7use serde_with::skip_serializing_none;
8use std::collections::HashMap;
9
10use super::common::CoreSortItem;
11
12#[derive(Serialize, Deserialize, Debug, Clone, Copy, Default)]
13#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
14/// Type of sequence value.
15pub enum SequenceValueType {
16    #[default]
17    /// Double precision floating point.
18    Double,
19    /// String.
20    String,
21    /// 64-bit integer.
22    Long,
23}
24
25#[skip_serializing_none]
26#[derive(Serialize, Deserialize, Debug, Clone, Default)]
27#[serde(rename_all = "camelCase")]
28/// Description of a single sequence column
29pub struct SequenceColumn {
30    /// Name of the sequence column.
31    pub name: Option<String>,
32    /// External ID of the sequence column. Must be unique for a given sequence.
33    pub external_id: String,
34    /// Description of the sequence column.
35    pub description: Option<String>,
36    /// Type of column value.
37    pub value_type: SequenceValueType,
38    /// Custom, application specific metadata. String key -> String value.
39    /// Maximum length of key is 128 bytes, up to 256 key-value pairs,
40    /// up to a total size of 10000 bytes across all keys and values.
41    pub metadata: Option<HashMap<String, String>>,
42    #[serde(skip_serializing)]
43    /// Time this sequence column was created, in milliseconds since epoch.
44    pub created_time: Option<i64>,
45    #[serde(skip_serializing)]
46    /// Time this sequence column was last updated, in milliseconds since epoch.
47    pub last_updated_time: Option<i64>,
48}
49
50#[skip_serializing_none]
51#[derive(Serialize, Deserialize, Debug, Default, Clone)]
52#[serde(rename_all = "camelCase")]
53/// A CDF sequence.
54pub struct Sequence {
55    /// Sequence internal ID.
56    pub id: i64,
57    /// External ID of the sequence. Must be unique within the project.
58    pub external_id: Option<String>,
59    /// Name of the sequence.
60    pub name: Option<String>,
61    /// Description of the sequence.
62    pub description: Option<String>,
63    /// ID of asset this sequence belongs to.
64    pub asset_id: Option<i64>,
65    /// Custom, application specific metadata. String key -> String value.
66    /// Maximum length of key is 128 bytes, up to 256 key-value pairs,
67    /// up to a total size of 10000 bytes across all keys and values.
68    pub metadata: Option<HashMap<String, String>>,
69    /// List of columns in this sequence.
70    pub columns: Vec<SequenceColumn>,
71    /// Time this sequence was created, in milliseconds since epoch.
72    pub created_time: i64,
73    /// Time this sequence was last updated, in milliseconds since epoch.
74    pub last_updated_time: i64,
75    /// ID of the data set this sequence belongs to.
76    pub data_set_id: Option<i64>,
77}
78
79#[skip_serializing_none]
80#[derive(Serialize, Deserialize, Debug, Default, Clone)]
81#[serde(rename_all = "camelCase")]
82/// Create a new sequence.
83pub struct AddSequence {
84    /// External ID of the sequence. Must be unique within the project.
85    pub external_id: Option<String>,
86    /// Name of the sequence.
87    pub name: Option<String>,
88    /// Description of the sequence.
89    pub description: Option<String>,
90    /// ID of asset this sequence belongs to.
91    pub asset_id: Option<i64>,
92    /// Custom, application specific metadata. String key -> String value.
93    /// Maximum length of key is 128 bytes, up to 256 key-value pairs,
94    /// up to a total size of 10000 bytes across all keys and values.
95    pub metadata: Option<HashMap<String, String>>,
96    /// List of columns in this sequence.
97    pub columns: Vec<SequenceColumn>,
98    /// ID of the data set this sequence belongs to.
99    pub data_set_id: Option<i64>,
100}
101
102impl From<Sequence> for AddSequence {
103    fn from(sequence: Sequence) -> Self {
104        AddSequence {
105            external_id: sequence.external_id,
106            name: sequence.name,
107            description: sequence.description,
108            asset_id: sequence.asset_id,
109            metadata: sequence.metadata,
110            columns: sequence.columns,
111            data_set_id: sequence.data_set_id,
112        }
113    }
114}
115
116impl EqIdentity for AddSequence {
117    fn eq(&self, id: &Identity) -> bool {
118        match id {
119            Identity::Id { id: _ } => false,
120            Identity::ExternalId { external_id } => self.external_id.as_ref() == Some(external_id),
121        }
122    }
123}
124
125#[skip_serializing_none]
126#[derive(Serialize, Deserialize, Debug, Default, Clone)]
127#[serde(rename_all = "camelCase")]
128/// Update the columns of a sequence.
129pub struct UpdateSequenceColumns {
130    /// List of column updates.
131    pub modify: Option<Vec<Patch<PatchSequenceColumn>>>,
132    /// List of column definitions to add.
133    pub add: Option<Vec<SequenceColumn>>,
134    /// List of columns to remove.
135    pub remove: Option<Vec<String>>,
136}
137
138impl From<UpdateList<SequenceColumn, String>> for UpdateSequenceColumns {
139    fn from(upd: UpdateList<SequenceColumn, String>) -> Self {
140        match upd {
141            UpdateList::AddRemove { add, remove } => Self {
142                modify: None,
143                add,
144                remove,
145            },
146            UpdateList::Set { set } => Self {
147                add: Some(set),
148                modify: None,
149                remove: None,
150            },
151        }
152    }
153}
154
155#[skip_serializing_none]
156#[derive(Serialize, Deserialize, Debug, Default, Clone)]
157#[serde(rename_all = "camelCase")]
158/// Update a sequence column.
159pub struct PatchSequenceColumn {
160    /// Description of the sequence.
161    pub description: Option<UpdateSetNull<String>>,
162    /// External ID of the sequence column. Must be unique for a given sequence.
163    pub external_id: Option<UpdateSet<String>>,
164    /// Name of the sequence column.
165    pub name: Option<UpdateSetNull<String>>,
166    /// Custom, application specific metadata. String key -> String value.
167    /// Maximum length of key is 128 bytes, up to 256 key-value pairs,
168    /// up to a total size of 10000 bytes across all keys and values.
169    pub metadata: Option<UpdateMap<String, String>>,
170}
171
172#[skip_serializing_none]
173#[derive(Serialize, Deserialize, Debug, Default, Clone)]
174#[serde(rename_all = "camelCase")]
175/// Update a sequence.
176pub struct PatchSequence {
177    /// Name of the sequence.
178    pub name: Option<UpdateSetNull<String>>,
179    /// Description of the sequence.
180    pub description: Option<UpdateSetNull<String>>,
181    /// ID of asset this sequence belongs to.
182    pub asset_id: Option<UpdateSetNull<i64>>,
183    /// External ID of the sequence. Must be unique within the project.
184    pub external_id: Option<UpdateSetNull<String>>,
185    /// Custom, application specific metadata. String key -> String value.
186    /// Maximum length of key is 128 bytes, up to 256 key-value pairs,
187    /// up to a total size of 10000 bytes across all keys and values.
188    pub metadata: Option<UpdateMap<String, String>>,
189    /// ID of the data set this sequence belongs to.
190    pub data_set_id: Option<UpdateSetNull<i64>>,
191    /// List of columns in this sequence.
192    pub columns: Option<UpdateSequenceColumns>,
193}
194
195impl IntoPatch<Patch<PatchSequence>> for Sequence {
196    fn patch(self, options: &UpsertOptions) -> Patch<PatchSequence> {
197        Patch::<PatchSequence> {
198            id: to_idt!(self),
199            update: PatchSequence {
200                name: self.name.patch(options),
201                description: self.description.patch(options),
202                asset_id: self.asset_id.patch(options),
203                external_id: self.external_id.patch(options),
204                metadata: self.metadata.patch(options),
205                data_set_id: self.data_set_id.patch(options),
206                columns: None,
207            },
208        }
209    }
210}
211
212impl IntoPatch<PatchSequence> for AddSequence {
213    fn patch(self, options: &UpsertOptions) -> PatchSequence {
214        PatchSequence {
215            name: self.name.patch(options),
216            description: self.description.patch(options),
217            asset_id: self.asset_id.patch(options),
218            external_id: self.external_id.patch(options),
219            metadata: self.metadata.patch(options),
220            data_set_id: self.data_set_id.patch(options),
221            columns: None,
222        }
223    }
224}
225
226impl From<Sequence> for Patch<PatchSequence> {
227    fn from(sequence: Sequence) -> Self {
228        IntoPatch::<Patch<PatchSequence>>::patch(sequence, &Default::default())
229    }
230}
231
232#[skip_serializing_none]
233#[derive(Serialize, Deserialize, Debug, Default, Clone)]
234#[serde(rename_all = "camelCase")]
235/// Filter on sequences.
236pub struct SequenceFilter {
237    /// Include sequences with this name.
238    pub name: Option<String>,
239    /// Filter by this (case-sensitive) prefix for the external ID.
240    pub external_id_prefix: Option<String>,
241    /// Filter by sequence metadata.
242    pub metadata: Option<HashMap<String, String>>,
243    /// Include sequences associated with one of these assets.
244    pub asset_ids: Option<Vec<i64>>,
245    /// Include sequences associated with an asset in the tree of one of these
246    /// root assets.
247    pub root_asset_ids: Option<Vec<i64>>,
248    /// Include sequences associated with an asset in the subtree of one of these
249    /// assets.
250    pub asset_subtree_ids: Option<Vec<Identity>>,
251    /// Range of timestamps for `created_time`.
252    pub created_time: Option<Range<i64>>,
253    /// Range of timestamps for `last_updated_time`.
254    pub last_updated_time: Option<Range<i64>>,
255    /// Include sequences which are tied to one of these data sets.
256    pub data_set_ids: Option<Vec<Identity>>,
257}
258
259#[skip_serializing_none]
260#[derive(Serialize, Deserialize, Debug, Default, Clone)]
261#[serde(rename_all = "camelCase")]
262/// Request for filtering sequences.
263pub struct SequenceFilterRequest {
264    /// Simple sequences filter.
265    pub filter: Option<SequenceFilter>,
266    /// Advanced filter.
267    pub advanced_filter: Option<AdvancedFilter>,
268    /// Maximum number of sequences to return.
269    pub limit: Option<i32>,
270    /// Optional cursor for pagination.
271    pub cursor: Option<String>,
272    /// Split the result set into partitions.
273    pub partition: Option<Partition>,
274    /// Sort the result by these properties. The order is significant.
275    pub sort: Option<Vec<CoreSortItem>>,
276}
277
278impl SetCursor for SequenceFilterRequest {
279    fn set_cursor(&mut self, cursor: Option<String>) {
280        self.cursor = cursor;
281    }
282}
283
284impl WithPartition for SequenceFilterRequest {
285    fn with_partition(&self, partition: Partition) -> Self {
286        let mut copy = self.clone();
287        copy.partition = Some(partition);
288        copy
289    }
290}
291
292#[skip_serializing_none]
293#[derive(Serialize, Deserialize, Debug, Default, Clone)]
294#[serde(rename_all = "camelCase")]
295/// Fuzzy search sequences.
296pub struct SequenceSearch {
297    /// Fuzzy search on name.
298    pub name: Option<String>,
299    /// Fuzzy search on description.
300    pub description: Option<String>,
301    /// Searches on name and description using wildcard search on each of the words
302    /// (separated by spaces). Retrieves results where at least one word must match.
303    pub query: Option<String>,
304}
305
306#[derive(Serialize, Deserialize, Debug)]
307#[serde(untagged)]
308/// Value of a sequence row.
309pub enum SequenceRowValue {
310    /// String value.
311    String(String),
312    /// 64-bit integer value.
313    Long(i64),
314    /// Double precision floating point value.
315    Double(f64),
316    /// Null value.
317    Null(()),
318}
319
320impl Default for SequenceRowValue {
321    fn default() -> Self {
322        SequenceRowValue::Null(())
323    }
324}
325
326#[derive(Serialize, Deserialize, Debug)]
327#[serde(rename_all = "camelCase")]
328/// A single sequence row.
329pub struct SequenceRow {
330    /// Row number.
331    pub row_number: i64,
332    /// List of values in order of requested columns.
333    pub values: Vec<SequenceRowValue>,
334}
335
336#[skip_serializing_none]
337#[derive(Serialize, Deserialize, Debug)]
338#[serde(rename_all = "camelCase")]
339/// A simple reference to a sequence column.
340pub struct BasicSequenceColumn {
341    /// Sequence column external ID.
342    pub external_id: String,
343    /// Sequence column name.
344    pub name: Option<String>,
345    /// Column value type.
346    pub value_type: SequenceValueType,
347}
348
349#[skip_serializing_none]
350#[derive(Serialize, Deserialize, Debug)]
351#[serde(rename_all = "camelCase")]
352/// Response when retrieving sequence rows.
353pub struct RetrieveSequenceRowsResponse {
354    /// Internal ID of sequence.
355    pub id: i64,
356    /// External ID of sequence.
357    pub external_id: Option<String>,
358    /// List of requested columns in sequence.
359    pub columns: Vec<BasicSequenceColumn>,
360    /// List of retrieved rows.
361    pub rows: Vec<SequenceRow>,
362    /// Cursor for pagination.
363    pub next_cursor: Option<String>,
364}
365
366#[derive(Serialize, Deserialize, Debug)]
367#[serde(rename_all = "camelCase")]
368/// Request to insert a list of rows into a sequence.
369pub struct InsertSequenceRows {
370    /// Column external IDs to insert into.
371    pub columns: Vec<String>,
372    /// List of rows to insert.
373    pub rows: Vec<SequenceRow>,
374    #[serde(flatten)]
375    /// ID or external ID of sequence to insert into.
376    pub id: Identity,
377}
378
379#[skip_serializing_none]
380#[derive(Serialize, Deserialize, Debug, Default, Clone)]
381#[serde(rename_all = "camelCase")]
382/// Request to retrieve sequence rows for a single sequence.
383pub struct RetrieveSequenceRows {
384    /// Optional lower bound on row numbers.
385    pub start: Option<i64>,
386    /// Optional upper bound on row numbers.
387    pub end: Option<i64>,
388    /// Maximum number of rows to return per request.
389    pub limit: Option<i32>,
390    /// Optional cursor for pagination.
391    pub cursor: Option<String>,
392    /// Columns to retrieve values from.
393    pub columns: Option<Vec<String>>,
394    #[serde(flatten)]
395    /// ID of sequence to retrieve from.
396    pub id: Identity,
397}
398
399#[skip_serializing_none]
400#[derive(Serialize, Deserialize, Debug)]
401#[serde(rename_all = "camelCase")]
402/// Request to retrieve the sequence row with the highest row number.
403pub struct RetrieveLastSequenceRow {
404    /// Columns to retrieve from.
405    pub columns: Option<Vec<String>>,
406    /// Retrieve the row with the highest row number that is smaller than this.
407    pub before: Option<i64>,
408    #[serde(flatten)]
409    /// ID of sequence to retrieve from.
410    pub id: Identity,
411}
412
413#[derive(Serialize, Deserialize, Debug)]
414#[serde(rename_all = "camelCase")]
415/// Request to delete a list of sequence rows from a sequence.
416pub struct DeleteSequenceRows {
417    /// Row numbers to delete.
418    pub rows: Vec<i64>,
419    #[serde(flatten)]
420    /// ID of sequence to delete from.
421    pub id: Identity,
422}