How to build query filter from string OR dynamic filter generation from changing input variable

Hi, I’m trying to reconstruct the multi value option for FLUX and Grafana. I have been trying out a lot in the REPL and think this is more a FLUX issue then Grafana.

Use case: via a template variable, one or more clients can be selected, and I want to see them all in one graph.

I have an array with the client names (can be of any length between 0 and 999):

> arr
[client1,client2,client3]

I do have a solution for when I know how long the array is:

data
  |> range(start: -1d)
  |> filter(fn: (r) => r._measurement == "custom_track")
  |> filter(fn: (r) => r._field == "value")
  |> filter(fn: (r) => r.client == arr[0] or r.client == arr[1] or r.client == arr[2])
  |> group(columns: ["client"])
  |> aggregateWindow(every: 1d, fn: sum)
  |> yield()

But I need dynamic code that can react on length == 1 or > 1, so writing the |> filter(fn: (r) => r.client ...-line dynamicly in the manner of |> filter(fn: (r) => r.client == arr[0] or r.client == arr[1] or r.client == arr[2] ...), depending on however many entries there exist in arr.

With strings.joinStr() I can concat my arr in the right syntax: s = strings.joinStr(arr: arr, v: " or r.client == ") which returns me: "client1 or r.client == client2 or r.client == client3"

if I now place this into the query, I get an empty _result because it is pasted as one large string, and the ors are not interpreted:

data
  |> range(start: -1d)
  |> filter(fn: (r) => r._measurement == "custom_track")
  |> filter(fn: (r) => r._field == "value")
  |> filter(fn: (r) => r.client == s)
  |> group(columns: ["client"])
  |> aggregateWindow(every: 1d, fn: sum)
  |> yield()

This makes sense, since it is an actual string, but I need it to be executed.
I guess I’m looking for something like a “string to stream”-function, but I couldn’t find anything in that direction in the documentation.

Any ideas how I can resolve that?

Hi @Nuri,
The flux function contains should fit your needs: contains() function | Flux 0.x Documentation

clients = [client1,client2,client3]
data
  |> range(start: -1d)
  |> filter(fn: (r) => r._measurement == "custom_track")
  |> filter(fn: (r) => r._field == "value")
   |> filter(fn: (r) => contains(value: r. client, set: clients))
  |> group(columns: ["client"])
  |> aggregateWindow(every: 1d, fn: sum)
  |> yield()

Awesome, works like a charm. Thank you so much @Jay_Clifford !

1 Like