Search code examples
grafanainfluxdbinfluxdb-2grafana-variable

How to disable some Influx queries in Grafana based on variable value?


I have a Dashboard graph in Grafana, where I need to select data from Influx. However, I want to use variable to change which queries are active and which are not.

Currently Grafana does not support variables in transformations, thus I am trying to move this logic to Influx Query. I do it by changing the measurement names to non-existing variable name. However, this approach seems to be very slow, when I select the empty option as the query takes forever to load. But I need to load around 80 queries for the one plot, they are similar, but have different names.

I tried adding if condition to the last part (have joint inside a conditioned variable), but found no way to return false/empty table properly.

mode = "${setpoint_mode:text}"
display = if mode == "Jiné" or mode == "All" then true else false
zadana_name = if display then "Zadana" else ""
merena_name = if display then "Vystup" else ""

zadana = from(bucket: "ot_01")
  |> range(start: v.timeRangeStart, stop:v.timeRangeStop)
  |> filter(fn: (r) => r._measurement == zadana_name)
  |> aggregateWindow(every: 15m, fn: mean, column: "_value")

aktualni = from(bucket: "ot_01")
  |> range(start: v.timeRangeStart, stop:v.timeRangeStop)
  |> filter(fn: (r) => r._measurement == merena_name)
  |> aggregateWindow(every: 15m, fn: mean, column: "_value")

join(tables:{zadana:zadana, aktualni:aktualni}, on:["_time"])
  |> map(fn:(r) => ({_time: r._time, _value: (r._value_aktualni - r._value_zadana) < -${threshold_nedotopeni}}))
  |> reduce(fn: (r, accumulator) => ({
      _name: "Vystup",
      _no: accumulator._no + 1,
      _value: if r["_value"] then accumulator._value + 1 else accumulator._value,
    }),
    identity: {_no: 0, _value: 0, _name: "Vystup"}
  )
  |> map(fn:(r) => ({_name: r._name, _value: float(v: r._value)/float(v: r._no) }))

Solution

  • Solution found:

      |> map(fn:(r) => ({_name: r._name, _value: float(v: r._value)/float(v: r._no), _display: display }))
    

    this way, the variable is passed as a value, which can be used inside transformations.