使用条件逻辑查询
Flux 提供了 if、then 和 else 条件表达式,使得 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 仪表板变量 来改变查询如何过滤数据。
metric 有三个可能的值:
- 内存
- 中央处理器
- 磁盘
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,
}),
),
)