Skip to content

Commit

Permalink
Remove breaking change, allow case insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
rigwild committed Nov 26, 2022
1 parent 9ca996a commit 1314195
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch/lib/private/statistic.ts
Expand Up @@ -53,6 +53,9 @@ export interface TrimmedSumStatistic extends PairStatistic {
function parseSingleStatistic(statistic: string, prefix: string): Omit<SingleStatistic, 'statName'> | undefined {
const prefixLower = prefix.toLowerCase();

// Allow `P99` uppercase
statistic = statistic.toLowerCase();

if (!statistic.startsWith(prefixLower)) {
return undefined;
}
Expand All @@ -77,6 +80,9 @@ function parseSingleStatistic(statistic: string, prefix: string): Omit<SingleSta
function parsePairStatistic(statistic: string, prefix: string): Omit<PairStatistic, 'statName'> | undefined {
const prefixUpper = prefix.toUpperCase();

// Allow `tm(10%:90%)` lowercase
statistic = statistic.toUpperCase();

if (!statistic.startsWith(prefixUpper)) {
return undefined;
}
Expand Down Expand Up @@ -244,6 +250,16 @@ export function parseStatistic(
}

export function normalizeStatistic(parsed: ReturnType<typeof parseStatistic>): string {
/* eslint-disable no-console */
console.log('normalizeStatistic');
console.log('normalizeStatistic');
console.log('normalizeStatistic');
console.log('normalizeStatistic');
console.log('normalizeStatistic');
console.log('normalizeStatistic');
console.log('normalizeStatistic');
console.log(parsed);
console.log(parsed.type);
if (parsed.type === 'simple' || parsed.type === 'generic') {
return parsed.statistic;
} else if (parsed.type === 'single') {
Expand Down
19 changes: 19 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch/test/alarm.test.ts
Expand Up @@ -269,6 +269,25 @@ describe('Alarm', () => {
});
});

test('can use a generic pair string for extended statistic to make alarm', () => {
// GIVEN
const stack = new Stack();

// WHEN
testMetric.with({
statistic: 'TM(10%:90%)',
}).createAlarm(stack, 'Alarm', {
threshold: 1000,
evaluationPeriods: 2,
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
Statistic: Match.absent(),
ExtendedStatistic: 'TM(10%:90%)',
});
});

test('metric warnings are added to Alarm', () => {
const stack = new Stack(undefined, 'MyStack');
const m = new MathExpression({ expression: 'oops' });
Expand Down
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch/test/metrics.test.ts
Expand Up @@ -286,6 +286,7 @@ describe('Metrics', () => {
// Check single statistics
checkParsingSingle('p9', 'p', 'percentile', 9);
checkParsingSingle('p99', 'p', 'percentile', 99);
checkParsingSingle('P99', 'p', 'percentile', 99);
checkParsingSingle('p99.99', 'p', 'percentile', 99.99);
checkParsingSingle('tm99', 'tm', 'trimmedMean', 99);
checkParsingSingle('wm99', 'wm', 'winsorizedMean', 99);
Expand All @@ -303,6 +304,7 @@ describe('Metrics', () => {
checkParsingPair('TM(:90%)', 'TM', 'trimmedMean', true, true, 'tm90', 10, 90);

// Check every case
checkParsingPair('tm(10%:90%)', 'TM', 'trimmedMean', true, false, undefined, 10, 90);
checkParsingPair('TM(10%:90%)', 'TM', 'trimmedMean', true, false, undefined, 10, 90);
checkParsingPair('TM(:90%)', 'TM', 'trimmedMean', true, true, 'tm90', undefined, 90);
checkParsingPair('TM(10%:)', 'TM', 'trimmedMean', true, false, undefined, 10, undefined);
Expand Down

0 comments on commit 1314195

Please sign in to comment.