mirror of
https://github.com/grafana/grafana.git
synced 2025-02-11 08:05:43 -06:00
AzureMonitor: Include macros and template variables as suggestions (#47689)
This commit is contained in:
parent
38809d73c2
commit
a57716f868
@ -28,6 +28,7 @@ describe('AzureLogAnalyticsDatasource', () => {
|
|||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
templateSrv.init([singleVariable]);
|
templateSrv.init([singleVariable]);
|
||||||
|
templateSrv.getVariables = jest.fn().mockReturnValue([singleVariable]);
|
||||||
ctx.instanceSettings = {
|
ctx.instanceSettings = {
|
||||||
jsonData: { subscriptionId: 'xxx' },
|
jsonData: { subscriptionId: 'xxx' },
|
||||||
url: 'http://azureloganalyticsapi',
|
url: 'http://azureloganalyticsapi',
|
||||||
@ -120,6 +121,27 @@ describe('AzureLogAnalyticsDatasource', () => {
|
|||||||
await ctx.ds.azureLogAnalyticsDatasource.getKustoSchema('myWorkspace/$var1');
|
await ctx.ds.azureLogAnalyticsDatasource.getKustoSchema('myWorkspace/$var1');
|
||||||
expect(ctx.mockGetResource).lastCalledWith('loganalytics/v1myWorkspace/var1-foo/metadata');
|
expect(ctx.mockGetResource).lastCalledWith('loganalytics/v1myWorkspace/var1-foo/metadata');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should include macros as suggested functions', async () => {
|
||||||
|
const result = await ctx.ds.azureLogAnalyticsDatasource.getKustoSchema('myWorkspace');
|
||||||
|
expect(result.database.functions.map((f: { name: string }) => f.name)).toEqual([
|
||||||
|
'Func1',
|
||||||
|
'_AzureBackup_GetVaults',
|
||||||
|
'$__timeFilter',
|
||||||
|
'$__timeFrom',
|
||||||
|
'$__timeTo',
|
||||||
|
'$__escapeMulti',
|
||||||
|
'$__contains',
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should include template variables as global parameters', async () => {
|
||||||
|
const result = await ctx.ds.azureLogAnalyticsDatasource.getKustoSchema('myWorkspace');
|
||||||
|
expect(result.globalParameters.map((f: { name: string }) => f.name)).toEqual([
|
||||||
|
`$${singleVariable.name}`,
|
||||||
|
'$__timeFilter',
|
||||||
|
]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('When performing annotationQuery', () => {
|
describe('When performing annotationQuery', () => {
|
||||||
|
@ -112,7 +112,7 @@ export default class AzureLogAnalyticsDatasource extends DataSourceWithBackend<
|
|||||||
const templateSrv = getTemplateSrv();
|
const templateSrv = getTemplateSrv();
|
||||||
const interpolatedUri = templateSrv.replace(resourceUri, {}, interpolateVariable);
|
const interpolatedUri = templateSrv.replace(resourceUri, {}, interpolateVariable);
|
||||||
const metadata = await this.getMetadata(interpolatedUri);
|
const metadata = await this.getMetadata(interpolatedUri);
|
||||||
return transformMetadataToKustoSchema(metadata, interpolatedUri);
|
return transformMetadataToKustoSchema(metadata, interpolatedUri, templateSrv.getVariables());
|
||||||
}
|
}
|
||||||
|
|
||||||
applyTemplateVariables(target: AzureMonitorQuery, scopedVars: ScopedVars): AzureMonitorQuery {
|
applyTemplateVariables(target: AzureMonitorQuery, scopedVars: ScopedVars): AzureMonitorQuery {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { AnnotationEvent, dateTime, TimeSeries } from '@grafana/data';
|
import { AnnotationEvent, dateTime, TimeSeries, VariableModel } from '@grafana/data';
|
||||||
import { concat, find, flattenDeep, forEach, get, map } from 'lodash';
|
import { concat, find, flattenDeep, forEach, get, map } from 'lodash';
|
||||||
|
|
||||||
import { AzureLogsTableData, AzureLogsVariable } from '../types';
|
import { AzureLogsTableData, AzureLogsVariable } from '../types';
|
||||||
@ -213,7 +213,11 @@ function transformMetadataFunction(sourceSchema: AzureLogAnalyticsMetadata) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function transformMetadataToKustoSchema(sourceSchema: AzureLogAnalyticsMetadata, nameOrIdOrSomething: string) {
|
export function transformMetadataToKustoSchema(
|
||||||
|
sourceSchema: AzureLogAnalyticsMetadata,
|
||||||
|
nameOrIdOrSomething: string,
|
||||||
|
templateVariables: VariableModel[]
|
||||||
|
) {
|
||||||
const database = {
|
const database = {
|
||||||
name: nameOrIdOrSomething,
|
name: nameOrIdOrSomething,
|
||||||
tables: sourceSchema.tables,
|
tables: sourceSchema.tables,
|
||||||
@ -222,25 +226,78 @@ export function transformMetadataToKustoSchema(sourceSchema: AzureLogAnalyticsMe
|
|||||||
minorVersion: 0,
|
minorVersion: 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: We should define macros here as functions so they are interpreted as valid
|
// Adding macros as known functions
|
||||||
// But we cannot do so for the issues listed here: https://github.com/Azure/monaco-kusto/issues/189
|
database.functions.push(
|
||||||
// For example:
|
{
|
||||||
// database.functions.push(
|
name: '$__timeFilter',
|
||||||
// {
|
body: '{ true }',
|
||||||
// name: '$__timeFilter',
|
inputParameters: [
|
||||||
// body: '',
|
{
|
||||||
// inputParameters: [
|
name: 'timeColumn',
|
||||||
// {
|
type: 'System.String',
|
||||||
// name: 'timeColumn',
|
defaultValue: 'TimeGenerated',
|
||||||
// type: 'System.String',
|
cslDefaultValue: 'TimeGenerated',
|
||||||
// cslType: 'string',
|
},
|
||||||
// cslDefaultValue: 'Timestamp',
|
],
|
||||||
// },
|
},
|
||||||
// ],
|
{
|
||||||
// docString: 'Filter by a time column',
|
name: '$__timeFrom',
|
||||||
// functionKind: 'Unknown',
|
body: '{ datetime(2018-06-05T18:09:58.907Z) }',
|
||||||
// },
|
inputParameters: [],
|
||||||
// );
|
},
|
||||||
|
{
|
||||||
|
name: '$__timeTo',
|
||||||
|
body: '{ datetime(2018-06-05T20:09:58.907Z) }',
|
||||||
|
inputParameters: [],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: '$__escapeMulti',
|
||||||
|
body: `{ @'\\grafana-vm\Network(eth0)\Total', @'\\hello!'}`,
|
||||||
|
inputParameters: [
|
||||||
|
{
|
||||||
|
name: '$myVar',
|
||||||
|
type: 'System.String',
|
||||||
|
defaultValue: '$myVar',
|
||||||
|
cslDefaultValue: '$myVar',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: '$__contains',
|
||||||
|
body: `{ colName in ('value1','value2') }`,
|
||||||
|
inputParameters: [
|
||||||
|
{
|
||||||
|
name: 'colName',
|
||||||
|
type: 'System.String',
|
||||||
|
defaultValue: 'colName',
|
||||||
|
cslDefaultValue: 'colName',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: '$myVar',
|
||||||
|
type: 'System.String',
|
||||||
|
defaultValue: '$myVar',
|
||||||
|
cslDefaultValue: '$myVar',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Adding macros as global parameters
|
||||||
|
const globalParameters = templateVariables.map((v) => {
|
||||||
|
return {
|
||||||
|
name: `$${v.name}`,
|
||||||
|
type: 'dynamic',
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
// It's not possible to define optional paramaters in Kusto
|
||||||
|
// and it's not possible to define the same function twice so
|
||||||
|
// we are defining $__timeFilter also as a parameter when used
|
||||||
|
// with no arguments as a workaround
|
||||||
|
globalParameters.push({
|
||||||
|
name: `$__timeFilter`,
|
||||||
|
type: 'boolean',
|
||||||
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
clusterType: 'Engine',
|
clusterType: 'Engine',
|
||||||
@ -249,5 +306,6 @@ export function transformMetadataToKustoSchema(sourceSchema: AzureLogAnalyticsMe
|
|||||||
databases: [database],
|
databases: [database],
|
||||||
},
|
},
|
||||||
database: database,
|
database: database,
|
||||||
|
globalParameters,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { CodeEditor, Monaco, MonacoEditor } from '@grafana/ui';
|
import { CodeEditor, Monaco, MonacoEditor } from '@grafana/ui';
|
||||||
import { Deferred } from 'app/core/utils/deferred';
|
import { Deferred } from 'app/core/utils/deferred';
|
||||||
import React, { useCallback, useEffect, useRef } from 'react';
|
import React, { useCallback, useEffect, useRef } from 'react';
|
||||||
|
|
||||||
import { AzureQueryEditorFieldProps } from '../../types';
|
import { AzureQueryEditorFieldProps } from '../../types';
|
||||||
import { setKustoQuery } from './setQueryValue';
|
import { setKustoQuery } from './setQueryValue';
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user