mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Azuremonitor: Multi resource fix (#68759)
* multi resource groups query for different resoucres * expand resources from multi var
This commit is contained in:
parent
b57ef1f2c7
commit
56812def50
@ -7,7 +7,7 @@ interface TemplateableValue {
|
||||
templateVariable: VariableWithOptions;
|
||||
}
|
||||
|
||||
export function createTemplateVariables(templateableProps: string[]): Map<string, TemplateableValue> {
|
||||
export function createTemplateVariables(templateableProps: string[], value = ''): Map<string, TemplateableValue> {
|
||||
const templateVariables = new Map<string, TemplateableValue>();
|
||||
templateableProps.map((prop) => {
|
||||
const variableName = prop.replace(/[\[\].]/g, '');
|
||||
@ -15,7 +15,7 @@ export function createTemplateVariables(templateableProps: string[]): Map<string
|
||||
current: {
|
||||
selected: false,
|
||||
text: `${variableName}-template-variable`,
|
||||
value: `${variableName}-template-variable`,
|
||||
value: value === '' ? `${variableName}-template-variable` : value,
|
||||
},
|
||||
id: variableName,
|
||||
name: variableName,
|
||||
|
@ -378,6 +378,24 @@ describe('AzureLogAnalyticsDatasource', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('should return a logs query with multiple resources template variables replaced', () => {
|
||||
const templateVariables = createTemplateVariables(['resource'], 'resource1,resource2');
|
||||
templateSrv.init(Array.from(templateVariables.values()).map((item) => item.templateVariable));
|
||||
const query = createMockQuery();
|
||||
const azureLogAnalytics: Partial<AzureLogsQuery> = {};
|
||||
azureLogAnalytics.resources = ['$resource'];
|
||||
query.queryType = AzureQueryType.LogAnalytics;
|
||||
query.azureLogAnalytics = {
|
||||
...query.azureLogAnalytics,
|
||||
...azureLogAnalytics,
|
||||
};
|
||||
const templatedQuery = ctx.ds.interpolateVariablesInQueries([query], {});
|
||||
expect(templatedQuery[0]).toHaveProperty('datasource');
|
||||
expect(templatedQuery[0].azureLogAnalytics).toMatchObject({
|
||||
resources: ['resource1', 'resource2'],
|
||||
});
|
||||
});
|
||||
|
||||
it('should return a traces query with any template variables replaced', () => {
|
||||
const templateableProps = ['resource', 'query', 'traceTypes', 'property', 'operation', 'filter', 'operationId'];
|
||||
const templateVariables = createTemplateVariables(templateableProps);
|
||||
@ -411,5 +429,23 @@ describe('AzureLogAnalyticsDatasource', () => {
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it('should return a trace query with multiple resources template variables replaced', () => {
|
||||
const templateVariables = createTemplateVariables(['resource'], 'resource1,resource2');
|
||||
templateSrv.init(Array.from(templateVariables.values()).map((item) => item.templateVariable));
|
||||
const query = createMockQuery();
|
||||
const azureTraces: Partial<AzureTracesQuery> = {};
|
||||
azureTraces.resources = ['$resource'];
|
||||
query.queryType = AzureQueryType.AzureTraces;
|
||||
query.azureTraces = {
|
||||
...query.azureTraces,
|
||||
...azureTraces,
|
||||
};
|
||||
const templatedQuery = ctx.ds.interpolateVariablesInQueries([query], {});
|
||||
expect(templatedQuery[0]).toHaveProperty('datasource');
|
||||
expect(templatedQuery[0].azureTraces).toMatchObject({
|
||||
resources: ['resource1', 'resource2'],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -116,7 +116,7 @@ export default class AzureLogAnalyticsDatasource extends DataSourceWithBackend<
|
||||
if (target.queryType === AzureQueryType.LogAnalytics && target.azureLogAnalytics) {
|
||||
item = target.azureLogAnalytics;
|
||||
const templateSrv = getTemplateSrv();
|
||||
const resources = item.resources?.map((r) => templateSrv.replace(r, scopedVars));
|
||||
const resources = this.expandResourcesForMultipleVariables(item.resources, scopedVars);
|
||||
let workspace = templateSrv.replace(item.workspace, scopedVars);
|
||||
|
||||
if (!workspace && !resources && this.firstWorkspace) {
|
||||
@ -142,7 +142,7 @@ export default class AzureLogAnalyticsDatasource extends DataSourceWithBackend<
|
||||
if (target.queryType === AzureQueryType.AzureTraces && target.azureTraces) {
|
||||
item = target.azureTraces;
|
||||
const templateSrv = getTemplateSrv();
|
||||
const resources = item.resources?.map((r) => templateSrv.replace(r, scopedVars));
|
||||
const resources = this.expandResourcesForMultipleVariables(item.resources, scopedVars);
|
||||
const query = templateSrv.replace(item.query, scopedVars, interpolateVariable);
|
||||
const traceTypes = item.traceTypes?.map((t) => templateSrv.replace(t, scopedVars));
|
||||
const filters = (item.filters ?? [])
|
||||
@ -174,6 +174,25 @@ export default class AzureLogAnalyticsDatasource extends DataSourceWithBackend<
|
||||
return target;
|
||||
}
|
||||
|
||||
private expandResourcesForMultipleVariables(
|
||||
resources: string[] | undefined,
|
||||
scopedVars: ScopedVars
|
||||
): string[] | undefined {
|
||||
if (!resources) {
|
||||
return undefined;
|
||||
}
|
||||
const expandedResources: string[] = [];
|
||||
const templateSrv = getTemplateSrv();
|
||||
resources.forEach((r: string) => {
|
||||
const tempVars = templateSrv.replace(r, scopedVars, 'raw');
|
||||
const values = tempVars.split(',');
|
||||
values.forEach((value) => {
|
||||
expandedResources.push(value);
|
||||
});
|
||||
});
|
||||
return expandedResources;
|
||||
}
|
||||
|
||||
/*
|
||||
In 7.5.x it used to be possible to set a default workspace id in the config on the auth page.
|
||||
This has been deprecated, however is still used by a few legacy template queries.
|
||||
|
Loading…
Reference in New Issue
Block a user