I’m trying to calculate the number of seconds since a specific event occurred. I already have a Flux query that gives me a list of systems grouped by hostname, with a time stamp of the last event of interest…for simplicity lets say the event is called “phase1”.
At the moment I am doing this in Grafana using unit of “Date & Time” set to “From Now” which does give me the info. The problem is I can’t add threshold colors to highlight if a elapsed time to NOW is over a certain time.
I thought it would be better to do it in InfluxDB_2 and just map it to a new column then I can apply the thresholds in Grafana.
I’m fairly new to Flux and both InfluxDB\Grafana…I tried to subtract the time stamp from the system.time() but got an error that I can’t subtract the time stamp, or something like that.
Thanks @MzazM, you gave me enough info to get something working. It may not be the most efficient but I was able to get it by mapping columns and doing the calculation there.
currentTime = system.time()
|> map(fn: (r) => ({ r with current_timestamp: uint(v: currentTime)}))
|> map(fn: (r) => ({ r with event_timestamp: uint(v: r._time) }))
|> map(fn: (r) => ({ r with time_since_event: r.current_timestamp - r.event_timestamp}))
or even do everything with 1 map (untested):
currentTime = system.time()
|> map(fn: (r) => ({ r with time_since_event: uint(v: currentTime) - uint(v: r._time)}))