JMU
April 11, 2022, 7:54am
1
Hi everyone,
I have a variable called NumLinea which has values from 1 to 6.
In the flux query, I used a filter to get this NumLinea values.
|> filter(fn: (r) => r[“Codlinea”] =~ “${NumLinea}”)
When a value is selected there is no problem. However, when multiple values are selected no data appears.
Anybody knows how to get multiple values in a filter.
Thank you beforehands.
Hi @JMU ,
I hope you are doing okay. So this is a little tricky, this is because filter is looking for conditional logic. For example:
|> filter(fn: (r) => r._measurement == "cpu" and r._field == "usage_system" and r.cpu == "cpu-total")
or
|> filter(fn: (r) => r.Codlinea == "1" or r. Codlinea == "2" or r. Codlinea == "3")
Are valid notation.
JMU
April 11, 2022, 3:23pm
3
I know but this would be manual filters.
The idea of the Numlinea variable is that the user chooses the number of lines he wants to visualize.
This solution doesn’t work for this situation.
scott
April 11, 2022, 3:39pm
4
You can structure NumLinea
as an array and use contains()
to see if the if the number is in the array. There are some caveats here:
contains()
can be pretty slow on large datasets.
You’re currently using a regular expression operator to compare r.Codelinea to the user specified variable. contains()
doesn’t support regular expressions. It needs to be an exact match.
selectedLines = ["1", "2"]
// ...
|> filter(fn: (r) => contains(value: r["Codlinea"], set: selectedLines)
1 Like
JMU
April 12, 2022, 6:31am
5
Hi Scott,
I tried your code but it didn’t work for my solution.
I find this one and it works.
|> filter(fn: (r) => r[“Codlinea”] =~ /^${NumLinea:regex}$/)
Best regards