文档

使用条件逻辑查询

Flux 提供了 ifthenelse 条件表达式,从而实现强大而灵活的 Flux 查询。

如果你刚开始使用 Flux 查询,请查看以下内容

  • Flux 入门 了解 Flux 的概念性概述和 Flux 查询的组成部分。
  • 执行查询 了解运行查询的各种方法。
条件表达式语法
// Pattern
if <condition> then <action> else <alternative-action>

// Example
if color == "green" then "008000" else "ffffff"

条件表达式在以下情况下最有用

评估条件表达式

Flux 按顺序评估语句,并在条件匹配时停止评估。

例如,给定以下语句

if r._value > 95.0000001 and r._value <= 100.0 then
    "critical"
else if r._value > 85.0000001 and r._value <= 95.0 then
    "warning"
else if r._value > 70.0000001 and r._value <= 85.0 then
    "high"
else
    "normal"

r._value 为 96 时,输出为 “critical”,其余条件不再评估。

示例

条件性地设置变量的值

以下示例根据 dueDate 变量与 now() 的关系设置 overdue 变量。

dueDate = 2019-05-01
overdue = if dueDate < now() then true else false

创建条件过滤器

以下示例使用示例 仪表盘变量来更改查询过滤数据的方式。 metric 有三个可能的值

  • 内存
  • CPU
  • 磁盘
from(bucket: "example-bucket")
    |> range(start: -1h)
    |> filter(
        fn: (r) => if v.metric == "Memory" then
            r._measurement == "mem" and r._field == "used_percent"
        else if v.metric == "CPU" then
            r._measurement == "cpu" and r._field == "usage_user"
        else if v.metric == "Disk" then
            r._measurement == "disk" and r._field == "used_percent"
        else
            r._measurement != "",
    )

使用 map() 条件性地转换列值

以下示例使用 map() 函数来条件性地转换列值。它根据 _value 列将 level 列设置为特定字符串。

from(bucket: "example-bucket")
    |> range(start: -5m)
    |> filter(fn: (r) => r._measurement == "mem" and r._field == "used_percent")
    |> map(
        fn: (r) => ({r with
            level: if r._value >= 95.0000001 and r._value <= 100.0 then
                "critical"
            else if r._value >= 85.0000001 and r._value <= 95.0 then
                "warning"
            else if r._value >= 70.0000001 and r._value <= 85.0 then
                "high"
            else
                "normal",
        }),
    )
from(bucket: "example-bucket")
    |> range(start: -5m)
    |> filter(fn: (r) => r._measurement == "mem" and r._field == "used_percent")
    |> map(
        fn: (r) => ({
            // Retain all existing columns in the mapped row
            r with
            // Set the level column value based on the _value column
            level: if r._value >= 95.0000001 and r._value <= 100.0 then
                "critical"
            else if r._value >= 85.0000001 and r._value <= 95.0 then
                "warning"
            else if r._value >= 70.0000001 and r._value <= 85.0 then
                "high"
            else
                "normal",
        }),
    )

使用 reduce() 条件性地递增计数

以下示例使用 aggregateWindow()reduce() 函数来计算每五分钟窗口中超过定义阈值的记录数。

threshold = 65.0

data = from(bucket: "example-bucket")
    |> range(start: -1h)
    |> filter(fn: (r) => r._measurement == "mem" and r._field == "used_percent")
    |> aggregateWindow(
        every: 5m,
        fn: (column, tables=<-) => tables
            |> reduce(
                identity: {above_threshold_count: 0.0},
                fn: (r, accumulator) => ({
                    above_threshold_count: if r._value >= threshold then
                        accumulator.above_threshold_count + 1.0
                    else
                        accumulator.above_threshold_count + 0.0,
                }),
            ),
    )
threshold = 65.0

from(bucket: "example-bucket")
    |> range(start: -1h)
    |> filter(fn: (r) => r._measurement == "mem" and r._field == "used_percent")
    // Aggregate data into 5 minute windows using a custom reduce() function
    |> aggregateWindow(
        every: 5m,
        // Use a custom function in the fn parameter.
        // The aggregateWindow fn parameter requires 'column' and 'tables' parameters.
        fn: (column, tables=<-) => tables
            |> reduce(
                identity: {above_threshold_count: 0.0},
                fn: (r, accumulator) => ({
                    // Conditionally increment above_threshold_count if
                    // r.value exceeds the threshold
                    above_threshold_count: if r._value >= threshold then
                        accumulator.above_threshold_count + 1.0
                    else
                        accumulator.above_threshold_count + 0.0,
                }),
            ),
    )

此页是否对您有帮助?

感谢您的反馈!


Flux 的未来

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

阅读更多

现已全面上市

InfluxDB 3 Core 和 Enterprise

快速启动。更快扩展。

获取更新

InfluxDB 3 Core 是一个开源、高速的近实时数据引擎,可实时收集和处理数据,并将其持久化到本地磁盘或对象存储。InfluxDB 3 Enterprise 构建在 Core 的基础上,增加了高可用性、只读副本、增强的安全性和数据压缩功能,从而实现更快的查询和优化的存储。InfluxDB 3 Enterprise 的免费层可供非商业家庭或业余爱好者使用。

有关更多信息,请查看