I have defined a dashboard variable with the following settings:
Type: text box
Name: varLimit
Default value: 0
I want to use this variable for filtering purposes in a flux query.
Everyting runs fine, as long as there is a value in the text box. When it’s removed, the query returns an error. This is normal, since there is no value to filter on.
To overcome this I added the following piece of code, to check if the variable has a null value:
_limit = if "${varLimit}" == "" then 0 else ${varLimit}
The _limit is then used inside the flux query filter.
This works fine as long as there is a value entered in the text box.
Problem is that, no matter what I try, the query still returns an error when no value is entered:
if "${varLimit}" == ""
if "${varLimit}" == null
if exists "${varLimit}"
I suspect this has to do with the fact that, when no value is entered in the text box, the variable simply doesn’t exist in the flux query.
How to overcome this issue?
I actually tried this, and guess what… the birds didn’t fall from the sky.
but it did gave me the chicken error:
undefined identifier chicken error @3:79-3:86:
Well… point taken so I tried an extra solution, which seems to tackle the “chicken error”
import "types"
_limiet = if types.isNumeric(v: "${varLimit}") then int(v: "${varLimit}") else 0
Problem here is that this always renders false due to the double quotes casting the variable to a string value.
If I remove them, then the first problem reappears when the variable is null.
Maybe there is a neater/better solution to this problem?
Update 2024-09-23T22:00:00Z:
I think this is (for now) the best way to do it:
_limiet = if "${varLimit}" =~ /^(0|[1-9][0-9]*)$/ then int(v: "${varLimit}") else 0
The problem is that it needs to be user input. The maximum can be a range from 1 to 5000.
So if I need to input all these numbers into a dropdown, then that list would become very long.
With the regex approach it’s working fine for my needs.
Thanks for the help.