Documentation

Assign custom states to data

This page documents an earlier version of . is the latest stable version. View this page in the documentation.

Problem

You may want to use the monitor package and take advantage of functions like monitor.stateChangesOnly(). However, monitor.stateChangesOnly() only allows you to monitor four states: “crit”, “warn”, “ok”, and “info”. What if you want to be able to assign and monitor state changes across custom states or more than four states?

Solution

Define your own custom stateChangesOnly() function. Use the function from the source code here and alter it to accommodate more than four levels. Here we account for six different levels instead of just four.

import "dict"
import "experimental"

stateChangesOnly = (tables=<-) => {
    levelInts =
        [
            "customLevel1": 1,
            "customLevel2": 2,
            "customLevel3": 3,
            "customLevel4": 4,
            "customLevel5": 5,
            "customLevel6": 6,
        ]

    return
        tables
            |> map(fn: (r) => ({r with level_value: dict.get(dict: levelInts, key: r._level, default: 0)}))
            |> duplicate(column: "_level", as: "____temp_level____")
            |> drop(columns: ["_level"])
            |> rename(columns: {"____temp_level____": "_level"})
            |> sort(columns: ["_source_timestamp", "_time"], desc: false)
            |> difference(columns: ["level_value"])
            |> filter(fn: (r) => r.level_value != 0)
            |> drop(columns: ["level_value"])
            |> experimental.group(mode: "extend", columns: ["_level"])
}

Construct some example data with array.from() and map custom levels to it:

array.from(
    rows: [
        {_value: 0.0},
        {_value: 3.0},
        {_value: 5.0},
        {_value: 7.0},
        {_value: 7.5},
        {_value: 9.0},
        {_value: 11.0},
    ],
)
    |> map(
        fn: (r) =>
            ({r with _level:
                    if r._value <= 2.0 then
                        "customLevel2"
                    else if r._value <= 4.0 and r._value > 2.0 then
                        "customLevel3"
                    else if r._value <= 6.0 and r._value > 4.0 then
                        "customLevel4"
                    else if r._value <= 8.0 and r._value > 6.0 then
                        "customLevel5"
                    else
                        "customLevel6",
            }),
    )

Where the example data looks like:

_value _level
0.0 customLevel2
3.0 customLevel3
5.0 customLevel4
7.0 customLevel5
7.5 customLevel5
9.0 customLevel6
11.0 customLevel6

Now apply our custom stateChangesOnly() function:

import "array"
import "dict"
import "experimental"

stateChangesOnly = (tables=<-) => {
    levelInts =
        [
            "customLevel1": 1,
            "customLevel2": 2,
            "customLevel3": 3,
            "customLevel4": 4,
            "customLevel5": 5,
            "customLevel6": 6,
        ]

    return
        tables
            |> map(fn: (r) => ({r with level_value: dict.get(dict: levelInts, key: r._level, default: 0)}))
            |> duplicate(column: "_level", as: "____temp_level____")
            |> drop(columns: ["_level"])
            |> rename(columns: {"____temp_level____": "_level"})
            |> sort(columns: ["_source_timestamp", "_time"], desc: false)
            |> difference(columns: ["level_value"])
            |> filter(fn: (r) => r.level_value != 0)
            |> drop(columns: ["level_value"])
            |> experimental.group(mode: "extend", columns: ["_level"])
}

data =
    array.from(
        rows: [
            {_value: 0.0},
            {_value: 3.0},
            {_value: 5.0},
            {_value: 7.0},
            {_value: 7.5},
            {_value: 9.0},
            {_value: 11.0},
        ],
    )
        |> map(
            fn: (r) =>
                ({r with _level:
                        if r._value <= 2.0 then
                            "customLevel2"
                        else if r._value <= 4.0 and r._value > 2.0 then
                            "customLevel3"
                        else if r._value <= 6.0 and r._value > 4.0 then
                            "customLevel4"
                        else if r._value <= 8.0 and r._value > 6.0 then
                            "customLevel5"
                        else
                            "customLevel6",
                }),
        )

data
    |> stateChangesOnly()

This returns:

_value _level
3.0 customLevel3
5.0 customLevel4
7.0 customLevel5
9.0 customLevel6

Was this page helpful?

Thank you for your feedback!


Introducing InfluxDB Clustered

A highly available InfluxDB 3.0 cluster on your own infrastructure.

InfluxDB Clustered is a highly available InfluxDB 3.0 cluster built for high write and query workloads on your own infrastructure.

InfluxDB Clustered is currently in limited availability and is only available to a limited group of InfluxData customers. If interested in being part of the limited access group, please contact the InfluxData Sales team.

Learn more
Contact InfluxData Sales

The future of Flux

Flux is going into maintenance mode. You can continue using it as you currently are without any changes to your code.

Flux is going into maintenance mode and will not be supported in InfluxDB 3.0. This was a decision based on the broad demand for SQL and the continued growth and adoption of InfluxQL. We are continuing to support Flux for users in 1.x and 2.x so you can continue using it with no changes to your code. If you are interested in transitioning to InfluxDB 3.0 and want to future-proof your code, we suggest using InfluxQL.

For information about the future of Flux, see the following:

State of the InfluxDB Cloud Serverless documentation

InfluxDB Cloud Serverless documentation is a work in progress.

The new documentation for InfluxDB Cloud Serverless is a work in progress. We are adding new information and content almost daily. Thank you for your patience!

If there is specific information you’re looking for, please submit a documentation issue.