mirror of
https://github.com/grafana/grafana.git
synced 2025-01-26 16:27:02 -06:00
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
This commit is contained in:
parent
382ebd6362
commit
27874a1881
@ -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
|
||||
|
@ -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()',
|
||||
|
@ -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(');
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
</pre>
|
||||
</div>
|
||||
|
@ -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)
|
||||
</pre>
|
||||
</div>
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user