Search code examples
aggregateinfluxdbfluxflux-influxdb

influx aggregateWindow with continuous counter


An influx 2 database stores incrementing values from an mechanical counter (gas counter). The goal is to build a query to get the consumption over a certain interval, e.g. one day. With SQL I would group the data in the desired interval and than calculate max() - min() +1 for that interval. What is the preferred way to do that with the flux query language?

from(bucket: "Energy")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "gas")
|> filter(fn: (r) => r["kind"] == "count")
|> aggregateWindow(every: 24h, fn: difference, createEmpty: false)
|> yield(name: "interval")

does not work, error @5:6-5:69: missing required argument column (argument fn)


Solution

  • The solution is to examine difference() before aggregateWindow and as aggregate function to use sum.

    from(bucket: "Energy")
      |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
      |> filter(fn: (r) => r["_measurement"] == "gas")
      |> filter(fn: (r) => r["_field"] == "count")
      |> difference()
      |> aggregateWindow(every: 1h, fn: sum, createEmpty: false)
      |> yield(name: "consumption")