Search code examples
grafanainfluxdbfluxinfluxql

InfluxDB: Flux vs InfluxQL chart differences


I am trying to migrate from InfluxQL to Flux but when displayed in a chart I get strange results and I was not able to solve it.

InfluxQL query and chart:

SELECT mean("machine_temperature") FROM "machine" WHERE time >= 1677625200000ms and time <= 1679039209170ms GROUP BY time(20m) fill(null)

enter image description here

This is the same basic version in Flux:

from(bucket: "mybucket")
  |> range(start: -30d, stop: -26d)
  |> filter(fn: (r) => r["_measurement"] == "machine")
  |> filter(fn: (r) => r["_field"] == "machine_temperature")
  |> aggregateWindow(every: 1h, fn: mean, createEmpty: false)
  |> yield(name: "Mean")

But in the chart I get multiple stacking lines, each for every tag with different colors. There are also "gaps" between parts of the chart and it appears broken. Why is it?

enter image description here

I tried reading all the guides about migration from InfluxQL and all the guides about Flux but I was not able to get a clean chart.

Is there something very basic that I am missing?

Please note that the two charts are generated on the same database.


Solution

  • Flux handles series different than InfluxQL. In flux series are "separate" by default. This means that any Flux query that does not specify otherwise will always group by every tag (like adding a GROUB BY * in your InfluxQL query).

    If you want to replicate the InfluxQL query than you should do:

    from(bucket: "mybucket")
      |> range(start: 1677625200000000000, stop: 1679039209170000000)
      |> filter(fn: (r) => r["_measurement"] == "machine")
      |> filter(fn: (r) => r["_field"] == "machine_temperature")
      |> group(columns: [])
      |> aggregateWindow(every: 20m, fn: mean, createEmpty: false)
      |> yield(name: "Mean")
    

    The group statement is telling flux to not group any of the tags (to replicate the fact that you are grouping by time only in the InfluxQL query).