cognite/dto/core/event/
aggregate.rs

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