Nested Loops Filtering with Grafana Variables

My goal is something like this:

data.filter(item => {
    foreach compareItem in compareCollection
        if(item equals compareItem) return true;
        return false;
}

I am working with Grafana and the variable ${measurements} can either have 1 or multiple strings
my query right now looks like this:

from(bucket: "MachineBucket")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => contains(value: r["_measurement"], set: "${measurements}"))
  |> drop(columns: ["_start", "_stop"])
  |> yield()

it tells me that it cannot iterate over a single item/collection of strings

EDIT: i made progress by changing “${measurements}” to ["${measurements}"].
It is now able to query a single measurement, but can still not query multiple because it writes writes the collection of measurments out as: ["{object1,object2,object3}"].

I think you can filter this with regex.

  • So first create a Grafana template variable with the measurement names.
  • Then use the template variable with a regex filter and the regex format, like the following:
from(bucket: "MachineBucket")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] =~ /${measurements:regex}/)
  |> drop(columns: ["_start", "_stop"])
  |> yield()

I hope this helps.

I think what you are describing is filtering the data based on the regex in the variable measurements, but that is not the behaviour im aiming for. Pls correct me if i am wrong on this.
I want to filter my query based on a collection or rather repetition of strings (i also get the strings from another query). But i appreciate the help.

@Rush_Ferchau Because Grafana returns the set as a string, it takes some string manipulation to transform the variable value into a Flux array. This should work:

import "array"
import "regexp"
import "strings"

measurementSet = strings.split(v: regexp.replaceAllString(r: /[\{\}]/, v: "${measurements}", t: ""), t: ",")

from(bucket: "MachineBucket")
    |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
    |> filter(fn: (r) => contains(value: r._measurement, set: measurementSet))
1 Like

You are my hero, this solved my Problem :smiley:

1 Like