docs-v2/content/flux/v0.x/stdlib/universe/statetracking.md

9.0 KiB

title description menu weight flux/v0.x/tags introduced
stateTracking() function `stateTracking()` returns the cumulative count and duration of consecutive rows that match a predicate function that defines a state.
flux_0_x_ref
name parent identifier
stateTracking universe universe/stateTracking
101
transformations
0.7.0

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

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

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

Input data

_time _value *tag state
2021-01-01T00:00:00Z -2 t1 ok
2021-01-01T00:00:10Z 10 t1 crit
2021-01-01T00:00:20Z 7 t1 crit
2021-01-01T00:00:30Z 17 t1 crit
2021-01-01T00:00:40Z 15 t1 crit
2021-01-01T00:00:50Z 4 t1 ok
_time _value *tag state
2021-01-01T00:00:00Z 19 t2 crit
2021-01-01T00:00:10Z 4 t2 ok
2021-01-01T00:00:20Z -3 t2 ok
2021-01-01T00:00:30Z 19 t2 crit
2021-01-01T00:00:40Z 13 t2 crit
2021-01-01T00:00:50Z 1 t2 ok

Output data

_time _value *tag state count
2021-01-01T00:00:00Z -2 t1 ok -1
2021-01-01T00:00:10Z 10 t1 crit 1
2021-01-01T00:00:20Z 7 t1 crit 2
2021-01-01T00:00:30Z 17 t1 crit 3
2021-01-01T00:00:40Z 15 t1 crit 4
2021-01-01T00:00:50Z 4 t1 ok -1
_time _value *tag state count
2021-01-01T00:00:00Z 19 t2 crit 1
2021-01-01T00:00:10Z 4 t2 ok -1
2021-01-01T00:00:20Z -3 t2 ok -1
2021-01-01T00:00:30Z 19 t2 crit 1
2021-01-01T00:00:40Z 13 t2 crit 2
2021-01-01T00:00:50Z 1 t2 ok -1

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

Return a cumulative state duration in milliseconds

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

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

Input data

_time _value *tag state
2021-01-01T00:00:00Z -2 t1 ok
2021-01-01T00:00:10Z 10 t1 crit
2021-01-01T00:00:20Z 7 t1 crit
2021-01-01T00:00:30Z 17 t1 crit
2021-01-01T00:00:40Z 15 t1 crit
2021-01-01T00:00:50Z 4 t1 ok
_time _value *tag state
2021-01-01T00:00:00Z 19 t2 crit
2021-01-01T00:00:10Z 4 t2 ok
2021-01-01T00:00:20Z -3 t2 ok
2021-01-01T00:00:30Z 19 t2 crit
2021-01-01T00:00:40Z 13 t2 crit
2021-01-01T00:00:50Z 1 t2 ok

Output data

_time _value *tag state duration
2021-01-01T00:00:00Z -2 t1 ok -1
2021-01-01T00:00:10Z 10 t1 crit 0
2021-01-01T00:00:20Z 7 t1 crit 10000
2021-01-01T00:00:30Z 17 t1 crit 20000
2021-01-01T00:00:40Z 15 t1 crit 30000
2021-01-01T00:00:50Z 4 t1 ok -1
_time _value *tag state duration
2021-01-01T00:00:00Z 19 t2 crit 0
2021-01-01T00:00:10Z 4 t2 ok -1
2021-01-01T00:00:20Z -3 t2 ok -1
2021-01-01T00:00:30Z 19 t2 crit 0
2021-01-01T00:00:40Z 13 t2 crit 10000
2021-01-01T00:00:50Z 1 t2 ok -1

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

Return a cumulative state count and duration

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

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

Input data

_time _value *tag state
2021-01-01T00:00:00Z -2 t1 ok
2021-01-01T00:00:10Z 10 t1 crit
2021-01-01T00:00:20Z 7 t1 crit
2021-01-01T00:00:30Z 17 t1 crit
2021-01-01T00:00:40Z 15 t1 crit
2021-01-01T00:00:50Z 4 t1 ok
_time _value *tag state
2021-01-01T00:00:00Z 19 t2 crit
2021-01-01T00:00:10Z 4 t2 ok
2021-01-01T00:00:20Z -3 t2 ok
2021-01-01T00:00:30Z 19 t2 crit
2021-01-01T00:00:40Z 13 t2 crit
2021-01-01T00:00:50Z 1 t2 ok

Output data

_time _value *tag state count duration
2021-01-01T00:00:00Z -2 t1 ok -1 -1
2021-01-01T00:00:10Z 10 t1 crit 1 0
2021-01-01T00:00:20Z 7 t1 crit 2 10
2021-01-01T00:00:30Z 17 t1 crit 3 20
2021-01-01T00:00:40Z 15 t1 crit 4 30
2021-01-01T00:00:50Z 4 t1 ok -1 -1
_time _value *tag state count duration
2021-01-01T00:00:00Z 19 t2 crit 1 0
2021-01-01T00:00:10Z 4 t2 ok -1 -1
2021-01-01T00:00:20Z -3 t2 ok -1 -1
2021-01-01T00:00:30Z 19 t2 crit 1 0
2021-01-01T00:00:40Z 13 t2 crit 2 10
2021-01-01T00:00:50Z 1 t2 ok -1 -1

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