added timeWeightedAvg function

pull/1393/head
Scott Anderson 2020-09-03 15:15:40 -06:00
parent 40021cb275
commit db6e89ce04
2 changed files with 56 additions and 2 deletions

View File

@ -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))
}))
```

View File

@ -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