diff --git a/docs/sources/features/datasources/azuremonitor.md b/docs/sources/features/datasources/azuremonitor.md index ee40248fe00..114187499f1 100644 --- a/docs/sources/features/datasources/azuremonitor.md +++ b/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 diff --git a/public/app/plugins/datasource/grafana-azure-monitor-datasource/editor/kusto/kusto.ts b/public/app/plugins/datasource/grafana-azure-monitor-datasource/editor/kusto/kusto.ts index 5bf7dfb19eb..172aa5ee077 100644 --- a/public/app/plugins/datasource/grafana-azure-monitor-datasource/editor/kusto/kusto.ts +++ b/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()', diff --git a/public/app/plugins/datasource/grafana-azure-monitor-datasource/log_analytics/querystring_builder.test.ts b/public/app/plugins/datasource/grafana-azure-monitor-datasource/log_analytics/querystring_builder.test.ts index fab268a3440..186c78743f8 100644 --- a/public/app/plugins/datasource/grafana-azure-monitor-datasource/log_analytics/querystring_builder.test.ts +++ b/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('); diff --git a/public/app/plugins/datasource/grafana-azure-monitor-datasource/log_analytics/querystring_builder.ts b/public/app/plugins/datasource/grafana-azure-monitor-datasource/log_analytics/querystring_builder.ts index afb64da8f4c..ad72c4eb2eb 100644 --- a/public/app/plugins/datasource/grafana-azure-monitor-datasource/log_analytics/querystring_builder.ts +++ b/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) diff --git a/public/app/plugins/datasource/grafana-azure-monitor-datasource/partials/annotations.editor.html b/public/app/plugins/datasource/grafana-azure-monitor-datasource/partials/annotations.editor.html index a5b2b2adc5b..7a855a10b44 100644 --- a/public/app/plugins/datasource/grafana-azure-monitor-datasource/partials/annotations.editor.html +++ b/public/app/plugins/datasource/grafana-azure-monitor-datasource/partials/annotations.editor.html @@ -67,8 +67,8 @@ Macros: - $__timeFilter(datetimeColumn) -> datetimeColumn ≥ datetime(2018-06-05T18:09:58.907Z) and datetimeColumn ≤ datetime(2018-06-05T20:09:58.907Z) Or build your own conditionals using these built-in variables which just return the values: - - $__from -> datetime(2018-06-05T18:09:58.907Z) - - $__to -> datetime(2018-06-05T20:09:58.907Z) + - $__timeFrom -> datetime(2018-06-05T18:09:58.907Z) + - $__timeTo -> datetime(2018-06-05T20:09:58.907Z) - $__interval -> 5m diff --git a/public/app/plugins/datasource/grafana-azure-monitor-datasource/partials/query.editor.html b/public/app/plugins/datasource/grafana-azure-monitor-datasource/partials/query.editor.html index 4690bc5be26..1c2b14f366e 100644 --- a/public/app/plugins/datasource/grafana-azure-monitor-datasource/partials/query.editor.html +++ b/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 -> 1 == 1 Or build your own conditionals using these built-in variables which just return the values: - - $__from -> datetime(2018-06-05T18:09:58.907Z) - - $__to -> datetime(2018-06-05T20:09:58.907Z) + - $__timeFrom -> datetime(2018-06-05T18:09:58.907Z) + - $__timeTo -> datetime(2018-06-05T20:09:58.907Z) - $__interval -> 5m Examples: - ¡ where $__timeFilter - - | where TimeGenerated ≥ $__from and TimeGenerated ≤ $__to + - | where TimeGenerated ≥ $__timeFrom and TimeGenerated ≤ $__timeTo - | summarize count() by Category, bin(TimeGenerated, $__interval) diff --git a/public/app/plugins/datasource/grafana-azure-monitor-datasource/query_ctrl.ts b/public/app/plugins/datasource/grafana-azure-monitor-datasource/query_ctrl.ts index cc623d8df98..9fc12e9b916 100644 --- a/public/app/plugins/datasource/grafana-azure-monitor-datasource/query_ctrl.ts +++ b/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); }