Skip to content

Commit

Permalink
fix(cloudwatch): unrecognized statistic warning when using percentile…
Browse files Browse the repository at this point in the history
…Rank statistic in Stats helper (#29498)

### Issue # (if applicable)

Closes #29465.

### Reason for this change

There shouldn't be a warning when `Stats.percentileRank`

### Description of changes

Add a new parser for percentileRank statistic

### Description of how you validated changes

unit test

### Checklist
- [ ] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
xazhao authored Mar 15, 2024
1 parent 87139ab commit f2ad980
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/aws-cdk-lib/aws-cloudwatch/lib/private/statistic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export interface PercentileStatistic extends SingleStatistic {
statName: 'percentile';
}

export interface PercentileRankStatistic extends PairStatistic {
statName: 'percentileRank';
}

export interface TrimmedMeanStatistic extends PairStatistic {
statName: 'trimmedMean';
}
Expand Down Expand Up @@ -154,6 +158,7 @@ export function parseStatistic(
):
| SimpleStatistic
| PercentileStatistic
| PercentileRankStatistic
| TrimmedMeanStatistic
| WinsorizedMeanStatistic
| TrimmedCountStatistic
Expand Down Expand Up @@ -188,6 +193,10 @@ export function parseStatistic(
m = parseSingleStatistic(stat, 'p');
if (m) return { ...m, statName: 'percentile' } as PercentileStatistic;

// Percentile Rank statistics
m = parsePairStatistic(stat, 'pr');
if (m) return { ...m, statName: 'percentileRank' } as PercentileRankStatistic;

// Trimmed mean statistics
m = parseSingleStatistic(stat, 'tm') || parsePairStatistic(stat, 'tm');
if (m) return { ...m, statName: 'trimmedMean' } as TrimmedMeanStatistic;
Expand Down
15 changes: 15 additions & 0 deletions packages/aws-cdk-lib/aws-cloudwatch/test/stats.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import { Metric, Stats } from '../../aws-cloudwatch';
import * as cloudwatch from '../lib';

it.each([
Stats.percentileRank(0),
Stats.percentileRank(0, 1),
Stats.percentileRank(0, undefined),
])('Stats can create valid statistics %s without causing warnings', (statistic) => {
const metric = new Metric({
namespace: 'example',
metricName: 'example',
statistic,
});

expect(metric.warningsV2).toEqual(undefined);
});

test('spot check some constants', () => {
expect(cloudwatch.Stats.AVERAGE).toEqual('Average');
expect(cloudwatch.Stats.IQM).toEqual('IQM');
Expand Down

0 comments on commit f2ad980

Please sign in to comment.