Transitioning from InfluxDB 1.x InfluxQL to 2.x Flux query language

Hi @quantenabler,

Yes, I’ve seen some confusion too:

  1. the v.timeRangeStop is indeed shown as v.timeRangeEnd in the manual… I guess they changed it. In my OSS v7.1 it luckily made some auto-suggestion which came up as …Stop.

image

  1. the $range variable is an interesting one. I had to use $range in Grafana 6.5, but once upgraded to 7.1 it went to v.timeRangeStart and v.timeRangeStop… I suggest you have a good look at the Query Inspector to see how your time variables are translated before sent to Influx.

In my case:
image
Results in:
image

I believe that is all you can do with Grafana’s global variables. You can define your own variables, supposedly with Flux - but I have not had any success with using Flux for variables in Grafana 7.1 (it was fine in 6.5). So in the end you’ll have to do your time offsets in the Flux query and let the backend deal with it.

For offsetting time, Flux has the following ‘experimental’ functions: https://v2.docs.influxdata.com/v2.0/reference/flux/stdlib/experimental/addduration/

Note:

  1. don’t forget to add import “experimental” at the start of your query.
  2. don’t use the ‘subDuration’ function - that doesnt seem to work. Instead use the ‘addDuration’ with a negative duration.

So for example:

import "experimental"
OffsetStart = experimental.addDuration(d: -48h,  to: now())
OffsetStop = experimental.addDuration(d: -24h,  to: now())

Now, you asked for specifically 11am - not sure why, and you didn’t specify if it is UTC or not. Let’s assume you meant UTC, you would then need to do something like this:

import "experimental"
import "date"
OffsetStart = experimental.addDuration(d: -37h,  to: date.truncate(t: now(), unit: 1d))
OffsetStop = experimental.addDuration(d: -13h,  to: date.truncate(t: now(), unit: 1d))

from(bucket: "yourbucket")
|> range(start: OffsetStart, stop: OffsetStop)

As you can see, I rounded now() to 1day precision - which gives a midnight timestamp, and I then added an offset of -48h + 11h for the start time.

Please note, I am using InfluxdB v2, not 1.8.

Good luck.