diff --git a/content/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/aggregates/timeweightedavg.md b/content/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/aggregates/timeweightedavg.md new file mode 100644 index 000000000..0b8b4cc1a --- /dev/null +++ b/content/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/aggregates/timeweightedavg.md @@ -0,0 +1,53 @@ +--- +title: timeWeightedAvg() function +description: The `timeWeightedAvg()` function outputs the timeWeightedAvg of non-null records as a float. +menu: + influxdb_2_0_ref: + name: timeWeightedAvg + parent: built-in-aggregates +weight: 501 +related: + - /influxdb/v2.0/reference/flux/stdlib/built-in/transformations/aggregates/integral/ +--- + +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_ + +```js +timeWeightedAvg(unit: "_value") +``` + +## Parameters + +### unit +Time duration used when computing the time-weighted average. + +_**Data type:** Duration_ + +## Examples +```js +from(bucket: "example-bucket") + |> range(start: -5m) + |> filter(fn: (r) => + r._measurement == "cpu" and + r._field == "usage_system" + ) + |> timeWeightedAvg(unit: 1m) +``` + +## Function definition +```js +timeWeightedAvg = (tables=<-, unit) => tables + |> integral( + unit: unit, + interpolate: "linear" + ) + |> map(fn: (r) => ({ + r with + _value: (r._value * float(v: uint(v: unit))) / float(v: int(v: r._stop) - int(v: r._start)) + })) +``` \ No newline at end of file diff --git a/content/influxdb/v2.0/reference/release-notes/flux.md b/content/influxdb/v2.0/reference/release-notes/flux.md index 7b8cdf586..6f983cb59 100644 --- a/content/influxdb/v2.0/reference/release-notes/flux.md +++ b/content/influxdb/v2.0/reference/release-notes/flux.md @@ -27,8 +27,9 @@ InfluxDB until the next InfluxDB v2.0 release._ - Improve window errors. - Add BigQuery support to [`sql` package](#). - Add `TypeExpression` to `BuiltinStmt` and fix tests. -- Time-weighted average ([`timeWeightedAvg()` function](#)). -- Integral with linear interpolation. +- Time-weighted average ([`timeWeightedAvg()` function](/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/aggregates/timeweightedavg/)). +- Update [`integral()`](/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/aggregates/integral/) + with linear interpolation. - Make experimental tracing an attribute of the context. ### Bug fixes