How to dynamic get system time range variable?


#1

Is it possible to get system time range variable?
I want to retrieve the variable and then place in my dashboard.

Please give me some suggestions.
Thank you.


#2

There is no time range variable. How do you want to use it?


#5

I want to retrieve it and then push into my dashboard.

Example:
Last 2 days
Last 7 days

Is it possible to get the information ?
Thanks for your great support.


#6

no there is no way to do that.


#7

Do you have any suggestions about it?
Or maybe I can retrieve the raw data and then push into it.

Thank you!


#8

Approach like this

function getDashboardTimeRange(){
    var tr = angular.element('grafana-app').injector().get('timeSrv').timeRange();
    return ['Current dashboard time range: from <', tr.from._d, '> to <', tr.to._d, '>'].join('');
}
// add this to some element on dashboard.
$('#some_id).append(getDashboardTimeRange());

allows to get dashboard time range. One can use it in in Text panel to append/modify any content of dashboard.
The question here is how to insure that this custom javascript is always run on dashboard load, even when Text panel is collapsed…


#9

Hi Yuyu,

I am a beginner for Grafana,Could you please tell me how should I use this code and place where to retrieve the time range variable data?
Thanks for your great support in advance.


#10

It depends on what you want to achieve.

If the goal is to add time range info somewhere in dashboard:

  1. Add “Text” panel and use “html” mode in panel options editor
  2. Put your html markup like this in panel Content
<div id="time_range_info">
Custom dashboard time range info returned by function will be added here.<br>
</div>
<script>
function getDashboardTimeRange(){
   var tr = angular.element('grafana-app').injector().get('timeSrv').timeRange();
   return ['Current dashboard time range: from &lt;', tr.from._d, '&gt; to &lt;', tr.to._d, '&gt;'].join('');
}
$('#time_range_info').append(getDashboardTimeRange());
</script>

Some basic html, javascript and DOM manipulationknowledge is needed to make this approach really useful.
Note, that it won’t give you time range as variable to be used like template variables in panels.


#11

Hi Yuyu,

My goal is to get time range variable to put into the dashboard.

For example:

If I choose Last 2 days then dashboard will show Last 2 days information in dashboard.
If I choose Last 7 days then dashboard will show Last 7 days information in dashboard.

If it possible to do it in Grafana or I just need to do it in Kibana?

Thanks for your great support.
Thanks a lot.