Alert query migration (influxQL to flux)

Hi, i am in the process of migrating a dashboard in particular two alert queries which look similar but have different outputs.

SELECT mean(“usage_user”) FROM “cpu” WHERE (“host” =~ /^$Host$/) AND $timeFilter GROUP BY time($__interval), “cpu” fill(null)
2)
SELECT mean(“usage_user”) FROM “cpu” WHERE (“host” =~ /^$Host$/) AND $timeFilter GROUP BY time($__interval) fill(null)

For query 1 I managed to migrate it using the below query:
import “influxdata/influxdb/schema”

windowPeriod = int(v: v.windowPeriod)
myWindow = if windowPeriod < int(v: 100000000) then 1s else v.windowPeriod
from(bucket: “telegraf/one_week_only”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r._measurement == “cpu”)
|> filter(fn: (r) => r._field == “usage_user”)
|> filter(fn: (r) => r.host =~ /^$Host$/)
|>aggregateWindow(every: myWindow, timeSrc: “_start”, fn: mean)

However the second one i am not sure how to migrate that.

Hello @Niikhiil1997,
So it looks like the only difference between 1) and 2) is 1) you’re grouping by cpu and in 2) you’re not grouping by cpu.
If you want the mean across all cpu’s and hosts I’d do (to get one line back):

windowPeriod = int(v: v.windowPeriod)
myWindow = if windowPeriod < int(v: 100000000) then 1s else v.windowPeriod
from(bucket: “telegraf/one_week_only”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r._measurement == “cpu”)
|> filter(fn: (r) => r._field == “usage_user”)
|> filter(fn: (r) => r.host =~ /^$Host$/)
|> group()
|>aggregateWindow(every: myWindow, timeSrc: “_start”, fn: mean)

Adding an empty group() allows you to ungroup your data.
https://docs.influxdata.com/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/group/

1 Like