const aggregates = await client.events.aggregate.count({ filter: { assetIds: [1, 2, 3] } });
console.log('Number of events: ', aggregates[0].count)
const uniqueValues = await client.events.aggregate.uniqueValues({ filter: { assetIds: [1, 2, 3] }, fields: ['subtype'] });
console.log('Unique values: ', uniqueValues)aggregate_type = client.events.aggregate(filter={"type": "failure"})
from cognite.client.data_classes.events import EventProperty
type_count = client.events.aggregate_cardinality_properties(EventProperty.metadata)
from cognite.client.data_classes.events import EventProperty
type_count = client.events.aggregate_cardinality_values(EventProperty.type)
from cognite.client.data_classes import filters
from cognite.client.data_classes.events import EventProperty
is_asset = filters.ContainsAny(EventProperty.asset_ids, 123)
plain_text_author_count = client.events.aggregate_cardinality_values(EventProperty.type, advanced_filter=is_asset)
count = client.events.aggregate_count()
from cognite.client.data_classes import filters
from cognite.client.data_classes.events import EventProperty
is_workorder = filters.Equals(EventProperty.type, "workorder")
workorder_count = client.events.aggregate_count(advanced_filter=is_workorder)
from cognite.client.data_classes.events import EventProperty
result = client.events.aggregate_unique_properties(EventProperty.metadata)
print(result.unique)
from cognite.client.data_classes.events import EventProperty
result = client.events.aggregate_unique_values(property=EventProperty.type)
print(result.unique)
from cognite.client.data_classes import filters
from cognite.client.data_classes.events import EventProperty
from cognite.client.utils import timestamp_to_ms
from datetime import datetime
is_after_2020 = filters.Range(EventProperty.start_time, gte=timestamp_to_ms(datetime(2020, 1, 1)))
result = client.events.aggregate_unique_values(EventProperty.type, advanced_filter=is_after_2020)
print(result.unique)
from cognite.client.data_classes.events import EventProperty
from cognite.client.data_classes import aggregations
agg = aggregations
not_planned = agg.Not(agg.Prefix("planned"))
is_after_2020 = filters.Range(EventProperty.start_time, gte=timestamp_to_ms(datetime(2020, 1, 1)))
result = client.events.aggregate_unique_values(EventProperty.type, advanced_filter=is_after_2020, aggregate_filter=not_planned)
print(result.unique)Aggregate aggregateResult =
client.events().aggregate(Request.create().withFilterParameter("source", "source"));curl --request POST \
--url https://{cluster}.cognitedata.com/api/v1/projects/{project}/events/aggregate \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"aggregate": "count",
"advancedFilter": {
"and": "<array>"
},
"filter": {
"startTime": {
"max": 1,
"min": 1
},
"endTime": {
"max": 1,
"min": 1
},
"activeAtTime": {
"max": 1,
"min": 1
},
"metadata": {},
"assetIds": [
4503599627370496
],
"assetExternalIds": [
"my.known.id"
],
"assetSubtreeIds": [
{
"id": 4503599627370496
}
],
"dataSetIds": [
{
"id": 4503599627370496
}
],
"source": "<string>",
"type": "<string>",
"subtype": "<string>",
"createdTime": {
"max": 1,
"min": 1
},
"lastUpdatedTime": {
"max": 1,
"min": 1
},
"externalIdPrefix": "my.known.prefix"
}
}
'<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://{cluster}.cognitedata.com/api/v1/projects/{project}/events/aggregate",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'aggregate' => 'count',
'advancedFilter' => [
'and' => '<array>'
],
'filter' => [
'startTime' => [
'max' => 1,
'min' => 1
],
'endTime' => [
'max' => 1,
'min' => 1
],
'activeAtTime' => [
'max' => 1,
'min' => 1
],
'metadata' => [
],
'assetIds' => [
4503599627370496
],
'assetExternalIds' => [
'my.known.id'
],
'assetSubtreeIds' => [
[
'id' => 4503599627370496
]
],
'dataSetIds' => [
[
'id' => 4503599627370496
]
],
'source' => '<string>',
'type' => '<string>',
'subtype' => '<string>',
'createdTime' => [
'max' => 1,
'min' => 1
],
'lastUpdatedTime' => [
'max' => 1,
'min' => 1
],
'externalIdPrefix' => 'my.known.prefix'
]
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://{cluster}.cognitedata.com/api/v1/projects/{project}/events/aggregate"
payload := strings.NewReader("{\n \"aggregate\": \"count\",\n \"advancedFilter\": {\n \"and\": \"<array>\"\n },\n \"filter\": {\n \"startTime\": {\n \"max\": 1,\n \"min\": 1\n },\n \"endTime\": {\n \"max\": 1,\n \"min\": 1\n },\n \"activeAtTime\": {\n \"max\": 1,\n \"min\": 1\n },\n \"metadata\": {},\n \"assetIds\": [\n 4503599627370496\n ],\n \"assetExternalIds\": [\n \"my.known.id\"\n ],\n \"assetSubtreeIds\": [\n {\n \"id\": 4503599627370496\n }\n ],\n \"dataSetIds\": [\n {\n \"id\": 4503599627370496\n }\n ],\n \"source\": \"<string>\",\n \"type\": \"<string>\",\n \"subtype\": \"<string>\",\n \"createdTime\": {\n \"max\": 1,\n \"min\": 1\n },\n \"lastUpdatedTime\": {\n \"max\": 1,\n \"min\": 1\n },\n \"externalIdPrefix\": \"my.known.prefix\"\n }\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}require 'uri'
require 'net/http'
url = URI("https://{cluster}.cognitedata.com/api/v1/projects/{project}/events/aggregate")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"aggregate\": \"count\",\n \"advancedFilter\": {\n \"and\": \"<array>\"\n },\n \"filter\": {\n \"startTime\": {\n \"max\": 1,\n \"min\": 1\n },\n \"endTime\": {\n \"max\": 1,\n \"min\": 1\n },\n \"activeAtTime\": {\n \"max\": 1,\n \"min\": 1\n },\n \"metadata\": {},\n \"assetIds\": [\n 4503599627370496\n ],\n \"assetExternalIds\": [\n \"my.known.id\"\n ],\n \"assetSubtreeIds\": [\n {\n \"id\": 4503599627370496\n }\n ],\n \"dataSetIds\": [\n {\n \"id\": 4503599627370496\n }\n ],\n \"source\": \"<string>\",\n \"type\": \"<string>\",\n \"subtype\": \"<string>\",\n \"createdTime\": {\n \"max\": 1,\n \"min\": 1\n },\n \"lastUpdatedTime\": {\n \"max\": 1,\n \"min\": 1\n },\n \"externalIdPrefix\": \"my.known.prefix\"\n }\n}"
response = http.request(request)
puts response.read_body{
"items": [
{
"count": 10
}
]
}{
"error": {
"code": 400,
"message": "`null` values aren't allowed."
}
}{
"error": {
"code": 429,
"message": "Project exceeded maximum number='50' of concurrent requests. Please try again later."
}
}Aggregate events
Required capabilities:
eventsAcl:READ
The aggregation API lets you compute aggregated results on events, such as getting the count of all Events in a project, checking different descriptions of events in your project, etc.
Aggregate filtering
Filter (filter & advancedFilter) data for aggregates
Filters behave the same way as for the Filter events endpoint.
In text properties, the values are aggregated in a case-insensitive manner.
aggregateFilter to filter aggregate results
aggregateFilter works similarly to advancedFilter but always applies to aggregate properties.
For instance, in an aggregation for the source property, only the values (aka buckets) of the source property can be filtered out.
Request throttling
This endpoint is meant for data analytics/exploration usage and is not suitable for high load data retrieval usage.
The Aggregates endpoint, as with all endpoints in the Events API, is subject to a request budget that applies
limits to both request rate and concurrency.
Please check Events resource description for more information.
const aggregates = await client.events.aggregate.count({ filter: { assetIds: [1, 2, 3] } });
console.log('Number of events: ', aggregates[0].count)
const uniqueValues = await client.events.aggregate.uniqueValues({ filter: { assetIds: [1, 2, 3] }, fields: ['subtype'] });
console.log('Unique values: ', uniqueValues)aggregate_type = client.events.aggregate(filter={"type": "failure"})
from cognite.client.data_classes.events import EventProperty
type_count = client.events.aggregate_cardinality_properties(EventProperty.metadata)
from cognite.client.data_classes.events import EventProperty
type_count = client.events.aggregate_cardinality_values(EventProperty.type)
from cognite.client.data_classes import filters
from cognite.client.data_classes.events import EventProperty
is_asset = filters.ContainsAny(EventProperty.asset_ids, 123)
plain_text_author_count = client.events.aggregate_cardinality_values(EventProperty.type, advanced_filter=is_asset)
count = client.events.aggregate_count()
from cognite.client.data_classes import filters
from cognite.client.data_classes.events import EventProperty
is_workorder = filters.Equals(EventProperty.type, "workorder")
workorder_count = client.events.aggregate_count(advanced_filter=is_workorder)
from cognite.client.data_classes.events import EventProperty
result = client.events.aggregate_unique_properties(EventProperty.metadata)
print(result.unique)
from cognite.client.data_classes.events import EventProperty
result = client.events.aggregate_unique_values(property=EventProperty.type)
print(result.unique)
from cognite.client.data_classes import filters
from cognite.client.data_classes.events import EventProperty
from cognite.client.utils import timestamp_to_ms
from datetime import datetime
is_after_2020 = filters.Range(EventProperty.start_time, gte=timestamp_to_ms(datetime(2020, 1, 1)))
result = client.events.aggregate_unique_values(EventProperty.type, advanced_filter=is_after_2020)
print(result.unique)
from cognite.client.data_classes.events import EventProperty
from cognite.client.data_classes import aggregations
agg = aggregations
not_planned = agg.Not(agg.Prefix("planned"))
is_after_2020 = filters.Range(EventProperty.start_time, gte=timestamp_to_ms(datetime(2020, 1, 1)))
result = client.events.aggregate_unique_values(EventProperty.type, advanced_filter=is_after_2020, aggregate_filter=not_planned)
print(result.unique)Aggregate aggregateResult =
client.events().aggregate(Request.create().withFilterParameter("source", "source"));curl --request POST \
--url https://{cluster}.cognitedata.com/api/v1/projects/{project}/events/aggregate \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"aggregate": "count",
"advancedFilter": {
"and": "<array>"
},
"filter": {
"startTime": {
"max": 1,
"min": 1
},
"endTime": {
"max": 1,
"min": 1
},
"activeAtTime": {
"max": 1,
"min": 1
},
"metadata": {},
"assetIds": [
4503599627370496
],
"assetExternalIds": [
"my.known.id"
],
"assetSubtreeIds": [
{
"id": 4503599627370496
}
],
"dataSetIds": [
{
"id": 4503599627370496
}
],
"source": "<string>",
"type": "<string>",
"subtype": "<string>",
"createdTime": {
"max": 1,
"min": 1
},
"lastUpdatedTime": {
"max": 1,
"min": 1
},
"externalIdPrefix": "my.known.prefix"
}
}
'<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://{cluster}.cognitedata.com/api/v1/projects/{project}/events/aggregate",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'aggregate' => 'count',
'advancedFilter' => [
'and' => '<array>'
],
'filter' => [
'startTime' => [
'max' => 1,
'min' => 1
],
'endTime' => [
'max' => 1,
'min' => 1
],
'activeAtTime' => [
'max' => 1,
'min' => 1
],
'metadata' => [
],
'assetIds' => [
4503599627370496
],
'assetExternalIds' => [
'my.known.id'
],
'assetSubtreeIds' => [
[
'id' => 4503599627370496
]
],
'dataSetIds' => [
[
'id' => 4503599627370496
]
],
'source' => '<string>',
'type' => '<string>',
'subtype' => '<string>',
'createdTime' => [
'max' => 1,
'min' => 1
],
'lastUpdatedTime' => [
'max' => 1,
'min' => 1
],
'externalIdPrefix' => 'my.known.prefix'
]
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://{cluster}.cognitedata.com/api/v1/projects/{project}/events/aggregate"
payload := strings.NewReader("{\n \"aggregate\": \"count\",\n \"advancedFilter\": {\n \"and\": \"<array>\"\n },\n \"filter\": {\n \"startTime\": {\n \"max\": 1,\n \"min\": 1\n },\n \"endTime\": {\n \"max\": 1,\n \"min\": 1\n },\n \"activeAtTime\": {\n \"max\": 1,\n \"min\": 1\n },\n \"metadata\": {},\n \"assetIds\": [\n 4503599627370496\n ],\n \"assetExternalIds\": [\n \"my.known.id\"\n ],\n \"assetSubtreeIds\": [\n {\n \"id\": 4503599627370496\n }\n ],\n \"dataSetIds\": [\n {\n \"id\": 4503599627370496\n }\n ],\n \"source\": \"<string>\",\n \"type\": \"<string>\",\n \"subtype\": \"<string>\",\n \"createdTime\": {\n \"max\": 1,\n \"min\": 1\n },\n \"lastUpdatedTime\": {\n \"max\": 1,\n \"min\": 1\n },\n \"externalIdPrefix\": \"my.known.prefix\"\n }\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}require 'uri'
require 'net/http'
url = URI("https://{cluster}.cognitedata.com/api/v1/projects/{project}/events/aggregate")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"aggregate\": \"count\",\n \"advancedFilter\": {\n \"and\": \"<array>\"\n },\n \"filter\": {\n \"startTime\": {\n \"max\": 1,\n \"min\": 1\n },\n \"endTime\": {\n \"max\": 1,\n \"min\": 1\n },\n \"activeAtTime\": {\n \"max\": 1,\n \"min\": 1\n },\n \"metadata\": {},\n \"assetIds\": [\n 4503599627370496\n ],\n \"assetExternalIds\": [\n \"my.known.id\"\n ],\n \"assetSubtreeIds\": [\n {\n \"id\": 4503599627370496\n }\n ],\n \"dataSetIds\": [\n {\n \"id\": 4503599627370496\n }\n ],\n \"source\": \"<string>\",\n \"type\": \"<string>\",\n \"subtype\": \"<string>\",\n \"createdTime\": {\n \"max\": 1,\n \"min\": 1\n },\n \"lastUpdatedTime\": {\n \"max\": 1,\n \"min\": 1\n },\n \"externalIdPrefix\": \"my.known.prefix\"\n }\n}"
response = http.request(request)
puts response.read_body{
"items": [
{
"count": 10
}
]
}{
"error": {
"code": 400,
"message": "`null` values aren't allowed."
}
}{
"error": {
"code": 429,
"message": "Project exceeded maximum number='50' of concurrent requests. Please try again later."
}
}Authorizations
Access token issued by the CDF project's configured identity provider. Access token must be an OpenID Connect token, and the project must be configured to accept OpenID Connect tokens. Use a header key of 'Authorization' with a value of 'Bearer $accesstoken'. The token can be obtained through any flow supported by the identity provider.
Body
- EventCount
- EventWithPropertyCount
- ApproximateCardinalityForValues
- ApproximateCardinalityForProperties
- WithProperties
- WithFields
- UniqueProperties
Request aggregate to count the number of Events matching the filters. Default aggregate for the endpoint.
Type of aggregation to apply.
count: Get an approximate number of Events matching the filters.
count A query that matches items matching boolean combinations of other queries.
It's built using one or more boolean clauses of the following types: and, or, or not
- and
- or
- not
- equals
- in
- range
- prefix
- exists
- containsAny
- containsAll
- search
Show child attributes
Show child attributes
Filter on events filter with exact match
Show child attributes
Show child attributes
Response
Response with a list of aggregation results.
- CountResult
- BucketsResult
Common aggregate structure to represent aggregate result which have one count for filter resultset (Documents Count, Property Cardinality, etc).
1 elementShow child attributes
Show child attributes
Was this page helpful?