I am trying to understand the relationship between aggregateWindow’s window size and the offset parameter.
My goal is to get the sum of a value over the dashboard’s time span, AND the sum of the previous timespan of the same length.
Dashboard time span: Last-1-year
import "date"
timeInRange = duration(v: (int(v: v.timeRangeStop) - int(v: v.timeRangeStart)))
newStart = date.sub(from: v.timeRangeStart, d: timeInRange)
from(bucket: "DOWNSAMPLE")
|> range(start: newStart, stop: v.timeRangeStop)
// simplified code here
|> filter(fn: (r) => r["_field"] == "triggers")
|> aggregateWindow(every: 365d, fn: sum, createEmpty: false)
|> yield(name: "sum")
Which gives me this:
(displaying in time-graph here to understand what’s happening)
The last datapoint (2024-08-21) does not span a full year, only the duration to the previous datapoint (2023-12-18).
When I add an offset of 247d (found by trial and error) to the aggreagteWindow function, I get the datapoints about where I expect them to be.
|> aggregateWindow(every: 365d, fn: sum, createEmpty: false, offset: 247d)
I also tried setting the window period to “1y”, and then I had to set the offset to the day-of-the-year minus one, to get the alignment I wanted. Which makes sense with the 1-y window starting on the first day of the year.
todayDay = duration(v: (date.yearDay(t: now())-1) * 24 * 60 * 60 * 1000 * 1000 * 1000 )
|> aggregateWindow(every: 1y, fn: sum, createEmpty: false, offset: todayDay)
Instead of figuring out a dynamic offset for each dashboard time span duration, is there a way to start the window period at the LAST data point (going right to left)? I tried negative ‘period’ parameters, but without success.
InfluxDB 2.7.3
Graphs with Grafana 10.3.3