InfluxDB and Flux: What's the easiest way to calculate time differences between data points in a series?

758 Views Asked by At

How to get time differences of data points of a series of influxdb data?

I have found the following working method, but it seams awfully inefficient. Is there a more simple way to do it?

Explanation: after selecting my series with filters, I replace every value with 1.0. Then I sum up each consecutive value. This sum is time independent. Basically it puts an index or number to each data point. Then I take the derivative (this IS time dependent). Since every consecutive value is exactly 1.0 higher, the derivative is 1.0/timediff, so take the inverse and get the timediff in seconds.

from(bucket: "MyBucket")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "main_sampled_data")
  |> filter(fn: (r) => r["_field"] == "Data")
  |> map(fn: (r) => ({ r with _value: 1.0}))
  |> cumulativeSum()
  |> derivative()
  |> map(fn: (r) => ({ r with _value: 1.0/r._value}))
  |> yield(name: "timediff")
0

There are 0 best solutions below