2

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
tgogos
  • 23,218
  • 20
  • 96
  • 128

2 Answers2

2

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.

Daniel Lee
  • 7,709
  • 2
  • 48
  • 57
  • Thank you so much. I wanted to edit the time filter. If the user selects now-15m to now, I wanted to show now-15m to now-5m, since last 5 min data is mostly not fully formed. This answer helps. – Vishnu Priyaa Jul 23 '21 at 09:46
  • How would I use it in the timeFilter query mentioned by OP? – trail99 Aug 01 '22 at 12:07
  • 1
    @trail99 an example of using the $__timeFrom() macro in the where clause: WHERE time_start > $__timeFrom() – Daniel Lee Aug 17 '22 at 14:00
  • 1
    Docs for MySQL data source with some macro examples: https://grafana.com/docs/grafana/latest/datasources/mysql/#macros – Daniel Lee Aug 17 '22 at 14:01
0

Generated Query, Doesn't fit inside the grafana time range. So you can simply change the time range.

Query From and To Fit inside the time range of the query.

enter image description here

I have added the time range of 100y (100 years) it fit's into my query. so you can adjust your time range accordingly,

enter image description here

Vicky
  • 819
  • 2
  • 13
  • 30