Set $timeInterval in query

Hiya

I have a query that uses a variable, FWIW like this:
SELECT value from dxdata where analyte='Insulin' and unit='$insulin_units' and time > $kraft_curves-20m and time < $kraft_curves+4h

This returns the correct dataset. However it does nothing to the “view window” or the time range in the panel. This is of course set by the time selector control.

How do I make the Grafana Panel extend to the time range as specified in the QUERY :point_up:

Thanks a lot for your help,
Eric

Hi @allomorphy - it would be best to flip the approach, and have your query use the time bounds specified by the dashboard’s time picker - see Global variables | Grafana Labs. Would that work for you?

1 Like

Thank you for your suggestion @svetb

I can understand why you suggest that. The problem here is that we want to relieve the poor time picker (aka the user :wink:) from this chore. You can see why it is so inconvenint to zoom in manually to the several hour long curves that exist randomly over a multi-year period like here:
https://humanalyse.com/d/kraft_panels/kraft-panels-in-development-202109?orgId=3&from=now-160d&to=now&var-glucose_units=mMol&var-insulin_units=µIU%2Fml&var-kraft_curves=1624958914211721500

to find this single curve for example:
https://humanalyse.com/d/meterbolic_standard_dashboard/meterbolic-org-standard-dashboard?from=1628664027956&to=1628678545779&orgId=3&viewPanel=161&var-glucose_units=mMol&var-insulin_units=µIU%2Fml

What I tried was to populate a variable with all the OGTT values – which act as identifiers of a single curve – and then for the user to select one of these to display a single curve. The QUERY selects the data fine but the $timeFilter or the range or whatever word one uses for the zooming in or out of the panel view itself, has not proved possible for me to change. So it remains a tiny collection of squashed up data points in large empty display.

Would love to find a breakthrough or for someone to say, Forget it. :blush:

Thanks again for any ideas and best wishes
Eric

Please Join the Health revolution https://blogs.meterbolic.org

This is as far as I can get :sweat:
https://humanalyse.com/d/kraft_panels/kraft-panels-in-development-202109?orgId=3&from=1619149168104&to=1631459703996&var-glucose_units=mMol&var-insulin_units=µIU%2Fml&var-kraft_curves=1627896070071975400&var-kraft_curves=1627629982331151600&var-kraft_curves=1626943854038761000&var-kraft_curves=1626074609676300800&var-kraft_curves=1624958914211721500&var-kraft_curves=1624605090533526300&var-kraft_curves=1624605090080756500&var-kraft_curves=1624345613686851300&var-kraft_curves=1623395632907429400&var-kraft_curves=1622790843748500000&var-kraft_curves=1622227520209311500&var-kraft_curves=1622227471837176300&var-kraft_curves=1621846942765649200&var-kraft_curves=1621666122408904200

Right, I see the issue. So, it is possible to do what you want, but it’ll be kind of hack-y.

Basically, you can create a text panel with some custom JS, which would pick up changes in template variable values, and set the time range appropriately. The latter can be achieved similar to what’s described here: JS within panel to set dashboard refresh interval - #5 by svetb. That thread is about the refresh interval, but the time range is accessible in a similar way (check out the other methods bound to timeSrv).

IIRC, the JS should get reloaded/triggered upon any changes in template variable values - but if that’s not the case, you might have to explicitly bind to that event.

Not sure what your appetite is for implementing something like that, but I feel that it’s probably the best option you have.

1 Like

Thank you @svetb
Great that there is a way out of this and you are very kind to give a solution.
Thank you.
Eric

Would love some helpful eyes on implementing the above suggestion :hugs:

On the Result url below, you see a months long time window and the ability to select on the Kraft Curves variable different events. What is required is that when a user selects a Kraft Curve on the dropdown, then the user is show the data properly zoomed in on the timeline.
The data QUERY works as may be seen in the link below.

Result url:
https://humanalyse.com/d/kraft_panels/kraft-panels-in-development-202109?orgId=3&var-glucose_units=mMol&var-insulin_units=%C2%B5IU%2Fml&var-kraft_curves=1627896070071975400&from=now-6M&to=now

The code in my panel currently combines a text output of a calculated date range set by a template variable $kraft_curves and attempts to set the zoom to this same time range using the example supplied here:
https://stackoverflow.com/questions/48264279/how-to-set-time-range-in-grafana-dashboard-from-text-panels

This was a wild attempt to have the Panel resize in the same way as using the interactive timePicker would resize the view of the Panel to the data.

I am not sure what is the format of time which need to be defined in timeService.setTime(newTime) or even if this is the correct approach at all.
Currently I am using nanoseconds like the url uses, and this is almost surely wrong and am not sure what the format of time should be.

Thank you for helping to solve this problem .

Best wishes
Eric

The above has been solved by using date time in the format 2021-09-01 08:02:46
After some adjustments for scoping, this worked.
Thanks again @svetb for getting this going :pray:

Best wishes
Eric

1 Like

Hi Folks,

The use case for selecting a single variable value works great.

However, the second use case is for a page of panels repeating for each value of the variable.
It seems that the call that worked in the first case is not useful here:
angular.element('grafana-app').injector().get('timeSrv').timeSrv.setTime

This is because the above sets the time range globally for the entire dashboard and not per panel

Does anyone know what the correct call would be to set the per panel tiem range?
Would be amazing if you could share.

At the bottom fo this thread, they discuss the Query Options per panel.
I have managed to make these do what I want per panel, but does anyone know how to set this through a call in js, like we call the global setTime for example.

Look forward to your ideas …
… Eric