Empty dashboard variables

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?

can you please share the query where this variable is used in?

Sure. It’s just a dummy query to be used in a transformation to set the threshold value of a representation.

import "array"
rows = [{limit: ${varLimit}}]
array.from(rows: rows)

When the variable has no input the following error occurs:

invalid: compilation failed: error @2:10-2:15: missing property value
1 Like

try this

import "array"

rows = [{limit: "${varLimit}"}]

array.from(rows: rows)

Putting the variable between double quotes did the trick.
One more addition:

_limiet = if "${varLimit}" == "" then 0 else int(v: "${varLimit}")

and then use the variable _limit in the query. Otherwise it might return the ‘expected int but got string’ error.

What if someone puts “chicken nuggets”
feels like a brittle solution

:smiley: 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

It overcomes the chicken problem, and null values

2 Likes

what is max possible number for limit?

:joy: :chicken: :egg:

I think you should go the business variable route for what you are doing especially as @solitcon mentioned if you know the max

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.

1 Like