How to write a generic task for all table?

Hello,

I have created a task in order to export each day to a second bucket the min/max/mean of my outside temperature sensor for long term storage.

It works well so now I’m looking to do the same for all my others sensors without doing a copy/past of the same script with different filters.

Is there a way to apply my query to all table throught a generic script ?

My original query :

import "date"
import "experimental"
TODAY = date.truncate(t: now(), unit: 1d )
YESTERDAY = experimental.addDuration(d: -1d, to: TODAY)
DATA = from(bucket: "DatabaseBaptisteV01")
  |> range(start: YESTERDAY, stop: TODAY)
  |> filter(fn: (r) => r["_measurement"] == "°C")
  |> filter(fn: (r) => r["_field"] == "value")
  |> filter(fn: (r) => r["instance"] == "Test")
  |> filter(fn: (r) => r["entity_id"] == "Jardin")
  |> filter(fn: (r) => r["city"] == "Lougres")
DATA
  |> mean()
  |> map(fn: (r) => ({r with _time: YESTERDAY}))
  |> set(key: "statistics", value: "mean")
  |> set(key: "source", value: "influxdb")
  |> to(bucket: "DatabaseBaptisteV02")
DATA
  |> min()
  |> map(fn: (r) => ({r with _time: YESTERDAY}))
  |> set(key: "statistics", value: "min")
  |> set(key: "source", value: "influxdb")
  |> to(bucket: "DatabaseBaptisteV02")
DATA
  |> max()
  |> map(fn: (r) => ({r with _time: YESTERDAY}))
  |> set(key: "statistics", value: "max")
  |> set(key: "source", value: "influxdb")
  |> to(bucket: "DatabaseBaptisteV02")

Hi there,

I’m not quite sure what you mean by “apply my query to all tables”, since your query does apply to all tables as InfluxDB returns from your DATA query.

Are you trying to make that initial query more generic? If so, you can probably just remove some of the 'filter()` calls

Hello @mhall119 ,

Maybe my previous explanation was not clear, sorry for that.

Result of my query :

  • If I remove some filters from my query, I got the result below with 4 differents tables (in yellow).

What I’m looking for :

  • Get min / mean / max values for each table independently in order to store them in an other bucket.
  • 12 data will be stored => 4 (tables) x 3 (min/mean/max values)

Finally I solved my issue thanks to the @mhall119 comment

My final task :

import "date"
import "experimental"
import "math"

option task = {name: "Daily_stat-v01", cron: "0 1 * * *"}

TODAY = date.truncate(t: now(), unit: 1d)
YESTERDAY = experimental.addDuration(d: -1d, to: TODAY)
DATA = from(bucket: "DatabaseBaptisteV01")
	|> range(start: YESTERDAY, stop: TODAY)
	|> filter(fn: (r) =>
		(r["_measurement"] == "°C"))
	|> filter(fn: (r) =>
		(r["_field"] == "value"))

DATA
	|> mean()
	|> map(fn: (r) =>
		({r with _time: YESTERDAY}))
	|> set(key: "statistics", value: "mean")
	|> set(key: "source", value: "influxdb")
	|> map(fn: (r) =>
		({r with _value: math.round(x: r._value * 10.0) / 10.0}))
	|> to(bucket: "DatabaseBaptisteV02")
DATA
	|> min()
	|> map(fn: (r) =>
		({r with _time: YESTERDAY}))
	|> set(key: "statistics", value: "min")
	|> set(key: "source", value: "influxdb")
	|> to(bucket: "DatabaseBaptisteV02")
DATA
	|> max()
	|> map(fn: (r) =>
		({r with _time: YESTERDAY}))
	|> set(key: "statistics", value: "max")
	|> set(key: "source", value: "influxdb")
	|> to(bucket: "DatabaseBaptisteV02")