aggregateWindow() function

The aggregateWindow() function applies an aggregate or selector function (any function with a column parameter) to fixed windows of time.

*Function type: Aggregate*

  1. aggregateWindow(
  2. every: 1m,
  3. fn: mean,
  4. column: "_value",
  5. timeSrc: "_stop",
  6. timeDst: "_time",
  7. createEmpty: true
  8. )

As data is windowed into separate tables and processed, the _time column is dropped from each group key. This function copies the timestamp from a remaining column into the _time column. View the function definition.

aggregateWindow() restores the original _start and _stop values of input data and, by default, uses _stop to set the _time value for each aggregated window. Each row in the output of aggregateWindow represents an aggregated window ending at _time.

Parameters

Make sure fn parameter names match each specified parameter. To learn why, see Match parameter names.

every

The duration of windows.

Calendar months and years

every supports all valid duration units, including calendar months (1mo) and years (1y).

*Data type: Duration*

fn

The aggregate function used in the operation.

*Data type: Function*

Only aggregate and selector functions with a column parameter (singular) work with aggregateWindow().

column

The column on which to operate. Defaults to "_value".

*Data type: String*

timeSrc

The time column from which time is copied for the aggregate record. Defaults to "_stop".

*Data type: String*

timeDst

The “time destination” column to which time is copied for the aggregate record. Defaults to "_time".

*Data type: String*

createEmpty

For windows without data, this will create an empty window and fill it with a null aggregate value. Defaults to true.

*Data type: Boolean*

Examples

The examples below use a data variable to represent a filtered data set.

  1. data = from(bucket: "example-bucket")
  2. |> range(start: -1h)
  3. |> filter(fn: (r) =>
  4. r._measurement == "mem" and
  5. r._field == "used_percent")
Use an aggregate function with default parameters

The following example uses the default parameters of the mean() function to aggregate time-based windows:

  1. data
  2. |> aggregateWindow(
  3. every: 5m,
  4. fn: mean
  5. )
Specify parameters of the aggregate function

To use functions that don’t provide defaults for required parameters with aggregateWindow(), define an anonymous function with column and tables parameters that pipes-forward tables into the aggregate or selector function with all required parameters defined:

  1. data
  2. |> aggregateWindow(
  3. column: "_value",
  4. every: 5m,
  5. fn: (column, tables=<-) => tables |> quantile(q: 0.99, column:column)
  6. )
Window and aggregate by calendar month
  1. data
  2. |> aggregateWindow(every: 1mo, fn: mean)

Function definition

  1. aggregateWindow = (every, fn, column="_value", timeSrc="_stop", timeDst="_time", tables=<-) =>
  2. tables
  3. |> window(every:every)
  4. |> fn(column:column)
  5. |> duplicate(column:timeSrc, as:timeDst)
  6. |> window(every:inf, timeColumn:timeDst)

Related articles