CloudWatch: Use scopedVars in expressions (#49178)

* CloudWatch: Use scopedVars in expressions

* fix spec test
This commit is contained in:
Isabella Siu 2022-05-26 17:03:55 -04:00 committed by GitHub
parent 909ebcf979
commit b80934617b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 11 deletions

View File

@ -43,7 +43,7 @@ export class ExpressionDatasourceApi extends DataSourceWithBackend<ExpressionQue
return query;
}
return ds?.interpolateVariablesInQueries([query], {})[0] as ExpressionQuery;
return ds?.interpolateVariablesInQueries([query], request.scopedVars)[0] as ExpressionQuery;
});
let sub = from(Promise.all(targets));

View File

@ -443,6 +443,30 @@ describe('datasource', () => {
});
});
describe('interpolateMetricsQueryVariables', () => {
it('interpolates dimensions correctly', () => {
const testQuery = {
id: 'a',
refId: 'a',
region: 'us-east-2',
namespace: '',
dimensions: { InstanceId: '$dimension' },
};
const ds = setupMockedDataSource({ variables: [dimensionVariable], mockGetVariableName: false });
const result = ds.datasource.interpolateMetricsQueryVariables(testQuery, {
dimension: { text: 'foo', value: 'foo' },
});
expect(result).toStrictEqual({
alias: '',
metricName: '',
namespace: '',
period: '',
sqlExpression: '',
dimensions: { InstanceId: ['foo'] },
});
});
});
describe('convertMultiFiltersFormat', () => {
const ds = setupMockedDataSource({ variables: [labelsVariable, dimensionVariable], mockGetVariableName: false });
it('converts keys and values correctly', () => {

View File

@ -958,13 +958,7 @@ export class CloudWatchDatasource
namespace: this.replace(query.namespace, scopedVars),
period: this.replace(query.period, scopedVars),
sqlExpression: this.replace(query.sqlExpression, scopedVars),
dimensions: Object.entries(query.dimensions ?? {}).reduce((prev, [key, value]) => {
if (Array.isArray(value)) {
return { ...prev, [key]: value };
}
return { ...prev, [this.replace(key, scopedVars)]: this.replace(value, scopedVars) };
}, {}),
dimensions: this.convertDimensionFormat(query.dimensions ?? {}, scopedVars),
};
}
}

View File

@ -585,7 +585,11 @@ describe('CloudWatchDatasource', () => {
});
it('should replace correct variables in CloudWatchMetricsQuery', () => {
const templateSrv: any = { replace: jest.fn(), getVariables: () => [] };
const templateSrv: any = {
replace: jest.fn(),
getVariables: () => [],
getVariableName: jest.fn((name: string) => name),
};
const { ds } = getTestContext({ templateSrv });
const variableName = 'someVar';
const logQuery: CloudWatchMetricsQuery = {
@ -608,9 +612,12 @@ describe('CloudWatchDatasource', () => {
ds.interpolateVariablesInQueries([logQuery], {});
// We interpolate `expression`, `region`, `period`, `alias`, `metricName`, `nameSpace` and `dimensions` in CloudWatchMetricsQuery
// We interpolate `expression`, `region`, `period`, `alias`, `metricName`, and `nameSpace` in CloudWatchMetricsQuery
expect(templateSrv.replace).toHaveBeenCalledWith(`$${variableName}`, {});
expect(templateSrv.replace).toHaveBeenCalledTimes(8);
expect(templateSrv.replace).toHaveBeenCalledTimes(7);
expect(templateSrv.getVariableName).toHaveBeenCalledWith(`$${variableName}`);
expect(templateSrv.getVariableName).toHaveBeenCalledTimes(1);
});
});