Skip to content

Commit

Permalink
Cloudwatch: Add expression field to interpolate variables in Metrics …
Browse files Browse the repository at this point in the history
…Code Builder (#64288)
  • Loading branch information
idastambuk authored Mar 9, 2023
1 parent 3cbeace commit 1c8ad04
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
4 changes: 2 additions & 2 deletions public/app/plugins/datasource/cloudwatch/datasource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,9 @@ describe('datasource', () => {

datasource.interpolateVariablesInQueries([metricsQuery], {});

// We interpolate `expression`, `region`, `period`, `alias`, `metricName`, and `nameSpace` in CloudWatchMetricsQuery
// We interpolate `expression`, `sqlExpression`, `region`, `period`, `alias`, `metricName`, `dimensions`, and `nameSpace` in CloudWatchMetricsQuery
expect(templateService.replace).toHaveBeenCalledWith(`$${variableName}`, {});
expect(templateService.replace).toHaveBeenCalledTimes(7);
expect(templateService.replace).toHaveBeenCalledTimes(8);

expect(templateService.getVariableName).toHaveBeenCalledWith(`$${variableName}`);
expect(templateService.getVariableName).toHaveBeenCalledTimes(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -694,26 +694,29 @@ describe('CloudWatchMetricsQueryRunner', () => {
);
});
});

describe('interpolateMetricsQueryVariables', () => {
it('interpolates dimensions correctly', () => {
it('interpolates values correctly', () => {
const testQuery = {
id: 'a',
refId: 'a',
region: 'us-east-2',
namespace: '',
expression: 'ABS($datasource)',
sqlExpression: 'select SUM(CPUUtilization) from $datasource',
dimensions: { InstanceId: '$dimension' },
};
const { runner } = setupMockedMetricsQueryRunner({ variables: [dimensionVariable], mockGetVariableName: false });
const result = runner.interpolateMetricsQueryVariables(testQuery, {
datasource: { text: 'foo', value: 'foo' },
dimension: { text: 'foo', value: 'foo' },
});
expect(result).toStrictEqual({
alias: '',
metricName: '',
namespace: '',
period: '',
sqlExpression: '',
sqlExpression: 'select SUM(CPUUtilization) from foo',
expression: 'ABS(foo)',
dimensions: { InstanceId: ['foo'] },
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,16 @@ export class CloudWatchMetricsQueryRunner extends CloudWatchRequest {
interpolateMetricsQueryVariables(
query: CloudWatchMetricsQuery,
scopedVars: ScopedVars
): Pick<CloudWatchMetricsQuery, 'alias' | 'metricName' | 'namespace' | 'period' | 'dimensions' | 'sqlExpression'> {
): Pick<
CloudWatchMetricsQuery,
'alias' | 'metricName' | 'namespace' | 'period' | 'dimensions' | 'sqlExpression' | 'expression'
> {
return {
alias: this.replaceVariableAndDisplayWarningIfMulti(query.alias, scopedVars),
metricName: this.replaceVariableAndDisplayWarningIfMulti(query.metricName, scopedVars),
namespace: this.replaceVariableAndDisplayWarningIfMulti(query.namespace, scopedVars),
period: this.replaceVariableAndDisplayWarningIfMulti(query.period, scopedVars),
expression: this.templateSrv.replace(query.expression, scopedVars),
sqlExpression: this.replaceVariableAndDisplayWarningIfMulti(query.sqlExpression, scopedVars),
dimensions: this.convertDimensionFormat(query.dimensions ?? {}, scopedVars),
};
Expand Down

0 comments on commit 1c8ad04

Please sign in to comment.