Documentation

为数据分配自定义状态

问题

您可能想使用monitor,并利用像monitor.stateChangesOnly()这样的函数。然而,monitor.stateChangesOnly()只允许您监控四种状态:“crit”、“warn”、“ok”和“info”。如果您想能够分配和监控自定义状态或超过四种状态的状态变化呢?

解决方案

定义您自己的自定义 stateChangesOnly() 函数。使用这里的源代码中的函数,并对其进行修改以适应超过四个级别。在这里,我们考虑六个不同的级别,而不仅仅是四个。

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"])
}

使用 array.from() 构造一些示例数据,并将自定义级别映射到其中:

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",
            }),
    )

示例数据如下:

级别
0.0自定义级别2
3.0自定义级别3
5.0自定义级别4
7.0自定义级别5
7.5自定义级别5
9.0自定义级别6
11.0自定义级别6

现在应用我们的自定义 stateChangesOnly() 函数:

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

这将返回:

级别
3.0自定义级别3
5.0自定义级别4
7.0自定义级别5
9.0自定义级别6


Flux的未来

Flux 正在进入维护模式。您可以像现在一样继续使用它,而无需对您的代码进行任何更改。

阅读更多

InfluxDB 3 开源版本现已公开Alpha测试

InfluxDB 3 Open Source is now available for alpha testing, licensed under MIT or Apache 2 licensing.

我们将发布两个产品作为测试版的一部分。

InfluxDB 3 核心,是我们新的开源产品。 它是一个用于时间序列和事件数据的实时数据引擎。 InfluxDB 3 企业版是建立在核心基础之上的商业版本,增加了历史查询能力、读取副本、高可用性、可扩展性和细粒度安全性。

有关如何开始的更多信息,请查看: