How to Dynamically Select Start and End Times for Flux Query in Grafana?

This Flux query is designed to dynamically select start and end times for querying time-series data stored in InfluxDB based on specific conditions for each sensor and motif. The start_time and end_time variables are calculated independently by filtering the data based on sensor name and timestamp values. These variables are then passed into the range() function to filter data within the specified time range.

start_time = () => {
    timeRecord = from(bucket: "motifs_timestamp")
        |> range(start: 0)
        |> filter(fn: (r) => r["sensor_name"] == "[3:25]")
        |> filter(fn: (r) => r["timestamp"] == "start")
        |> findRecord(fn: (key) => true, idx: 0)
    
    return timeRecord._time
}

end_time = () => {
    timeRecord = from(bucket: "motifs_timestamp")
        |> range(start: 0)
        |> filter(fn: (r) => r["sensor_name"] == "[3:25]")
        |> filter(fn: (r) => r["timestamp"] == "end")
        |> map(fn: (r) => ({_time: r._time}))
        |> findRecord(fn: (key) => true, idx: 0)

    return timeRecord._time
}

from(bucket: "motifs_timestamp")
    |> range(start: start_time(), stop: end_time())
    |> filter(fn: (r) => r["_measurement"] == "motifs_per_signal")
    |> filter(fn: (r) => r["_field"] == "sensor_reading")
    |> filter(fn: (r) => r["sensor_name"] == "[3:25]")
    |> filter(fn: (r) => r["motif_name"] == "Motifs_1")

While the query works as expected in InfluxDB, when integrated into Grafana, it encounters issues. Despite specifying dynamic time range selection in the Flux query, Grafana continues to utilize the default time range set in the dashboard, resulting in discrepancies between expected and actual time ranges used for data visualization.

Any insights or assistance would be greatly appreciated.

What is the value of start_time and end_time look like?

start_time and end_time are time variables from database in influxdb.Basically this are placeholder for the value of start time and end time for motif which I have stored in influxdb database. I have added start and end tags to find the value for start and end time and I am simply fetching and storing value in this flux variables.

1 Like

But does their value look like in case it is something grafana cannot use such as a timeseries instead just a datetime value

No, I double checked it but the values are correct. I also plotted in influxdb to confirm this. The plot was visible in influxdb so I believe the values are correct. Still if you believe this could be the issue then please forward any resources regarding this. I will again check it.

1 Like

Ok, now use a static value that matches the values of those variables by hand and see what happens

I tried taking static variables like this:


start_time = () => "2024-03-05T05:18:50Z"

end_time = () => "2024-03-05T05:19:30Z"

from(bucket: "motifs_timestamp")

>> range(start: start_time(), stop: end_time())

>> filter(fn: (r) => r["_measurement"] == "motifs_per_signal")

>> filter(fn: (r) => r["_field"] == "sensor_reading")

>> filter(fn: (r) => r["sensor_name"] == "[3:25]")

>> filter(fn: (r) => r["motif_name"] == "Motifs_1")

I’m encountering an error even after attempting to adjust the datetime format. Despite trying various formats, the issue persists. Please help with this!

we cant see the error remotely :wink:

Oh, sorry, I forgot to add the error. Here it is:

A
Status: 500. Message: invalid: compilation failed: error @7:2-7:3: invalid expression: invalid token for primary expression: GT error @9:2-9:3: invalid expression: invalid token for primary expression: GT error @11:2-11:3: invalid expression: invalid token for primary expression: GT error @13:2-13:3: invalid expression: invalid token for primary expression: GT error @15:2-15:3: invalid expression: invalid token for primary expression: GT

aren’t these >> supposed to be |> ? or is that due to when you posted the code

1 Like

Yes it was because I posted the code that those symbols got added. I modified the query a little bit and now its plotting but again the variables are only working static and also the panel range is still the default time range of dashboard instead of plotting only for this range. Is there any way to fetch this variables dynamically as I originally posted in my question. Also how can I convert this variables to this time format in flux. I had converted using python.

start_time = () => 2024-03-05T05:18:50Z

end_time = () => 2024-03-05T05:19:30Z

from(bucket: "motifs_timestamp")
    |> range(start: start_time(), stop: end_time())
    |> filter(fn: (r) => r["_measurement"] == "motifs_per_signal")
    |> filter(fn: (r) => r["_field"] == "sensor_reading")
    |> filter(fn: (r) => r["sensor_name"] == "[3:25]")
    |> filter(fn: (r) => r["motif_name"] == "Motifs_1")