Documentation

Assign custom states to data

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!


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.

Read more

InfluxDB v3 enhancements and InfluxDB Clustered is now generally available

New capabilities, including faster query performance and management tooling advance the InfluxDB v3 product line. InfluxDB Clustered is now generally available.

InfluxDB v3 performance and features

The InfluxDB v3 product line has seen significant enhancements in query performance and has made new management tooling available. These enhancements include an operational dashboard to monitor the health of your InfluxDB cluster, single sign-on (SSO) support in InfluxDB Cloud Dedicated, and new management APIs for tokens and databases.

Learn about the new v3 enhancements


InfluxDB Clustered general availability

InfluxDB Clustered is now generally available and gives you the power of InfluxDB v3 in your self-managed stack.

Talk to us about InfluxDB Clustered