How do I set dynamically every on aggregateWindow?

Hi,
I would like to change every on aggregateWindow dynamically.
for example, range on Dashboard is

  • less than 1hour : every : 10s
  • 1hour to 3hours : every : 1m
  • 3hours to 1day : every : 10m
  • 1day to 1week : every : 1h

However, I cannot find solution.
How do I set it?
My script is here.

from(bucket: “my_bucket”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r[“_measurement”] == “Point”)
|> filter(fn: (r) => r[“_field”] == “Target”)
|> aggregateWindow(every: 10m, fn: last, createEmpty: false)
|> difference()
|> yield(name: “last”)

1 Like

You could compute delta= v.timerangestart - v timerangestop in a variable at the beginning. Note that, one of the 2, at least in the past, was a duration, so you have to convert it to a time (or an integer?) I don’t remember if it was start or stop but you’ll figure it out.
Then you make a sequence of if-else. Something like:

Test = if delta <1h then… Else if delta < 3h then…
I don’t remember exactly the if structure and i am on the phone. Anyway, we do something similar somewhere so it is doable

Then you replace your 10m in your script inside of the aggregate window with the variable Test
Good luck

Hi, MzazM,
Thank you for your advice!
I could set variable time in every.
I share the solution.
Every is 1m when range is less than 1h.
When range is over 1h, every is 10m.

from(bucket: “my_bucket”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r[“_measurement”] == “Point”)
|> filter(fn: (r) => r[“_field”] == “Target”)
|> aggregateWindow(every:
if int(v: v.timeRangeStop) - int(v: v.timeRangeStart) < 60 * 60 * 1000000000 then 1m
else 10m, fn: last, createEmpty: false)
|> difference()
|> yield(name: “last”)

old thread, but maybe interesting for others as well.
you can also use |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: true) and it will use reasonable values automatically.
(got it from: New to Influxdb Grafana query - InfluxDB - Grafana Labs Community Forums and [Flux] When applied "group()" after aggregateWindow() in a query, graph doesn't show - InfluxDB - Grafana Labs Community Forums)

but i not sure where the best position for the aggregateWindow() function is? is it the right after range or better after the filters? depends where you can leave behind the most datapoints, right? if you have datapoints every few seconds and a time range of a few days it is best to get rid of them as early as possible, right?