Calculate given time difference

Hi there,

I would like to divide the measurement series into 8h segments if the selected time range is greater than or equal to 8h. With increase() I “reset” the value at a given Time so I am also using the offset parameter in the window() function.

|> window(every: 8h, offset: 4h)
|> increase()

When the selected time range is less than 8h, I do not need to window() and increase() the series.

So my question is, how can I calculate the difference from v.timeRangeStart and v.timeRangeStop to make an if-else statement in the query?

dataset = from(bucket: "statData")
    |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
    |> filter(fn: (r) => r["_measurement"] == "counterData")
    |> filter(fn: (r) => r["_field"] == "counter")
   
if (d >= 8h) then
  dataset
    |> window(every: 8h, createEmpty: false, offset: 4h)
    |> increase()
else
  dataset
   |> yield()

You can create a new column by converting the timeRangeStart and timeRangeStop timestamps to int, subtracting, and then converting from nanoseconds to hours:

 |> map(fn: (r) => ({  r with measurentDuration:  float(v: uint(v: v.timeRangeStop) - uint(v: v.timeRangeStart))/float(v:3600000000000)}))

Solution was:

windowPeriod = int(v: v.windowPeriod)
if (windowPeriod >= int(v: 8h)) then

Didn’t test your approach. But I like to thank you for your thoughts.

1 Like