Flux for querying across multiple measurements

Hi,

I was wondering if Flux for InfluxDB v2.0 (or even TICKscript for Kapacitor) could be used for real-time processing of data for multiple measurements.

As an example, let’s say I have the following 2 data sets (measurements):
Measurement #1: cpu
measurement,tag,field
cpu,temperature=50,cpu_usage=30
cpu,temperature=60,cpu_usage=40
cpu,temperature=70,cpu_usage=50
cpu,temperature=80,cpu_usage=60

Measurement #2: gpu
measurement,tag,field
gpu,temperature=50,gpu_usage=30
cpu,temperature=50,gpu_usage=31
cpu,temperature=50,gpu_usage=32
gpu,temperature=61,gpu_usage=41
cpu,temperature=62,gpu_usage=42
cpu,temperature=80,gpu_usage=60

I want to use Flux to query and retrieve all the points (all those 4 lines) from the cpu measurement, then iterate through each of those points one at a time (row by row), checking if there are any points in the gpu measurement that have a matching temperature. If the gpu.temperature matches cpu.temperature, output that point to a new measurement named after the matching temperature value.

> Is this possible using a Flux task?

Example:

  • The first point in the cpu measurement has cpu.temperature = 50. Can we store that 50 value (cpu.temperature value), then send a query to the gpu measurement to return all rows that have gpu.measurement = 50 to a new measurement also named after the value 50?

  • The next two points in the cpu measurement do not have any matching temperatures with the gpu measurements, so nothing else would be stored.

  • For the fourth point in the cpu measurement, cpu.temperature = 80 = gpu.temperature (the last point), so it should output that last point to a measurement named 80.

New Measurement #1: 50
measurement,tag,field
50,temperature=50,gpu_usage=30
50,temperature=50,gpu_usage=31
50,temperature=50,gpu_usage=32

New Measurement #2: 80
measurement,tag,field
50,temperature=80,gpu_usage=60

Another question I have is, let’s say I had the exact same situation and data as above, but now I have an extra field key/value:
Measurement #1 (modified): cpu
measurement,tag,field,field2
cpu,temperature=50,cpu_usage=30,measurement_to_query=gpu
cpu,temperature=60,cpu_usage=40,measurement_to_query=gpu
cpu,temperature=70,cpu_usage=50,measurement_to_query=gpu
cpu,temperature=80,cpu_usage=60,measurement_to_query=gpu

> Is there a way I can extract the value from the field key “measurement_to_query” and store it in a variable for use in a query?

This way, if I had multiple gpu measurements (gpu1,gpu2,gpu3), I could specify which measurement to find matching temperatures from.

I am new to using InfluxDB 2 and Flux is not like typical programming languages so I am not sure what I am able to and unable to do. Thanks!

Bumping this post…

So what I did have in mind for at least a starting point is that since I am only interested in the temperature for the first measurement, I can extract all the temperatures from the data points/records/rows:

result = from(bucket: “telegraf”)
|> range(start: startDate)
|> filter(fn: (r ) => r._measurement == “cpu”)
|> tableFind(fn: (key) => key._field == “cpu_usage”)
|> getColumn(column: “temperature”)

result //this returns an array of temperature values: [60, 70, 80]

Then I can do a query on the second measurement gpu:

from(bucket: “telegraf”)
|> range(start: startDate)
|> filter(fn: (r ) => r._measurement == “gpu”)
|>

//This is where I am not sure of what to do next
// In pseudocode, this is what I want to achieve:
for each row/record in the gpu measurement table{
if gpu.temperature in [60, 70, 80]{
output this row/record to a new measurement named after the gpu.temperature value
}
}

Is there a way to do this in Flux syntax?