Back to Influxdb

timeWeightedAvg() function

content/flux/v0/stdlib/universe/timeweightedavg.md

latest3.1 KB
Original Source
<!------------------------------------------------------------------------------ IMPORTANT: This page was generated from comments in the Flux source code. Any edits made directly to this page will be overwritten the next time the documentation is generated. To make updates to this documentation, update the function comments above the function definition in the Flux source code: https://github.com/influxdata/flux/blob/master/stdlib/universe/universe.flux#L3664-L3674 Contributing to Flux: https://github.com/influxdata/flux#contributing Fluxdoc syntax: https://github.com/influxdata/flux/blob/master/docs/fluxdoc.md ------------------------------------------------------------------------------->

timeWeightedAvg() returns the time-weighted average of non-null values in _value column as a float for each input table.

Time is weighted using the linearly interpolated integral of values in the table.

Function type signature
js
(<-tables: stream[A], unit: duration) => stream[{B with _value: float, _value: float, _stop: D, _start: C}] where A: Record

{{% caption %}} For more information, see Function type signatures. {{% /caption %}}

Parameters

unit

({{< req >}}) Unit of time to use to compute the time-weighted average.

tables

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

Examples

Calculate the time-weighted average of values

js
data
    |> timeWeightedAvg(unit: 1s)

{{< expand-wrapper >}} {{% expand "View example input and output" %}}

Input data

*_start*_stop_time_value*tag
2021-01-01T00:00:00Z2021-01-01T00:01:00Z2021-01-01T00:00:00Z-2t1
2021-01-01T00:00:00Z2021-01-01T00:01:00Z2021-01-01T00:00:20Z7t1
2021-01-01T00:00:00Z2021-01-01T00:01:00Z2021-01-01T00:00:50Z4t1
*_start*_stop_time_value*tag
2021-01-01T00:00:00Z2021-01-01T00:01:00Z2021-01-01T00:00:00Zt2
2021-01-01T00:00:00Z2021-01-01T00:01:00Z2021-01-01T00:00:10Z4t2
2021-01-01T00:00:00Z2021-01-01T00:01:00Z2021-01-01T00:00:20Z-3t2
2021-01-01T00:00:00Z2021-01-01T00:01:00Z2021-01-01T00:00:30Z19t2
2021-01-01T00:00:00Z2021-01-01T00:01:00Z2021-01-01T00:00:50Z1t2

Output data

*_start*_stop_value*tag
2021-01-01T00:00:00Z2021-01-01T00:01:00Z4.166666666666667t1
*_start*_stop_value*tag
2021-01-01T00:00:00Z2021-01-01T00:01:00Z5.416666666666667t2

{{% /expand %}} {{< /expand-wrapper >}}