"Query Discrepancy: Flux Query Renders Multiple Lines in InfluxDB but Only Displays First Point in Grafana"

Hello,
I have the following Flux query:

import "http/requests"
import "experimental/json"
import "array"
import "join"

HOSTs = ["X1","X2","X3","X4"]

mainQuery = from(bucket: "foo_bucket")
  |> range(start: 2023-06-13T08:10:18.741Z, stop: 2023-06-15T08:10:18.741Z)
  |> filter(fn: (r) => r["_measurement"] == "foo")
  |> filter(fn: (r) => r["_field"] == "bar")
  |> filter(fn: (r) => contains(value: r["host"], set: HOSTs))
  |> group()

requrestParams = mainQuery
  |> distinct(column: "host")
  |> findColumn(fn: (key) => true, column: "_value")

// convert the host to an human readable name
response = requests.get(url: "http://<<MAGIC-HOST>>/id2name", params: ["id[]": requrestParams])
data = json.parse(data: response.body)
id2Name = array.from(rows: data)

translated = join.left(
    left: mainQuery,
    right: id2Name,
    on: (l, r) => l.host == r.id,
    as: (l, r) => ({l with rName: (if exists r.name then r.name else l.host)}),
)
  |> group(columns: ["host"])
  
yield translated

When I run this query in the InfluxDB data explorer, it gives me a nice result, with each host on its own line. However, when I execute the same query in Grafana, I only see the first point in the graph for each host, so there is no line. In the query inspector, I can see that all the data is there and categorized by the respective hosts. Does anyone have an idea?

Hi @skydiablo

That’s pretty perplexing. I can only speculate that maybe one of these settings is affecting the results in Grafana. Maybe try changing some and see if it displays more than 1 point for each host.

so sorry, nothing of these options are changing any of this behavior. other ideas?

EDIT:
current version is: Grafana v10.0.0 (81d85ce802)

If the contains() statement is the problem, maybe this thread will help: Dropdown menu in dashboards where data are coming from influxdb - #9 by fercasjr

hmmm, if i remove this last grouping by host, it is working fine. but why?

okay, here is the working query:

import "http/requests"
import "experimental/json"
import "array"
import "join"

HOSTs = ${host:json}

mainQuery = from(bucket: "foobar")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "foo")
  |> filter(fn: (r) => r["_field"] == "bar")
  |> filter(fn: (r) => contains(value: r["host"], set: HOSTs))
  |> group()

requrestParams = mainQuery
  |> distinct(column: "host")
  |> findColumn(fn: (key) => true, column: "_value")

response = requests.get(url: "http://${metaAssetProviderHost}/id2name", params: ["id[]": requrestParams])
data = json.parse(data: response.body)
id2Name = array.from(rows: data)

translated = join.left(
    left: mainQuery,
    right: id2Name,
    on: (l, r) => l.host == r.id,
    as: (l, r) => ({l with rName: (if exists r.name then r.name else l.host)}),
)
  
yield translated
  |> group(columns: ["host"])

The output still consists of a point per host. However, when I check the “Query inspector,” I can see the correct result in the table selector dropdown. I also noticed a dropdown selection called “series joined by time,” where I can see a single line result with additional columns for each host. I believe this is the desired output for the line graph. How can I skip this step and directly use the other tables to obtain the line graph result?

I got it working! Although I can’t pinpoint the exact reason, when I reduced the fields to the essentials, I achieved the desired result. I added |> keep(columns: ["_time", "_value", "host"]) to the mainQuery , and now it’s working.
thx for the attention!

1 Like