cognite/dto/data_organization/
relationships.rs1use serde::{Deserialize, Serialize};
2use serde_json::json;
3use serde_with::skip_serializing_none;
4
5use crate::sequences::Sequence;
6use crate::time_series::TimeSeries;
7use crate::{
8 CogniteExternalId, Identity, IntoPatch, IntoPatchItem, LabelsFilter, Partition, Patch, Range,
9 SetCursor, UpdateList, UpdateSet, UpdateSetNull, UpsertOptions, WithPartition,
10};
11
12use crate::{assets::Asset, events::Event, files::FileMetadata};
13
14#[derive(Serialize, Deserialize, Clone, Copy, Debug, Default)]
15#[serde(rename_all = "camelCase")]
16pub enum RelationshipVertexType {
18 #[default]
19 Asset,
21 TimeSeries,
23 File,
25 Event,
27 Sequence,
29}
30
31#[derive(Serialize, Deserialize, Debug)]
32#[serde(untagged)]
33pub enum RelationshipVertex {
35 Asset(Asset),
37 TimeSeries(TimeSeries),
39 File(FileMetadata),
41 Event(Event),
43 Sequence(Sequence),
45}
46
47#[skip_serializing_none]
48#[derive(Serialize, Deserialize)]
49#[serde(rename_all = "camelCase")]
50pub struct Relationship {
52 pub external_id: String,
54 pub source_external_id: String,
56 pub source_type: RelationshipVertexType,
58 pub target_external_id: String,
60 pub target_type: RelationshipVertexType,
62 pub start_time: Option<i64>,
64 pub end_time: Option<i64>,
66 pub confidence: Option<f32>,
71 pub data_set_id: Option<i64>,
73 pub labels: Option<Vec<CogniteExternalId>>,
75 pub created_time: Option<i64>,
77 pub last_updated_time: Option<i64>,
79 pub source: Option<RelationshipVertex>,
81 pub target: Option<RelationshipVertex>,
83}
84
85#[skip_serializing_none]
86#[derive(Serialize, Deserialize, Default)]
87#[serde(rename_all = "camelCase")]
88pub struct AddRelationship {
90 pub external_id: String,
92 pub source_external_id: String,
94 pub source_type: RelationshipVertexType,
96 pub target_external_id: String,
98 pub target_type: RelationshipVertexType,
100 pub start_time: Option<i64>,
102 pub end_time: Option<i64>,
104 pub confidence: Option<f32>,
109 pub data_set_id: Option<i64>,
111 pub labels: Option<Vec<CogniteExternalId>>,
113}
114
115impl From<Relationship> for AddRelationship {
116 fn from(rel: Relationship) -> Self {
117 AddRelationship {
118 external_id: rel.external_id,
119 source_external_id: rel.source_external_id,
120 source_type: rel.source_type,
121 target_external_id: rel.target_external_id,
122 target_type: rel.target_type,
123 start_time: rel.start_time,
124 end_time: rel.end_time,
125 confidence: rel.confidence,
126 data_set_id: rel.data_set_id,
127 labels: rel.labels,
128 }
129 }
130}
131
132#[skip_serializing_none]
133#[derive(Serialize, Deserialize, Default, Clone)]
134#[serde(rename_all = "camelCase")]
135pub struct PatchRelationship {
137 pub source_type: Option<UpdateSet<RelationshipVertexType>>,
139 pub source_external_id: Option<UpdateSet<String>>,
141 pub target_type: Option<UpdateSet<RelationshipVertexType>>,
143 pub target_external_id: Option<UpdateSet<String>>,
145 pub confidence: Option<UpdateSetNull<f32>>,
150 pub start_time: Option<UpdateSetNull<i64>>,
152 pub end_time: Option<UpdateSetNull<i64>>,
154 pub data_set_id: Option<UpdateSetNull<i64>>,
156 pub labels: Option<UpdateList<CogniteExternalId, CogniteExternalId>>,
158}
159
160impl IntoPatch<Patch<PatchRelationship>> for Relationship {
161 fn patch(self, options: &UpsertOptions) -> Patch<PatchRelationship> {
162 Patch::<PatchRelationship> {
163 id: Identity::ExternalId {
164 external_id: self.external_id,
165 },
166 update: PatchRelationship {
167 source_type: self.source_type.patch(options),
168 source_external_id: self.source_external_id.patch(options),
169 target_type: self.target_type.patch(options),
170 target_external_id: self.target_external_id.patch(options),
171 confidence: self.confidence.patch(options),
172 start_time: self.start_time.patch(options),
173 end_time: self.end_time.patch(options),
174 data_set_id: self.data_set_id.patch(options),
175 labels: self.labels.patch(options),
176 },
177 }
178 }
179}
180
181impl IntoPatch<PatchRelationship> for AddRelationship {
182 fn patch(self, options: &UpsertOptions) -> PatchRelationship {
183 PatchRelationship {
184 source_type: self.source_type.patch(options),
185 source_external_id: self.source_external_id.patch(options),
186 target_type: self.target_type.patch(options),
187 target_external_id: self.target_external_id.patch(options),
188 confidence: self.confidence.patch(options),
189 start_time: self.start_time.patch(options),
190 end_time: self.end_time.patch(options),
191 data_set_id: self.data_set_id.patch(options),
192 labels: self.labels.patch(options),
193 }
194 }
195}
196
197impl From<Relationship> for Patch<PatchRelationship> {
198 fn from(rel: Relationship) -> Self {
199 IntoPatch::<Patch<PatchRelationship>>::patch(rel, &Default::default())
200 }
201}
202
203#[derive(Serialize, Deserialize, Debug, Clone)]
204#[serde(rename_all = "camelCase")]
205pub(crate) struct RetrieveRelationshipsRequest {
206 pub items: ::serde_json::Value,
207 pub ignore_unknown_ids: bool,
208 pub fetch_resources: bool,
209}
210
211impl<T: Serialize> From<&Vec<T>> for RetrieveRelationshipsRequest {
212 fn from(items: &Vec<T>) -> RetrieveRelationshipsRequest {
213 RetrieveRelationshipsRequest {
214 items: json!(items),
215 ignore_unknown_ids: true,
216 fetch_resources: false,
217 }
218 }
219}
220
221impl<T: Serialize> From<&[T]> for RetrieveRelationshipsRequest {
222 fn from(items: &[T]) -> RetrieveRelationshipsRequest {
223 RetrieveRelationshipsRequest {
224 items: json!(items),
225 ignore_unknown_ids: true,
226 fetch_resources: false,
227 }
228 }
229}
230
231#[derive(Serialize, Debug, Clone)]
232#[serde(rename_all = "camelCase")]
233pub struct SourceOrTargetFilter {
235 pub r#type: RelationshipVertexType,
237 pub external_id: String,
239}
240
241#[skip_serializing_none]
242#[derive(Serialize, Default, Debug, Clone)]
243#[serde(rename_all = "camelCase")]
244pub struct RelationshipsFilter {
246 pub source_external_ids: Option<Vec<String>>,
248 pub source_types: Option<Vec<RelationshipVertexType>>,
250 pub target_external_ids: Option<Vec<String>>,
252 pub target_types: Option<Vec<RelationshipVertexType>>,
254 pub data_set_ids: Option<Vec<Identity>>,
256 pub start_time: Option<Range<i64>>,
258 pub end_time: Option<Range<i64>>,
260 pub confidence: Option<Range<f64>>,
262 pub last_updated_time: Option<Range<i64>>,
264 pub created_time: Option<Range<i64>>,
266 pub active_at_time: Option<Range<i64>>,
276 pub sources_or_targets: Option<Vec<SourceOrTargetFilter>>,
279 pub labels: Option<LabelsFilter>,
281}
282
283#[skip_serializing_none]
284#[derive(Serialize, Default, Clone)]
285#[serde(rename_all = "camelCase")]
286pub struct FilterRelationshipsQuery {
288 pub filter: RelationshipsFilter,
290 pub limit: Option<u32>,
292 pub cursor: Option<String>,
294 pub fetch_resources: Option<bool>,
296 pub partition: Option<Partition>,
298}
299
300impl SetCursor for FilterRelationshipsQuery {
301 fn set_cursor(&mut self, cursor: Option<String>) {
302 self.cursor = cursor;
303 }
304}
305
306impl WithPartition for FilterRelationshipsQuery {
307 fn with_partition(&self, partition: Partition) -> Self {
308 Self {
309 filter: self.filter.clone(),
310 limit: self.limit,
311 cursor: None,
312 fetch_resources: self.fetch_resources,
313 partition: Some(partition),
314 }
315 }
316}