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

3.2 KiB

title description aliases menu weight flux/v0.x/tags related introduced
stateCount() function The `stateCount()` function computes the number of consecutive records in a given state.
/influxdb/v2.0/reference/flux/functions/transformations/statecount
/influxdb/v2.0/reference/flux/functions/built-in/transformations/statecount/
/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/statecount/
/influxdb/cloud/reference/flux/stdlib/built-in/transformations/statecount/
flux_0_x_ref
name parent
stateCount universe
102
transformations
/{{< latest "influxdb" >}}/query-data/flux/monitor-states/
0.7.0

The stateCount() function computes the number of consecutive records in a given state. The state is defined via the function fn. For each consecutive point that evaluates as true, the state count is incremented. When a point evaluates as false, the state count is reset. The state count is added as an additional column to each record.

stateCount(fn: (r) => r._field == "state", column: "stateCount")

If the expression generates an error during evaluation, the point is discarded and does not affect the state count.

Parameters

{{% note %}} Make sure fn parameter names match each specified parameter. To learn why, see Match parameter names. {{% /note %}}

fn

({{< req >}}) A single argument function that evaluates true or false to identify the state of the record. Records are passed to the function. Those that evaluate to true increment the state count. Those that evaluate to false reset the state count.

column

Name of the column added to each record that contains the incremented state count. Default is stateCount.

tables

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

Examples

{{% flux/sample-example-intro %}}

import "sampledata"

sampledata.int()
    |> stateCount(fn: (r) => r._value > 10)

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

Input data

{{% flux/sample "int" %}}

{{% /flex-content %}} {{% flex-content %}}

Output data
_time tag _value stateCount
2021-01-01T00:00:00Z t1 -2 -1
2021-01-01T00:00:10Z t1 10 -1
2021-01-01T00:00:20Z t1 7 -1
2021-01-01T00:00:30Z t1 17 1
2021-01-01T00:00:40Z t1 15 2
2021-01-01T00:00:50Z t1 4 -1
_time tag _value stateCount
2021-01-01T00:00:00Z t2 19 1
2021-01-01T00:00:10Z t2 4 -1
2021-01-01T00:00:20Z t2 -3 -1
2021-01-01T00:00:30Z t2 19 1
2021-01-01T00:00:40Z t2 13 2
2021-01-01T00:00:50Z t2 1 -1
{{% /flex-content %}}
{{< /flex >}}
{{% /expand %}}
{{< /expand-wrapper >}}