Use variables to dynamicly change where condition in query

Hello. I am using InfluxDB

Lets say i have a query

Select * from customMeasurements.

Now i would like to make the WHERE condition as much dynamic as possible. Is there a way how to define variables, so the user can input/select the where condition using it?

I can create variable query to get all tags from the test

SHOW TAG KEYS ON "myTestDB" FROM "customMeasurements" WHERE TestName =~ /^$testName$/

This query correctly shows all tags on selected testName ( testName is another variable )

Now since i have the list of all tags, is it possible to create variables that would allow user to determine WHERE condition?

For example the query would return tags : age, gender, eyesColor

And the user would input into variable something like “age=‘10’”

And the query would look like Select * from customMeasurements WHERE age ='10';

if the user would input age=10;gender= male , the condition would look like:

Select * from customMeasurements WHERE age ='10' and gender='male':

and if he wouldnt input anything the query would be simply

Select * from customMeasurements

is something like this possible with grafanna?

Use ad-hoc filters variable: Add ad hoc filters | Grafana Labs

BTW: Select * from customMeasurements is wrong query. If you have a lot of records in that customMeasurements, then it may cause a problem to your InfluxDB server or browser may have a problem to process so many records. It will be better to have time condition + time grouping/aggregation or at least row LIMIT in the query.

It is possible, but be aware that giving user possibility to determine WHERE clause can result in incorrect query, also it gives possiblity of not dermining query which will result in getting whole result set which can cause performance problems. Even worse: users can exploit it for sql injection attack. I don’t recommend this solution and I propose to give to the user possibility to pick some values from the lists instead.

Whichever of these two you choose the solution is same. You need to go to dashboard settings and create a variable. For example you can pick variable of Query type and name it age and populate it with query to InfluxDB. The variable will show up on the top of the dashboard. Then you reference this variable in panel queries by dollar sign and curly brackets: ${age}. You can also use Text Box variable to enable user to type whole text, but again, I strongly dissuade it.

This topic was automatically closed after 365 days. New replies are no longer allowed.