influxdbinfluxdb-2flux-influxdb

Finding count of data grouped by tag value with InfluxDB and Flux


I am migrating our applications from InfluxQL and Influx 1.8 to Flux and am struggling with grouping data by tag value.

We were previously writing directly to InfluxDB and are now writing to InfluxDB via OpenTelemetry and Telegraf.

An example of an old InfluxQL query is:

SELECT min("value") FROM "health" GROUP BY "category" fill(previous)

In the new Influx UI, I can see in a Simple Table output that I have a category tag, but it's not present in the list of Group columns:

Missing group tag

I've tried a very naive Flux query:

from(bucket: "interpay")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "health")
  |> group(columns: ["category"])
  |> yield(name: "min")

But this is returning all rows. I clearly have a fundamental misunderstanding of Influx 1.x vs. Influx 2.x queries and terminology differences. I've looked at the Influx 'Group data in InfluxDB with Flux' page, but I'm clearly still missing something.


Solution

  • Ignoring the InfluxDB query builder, I came up with this - which I think may be correct:

    from(bucket: "interpay")
      |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
      |> filter(fn: (r) => r["_measurement"] == "health")
      |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
      |> group(columns: ["category"])
      |> min(column: "_value")
      |> keep(columns: ["_value", "category", "_time"])
    

    I'm still not sure why category wasn't present in the query builder UI though.