timeWeightedAvg() function

The timeWeightedAvg() function outputs the time-weighted average of non-null records in a table as a float. Time is weighted using the linearly interpolated integral of values in the table.

*Function type: Aggregate
**
Output data type:* Float

  1. timeWeightedAvg(unit: "_value")

Parameters

unit

Time duration used when computing the time-weighted average.

*Data type: Duration*

Examples

  1. from(bucket: "example-bucket")
  2. |> range(start: -5m)
  3. |> filter(fn: (r) =>
  4. r._measurement == "cpu" and
  5. r._field == "usage_system"
  6. )
  7. |> timeWeightedAvg(unit: 1m)

Function definition

  1. timeWeightedAvg = (tables=<-, unit) => tables
  2. |> integral(
  3. unit: unit,
  4. interpolate: "linear"
  5. )
  6. |> map(fn: (r) => ({
  7. r with
  8. _value: (r._value * float(v: uint(v: unit))) / float(v: int(v: r._stop) - int(v: r._start))
  9. }))

Related articles