Influxdb 2.0 Flux - How to return 0 instead null

3.6k Views Asked by At

I want to count amount of values that are greater than specific value. Data:

enter image description here

from(bucket: "bucket name")
    |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
    |> filter(fn: (r) => r._value > 35)
    |> count()

If there are no values in the processing data range that are greater the specified value than the influx returns nothing (no data).

2

There are 2 best solutions below

0
On BEST ANSWER

Solution with a little trick... Instead of filter() and count() - need to use map() and sum()

from(bucket: "bucket name")
     |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
     |> map(fn: (r) => ({ r with _value: if r._value 35 then 1 else 0 }))
     |> sum()

0
On

Failing: InfluxDB2 null as zero (float())

Issues appeared with float values ,

where a virtual instance not running for a (missing) timeframe

appeared as constantly-cpu-consuming...

Influxdb connects series without 0 ,here a cpu graph with missing timeframes Influxdb connects series without 0 ,here a cpu graph with missing timeframes

  • fill() only works on integers,
  • non-existing datapoints can't be if-catched by value (...)
  • aggregateWindow() finally helped

Solution: InfluxDB2 null as zero (float())

from(bucket: "sys")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r._measurement == "docker_cpu_percent" )
//  |> window(every: 5m, period: 5m, createEmpty: true)
  |> aggregateWindow(every: 5m, fn: mean, createEmpty: true)
  |> map(fn: (r) => ({
      r with
      _value: if exists r._value then float(v: r._value) * 1.0  else 0.0
    })
  )

Proper resonse from influx with null as 0 float() when using aggregateWindow()

Resources

You might refer to