Query using conditional logic

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

  • for a conceptual overview of Flux and parts of a Flux query.
  • Execute queries to discover a variety of ways to run your queries.
Conditional expression syntax

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(), , reduce() ).

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

  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

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

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. |> range(start: -1h)
  2. |> filter(
  3. fn: (r) => if v.metric == "Memory" then
  4. r._measurement == "mem" and r._field == "used_percent"
  5. else if v.metric == "CPU" then
  6. r._measurement == "cpu" and r._field == "usage_user"
  7. r._measurement == "disk" and r._field == "used_percent"
  8. else
  9. r._measurement != "",
  10. )

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) => ({
  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",

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

Comments

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