Macros, query snippets, shared custom functions

Okay, I have a dashboard with like 8 panels (10 or so queries)…
SDR stuff, pulling in as much weather station info as I can.

I have a filter list that I used to filter out devices that produce some bogus data.
And I want to filter out things that are not weather stations…
Looks like so:

  |> filter(fn: (r) => not strings.hasPrefix(v: r._measurement, prefix: "Abarth"))
  |> filter(fn: (r) => not strings.hasPrefix(v: r._measurement, prefix: "Schrader"))
  |> filter(fn: (r) => not strings.hasPrefix(v: r._measurement, prefix: "Hyundai"))
  |> filter(fn: (r) => not strings.hasPrefix(v: r._measurement, prefix: "Renault"))
  |> filter(fn: (r) => not strings.hasPrefix(v: r._measurement, prefix: "Springfield-Soil"))
  |> filter(fn: (r) => not strings.hasPrefix(v: r._measurement, prefix: "GT-WT"))
  |> filter(fn: (r) => not strings.hasPrefix(v: r._measurement, prefix: "Oregon-SL109H"))
  |> filter(fn: (r) => not strings.hasPrefix(v: r._measurement, prefix: "Sharp-SPC775"))

I dont want to copy and paste this around to the 10 or so queries I have driving various panels. I would LOVE to be able to just write the “query snippet” in one spot and sub it in to each query as needed.

So something like

import "strings"

from(bucket: "rtl_433")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_field"] == "temperature_C")
  |> drop(columns: ["channel"])
$MY_FILTERS
...

Or create some custom function my_filters() and just do something like

import "strings"

from(bucket: "rtl_433")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_field"] == "temperature_C")
  |> drop(columns: ["channel"])
  |> my_filters()
...

Im having a hard time understanding if this is possible in Flux though.
Its a hard topic to google :smiley:
Anyone know how to accomplish this??
Thanks!

1 Like

I should say that not all the queries end up using the exact same filters.
It would be nice to have a function or macro like this that I could use because I would define more than one, and use them where appropriate.

I really dont want to change how things are flowing into influx (wanna jam all this data into the existing bucket).