If cause for range duration change

dear, please

i have debug dash board, where i use variables to see my

  • actual buckets
  • all measurements for each bucket
  • sample data for each measurement

one of my measurement is conversion table between machine_id to human_readable. as this i only import once it can be very old. so i am looking for function where i detect such a measurement and then want to shift v.timeRangeStart to -1y

time_range = (measurement) =>
measurement
//|> filter(fn: (r) => if measurement == “ttn_2_machines” then string(v: duration(v: -1y))
//else string(v: duration(v: v.timeRangeStart)))
//v.timeRangeStart
|> filter(fn: (r) => if measurement == “ttn_2_machines” then -1y
else v.timeRangeStart)
v.timeRangeStart

tr = time_range(measurement: “_measurement”)

from(bucket: v.bucket)
|> range(start: tr)
|> filter(fn: (r) => r["_measurement"] == v.measurement)
|> map(fn: (r) => ({ r with value: r._value}))
|> drop(columns:["_start", “_stop”, “host”]) // “_measurement”])
|> sort(columns: ["_time"], desc:true)
|> limit(n:10)

thank you for pointing to right place, pavel

Hello @srbp,
I’m having some trouble understanding what it is that you’re trying to accomplish, but I’ll try to point you in the right direction.

  1. You can apply multiple time range functions to your data.
    For example
from(bucket: "system")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "cpu")
  |> filter(fn: (r) => r["_field"] == "usage_system" and exists r["_value"])
  |> range(start: v.timeRangeStart)

You can use exists to check whether something exists first.
https://docs.influxdata.com/influxdb/cloud/query-data/flux/exists/

You can also filter for data from a point in time:

cutoff = <some timestamp>
   |> filter(fn: (r) => r._time >= cutoff)

You can also return a timestamp from a previous query with getRecord and use that in a subsequent query getRecord() function | InfluxDB Cloud Documentation

Does anything here help? If not, can you describe in a little more detail what you’re trying to do?

Finally, below is a section from Top 5 Hurdles for Intermediate Flux Users | InfluxData. It’s a list of flux functions you can use to manipulate time:

Functions to manipulate timestamps: Time series data is difficult to work with. Flux includes several functions to work with time. However, a couple of these functions are spread out between various community package contributions and Flux packages within the documentation. A consolidated list of all of these functions provide you with a deeper understanding of how you can use Flux to work with time. The following functions allow you to manipulate your timestamps and execute a variety of time series transformations. Including:

hallo @Anaisdg , thank you for your answer. i will try to describe again and better:

what i really like with FLUX is, that we can achieve same goal via multiple ways:

  • the goal here is to use condition/function to detect static table / measurement: “ttn_2_machines” and if True then overcharge the v.timeRangeStart with -3y

  • the reason for this is to limit range for sensor data to work with let’s say only -24h or -4d and not -3y

  • i have succeed with two tables and union

//static table which can be 2year old
ttn_table =
from(bucket: v.bucket)
|> range(start: -1y)
|> filter(fn: (r) => r["_measurement"] == v.measurement)
|> filter(fn: (r) => v.measurement == “ttn_2_machines”)
|> map(fn: (r) => ({ r with value: r._value}))
|> drop(columns:["_start", “_stop”, “host”, “_field”, “_measurement”])
|> sort(columns: ["_time"], desc:true)
|> limit(n:10)
|> group()

//dynamic table incremented every 1m with lot’s of sensor data
all_data_tables =
from(bucket: v.bucket)
|> range(start: -1y)
|> filter(fn: (r) => r["_measurement"] == v.measurement)
|> filter(fn: (r) => v.measurement != “ttn_2_machines”)
|> range(start: v.timeRangeStart)
|> map(fn: (r) => ({ r with value: r._value}))
|> drop(columns:["_start", “_stop”, “host”]) // “_measurement”])
|> sort(columns: ["_time"], desc:true)
|> limit(n:10)

union(tables: [ttn_table, all_data_tables])

  • but i believe there will be more efficient way and also interested if i can use IF to shift duration for RANGE like described in initial question

thank you, pavel

Hello @srbp,
I agree that’s what I like about Flux too. I think the solution that I gave you is more efficient, but maybe @scott has a better idea.

You could create a custom function that returns a different dataset based on the selected measurement:

getData = (bucket, measurement) => {
  ttn_table =
    from(bucket: bucket)
      |> range(start: -2y)
      |> filter(fn: (r) => r["_measurement"] == measurement)
      |> filter(fn: (r) => measurement == "ttn_2_machines")
      |> drop(columns: ["_start", "_stop", "host", "_field", "_measurement"])
      |> map(fn: (r) => ({ r with value: r._value}))
      |> sort(columns: ["_time"], desc:true)
      |> limit(n:10)
      |> group()

  dynamic_tables =
    from(bucket: bucket)
      |> range(start: timeRangeStart)
      |> filter(fn: (r) => r["_measurement"] == measurement)
      |> filter(fn: (r) => measurement != "ttn_2_machines")
      |> map(fn: (r) => ({ r with value: r._value}))
      |> drop(columns:["_start", "_stop", "host"]) // "_measurement"])
      |> sort(columns: ["_time"], desc:true)
      |> limit(n:10)
  
  returnData = if measurement == "ttn_2_machines" then ttn_table else dynamic_tables

  return returnData
}

getData(bucket: v.bucket, measurement: v.measurement)
1 Like

this is great, very nice solution. thank you @scott and @anaisdg

pavel