grafana / influxdb-flux-datasource

Grafana datasource plugin for Flux (InfluxDB)
Apache License 2.0
51 stars 21 forks source link

Influx column names include every single tag #70

Closed wz2b closed 4 years ago

wz2b commented 4 years ago

When I request values back from influx from a plugin (e.g. PictureIt) I get a weird result. Here is the query:

from(bucket: "powermon")
  |> range(start: -1h)
  |> filter(fn: (r) => r._measurement == "modbus")
  |> filter(fn: (r) => r._field == "pv1" or r._field == "pv2" or r._field == "pv3" or r._field == "pv4" or r._field == "pv5" or r._field == "pv6" or r._field == "pv7")
  |> last()

The text result is this:

,result,table,_start,_stop,_time,_value,_field,_measurement,address,gateway,register
,_result,0,2020-01-30T14:57:52.00887398Z,2020-01-30T15:57:52.00887398Z,2020-01-30T15:57:44.259Z,0.59,pv1,modbus,28,10.7.4.131,1252
,_result,1,2020-01-30T14:57:52.00887398Z,2020-01-30T15:57:52.00887398Z,2020-01-30T15:57:44.348Z,0.51,pv2,modbus,28,10.7.4.131,1254
,_result,2,2020-01-30T14:57:52.00887398Z,2020-01-30T15:57:52.00887398Z,2020-01-30T15:57:44.438Z,1.46,pv3,modbus,28,10.7.4.131,1256
,_result,3,2020-01-30T14:57:52.00887398Z,2020-01-30T15:57:52.00887398Z,2020-01-30T15:57:44.529Z,0.45,pv4,modbus,28,10.7.4.131,1258
,_result,4,2020-01-30T14:57:52.00887398Z,2020-01-30T15:57:52.00887398Z,2020-01-30T15:57:44.618Z,0.39,pv5,modbus,28,10.7.4.131,1260
,_result,5,2020-01-30T14:57:52.00887398Z,2020-01-30T15:57:52.00887398Z,2020-01-30T15:57:44.709Z,0.37,pv6,modbus,28,10.7.4.131,1262
,_result,6,2020-01-30T14:57:52.00887398Z,2020-01-30T15:57:52.00887398Z,2020-01-30T15:57:44.888Z,0.04,pv7,modbus,28,10.7.4.131,1266

I intercepted the 'data-received' callback to see what I was getting (registering with base class MetrisPanelCtrl) and I see this:

{
  "alias": "modbus pv7 address=28 gateway=10.7.4.131 register=1266",
  "target": "modbus pv7 address=28 gateway=10.7.4.131 register=1266",
  "datapoints": [
    [
      0.04,
      1580399864888
    ]
  ]
}

In this particular case, what's coming back is actually a table of 7 values. Each one has a correct _field and _value and the _field name is pv1, pv2, pv3, ... in other words, it makes sense. What doesn't make sense to me is that I get this 'alias' that's everything.

Is this a bug in the driver, or minimally a difference between influx 1.7 and 2.0? Should the resultant 'target' be mapped to the _field (the actual name of the field) ?

amotl commented 4 years ago

Dear Christopher,

regarding proper field name aliasing, https://github.com/grafana/influxdb-flux-datasource/issues/42#issuecomment-532402207 by @mdb5108 might help you along.

With kind regards, Andreas.

wz2b commented 4 years ago

That actually did help, a little, in terms of being a workaround. Well, sort of. I changed my querion to this

from(bucket: "powermon")
  |> range(start: -1h)
  |> filter(fn: (r) => r._measurement == "modbus")
  |> filter(fn: (r) => r._field == "pv1" or r._field == "pv2" or r._field == "pv3" or r._field == "pv4" or r._field == "pv5" or r._field == "pv6" or r._field == "pv7")
  |> last()
  |> map(fn: (r) => ({_value:r._value, _time:r._time, _field:r._field}))

that sets _field to the simple, actual field name. When it makes it to grafana, though, it has an extra space in it:


1: Object { alias: " pv2", target: " pv2", datapoints: (1) […], target: " pv2", … }
​```

so using this query all I had to do to the plugin was add a .trim() but I'm still suggesting something is not quite right between what comes out of this driver and what makes its way in through the plugin API ... hmmmm.
amotl commented 4 years ago

That sets _field to the simple, actual field name. When it makes it through Grafana, though, it has an extra space in it.

Huh? Do you observe this as well, @wetterfrosch?

wetterfrosch commented 4 years ago

That sets _field to the simple, actual field name. When it makes it through Grafana, though, it has an extra space in it.

Huh? Do you observe this as well, @wetterfrosch?

Yes, but haven't investigated it in much detail. I darkly recall that the rendered legend-title might be a composition of _measurement and _field, so @wz2b tinker with

|> map(fn: (r) => ({_value:r._value, _time:r._time, _field:r._field, _measurement: "foo"}))

wetterfrosch commented 4 years ago

jep, if you just use _measurement and leave _field empty you'll get the space at the end.

wz2b commented 4 years ago

Yeah. That's right. If I set measurement to 'foo' then instead of " pv2" I get "foo pv2" ... so something somewhere in there needs to stop doing that, at least to say if _measurement == "" (or null or nil or strlen=0 or whatever) then don't put the space in there when you join the strings. So I think that's a bug ... but I'm not clear on if it's a problem with this datasource, or further upstream in grafana.

ryantxu commented 4 years ago

In 7x, the tags/labels are now included in the field display name. please open a new issue for suggestions on how to do this better, but I hope a big improvement