Aggregates
Aggregates summarise the items matching a query instead of returning them. Each Aggregate
subclass here represents one aggregate and serialises to the request body the API expects; each
Result subclass is what that aggregate comes back as.
The classes are shared between the data modeling aggregate endpoints and are therefore not named
after any single one of them. Properties are referenced by their full container path, e.g.
Average(["mySpace", "myContainer", "temp"]). Import the module by its own path:
from cognite.client.data_classes.data_modeling import aggregates as aggs
res = client.data_modeling.records.aggregate(
stream_id="my-stream",
aggregates={
"per_day": aggs.TimeHistogram(
["mySpace", "Game", "startTime"],
calendar_interval="1d",
aggregates={"games": aggs.Count()},
),
},
)
per_day = res["per_day"] # aggs.TimeHistogramResult
for bucket in per_day.buckets:
print(bucket.interval_start, bucket.aggregates["games"].value)
Note
These are not the same classes as the identically named ones in
cognite.client.data_classes.aggregations, which belong to the instances.aggregate
endpoint and reference view properties by identifier instead.
Aggregate data classes
Typed aggregate request builders and results shared by the data modeling aggregate endpoints.
The classes here describe wire shapes that are endpoint-independent, so they are deliberately not
named after any single endpoint: Records uses them today, and the upcoming data modeling aggregate
endpoint - which is modelled on the Records one - will reuse them. Aggregate and its
subclasses are the request side; Result and its subclasses are what the API returns.
Nothing in this module is exported through cognite.client.data_classes.data_modeling. That
namespace already re-exports cognite.client.data_classes.aggregations, which defines its own
Average, Count, Min, Max and Sum for instances.aggregate. Keeping this module
reachable only by its own path is what lets the two families share those names without either
shadowing the other. Import it as:
from cognite.client.data_classes.data_modeling import aggregates as aggs
The two families are independent: Aggregate is unrelated to the legacy Aggregation, and
because both use the same wire keys (avg, count, …), Aggregate.load() only ever
returns members of this family.
- class cognite.client.data_classes.data_modeling.aggregates.Aggregate
Bases:
CogniteResourceBase class for typed aggregate request builders.
Aggregates are request bodies: they serialize via
dump()and can be loaded back from that same representation viaload(), so an aggregate spec round-trips through a config file.- dump(camel_case: bool = True) dict[str, Any]
Dump the instance into a json serializable Python data type.
- Parameters:
camel_case (bool) – Use camelCase for attribute names. Defaults to True.
- Returns:
A dictionary representation of the instance.
- Return type:
dict[str, Any]
- class cognite.client.data_classes.data_modeling.aggregates.Average(property: SequenceNotStr[str])
Bases:
AggregateAverage aggregate over a container property.
- class cognite.client.data_classes.data_modeling.aggregates.Bucket(
- count: int,
- value: Any | None = None,
- interval_start: float | str | None = None,
- aggregates: dict[str, Result] | None = None,
Bases:
CogniteResourceOne bucket of a
BucketResult.- Parameters:
count (int) – Number of matched items in this bucket.
value (Any) – The unique property value this bucket represents, for
UniqueValues.interval_start (float | str | None) – Lower bound of this bucket, for the histograms.
aggregates (dict[str, Result] | None) – Results of the aggregates nested under this bucket, keyed by the client-defined aggregate IDs.
- dump(camel_case: bool = True) dict[str, Any]
Dump the instance into a json serializable Python data type.
- Parameters:
camel_case (bool) – Use camelCase for attribute names. Defaults to True.
- Returns:
A dictionary representation of the instance.
- Return type:
dict[str, Any]
- class cognite.client.data_classes.data_modeling.aggregates.BucketResult(
- buckets: Sequence[Bucket],
Bases:
ResultResult of an aggregate that buckets the matched items.
- Parameters:
buckets (Sequence[Bucket]) – The buckets the aggregate produced.
- dump(camel_case: bool = True) dict[str, Any]
Dump the instance into a json serializable Python data type.
- Parameters:
camel_case (bool) – Use camelCase for attribute names. Defaults to True.
- Returns:
A dictionary representation of the instance.
- Return type:
dict[str, Any]
- class cognite.client.data_classes.data_modeling.aggregates.Count(
- property: SequenceNotStr[str] | None = None,
Bases:
AggregateCount matched items, or non-null values when
propertyis provided.
- class cognite.client.data_classes.data_modeling.aggregates.Filters(
- filters: Sequence[Filter | dict[str, Any]],
- aggregates: Mapping[str, Aggregate | dict[str, Any]] | None = None,
Bases:
AggregateBucket matched items by a list of filter expressions.
- class cognite.client.data_classes.data_modeling.aggregates.FiltersResult(
- buckets: Sequence[Bucket],
Bases:
BucketResultResult of a
Filtersaggregate.
- class cognite.client.data_classes.data_modeling.aggregates.Max(property: SequenceNotStr[str])
Bases:
AggregateMaximum aggregate over a property.
- class cognite.client.data_classes.data_modeling.aggregates.MetricResult(aggregate: str, value: Any)
Bases:
ResultResult of a metric aggregate (
Average,Count,Min,Max,Sum).- dump(camel_case: bool = True) dict[str, Any]
Dump the instance into a json serializable Python data type.
- Parameters:
camel_case (bool) – Use camelCase for attribute names. Defaults to True.
- Returns:
A dictionary representation of the instance.
- Return type:
dict[str, Any]
- class cognite.client.data_classes.data_modeling.aggregates.Min(property: SequenceNotStr[str])
Bases:
AggregateMinimum aggregate over a property.
- class cognite.client.data_classes.data_modeling.aggregates.MovingFunction(
- buckets_path: str,
- window: int,
- function: MovingFunctions,
Bases:
AggregateSmooth a histogram’s buckets by re-aggregating them over a sliding window.
Nested inside a
NumberHistogramorTimeHistogram, it reads one of that histogram’s own aggregates bucket by bucket and reduces everywindowconsecutive buckets to one value - a 7-day moving average of a daily count, say - so a trend stays readable through the noise in individual buckets.buckets_pathnames the sibling aggregate to read, or"_count"for the buckets’ own item counts.
- class cognite.client.data_classes.data_modeling.aggregates.MovingFunctionResult(fn_value: float)
Bases:
ResultResult of a
MovingFunctionaggregate.- dump(camel_case: bool = True) dict[str, Any]
Dump the instance into a json serializable Python data type.
- Parameters:
camel_case (bool) – Use camelCase for attribute names. Defaults to True.
- Returns:
A dictionary representation of the instance.
- Return type:
dict[str, Any]
- class cognite.client.data_classes.data_modeling.aggregates.MovingFunctions(value)
Bases:
str,EnumHow a
MovingFunctionreduces each window of buckets to a single number.MAX,MINandSUMtake that statistic over the window.UNWEIGHTED_AVGaverages the window;LINEAR_WEIGHTED_AVGaverages it too, but weights the most recent buckets highest, so it follows a trend more closely at the cost of being noisier.The wire values carry a
MovingFunctions.prefix, but the unprefixed suffix (e.g."sum") is also accepted, soMovingFunctions("sum") is MovingFunctions.SUM.
- class cognite.client.data_classes.data_modeling.aggregates.NumberHistogram(
- property: SequenceNotStr[str],
- interval: float,
- aggregates: Mapping[str, Aggregate | dict[str, Any]] | None = None,
- hard_bounds: Mapping[str, float] | None = None,
Bases:
AggregateBucket numeric property values into fixed-width intervals.
- class cognite.client.data_classes.data_modeling.aggregates.NumberHistogramResult(
- buckets: Sequence[Bucket],
Bases:
BucketResultResult of a
NumberHistogramaggregate.
- class cognite.client.data_classes.data_modeling.aggregates.Result
Bases:
CogniteResourceBase class for typed aggregate results.
One result type corresponds to each request builder above;
load()dispatches on the wire key the API returns.- dump(camel_case: bool = True) dict[str, Any]
Dump the instance into a json serializable Python data type.
- Parameters:
camel_case (bool) – Use camelCase for attribute names. Defaults to True.
- Returns:
A dictionary representation of the instance.
- Return type:
dict[str, Any]
- class cognite.client.data_classes.data_modeling.aggregates.Sum(property: SequenceNotStr[str])
Bases:
AggregateSum aggregate over a container property.
- class cognite.client.data_classes.data_modeling.aggregates.TimeHistogram(
- property: SequenceNotStr[str],
- *,
- calendar_interval: str | None = None,
- fixed_interval: str | None = None,
- aggregates: Mapping[str, Aggregate | dict[str, Any]] | None = None,
- hard_bounds: Mapping[str, str] | None = None,
Bases:
AggregateBucket timestamp values into calendar or fixed time intervals.
- class cognite.client.data_classes.data_modeling.aggregates.TimeHistogramResult(
- buckets: Sequence[Bucket],
Bases:
BucketResultResult of a
TimeHistogramaggregate.
- class cognite.client.data_classes.data_modeling.aggregates.UniqueValues(
- property: SequenceNotStr[str],
- aggregates: Mapping[str, Aggregate | dict[str, Any]] | None = None,
- size: int | None = None,
Bases:
AggregateBucket matched items by unique property values.
- class cognite.client.data_classes.data_modeling.aggregates.UniqueValuesResult(
- buckets: Sequence[Bucket],
Bases:
BucketResultResult of a
UniqueValuesaggregate.
- class cognite.client.data_classes.data_modeling.aggregates.UnknownAggregate(raw: dict[str, Any])
Bases:
AggregateFallback for aggregate request shapes this SDK version does not model yet.
Preserves the raw request body verbatim so an unknown or newer aggregate type still round-trips through
dump()/load()instead of failing. The request builders’dump()is always camelCase, so the payload is returned as-is regardless ofcamel_case.- dump(camel_case: bool = True) dict[str, Any]
Dump the instance into a json serializable Python data type.
- Parameters:
camel_case (bool) – Use camelCase for attribute names. Defaults to True.
- Returns:
A dictionary representation of the instance.
- Return type:
dict[str, Any]
- class cognite.client.data_classes.data_modeling.aggregates.UnknownResult(raw_result: dict[str, Any])
Bases:
ResultFallback for aggregate result shapes this SDK version does not model yet.
Preserves the raw payload verbatim so nothing is lost, snake-casing the API keys on request.
- dump(camel_case: bool = True) dict[str, Any]
Dump the instance into a json serializable Python data type.
- Parameters:
camel_case (bool) – Use camelCase for attribute names. Defaults to True.
- Returns:
A dictionary representation of the instance.
- Return type:
dict[str, Any]