@Sven_Achtelik I think you could accomplish this with the events.duration()
function. It’s a little convoluted, but you’d first have to logically assign a _level
of either crit
, warn
, info
, or ok
, then use the monitor.stateChangesOnly()
function (which depends on those four levels). Then ungroup and sort by time again (since monitor.stateChangesOnly()
changes the grouping and sorting). Then with the output of events.duration()
, you can calculate the stop time by adding the returned duration to the start time of the state:
import "influxdata/influxdb/monitor"
import "contrib/tomhollingworth/events"
from(bucket: "example-bucket")
|> range(start: -15m)
|> filter(fn: (r) => r.field == "sensor1")
|> map(fn: (r) => ({ r with _level: if r._value >= 1000 then "crit" else "ok" }))
|> monitor.stateChangesOnly()
|> group()
|> sort(columns: ["_time"])
|> events.duration(unit: 1ns, stop: now())
|> map(fn: (r) => ({
state: if r._level == "crit" then "on" else "off",
start_time: r._time,
stop_time: time(v: int(v: r._time) + r.duration),
}))
The resulting output will look something like:
state | start_time | stop_time |
---|---|---|
off | 02020-12-22T20:48:08Z | 2020-12-22T20:48:18Z |
on | 2020-12-22T20:48:18Z | 2020-12-22T20:48:28Z |
off | 2020-12-22T20:48:28Z | 2020-12-22T20:48:58Z |
on | 2020-12-22T20:48:58Z | 2020-12-22T20:49:08Z |
off | 2020-12-22T20:49:08Z | 2020-12-22T20:49:58Z |
on | 2020-12-22T20:49:58Z | 2020-12-22T20:58:27Z |