Bladeren bron

mysql: pass timerange for template variable queries (#10071)

* mysql: pass timerange for template variable queries

* mysql: document time range macro usage in template variables

* mysql: docs for on time range change refresh mode for template queries

* Revert "mysql: docs for on time range change refresh mode for template queries"

This reverts commit 5325972aa4f66257aa20f7ae6d4de8fa8fe628b6.
Sven Klemm 8 jaren geleden
bovenliggende
commit
a62ebb3e59
2 gewijzigde bestanden met toevoegingen van 18 en 3 verwijderingen
  1. 6 0
      docs/sources/features/datasources/mysql.md
  2. 12 3
      public/app/plugins/datasource/mysql/datasource.ts

+ 6 - 0
docs/sources/features/datasources/mysql.md

@@ -127,6 +127,12 @@ A query can returns multiple columns and Grafana will automatically create a lis
 SELECT my_host.hostname, my_other_host.hostname2 FROM my_host JOIN my_other_host ON my_host.city = my_other_host.city
 ```
 
+To use time range dependent macros like `$__timeFilter(column)` in your query the refresh mode of the template variable needs to be set to *On Time Range Change*.
+
+```sql
+SELECT event_name FROM event_log WHERE $__timeFilter(time_column)
+```
+
 Another option is a query that can create a key/value variable. The query should return two columns that are named `__text` and `__value`. The `__text` column value should be unique (if it is not unique then the first value is used). The options in the dropdown will have a text and value that allows you to have a friendly name as text and an id as the value. An example query with `hostname` as the text and `id` as the value:
 
 ```sql

+ 12 - 3
public/app/plugins/datasource/mysql/datasource.ts

@@ -103,12 +103,21 @@ export class MysqlDatasource {
       format: 'table',
     };
 
+    var data = {
+        queries: [interpolatedQuery],
+    };
+
+    if (optionalOptions && optionalOptions.range && optionalOptions.range.from) {
+      data['from'] = optionalOptions.range.from.valueOf().toString();
+    }
+    if (optionalOptions && optionalOptions.range && optionalOptions.range.to) {
+      data['to'] = optionalOptions.range.to.valueOf().toString();
+    }
+
     return this.backendSrv.datasourceRequest({
       url: '/api/tsdb/query',
       method: 'POST',
-      data: {
-        queries: [interpolatedQuery],
-      }
+      data: data
     })
     .then(data => this.responseParser.parseMetricFindQueryResult(refId, data));
   }