Query using conditional logic

Flux provides if, then, and else conditional expressions that allow for powerful and flexible Flux queries.

If you’re just getting started with Flux queries, check out the following:

Conditional expression syntax
1. // Pattern
2. if <condition> then <action> else <alternative-action>

4. // Example
5. if color == "green" then "008000" else "ffffff"

Conditional expressions are most useful in the following contexts:

  • When defining variables.
  • When using functions that operate on a single row at a time ( filter(), map(), reduce() ).

Evaluating conditional expressions

Flux evaluates statements in order and stops evaluating once a condition matches.

For example, given the following statement:

1. if r._value > 95.0000001 and r._value <= 100.0 then
2. "critical"
3. else if r._value > 85.0000001 and r._value <= 95.0 then
4. "warning"
5. else if r._value > 70.0000001 and r._value <= 85.0 then
6. "high"
7. else
8. "normal"

When r._value is 96, the output is “critical” and the remaining conditions are not evaluated.

Examples

Conditionally set the value of a variable

The following example sets the overdue variable based on the dueDate variable’s relation to now().

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

Create conditional filters

The following example uses an example metric dashboard variable to change how the query filters data. metric has three possible values:

  • Memory
  • CPU
  • Disk
1. from(bucket: "example-bucket")
2. |> range(start: -1h)
3. |> filter(
4. fn: (r) => if v.metric == "Memory" then
5. r._measurement == "mem" and r._field == "used_percent"
6. else if v.metric == "CPU" then
7. r._measurement == "cpu" and r._field == "usage_user"
8. else if v.metric == "Disk" then
9. r._measurement == "disk" and r._field == "used_percent"
10. else
11. r._measurement != "",
12. )

Conditionally transform column values with map()

The following example uses the map() function to conditionally transform column values. It sets the level column to a specific string based on _value column.

No Comments Comments

1. from(bucket: "example-bucket")
2. |> range(start: -5m)
3. |> filter(fn: (r) => r._measurement == "mem" and r._field == "used_percent")
4. |> map(
5. fn: (r) => ({r with
6. level: if r._value >= 95.0000001 and r._value <= 100.0 then
7. "critical"
8. else if r._value >= 85.0000001 and r._value <= 95.0 then
9. "warning"
10. else if r._value >= 70.0000001 and r._value <= 85.0 then
11. "high"
12. else
13. "normal",
14. }),
15. )
1. from(bucket: "example-bucket")
2. |> range(start: -5m)
3. |> filter(fn: (r) => r._measurement == "mem" and r._field == "used_percent")
4. |> map(
5. fn: (r) => ({
6. // Retain all existing columns in the mapped row
7. r with
8. // Set the level column value based on the _value column
9. level: if r._value >= 95.0000001 and r._value <= 100.0 then
10. "critical"
11. else if r._value >= 85.0000001 and r._value <= 95.0 then
12. "warning"
13. else if r._value >= 70.0000001 and r._value <= 85.0 then
14. "high"
15. else
16. "normal",
17. }),
18. )

Conditionally increment a count with reduce()

The following example uses the aggregateWindow() and reduce() functions to count the number of records in every five minute window that exceed a defined threshold.

No Comments Comments

1. threshold = 65.0

3. data = from(bucket: "example-bucket")
4. |> range(start: -1h)
5. |> filter(fn: (r) => r._measurement == "mem" and r._field == "used_percent")
6. |> aggregateWindow(
7. every: 5m,
8. fn: (column, tables=<-) => tables
9. |> reduce(
10. identity: {above_threshold_count: 0.0},
11. fn: (r, accumulator) => ({
12. above_threshold_count: if r._value >= threshold then
13. accumulator.above_threshold_count + 1.0
14. else
15. accumulator.above_threshold_count + 0.0,
16. }),
17. ),
18. )
1. threshold = 65.0

3. from(bucket: "example-bucket")
4. |> range(start: -1h)
5. |> filter(fn: (r) => r._measurement == "mem" and r._field == "used_percent")
6. // Aggregate data into 5 minute windows using a custom reduce() function
7. |> aggregateWindow(
8. every: 5m,
9. // Use a custom function in the fn parameter.
10. // The aggregateWindow fn parameter requires 'column' and 'tables' parameters.
11. fn: (column, tables=<-) => tables
12. |> reduce(
13. identity: {above_threshold_count: 0.0},
14. fn: (r, accumulator) => ({
15. // Conditionally increment above_threshold_count if
16. // r.value exceeds the threshold
17. above_threshold_count: if r._value >= threshold then
18. accumulator.above_threshold_count + 1.0
19. else
20. accumulator.above_threshold_count + 0.0,
21. }),
22. ),
23. )

conditionals flux