cognite/dto/data_modeling/
records.rs

1pub(crate) mod aggregates;
2
3use serde::{Deserialize, Serialize};
4use serde_with::skip_serializing_none;
5
6use crate::{time_series::TimestampOrRelative, AdvancedFilter, PropertyIdentifier};
7
8use super::{
9    common::{SortDirection, TaggedContainerReference},
10    instances::PropertiesObject,
11};
12
13/// Matches records with the last updated time within the provided range.
14///
15/// The range must include at least a left (gt or gte) bound.
16/// It is not allowed to specify two upper or lower bounds, e.g. gte and gt,
17/// in the same filter.
18#[skip_serializing_none]
19#[derive(Debug, Default, Clone, Serialize, Deserialize)]
20#[serde(rename_all = "camelCase")]
21pub struct LastUpdatedTimeFilter {
22    /// Greater than or equal to
23    pub gte: Option<TimestampOrRelative>,
24    /// Greater than
25    pub gt: Option<TimestampOrRelative>,
26    /// Less than or equal to
27    pub lte: Option<TimestampOrRelative>,
28    /// Less than
29    pub lt: Option<TimestampOrRelative>,
30}
31
32#[derive(Debug, Clone, Serialize, Deserialize)]
33#[serde(rename_all = "camelCase")]
34/// Data to be written to a record.
35/// The `TProperties` type parameter is used to specify the property object.
36/// A generic version could be `HashMap<String, RawValue>`.
37pub struct RecordData<TProperties> {
38    /// The container of the property.
39    pub source: TaggedContainerReference,
40    /// The properties to be written.
41    pub properties: TProperties,
42}
43
44#[derive(Debug, Clone, Serialize, Deserialize)]
45#[serde(rename_all = "camelCase")]
46/// Create/update of a record.
47pub struct RecordWrite<TProperties> {
48    /// The space of the record.
49    pub space: String,
50    /// The external ID of the record.
51    pub external_id: String,
52    /// The properties to be written.
53    pub sources: Vec<RecordData<TProperties>>,
54}
55
56#[derive(Debug, Clone, Serialize, Deserialize)]
57#[serde(rename_all = "camelCase")]
58/// A record retrieved from CDF.
59/// The `TProperties` type parameter is used to specify the property object.
60/// A generic version could be `HashMap<String, RawValue>`.
61pub struct Record<TProperties> {
62    /// The space of the record.
63    pub space: String,
64    /// The external ID of the record.
65    pub external_id: String,
66    /// The properties of the record, as a dictionary from
67    /// space to container to property object.
68    pub properties: PropertiesObject<TProperties>,
69    /// Time this record was created, in milliseconds since epoch.
70    pub created_time: i64,
71    /// Time this record was last modified, in milliseconds since epoch.
72    pub last_updated_time: i64,
73}
74
75#[derive(Debug, Clone, Serialize, Deserialize)]
76#[serde(rename_all = "camelCase")]
77/// Which properties to retrieve from a container.
78pub struct PropertiesPerContainer {
79    /// The container to retrieve properties from.
80    pub source: TaggedContainerReference,
81    /// The properties to retrieve.
82    pub properties: Vec<String>,
83}
84
85#[skip_serializing_none]
86#[derive(Serialize, Deserialize, Clone, Debug)]
87#[serde(rename_all = "camelCase")]
88/// Sort on a dynamic property
89pub struct RecordsPropertySort {
90    /// List of strings representing the property
91    pub property: Vec<String>,
92    /// Direction to sort.
93    pub direction: Option<SortDirection>,
94}
95
96impl RecordsPropertySort {
97    /// Create a new property sort object.
98    ///
99    /// # Arguments
100    ///
101    /// * `property` - Property to sort by
102    /// * `direction` - Direction to sort in.
103    pub fn new(property: impl PropertyIdentifier, direction: SortDirection) -> Self {
104        Self {
105            property: property.into_identifier(),
106            direction: Some(direction),
107        }
108    }
109}
110
111#[skip_serializing_none]
112#[derive(Debug, Clone, Serialize, Deserialize)]
113#[serde(rename_all = "camelCase")]
114/// Request to retrieve records from CDF.
115pub struct RecordsRetrieveRequest {
116    /// The time the record was last updated.
117    pub last_updated_time: LastUpdatedTimeFilter,
118    /// The filter to apply to the records.
119    pub filter: Option<AdvancedFilter>,
120    /// The properties to retrieve.
121    pub sources: Option<Vec<PropertiesPerContainer>>,
122    /// Limit the number of records to retrieve, defaults to 10.
123    pub limit: Option<u32>,
124    /// Optionally sort the records.
125    pub sort: Option<Vec<RecordsPropertySort>>,
126}
127
128#[derive(Debug, Clone, Serialize, Deserialize)]
129#[serde(rename_all = "camelCase")]
130/// An cursor to send to the records API in a sync request.
131pub enum RecordCursor {
132    /// A cursor received from a previous request.
133    Cursor(String),
134    /// The value to use to initialize the cursor.
135    /// On the form `[duration]-ago`, for example `3m-ago`, `1h-ago`, etc.
136    InitializeCursor(String),
137}
138
139#[skip_serializing_none]
140#[derive(Debug, Clone, Serialize, Deserialize)]
141#[serde(rename_all = "camelCase")]
142/// Request to sync records from CDF.
143pub struct RecordsSyncRequest {
144    /// The properties to retrieve.
145    pub sources: Option<Vec<PropertiesPerContainer>>,
146    /// An optional filter to apply to the records.
147    pub filter: Option<AdvancedFilter>,
148    /// Limit the number of records to retrieve, defaults to 10.
149    pub limit: Option<u32>,
150    /// When to initialize the cursor
151    #[serde(flatten)]
152    pub cursor: RecordCursor,
153}
154
155#[derive(Debug, Clone, Serialize, Deserialize)]
156#[serde(rename_all = "camelCase")]
157/// Part of the records sync response, containing `has_next` and `next_cursor`.
158pub struct CursorAndHasNext {
159    /// The cursor to use in the next request.
160    pub next_cursor: String,
161    /// Whether there are more records to retrieve.
162    /// If this is `false`, the client should back off and wait a bit before
163    /// asking again.
164    pub has_next: bool,
165}