Window duration in seconds with gaps in data


I have a table with fuel consumption measurements. The measurements are in liters per hour. Roughly one measurement every 10 seconds. I’m trying to find a way to get a result that shows the total amount of liters consumed in a 10 minute window.

I now have this bit of Flux.

from(bucket: "dev_measurements/autogen")
  |> range(start: 2020-01-01T00:00:00Z)
  |> filter(fn: (r) => 
    r.shipId == "71958fc6-194f-4cbb-8b20-52835b77fc64" and
    r._measurement == "fuel_consumption" and 
    r._field == "l/h" 
  |> window(every: 10m)
  |> reduce(fn: (r, accumulator) => ({
        sum: r._value + accumulator.sum,
        count: accumulator.count + 1.0,
      identity: {sum: 0.0, count: 0.0}
  |> group(columns: ["sourceId"])
  |> elapsed(unit: 1s, timeColumn: "_start")
  |> map(fn: (r) => ({ r with average: r.sum / r.count }))
  |> map(fn: (r) => ({ r with absolute: (r.average / 3600.0) * float(v: r.elapsed) }))

This works most of the time as long as there is no gap in the data longer than 10 minutes.

How can I get the duration of the window in seconds without comparing it against the last window? That would make this work correct also when there are gaps in the data I think.


I solved it by replacing

|> elapsed(unit: 1s, timeColumn: "_start")


|> map(fn: (r) => ({ r with elapsed: (uint(v: r._stop) - uint(v: r._start)) / uint(v: 1000000000)}))