mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
AzureMonitor: Fix macro resolution for template variables (#36944)
This commit is contained in:
committed by
GitHub
parent
3b728ed78a
commit
133e292052
@@ -235,6 +235,30 @@ describe('AzureLogAnalyticsDatasource', () => {
|
||||
expect(results[1].value).toBe('Policy');
|
||||
});
|
||||
});
|
||||
|
||||
describe('and contain options', () => {
|
||||
const queryResponse = {
|
||||
tables: [],
|
||||
};
|
||||
|
||||
it('should substitute macros', async () => {
|
||||
ctx.ds.azureLogAnalyticsDatasource.getResource = jest.fn().mockImplementation((path: string) => {
|
||||
const params = new URLSearchParams(path.split('?')[1]);
|
||||
const query = params.get('query');
|
||||
expect(query).toEqual(
|
||||
'Perf| where TimeGenerated >= datetime(2021-01-01T05:01:00.000Z) and TimeGenerated <= datetime(2021-01-01T05:02:00.000Z)'
|
||||
);
|
||||
return Promise.resolve(queryResponse);
|
||||
});
|
||||
ctx.ds.azureLogAnalyticsDatasource.defaultOrFirstWorkspace = 'foo';
|
||||
await ctx.ds.metricFindQuery('Perf| where TimeGenerated >= $__timeFrom() and TimeGenerated <= $__timeTo()', {
|
||||
range: {
|
||||
from: new Date('2021-01-01 00:01:00'),
|
||||
to: new Date('2021-01-01 00:02:00'),
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('When performing annotationQuery', () => {
|
||||
|
||||
@@ -220,7 +220,7 @@ export default class AzureLogAnalyticsDatasource extends DataSourceWithBackend<
|
||||
* And some of the azure internal data sources return null in this function, which the
|
||||
* external interface does not support
|
||||
*/
|
||||
metricFindQueryInternal(query: string): Promise<MetricFindValue[]> {
|
||||
metricFindQueryInternal(query: string, optionalOptions?: unknown): Promise<MetricFindValue[]> {
|
||||
// workspaces() - Get workspaces in the default subscription
|
||||
const workspacesQuery = query.match(/^workspaces\(\)/i);
|
||||
if (workspacesQuery) {
|
||||
@@ -245,7 +245,7 @@ export default class AzureLogAnalyticsDatasource extends DataSourceWithBackend<
|
||||
return [];
|
||||
}
|
||||
|
||||
const queries = this.buildQuery(query, null, resourceURI);
|
||||
const queries = this.buildQuery(query, optionalOptions, resourceURI);
|
||||
const promises = this.doQueries(queries);
|
||||
|
||||
return Promise.all(promises)
|
||||
|
||||
@@ -136,7 +136,7 @@ export default class Datasource extends DataSourceApi<AzureMonitorQuery, AzureDa
|
||||
return this.azureLogAnalyticsDatasource.annotationQuery(options);
|
||||
}
|
||||
|
||||
async metricFindQuery(query: string) {
|
||||
async metricFindQuery(query: string, optionalOptions?: unknown) {
|
||||
if (!query) {
|
||||
return Promise.resolve([]);
|
||||
}
|
||||
@@ -151,7 +151,7 @@ export default class Datasource extends DataSourceApi<AzureMonitorQuery, AzureDa
|
||||
return amResult;
|
||||
}
|
||||
|
||||
const alaResult = this.azureLogAnalyticsDatasource.metricFindQueryInternal(query);
|
||||
const alaResult = this.azureLogAnalyticsDatasource.metricFindQueryInternal(query, optionalOptions);
|
||||
if (alaResult) {
|
||||
return alaResult;
|
||||
}
|
||||
|
||||
@@ -163,4 +163,23 @@ describe('LogAnalyticsDatasource', () => {
|
||||
expect(query).toContain(`%24__escapeMulti(%5Cgrafana-vm%2CNetwork(eth0)Total%20Bytes%20Received)`);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when there is no raw range', () => {
|
||||
it('should still generate a time filter condition', () => {
|
||||
builder = new LogAnalyticsQuerystringBuilder(
|
||||
'query=Tablename | where $__timeFilter()',
|
||||
{
|
||||
interval: '5m',
|
||||
range: {
|
||||
from: dateTime().subtract(24, 'hours'),
|
||||
to: dateTime(),
|
||||
},
|
||||
},
|
||||
'TimeGenerated'
|
||||
);
|
||||
const query = builder.generate().uriString;
|
||||
|
||||
expect(query).toContain('where%20TimeGenerated%20%20%3E%3D%20datetime(');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -45,7 +45,7 @@ export default class LogAnalyticsQuerystringBuilder {
|
||||
}
|
||||
|
||||
getUntil(options: any) {
|
||||
if (options.rangeRaw.to === 'now') {
|
||||
if (options.rangeRaw?.to === 'now') {
|
||||
const now = Date.now();
|
||||
return `datetime(${dateTime(now).startOf('minute').toISOString()})`;
|
||||
} else {
|
||||
@@ -56,7 +56,7 @@ export default class LogAnalyticsQuerystringBuilder {
|
||||
|
||||
getTimeFilter(timeFieldArg: any, options: any) {
|
||||
const timeField = timeFieldArg || this.defaultTimeField;
|
||||
if (options.rangeRaw.to === 'now') {
|
||||
if (options.rangeRaw?.to === 'now') {
|
||||
return `${timeField} >= ${this.getFrom(options)}`;
|
||||
} else {
|
||||
return `${timeField} >= ${this.getFrom(options)} and ${timeField} <= ${this.getUntil(options)}`;
|
||||
|
||||
Reference in New Issue
Block a user