'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 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 alespour