Grafana shows 'no data' when check more than one choose in variable

Hi all,
Can you help to share some steps to implement the variable/template in Grafana7.3 with influxdb2.0-rc3 ? Thank you.
When I add a tag filter in the query cmd (add ‘r.host == “${host}”’), the grafana has the right action. No matter which one host I selecte, it shows the right host data. But if I select more than one host, it shows no data (no error shows).

from(bucket: “flower”)
|> range(start: v.timeRangeStart, stop:v.timeRangeStop)
|> filter(fn: ® =>
r._measurement == “mem” and
r.host == “${host}” and
r._field == “used_percent”
)

I only post a thread in grafana community but I’m not sure whether this is a grafana problem or influxdb2.0 problem.

Hello @wolfohyeah,
I’m not sure either. I know that if you wanted to filter for multiple hosts with flux your query would look like this:

from(bucket: “flower”)
|> range(start: -30d)
|> filter(fn: (r) =>
r._measurement == “mem” and
r.host == “host1” or
r.host == “host2” and
r._field == “used_percent”
) 

Hi @Anaisdg,
Thank you for you reply. My problem is that if I have thousands of hosts, it’s impossible to write all host in the filter. Instead we need a variable to filter the search.
In the dashboard inner influxdb2.0, I can set the variable like this:

from(bucket: “flower”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r[“_measurement”] == “mem”)
|> filter(fn: (r) => r[“_field”] == “used_percent”)
|> filter(fn: (r) => r.host == v.ahost)
|> aggregateWindow(every: v.windowPeriod, fn: last, createEmpty: false)
|> yield(name: “last”)


But I don’t know how to write the query cmd in grafana.

Hello @wolfohyeah,
I don’t know either. I’m not that familiar with grafana, but I feel like (without being sure) this is a feature specific to InfluxDB. Have you tried asking on the grafana community? I’ll ask around.

Did you try the following
|> filter(fn: ® => r[“host”] =~ /.*/)

Also there is an option in grafana called Include All Option and you specify the regex
image

The solution:

In the Grafana query to get the tag values ​​for the variable:

import "influxdata/influxdb/schema"
schema.tagValues(bucket: "flower", tag: "host")

In Grafana’s query:

from(bucket: "flower")
|> range(start: v.timeRangeStart, stop:v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "mem")
|> filter(fn: (r) => r["_field"] == "used_percent")
|> filter(fn: (r) => contains(value: r["host"], set: ${host:json}))

Where set: ${host:json} is the name of the variable. Adding attribute :json you can select multiple values ​​in variable.

Regards !! :wave:

2 Likes

Yes. That works fine by using contains. Thanks @delapuentem and @Anaisdg and @Ashish_Sikarwar .

Hi @delapuentem , I have another problem with multiple variables. My measurement looks like below:

name1=animal,name2=tiger count=20 
name1=animal,name2=lion count=25
name1=fruit,name2=apple count=10
name1=fruit,name2=banana count=15

name1 and name2 are my tags and count is my field.
In influxdb2.0.6, I create 2 variable:
variable name1:

import “influxdata/influxdb/schema”
schema.tagValues(bucket:“mybucket”, tag:“name1”)

variable name2:

import “influxdata/influxdb/schema”
schema.tagValues(bucket:“mybucket”, tag:“name2”)

In grafana 7.5.7(also tried 8.0.0-beta2) , I create 2 variables named query1 and query2:
query1(enable multi-value):

import “influxdata/influxdb/v1”
v1.tagValues(
buket: v.bucket,
tag: “name1”,
predicate: (r) => true,
start: -1d
)

query2(enable multi-value):

import “influxdata/influxdb/v1”
v1.tagValues(
buket: v.bucket,
tag: “name2”,
predicate: (r) => r.name1 == “${query1}”,
start: -1d
)

What I want is that the item of query2 should be based on the selection of query1.
eg. If I only select animal, the item list of query2 should only include animal item like tiger and lion but not banana. And if I select both animal and fruit in query1, the item list of query2 should display all animals and fruits(in my example above they are tiger/lion/apply/banana).
My query in grafana looks like below:

from(bucket: “mybucket”)
|> range(start: v.timeRangeStart, stop:v.timeRangeStop)
|> filter(fn:(r) => r._measurement == “mymeasurement” and r._field == “count”
and contains(value: r.name2, set:${query2:json}))

In my grafana dashboard, when I select only one item of query1 let’s say animal, I can see tiger and lion in query2 drop-down items. And it works well if i select one or more item in query2.
The problem is that if I select more than one item in query1, I see nothing in query2 drop-down item.

I know that the problem is happened in my query command of query2 or name2. But I don’t know how to fix it out.
Many thanks.

@jorgedlcruz Any chance you can help here please?

Hello, I am afraid I am not that versatile with Flux just yet, I need more time to play with it. My apologies :frowning:

Hi @Anaisdg , Can influxdb dashboard reach my point as I describe above ?

Hi @wolfohyeah . Sorry for taking so long to answer. I don’t know if you still have this problem.

I give you a solution:

name1
from(bucket: “mybucket”)
|> range(start: v.timeRangeStart)
|> filter(fn: (r) => r["_measurement"] == “mymesurement”)
|> keyValues(keyColumns: [“name1”])
|> group()
|> keep(columns: [“name1”])
|> distinct(column: “name1”)

name2
from(bucket: “mybucket”)
|> range(start: v.timeRangeStart)
|> filter(fn: (r) => r["_measurement"] == “mymesurement”)
|> filter(fn: (r) => r[“name1”] =~ /^${name1:regex}$/)
|> keyValues(keyColumns: [“name2”])
|> group()
|> keep(columns: [“name2”])
|> distinct(column: “name2”)

The solution is in this line:
|> filter(fn: (r) => r[“name1”] =~ /^${name1:regex}$/)
It is what makes the variables hierarchical, and the value of this variable changes according to the previous variable. This supports the “All” parameter, the Multi-select and single values ​​in Grafana.