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

conditionals flux