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.0customLevel2
3.0customLevel3
5.0customLevel4
7.0customLevel5
7.5customLevel5
9.0customLevel6
11.0customLevel6

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.0customLevel3
5.0customLevel4
7.0customLevel5
9.0customLevel6

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