Hi,
I struggle with the following flux query challenge:
We use InfluxDB v1.8.3 and stores a lot of process data in a single InfluxDB Measurement.
Fore instance we store tank level readings every 1 second and pump start and stop events, typically every 10 minutes or so.
I would like to make a query that finds every pump event (this is not a problem) and then looks up the corresponding tank level for every pump event.
The timestamp of a pump event and timestamp of a level measurement do not match so an average of the level measurements in the range of pump event +/- 3 seconds is fine.
I have succeeded doing this using a join but it’s very slow, so I am hoping for a more efficient way.
A level query will look something like this:
from(bucket: “machines”)
|> range(start: {start_time}, stop: {stop_time})
|> filter(fn: (r) => r._measurement == “SN000022”)
|> filter(fn: (r) => r.equipment == “T5001”)
|> filter(fn: (r) => r.data == “level”)
|> drop(columns: ["_start", “_stop”, “_field”, “_measurement”, “equipment”, “data” “system”, “unit”])
Output:
result table _time _value
0 _result 0 2022-01-14 11:23:54.354000+00:00 77.390625
1 _result 0 2022-01-14 11:23:55.356000+00:00 77.531250
2 _result 0 2022-01-14 11:23:56.357000+00:00 77.656250
A pump event query will look something like this:
Including two new columns with time shifted timestamps
from(bucket:“machines”)
|> range(start: {start_time}, stop: {stop_time})
|> filter(fn: (r) => r._measurement == “SN000022”)
|> filter(fn: (r) => r.equipment == “P5001”)
|> filter(fn: (r) => r.data == “running”)
|> filter(fn: (r) => r._field == “value”)
|> difference()
|> filter(fn: (r) => r._value != 0)
|> map(fn: (r) => ({{r with t1: r._time}}))
|> map(fn: (r) => ({{r with t2: r._time}}))
|> timeShift(duration: -3s, columns: [“t1”])
|> timeShift(duration: 3s, columns: [“t2”])
|> keep(columns: ["_time", “t1”, “t2”])
Output:
result table _time t1 t2
0 _result 0 2022-01-14 11:23:58.501000+00:00 2022-01-14 11:23:55.501000+00:00 2022-01-14 11:24:01.501000+00:00
1 _result 0 2022-01-14 11:26:08.067000+00:00 2022-01-14 11:26:05.067000+00:00 2022-01-14 11:26:11.067000+00:00
2 _result 0 2022-01-14 11:26:49.831000+00:00 2022-01-14 11:26:46.831000+00:00 2022-01-14 11:26:52.831000+00:00
So, my dream solution would be to embed the level query as a nested query in the pump event query and use t1 and t2 as start: and stop: parameters in the nested level query.
So far, I have not been able to figure out how to do this…?
Any advice would be appreciated.
BTW, the examples are run from Jupyter using InfluxDBclient Python library and hence the single and double { } around variables.
Thanx