Documentation

aggregateWindow() function

Flux 0.7.0+

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

aggregateWindow(
  every: 1m,
  period: 1m,
  fn: mean,
  column: "_value",
  timeSrc: "_stop",
  timeDst: "_time",
  location: "UTC",
  createEmpty: true
)

aggregateWindow() requires that input data have _start and _stop columns to calculate windows of time to operate on. Use range() to assign _start and _stop values.

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.

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

Duration of windows.

Calendar months and years

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

Aggregate by week

When aggregating by week (1w), weeks are determined using the Unix epoch (1970-01-01T00:00:00Z UTC). The Unix epoch was on a Thursday, so all calculated weeks begin on Thursday.

period

Duration of the window. Period is the length of each interval. The period can be negative, indicating the start and stop boundaries are reversed. Defaults to every value.

fn

Aggregate or selector function used to operate on each window of time.

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

column

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

timeSrc

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

timeDst

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

location

Location used to determine timezone. Default is the location option.

Flux uses the timezone database (commonly referred to as “tz” or “zoneinfo”) provided by the operating system.

createEmpty

For windows without data, create a single-row table for each empty window (using table.fill()). Defaults to true.

When using createEmpty: true, aggregate functions return empty tables, but selector functions do not. By design, selectors drop empty tables.

tables

Input data. Default is piped-forward data (<-).

Examples

The following examples use data provided by the sampledata package to show how aggregateWindow() transforms data.

Use an aggregate function with default parameters

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

import "sampledata"

data = sampledata.float()
  |> range(start: sampledata.start, stop: sampledata.stop)

data
  |> aggregateWindow(
    every: 20s,
    fn: mean
  )

View input and output

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:

import "sampledata"

data = sampledata.float()
  |> range(start: sampledata.start, stop: sampledata.stop)

data
  |> aggregateWindow(
    column: "_value",
    every: 20s,
    fn: (column, tables=<-) => tables |> quantile(q: 0.99, column:column)
  )

View input and output

Window and aggregate by calendar month

import "sampledata"

data = sampledata.float()
  |> range(start: sampledata.start, stop: sampledata.stop)

data
  |> aggregateWindow(every: 1mo, fn: mean)

View input and output


Upgrade to InfluxDB Cloud or InfluxDB 2.0!

InfluxDB Cloud and InfluxDB OSS 2.0 ready for production.