Back to Influxdb

stateTracking() function

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

latest8.7 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#L2569-L2579 Contributing to Flux: https://github.com/influxdata/flux#contributing Fluxdoc syntax: https://github.com/influxdata/flux/blob/master/docs/fluxdoc.md ------------------------------------------------------------------------------->

stateTracking() returns the cumulative count and duration of consecutive rows that match a predicate function that defines a state.

To return the cumulative count of consecutive rows that match the predicate, include the countColumn parameter. To return the cumulative duration of consecutive rows that match the predicate, include the durationColumn parameter. Rows that do not match the predicate function fn return -1 in the count and duration columns.

Function type signature
js
(
    <-tables: stream[A],
    fn: (r: A) => bool,
    ?countColumn: string,
    ?durationColumn: string,
    ?durationUnit: duration,
    ?timeColumn: string,
) => stream[B] where A: Record, B: Record

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

Parameters

fn

({{< req >}}) Predicate function to determine state.

countColumn

Column to store state count in.

If not defined, stateTracking() does not return the state count.

durationColumn

Column to store state duration in.

If not defined, stateTracking() does not return the state duration.

durationUnit

Unit of time to report state duration in. Default is 1s.

timeColumn

Column with time values used to calculate state duration. Default is _time.

tables

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

Examples

Return a cumulative state count

js
data
    |> stateTracking(fn: (r) => r.state == "crit", countColumn: "count")

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

Input data

_time_value*tagstate
2021-01-01T00:00:00Z-2t1ok
2021-01-01T00:00:10Z10t1crit
2021-01-01T00:00:20Z7t1crit
2021-01-01T00:00:30Z17t1crit
2021-01-01T00:00:40Z15t1crit
2021-01-01T00:00:50Z4t1ok
_time_value*tagstate
2021-01-01T00:00:00Z19t2crit
2021-01-01T00:00:10Z4t2ok
2021-01-01T00:00:20Z-3t2ok
2021-01-01T00:00:30Z19t2crit
2021-01-01T00:00:40Z13t2crit
2021-01-01T00:00:50Z1t2ok

Output data

_time_value*tagstatecount
2021-01-01T00:00:00Z-2t1ok-1
2021-01-01T00:00:10Z10t1crit1
2021-01-01T00:00:20Z7t1crit2
2021-01-01T00:00:30Z17t1crit3
2021-01-01T00:00:40Z15t1crit4
2021-01-01T00:00:50Z4t1ok-1
_time_value*tagstatecount
2021-01-01T00:00:00Z19t2crit1
2021-01-01T00:00:10Z4t2ok-1
2021-01-01T00:00:20Z-3t2ok-1
2021-01-01T00:00:30Z19t2crit1
2021-01-01T00:00:40Z13t2crit2
2021-01-01T00:00:50Z1t2ok-1

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

Return a cumulative state duration in milliseconds

js
data
    |> stateTracking(fn: (r) => r.state == "crit", durationColumn: "duration", durationUnit: 1ms)

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

Input data

_time_value*tagstate
2021-01-01T00:00:00Z-2t1ok
2021-01-01T00:00:10Z10t1crit
2021-01-01T00:00:20Z7t1crit
2021-01-01T00:00:30Z17t1crit
2021-01-01T00:00:40Z15t1crit
2021-01-01T00:00:50Z4t1ok
_time_value*tagstate
2021-01-01T00:00:00Z19t2crit
2021-01-01T00:00:10Z4t2ok
2021-01-01T00:00:20Z-3t2ok
2021-01-01T00:00:30Z19t2crit
2021-01-01T00:00:40Z13t2crit
2021-01-01T00:00:50Z1t2ok

Output data

_time_value*tagstateduration
2021-01-01T00:00:00Z-2t1ok-1
2021-01-01T00:00:10Z10t1crit0
2021-01-01T00:00:20Z7t1crit10000
2021-01-01T00:00:30Z17t1crit20000
2021-01-01T00:00:40Z15t1crit30000
2021-01-01T00:00:50Z4t1ok-1
_time_value*tagstateduration
2021-01-01T00:00:00Z19t2crit0
2021-01-01T00:00:10Z4t2ok-1
2021-01-01T00:00:20Z-3t2ok-1
2021-01-01T00:00:30Z19t2crit0
2021-01-01T00:00:40Z13t2crit10000
2021-01-01T00:00:50Z1t2ok-1

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

Return a cumulative state count and duration

js
data
    |> stateTracking(fn: (r) => r.state == "crit", countColumn: "count", durationColumn: "duration")

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

Input data

_time_value*tagstate
2021-01-01T00:00:00Z-2t1ok
2021-01-01T00:00:10Z10t1crit
2021-01-01T00:00:20Z7t1crit
2021-01-01T00:00:30Z17t1crit
2021-01-01T00:00:40Z15t1crit
2021-01-01T00:00:50Z4t1ok
_time_value*tagstate
2021-01-01T00:00:00Z19t2crit
2021-01-01T00:00:10Z4t2ok
2021-01-01T00:00:20Z-3t2ok
2021-01-01T00:00:30Z19t2crit
2021-01-01T00:00:40Z13t2crit
2021-01-01T00:00:50Z1t2ok

Output data

_time_value*tagstatecountduration
2021-01-01T00:00:00Z-2t1ok-1-1
2021-01-01T00:00:10Z10t1crit10
2021-01-01T00:00:20Z7t1crit210
2021-01-01T00:00:30Z17t1crit320
2021-01-01T00:00:40Z15t1crit430
2021-01-01T00:00:50Z4t1ok-1-1
_time_value*tagstatecountduration
2021-01-01T00:00:00Z19t2crit10
2021-01-01T00:00:10Z4t2ok-1-1
2021-01-01T00:00:20Z-3t2ok-1-1
2021-01-01T00:00:30Z19t2crit10
2021-01-01T00:00:40Z13t2crit210
2021-01-01T00:00:50Z1t2ok-1-1

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