0

Using Grafana 8 & influx db2 I want to combine two measurements & add the values

measurement 1:

from(bucket: "myBucket")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "SCP" )
  |> filter(fn: (r) => r["device"] == "device1")
  |> filter(fn: (r) => r["_field"] == "value")
  |> aggregateWindow(every: v.windowPeriod, fn: sum, createEmpty: false)
  |> map(fn: (r) => ({ _value:r._value, _time:r._time, _field:"SC P" })) 

This shows a time series in grafana with legend 'SC P'

measurement 2

from(bucket: "myBucket")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "SCO" )
  |> filter(fn: (r) => r["device"] == "device1")
  |> filter(fn: (r) => r["_field"] == "value")
  |> aggregateWindow(every: v.windowPeriod, fn: sum, createEmpty: false)
  |> map(fn: (r) => ({ _value:r._value, _time:r._time, _field:"SC O" })) 

This shows a time series in grafana with legend 'SC O'

Combined measurement

from(bucket: "myBucket")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "SCO" or r["_measurement"] == "SCP")
  |> filter(fn: (r) => r["device"] == "device1")
  |> filter(fn: (r) => r["_field"] == "value")
  |> group()
  |> aggregateWindow(every: v.windowPeriod, fn: sum, createEmpty: false)
  |> map(fn: (r) => ({ _value:r._value, _time:r._time, _field:"SC T" }))

This shows a time series in grafana with legend '_value', whereas I want it to be "SC T" The value itself is correctly calculated as the sum of both (after I added group() in the query)

grafana legend issue

My question : How can I get it to show the correct legend ( "SC T" i.s.o. "_value" ) ?

Remark 1 : As far as I can tell, the 3 queries return exactly the same structure of data Remark 2 : I can't use the override, as I have multiple of these queries in the same panel, all showing '_value' in the legend.

1 Answer 1

1

Managed to find the answer. All I had to do was add this line at the end

|> map(fn: (r) => ({ time : r["_time"], "SC T" : r._value}))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.