Measurement exploration FLUX

Hello,

I am desperated trying to build what should be a simple query. I want to build a “Data Exploration” dashboard in grafana in the same way as Chronograf does but I am blocked on the last query. The idea is to have 3 drop down menu in Grafana, first filtering by bucket, second, filtering by topic (tag) and third, showing all the measurements of the topic with selected tag (topic) value. I hope someone could help me on the topic:

  1. Query: Filter bucket
buckets()
  1. Query: Filter by tag called topic
import "influxdata/influxdb/v1"
v1.tagValues(
    bucket: v.bucket,
    tag: "topic",
    predicate: (r) => true
)
  1. Query: Filter by bucket + tag.
import "influxdata/influxdb/v1"
v1.measurements(bucket: v.bucket)

But I don’t know how to include the filter by tag(topic)== something.

Thanks in adavance,

@alex1510 I’m not sure I fully understand your use case and I’m wondering if this is a misunderstanding of InfluxDB semantics and data structure. Are you trying to list measurements with a tag or list fields with a tag?

A measurement is a logical container for grouping fields and tags. Measurements contain tags and fields.

It is possible to get a list of measurements with a specific tag, but it’s an incredibly expensive query. It essentially has to query all the data in your database from a given time range.

Hello @scott , sorry for the late reply but I didn’t receive any notification from the forum…I need to have a look…
I will try to explain the question with the screenshot:

First filter (red line): The user will be able to select the bucket that will be used for following filters.
Second filter (blue line): The user will be able to select the tag that will be used for filtering the measurements.
Third filter: The user will be able to select the measurement that wants to display.

Basically all the measurements come from different machines in the shopfloor that are publising data to a MQTT broker. Depending on the plant, measurements are stored in different buckets. Production line name (topic in mqtt) is stored as tag in the measurement.

I hope now you have a better understanding of the global need.

Thanks in advance.

Ok, I think these are what you’re looking for:

Get a list of buckets

buckets()

Get a list of tags in a bucket

import "influxdata/influxdb/schema"

schema.tagValues(
    bucket: v.bucket,
    tag: "topic",
    predicate: (r) => true,
    start: -30d,
)

Get a list of measurements with that tag

from(bucket: v.bucket)
    |> range(start: -30d)
    |> filter(fn: (r) => r.topic == v.prod_line)
    |> group()
    |> distinct(column: "_measurement")

Query that uses all the selected variables

from(bucket: v.bucket)
    |> range(start: -30d)
    |> filter(fn: (r) => r.topic == v.prod_line)
    |> filter(fn: (r) => r._measurement == v.measurement)
1 Like

Hello @scott , sorry but it does not work…

I have tried to include the query as template variable and also to include the query directly but none of them returns anything. I have also slightly modify your query following Chronograf editor with no luck…

The only thing I want to reproduce in Grafana is the same behavior I can do with Chronograf. Filter by bucket, filter by tag=topic and list all the measurements…that’s all.

@alex1510 Should PRODLINE have an underscore in it?

@scott , underscore is just related to “name” and “label”. But as you can see, variable name is without underscore.

@alex1510 Gotcha. What’s the output of:

from(bucket: "${BUCKET}")
    |> range(start: -30d)
    |> filter(fn: (r) => r["topic"] == "${PRODLINE}")


Better but still getting “false”

I don’t know why that would return false. That query should return all the data where topic equals the selected PROD_LINE. It’s been a while since I’ve messed with Grafana variables, but I wonder if the Grafana will only pull variable values from the _value column of the query results. Try this:

from(bucket: v.bucket)
    |> range(start: -30d)
    |> filter(fn: (r) => r.topic == v.prod_line)
    |> group()
    |> distinct(column: "_measurement")
    |> rename(columns: {_measurement: "_value"})

No data

@alex1510 Do you get no results from that query if you remove distinct() and rename()??

Hi, I have “v”

And from Chronograf:

Hello,

I found the following topic and it helped me to write the query:

I don’t know if it is well optimized…at least it works. Do you know if it could be improved and it time range could be removed just to avoid loosing some tags that maybe does not change in the last 30d?

from(bucket: "${BUCKET}")
  |> range(start: -30d)
  |> filter(fn: (r) => r.topic == "${PRODLINE}")
  |> group(columns: ["_measurement"], mode:"by")
  |> distinct(column:"_measurement")
  |> group()

InfluxDB doesn’t allow “unbounded” queries, so you have to provide a time range, but you can do longer than 30 days.