grafanaflux-influxdb

Grafana, Use of local variable in FLux query


beeing relativ new to Flux, I'm trying to make calculation on rows based on a value (tot) I get in amount.

> tot=from(bucket: "stat_tst")
  |> range(start: v.timeRangeStart, stop:v.timeRangeStop)
  |> filter(fn: (r) =>r._field == "speed" and r._value > 0.0)
  |> group()
  |> count()

from(bucket: "stat_tst")
  |> range(start: v.timeRangeStart, stop:v.timeRangeStop)
  |> filter(fn: (r) =>r._field == "speed" and r._value > 0.0)
  |> map(fn: (r) => ({ r with _value: r._value / tot }))

and this is yielding me

invalid: type error @9:39-9:53: [A] is not Divisible

What am I doing wrong ? In advance thank you for your support.


Solution

  • In your script, tot is a table. You need to extract scalar value like

    tot = from(bucket: "stat_tst")
      ...
      |> count()
      |> findColumn(fn: (key) => true, column: "_value")
    

    See Extract scalar values in Flux for details.