Browse Source

16365 change clashing variable names (#17140)

* Fix: change  and  to  and  so not clashing with grafana vars (#16365)

* Fix: change  and  to  and  so not clashing with grafana vars (#16365)

* Fix: update now to datetime (#16365)

* Fix: test should look for datetime instead of now (#16365)

* Fix: _az suffix to datasource convention (#16365)

* Fix: convert vars to macro, update doc (#16365)

* Fix: convert vars to macro, update doc (#16365)

* Fix: remove support for time vars (#16365)

* Fix: confilct from master

* add migration on editor open

* fix migration var name
Shavonn Brown 6 years ago
parent
commit
27874a1881

+ 4 - 2
docs/sources/features/datasources/azuremonitor.md

@@ -254,6 +254,10 @@ To make writing queries easier there are several Grafana macros that can be used
   `datetimeColumn ≥ datetime(2018-06-05T18:09:58.907Z) and`
   `datetimeColumn ≤ datetime(2018-06-05T20:09:58.907Z)` where the from and to datetimes are from the Grafana time picker.
 
+- `$__timeFrom()` - Returns the From datetime from the Grafana picker. Example: `datetime(2018-06-05T18:09:58.907Z)`.
+
+- `$__timeTo()` - Returns the From datetime from the Grafana picker. Example: `datetime(2018-06-05T20:09:58.907Z)`.
+
 - `$__escapeMulti($myVar)` - is to be used with multi-value template variables that contain illegal characters. If `$myVar` has the following two values as a string `'\\grafana-vm\Network(eth0)\Total','\\hello!'`, then it expands to: `@'\\grafana-vm\Network(eth0)\Total', @'\\hello!'`. If using single value variables there is no need for this macro, simply escape the variable inline instead - `@'\$myVar'`.
 
 - `$__contains(colName, $myVar)` - is to be used with multi-value template variables. If `$myVar` has the value `'value1','value2'`, it expands to: `colName in ('value1','value2')`.
@@ -264,8 +268,6 @@ To make writing queries easier there are several Grafana macros that can be used
 
 There are also some Grafana variables that can be used in Azure Log Analytics queries:
 
-- `$__from` - Returns the From datetime from the Grafana picker. Example: `datetime(2018-06-05T18:09:58.907Z)`.
-- `$__to` - Returns the From datetime from the Grafana picker. Example: `datetime(2018-06-05T20:09:58.907Z)`.
 - `$__interval` - Grafana calculates the minimum time grain that can be used to group by time in queries. More details on how it works [here]({{< relref "reference/templating.md#interval-variables" >}}). It returns a time grain like `5m` or `1h` that can be used in the bin function. E.g. `summarize count() by bin(TimeGenerated, $__interval)`
 
 ### Azure Log Analytics Alerting

+ 10 - 0
public/app/plugins/datasource/grafana-azure-monitor-datasource/editor/kusto/kusto.ts

@@ -649,6 +649,16 @@ export const grafanaMacros = [
     display: '$__timeFilter()',
     hint: 'Macro that uses the selected timerange in Grafana to filter the query.',
   },
+  {
+    text: '$__timeTo',
+    display: '$__timeTo()',
+    hint: 'Returns the From datetime from the Grafana picker. Example: datetime(2018-06-05T20:09:58.907Z).',
+  },
+  {
+    text: '$__timeFrom',
+    display: '$__timeFrom()',
+    hint: 'Returns the From datetime from the Grafana picker. Example: datetime(2018-06-05T18:09:58.907Z).',
+  },
   {
     text: '$__escapeMulti',
     display: '$__escapeMulti()',

+ 7 - 7
public/app/plugins/datasource/grafana-azure-monitor-datasource/log_analytics/querystring_builder.test.ts

@@ -90,27 +90,27 @@ describe('LogAnalyticsDatasource', () => {
     });
   });
 
-  describe('when using $__from and $__to is in the query and range is until now', () => {
+  describe('when using $__timeFrom and $__timeTo is in the query and range is until now', () => {
     beforeEach(() => {
-      builder.rawQueryString = 'query=Tablename | where myTime >= $__from and myTime <= $__to';
+      builder.rawQueryString = 'query=Tablename | where myTime >= $__timeFrom() and myTime <= $__timeTo()';
     });
 
-    it('should replace $__from and $__to with a datetime and the now() function', () => {
+    it('should replace $__timeFrom and $__timeTo with a datetime and the now() function', () => {
       const query = builder.generate().uriString;
 
       expect(query).toContain('where%20myTime%20%3E%3D%20datetime(');
-      expect(query).toContain('myTime%20%3C%3D%20now()');
+      expect(query).toContain('myTime%20%3C%3D%20datetime(');
     });
   });
 
-  describe('when using $__from and $__to is in the query and range is a specific interval', () => {
+  describe('when using $__timeFrom and $__timeTo is in the query and range is a specific interval', () => {
     beforeEach(() => {
-      builder.rawQueryString = 'query=Tablename | where myTime >= $__from and myTime <= $__to';
+      builder.rawQueryString = 'query=Tablename | where myTime >= $__timeFrom() and myTime <= $__timeTo()';
       builder.options.range.to = dateTime().subtract(1, 'hour');
       builder.options.rangeRaw.to = 'now-1h';
     });
 
-    it('should replace $__from and $__to with datetimes', () => {
+    it('should replace $__timeFrom and $__timeTo with datetimes', () => {
       const query = builder.generate().uriString;
 
       expect(query).toContain('where%20myTime%20%3E%3D%20datetime(');

+ 10 - 3
public/app/plugins/datasource/grafana-azure-monitor-datasource/log_analytics/querystring_builder.ts

@@ -21,12 +21,16 @@ export default class LogAnalyticsQuerystringBuilder {
         if (p1 === 'timeFilter') {
           return this.getTimeFilter(p2, this.options);
         }
+        if (p1 === 'timeFrom') {
+          return this.getFrom(this.options);
+        }
+        if (p1 === 'timeTo') {
+          return this.getUntil(this.options);
+        }
 
         return match;
       });
       queryString = queryString.replace(/\$__interval/gi, this.options.interval);
-      queryString = queryString.replace(/\$__from/gi, this.getFrom(this.options));
-      queryString = queryString.replace(/\$__to/gi, this.getUntil(this.options));
     }
     const rawQuery = queryString;
     queryString = encodeURIComponent(queryString);
@@ -44,7 +48,10 @@ export default class LogAnalyticsQuerystringBuilder {
 
   getUntil(options) {
     if (options.rangeRaw.to === 'now') {
-      return 'now()';
+      const now = Date.now();
+      return `datetime(${dateTime(now)
+        .startOf('minute')
+        .toISOString()})`;
     } else {
       const until = options.range.to;
       return `datetime(${dateTime(until)

+ 2 - 2
public/app/plugins/datasource/grafana-azure-monitor-datasource/partials/annotations.editor.html

@@ -67,8 +67,8 @@ Macros:
     - $__timeFilter(datetimeColumn) -&gt;  datetimeColumn  &ge; datetime(2018-06-05T18:09:58.907Z) and datetimeColumn &le; datetime(2018-06-05T20:09:58.907Z)
 
     Or build your own conditionals using these built-in variables which just return the values:
-    - $__from -&gt;  datetime(2018-06-05T18:09:58.907Z)
-    - $__to -&gt; datetime(2018-06-05T20:09:58.907Z)
+    - $__timeFrom -&gt;  datetime(2018-06-05T18:09:58.907Z)
+    - $__timeTo -&gt; datetime(2018-06-05T20:09:58.907Z)
     - $__interval -&gt; 5m
 </pre>
   </div>

+ 3 - 3
public/app/plugins/datasource/grafana-azure-monitor-datasource/partials/query.editor.html

@@ -189,13 +189,13 @@
       If using the All option, then check the Include All Option checkbox and in the Custom all value field type in: all. If All is chosen -&gt; 1 == 1
 
     Or build your own conditionals using these built-in variables which just return the values:
-    - $__from -&gt;  datetime(2018-06-05T18:09:58.907Z)
-    - $__to -&gt; datetime(2018-06-05T20:09:58.907Z)
+    - $__timeFrom -&gt;  datetime(2018-06-05T18:09:58.907Z)
+    - $__timeTo -&gt; datetime(2018-06-05T20:09:58.907Z)
     - $__interval -&gt; 5m
 
     Examples:
     - ¡ where $__timeFilter
-    - | where TimeGenerated &ge; $__from and TimeGenerated &le; $__to
+    - | where TimeGenerated &ge; $__timeFrom and TimeGenerated &le; $__timeTo
     - | summarize count() by Category, bin(TimeGenerated, $__interval)
       </pre>
     </div>

+ 7 - 0
public/app/plugins/datasource/grafana-azure-monitor-datasource/query_ctrl.ts

@@ -110,6 +110,8 @@ export class AzureMonitorQueryCtrl extends QueryCtrl {
 
     this.migrateTimeGrains();
 
+    this.migrateToFromTimes();
+
     this.panelCtrl.events.on('data-received', this.onDataReceived.bind(this), $scope);
     this.panelCtrl.events.on('data-error', this.onDataError.bind(this), $scope);
     this.resultFormats = [{ text: 'Time series', value: 'time_series' }, { text: 'Table', value: 'table' }];
@@ -171,6 +173,11 @@ export class AzureMonitorQueryCtrl extends QueryCtrl {
     }
   }
 
+  migrateToFromTimes() {
+    this.target.azureLogAnalytics.query = this.target.azureLogAnalytics.query.replace(/\$__from\s/gi, '$__timeFrom() ');
+    this.target.azureLogAnalytics.query = this.target.azureLogAnalytics.query.replace(/\$__to\s/gi, '$__timeTo() ');
+  }
+
   replace(variable: string) {
     return this.templateSrv.replace(variable, this.panelCtrl.panel.scopedVars);
   }