cognite/dto/core/asset/
aggregate.rs

1use serde::{Deserialize, Serialize};
2use serde_with::skip_serializing_none;
3
4use crate::{AdvancedFilter, AggregateFilter};
5
6use super::AssetFilter;
7
8#[derive(Serialize, Deserialize, Debug, Clone, Copy, Eq, PartialEq)]
9#[serde(rename_all = "camelCase")]
10/// Aggregated property on assets.
11pub enum AssetAggregatedProperty {
12    /// The total number of children for each asset.
13    ChildCount,
14    /// The path to the asset from the root node.
15    Path,
16    /// The depth of the asset.
17    Depth,
18}
19
20#[skip_serializing_none]
21#[derive(Serialize, Deserialize, Debug, Clone)]
22#[serde(rename_all = "camelCase")]
23/// Descriptor for asset properties to compute aggregates on.
24pub struct AggregateProperty {
25    /// An array of strings specifying a nested property.
26    property: Vec<String>,
27    /// Filter on which property values to include.
28    filter: Option<AggregateFilter>,
29}
30
31#[skip_serializing_none]
32#[derive(Serialize, Deserialize, Debug, Clone)]
33#[serde(untagged, rename_all_fields = "camelCase")]
34/// Variants of the `count` aggregate on assets.
35pub enum AssetAggregateCount {
36    /// Count the number of assets with a given property (non-null),
37    /// matching the filters.
38    PropertyCount {
39        /// Advanced filter on assets.
40        advanced_filter: Option<AdvancedFilter>,
41        /// Simple filter on assets.
42        filter: Option<AssetFilter>,
43        /// Properties to apply the aggration on. Currently limited to one property per request.
44        properties: Vec<AggregateProperty>,
45    },
46    /// Count the number of assets matching filters.
47    AssetCount {
48        /// Advanced filter on assets.
49        advanced_filter: Option<AdvancedFilter>,
50        /// Simple filter on assets.
51        filter: Option<AssetFilter>,
52    },
53}
54
55#[skip_serializing_none]
56#[derive(Serialize, Deserialize, Debug, Clone, Default)]
57#[serde(rename_all = "camelCase")]
58/// Aggregate query with a list of properties.
59pub struct AggregateWithProperty {
60    /// Filter on aggregate property values.
61    aggregate_filter: Option<AggregateFilter>,
62    /// Advanced filter on assets.
63    advanced_filter: Option<AdvancedFilter>,
64    /// Simple filter on assets.
65    filter: Option<AssetFilter>,
66    /// Properties to apply the aggration on. Currently limited to one property per request.
67    properties: Vec<AggregateProperty>,
68}
69
70#[skip_serializing_none]
71#[derive(Serialize, Deserialize, Debug, Clone, Default)]
72#[serde(rename_all = "camelCase")]
73/// Aggregate query with a path within a metadata object.
74pub struct AggregateWithPath {
75    /// Filter on aggregate property values.
76    aggregate_filter: Option<AggregateFilter>,
77    /// Advanced filter on assets.
78    advanced_filter: Option<AdvancedFilter>,
79    /// Simple filter on assets.
80    filter: Option<AssetFilter>,
81    /// Scope in each document to aggregate properties. Currently the only allowed value is
82    /// `["metadata"]`, meaning aggregates are computed on metadata properties.
83    path: Vec<String>,
84}
85
86#[skip_serializing_none]
87#[derive(Serialize, Deserialize, Debug, Clone)]
88#[serde(rename_all = "camelCase")]
89/// Request for aggregates on assets.
90pub enum AssetAggregateRequest {
91    /// Count the number of assets matching filters.
92    Count(AssetAggregateCount),
93    /// Compute the approximate number of unique values for the specified property.
94    CardinalityValues(AggregateWithProperty),
95    /// Compute the approximate number of unique metadata properties.
96    CardinalityProperties(AggregateWithPath),
97    /// Get up to 1000 unique values for the specified property ordered by frequency.
98    /// Note: when aggregating on metadata, a value may occur multiple times in one asset
99    /// for different metadata keys. Each occurence is counted.
100    UniqueValues(AggregateProperty),
101    /// Get unique metadata keys in a given asset. Ordered by frequency.
102    UniqueProperties(AggregateWithPath),
103}
104
105#[skip_serializing_none]
106#[derive(Serialize, Deserialize, Debug, Clone, Default)]
107#[serde(rename_all = "camelCase")]
108/// Describes a property in an asset.
109pub struct AggregatedProperty {
110    /// Path to the property.
111    property: Vec<String>,
112}
113
114#[skip_serializing_none]
115#[derive(Serialize, Deserialize, Debug, Clone)]
116#[serde(untagged, rename_all_fields = "camelCase")]
117/// Response for an asset aggregation request. The type of result depends
118/// on the requested aggregate.
119pub enum AssetAggregateResponse {
120    /// Aggregate with a list of string values
121    Strings {
122        /// Number of items in this bucket.
123        count: i64,
124        /// Array of unique values in the property.
125        values: Vec<String>,
126    },
127    /// Aggregate with a list of integer values.
128    Integers {
129        /// Number of items in this bucket.
130        count: i64,
131        /// Array of unique values in the property.
132        values: Vec<i64>,
133    },
134    /// A bucket representing the result of the `UniqueProperties` aggregate.
135    Properties {
136        /// Number of items in this bucket.
137        count: i64,
138        /// An array of unique properties.
139        values: Vec<AggregatedProperty>,
140    },
141    /// Aggregate returned when only a simple count is requested.
142    Count {
143        /// Number of items in this aggregation group.
144        count: i64,
145    },
146}