Time since last event


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.

Any help would be awesome.


For subtracting 2 timestamps give a look here Manipulate timestamps with Flux | InfluxDB Cloud Documentation

Thanks for the response @MzazM.

I’m trying to assign

time1 = uint(v: ["_time"])

with the value of my time column but getting an error calling function uint() cannot convert [string] to uint.

my guess
time1=time(v: uint(_time)) or time1=time(v: uint(["_time"])) but i am not an expert myself. Sorry.

1 Like

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}))

1 Like

as currentTime is a constant for each row/record, the first map could be replaced by a set() set() function | Flux 0.x Documentation

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)}))