I managed to get my query working - I am dividing the idle CPU percentage by the number of CPUs. However, in Grafana where I am plotting the results as a time based Graph, I do not seem to get the value of the grouped by column (_cpu) as the series name. How do I transform my result set into something that will output the grouped by column value in such a way that grafana will pick it up as the name of the series?
a = from(bucket: "telegraf/autogen")
|> range($range)
|> filter(fn: (r) => r._measurement == "system" and (r._field == "n_cpus" and r.host =~ /^$source$/))
|> window(every: $__interval)
|> mean()
|> duplicate(column: "_stop", as: "_time")
b = from(bucket: "telegraf/autogen")
|> range($range)
|> filter(fn: (r) => r._measurement == "cpu" and (r._field == "usage_idle" and r.host =~ /^$source$/ and r.cpu != "cpu-total" ))
|> window(every: $__interval)
|> mean()
|> duplicate(column: "_stop", as: "_time")
join(tables: {a:a, b:b}, on: ["_time", "_stop", "_start", "host"])
|> map(fn:(r) => ({
_time: r._time,
_cpu: r.cpu,
_value: (100.0 - r._value_b) / float(v: r._value_a)
}))
|> group(columns: ["_cpu"])
I do see the name of the _cpu field in the raw output data:
#datatype,string,long,string,dateTime:RFC3339,double
#group,false,false,true,false,false
#default,_result,,,,
,result,table,_cpu,_time,_value
,,0,cpu0,2019-09-09T21:32:20Z,0.5744255732288899
,,0,cpu0,2019-09-09T21:32:30Z,0.8258258259937996
,,0,cpu0,2019-09-09T21:32:40Z,1.0468594216190752
...
In Grafana I do not see the value of _cpu in the series name: