Grafana: global variables to access [from,to] values of the time range

tgogos picture tgogos · Sep 12, 2017 · Viewed 8k times · Source

I am doing some tests with a MySQL data-source and make use of the timefilter inside the SQL query:

// my query goes here...
WHERE $__timeFilter(time_start);

which generates something like the following:

WHERE time_start >= FROM_UNIXTIME(1505207821) AND time_start <= FROM_UNIXTIME(1505229421);

Are there any variables that can give me access to the 2 timestamps that define the time range?

For instance, at the example above:

from = 1505207821
to = 1505229421

Answer

Daniel Lee picture Daniel Lee · Sep 22, 2017

Yes, $__timeFrom() and $__timeTo() if you are using the latest 4.5.1 version of Grafana (earlier versions of Grafana only include an alpha version of the MySQL data source). There is a "Show Help" button under the query field where all the macros are listed. See below:

enter image description here

The first two macros return from and to in the datetime format, the last two return them as epochs.