mirror of
https://github.com/grafana/grafana.git
synced 2024-11-29 20:24:18 -06:00
CloudWatch: Use scopedVars in expressions (#49178)
* CloudWatch: Use scopedVars in expressions * fix spec test
This commit is contained in:
parent
909ebcf979
commit
b80934617b
@ -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));
|
||||
|
@ -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', () => {
|
||||
|
@ -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),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user