3

We've set up a Grafana cloud + InfluxDB 2.0 (Flux language) cloud instance. As part of this, we've created a custom variable list with "device IDs", called devices.

In a panel, we wish to display parameter data where the user can select one or more device IDs from the devices list to have them displayed in the panel. This works fine for single device ID selection, but not for multiple devices.

How should the query be modified to display data from a variable number of devices based on a multi-select entry in the dropdown in Grafana?

from(bucket: "test-bucket-new")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "${devices}")
  |> filter(fn: (r) => r["_field"] == "Speed")
  |> aggregateWindow(every: v.windowPeriod, fn: mean)
  |> yield(name: "mean")
mfcss
  • 1,039
  • 1
  • 9
  • 25

1 Answers1

5

It seems the below solves it:

from(bucket: "test-bucket-new")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => contains(value: r["_measurement"], set: ${devices:json}))
  |> filter(fn: (r) => r["_field"] == "Speed")
  |> aggregateWindow(every: v.windowPeriod, fn: mean)
  |> yield(name: "mean")
mfcss
  • 1,039
  • 1
  • 9
  • 25
  • How does it perform when you feed large number of "devices"? I have something very similar and when I set the grafana variable to "All" and perform a flux query with "contains", there is an exponential slowdown. I'm curious if you figured some performance workaround. – Arvind Jan 26 '22 at 00:58
  • I was able to slap a band-aid on this performance hit and get a lot of gain. I created a function called `containsWrapper` and made Grafana return empty array if "All" is selected. `containsWrapper` would check if the set is empty, and if so, it would query the database without `contains` filter. – Arvind Feb 03 '22 at 01:45