Hello,
I’m encountering an issue with a Flux query, and I’d appreciate some guidance on how to handle it properly. Here’s the problematic query:
flux
import "http/requests"
import "experimental/json"
import "array"
import "join"
import "strings"
import "math"
mainQuery = from(bucket: "foobar")
|> range(start: -7d)
|> filter(fn: (r) => r["_measurement"] == "muh")
|> filter(fn: (r) => r["_field"] == "baz")
|> last()
|> pivot(rowKey: ["lala"], columnKey: ["_field"], valueColumn: "_value")
|> group()
|> filter(fn: (r) =>
r.baz >= 42
and r.baz <= 100
)
|> sort(columns: ["baz"], desc: true)
|> limit(n: 40)
|> keep(columns: ["baz", "dingdong",])
requestParams = mainQuery
|> unique(column: "dingdong")
|> findColumn(fn: (key) => true, column: "dingdong")
response = requests.get(url: "http://<MAGIC-HOST>/id2name", params: ["id[]": requestParams])
data = json.parse(data: response.body)
id2Name = array.from(rows: data)
translated = join.left(
left: mainQuery,
right: id2Name,
on: (l, r) => l.dingdong == r.id,
as: (l, r) => ({l with rName: (if exists r.name then r.name else l.dingdong)}),
)
|> sort(columns: ["baz"], desc: true)
yield translated
The issue arises when mainQuery
returns an empty result. In this case, the subsequent line id2Name = array.from(rows: data)
throws an error because data
is empty. I want the entire query to return an empty result when mainQuery
has no results, to avoid this error.
Could you please provide guidance on how to handle this situation gracefully? I want the query to terminate immediately and return an empty result when no data is available in mainQuery
.
Thank you for your help!
Best regards, Volker.