cheesus
cheesus

Reputation: 1181

Adding cross-measurement values using Flux query language in grafana

I have an InfluxDB set up with Flux and want to display a sum the most recent values of two measurements.

The individual values can be displayed via

x1 = from(bucket: "telegraf/autogen")
|> range(start: -1h)
|> filter(fn: (r) =>
   r._measurement == "value1")

x1

if I attempt to display the sum via

x1 = from(bucket: "telegraf/autogen")
|> range(start: -1h)
|> filter(fn: (r) =>
  r._measurement == "value1")

x2 = from(bucket: "telegraf/autogen")
|> range(start: -1h)
|> filter(fn: (r) =>
  r._measurement == "value2")

x1 + x2

I get an error. What is the proper way to add the most recent value of measurements?

Upvotes: 0

Views: 1140

Answers (1)

JrBenito
JrBenito

Reputation: 1013

you managed to got x1 table and x2 table, now you join both and add new field with the sum:

new_table = join(tables: {x1: x1, x2: x1}, on: ["_time"])
  |> map(fn: (r) => ({ r with "sum_x1_x2": (r._value_x1 + r._value_x2)})

The map function allows you to modify/create new values on a table. The result table might have columns "_field", "_measurement", "_value", one for each input table (x1 and x2). Also, the mapped filed "sum_x1_x2" with summed value.

Aggregation / window of data might help as well.

Upvotes: 0

Related Questions