From 26d33d8fd47a6a16c0cec82d996bfac5d2dcf126 Mon Sep 17 00:00:00 2001 From: Sladyn Date: Wed, 10 May 2023 01:20:41 -0700 Subject: [PATCH] CloudWatch: Update metric stat editor to match aws statistics (#66532) --- .../MetricStatEditor.test.tsx | 50 +++++++++++++++++-- .../MetricStatEditor/MetricStatEditor.tsx | 6 ++- .../cloudwatch/standardStatistics.ts | 2 +- .../datasource/cloudwatch/variables.test.ts | 1 + 4 files changed, 53 insertions(+), 6 deletions(-) diff --git a/public/app/plugins/datasource/cloudwatch/components/MetricStatEditor/MetricStatEditor.test.tsx b/public/app/plugins/datasource/cloudwatch/components/MetricStatEditor/MetricStatEditor.test.tsx index bc1126972f3..f405361efb9 100644 --- a/public/app/plugins/datasource/cloudwatch/components/MetricStatEditor/MetricStatEditor.test.tsx +++ b/public/app/plugins/datasource/cloudwatch/components/MetricStatEditor/MetricStatEditor.test.tsx @@ -42,7 +42,6 @@ describe('MetricStatEditor', () => { describe('statistics field', () => { test.each(['Average', 'p23.23', 'p34', '$statistic'])('should accept valid values', async (statistic) => { const onChange = jest.fn(); - render(); const statisticElement = await screen.findByLabelText('Statistic'); @@ -53,9 +52,52 @@ describe('MetricStatEditor', () => { expect(onChange).toHaveBeenCalledWith({ ...props.metricStat, statistic }); }); - test.each(['CustomStat', 'p23,23', '$someUnknownValue'])('should not accept invalid values', async (statistic) => { - const onChange = jest.fn(); + test.each(['CustomStat', 'p23,23', 'tc(80%:)', 'ts', 'wm', 'pr', 'tm', 'tm(10:90)', '$someUnknownValue'])( + 'should not accept invalid values', + async (statistic) => { + const onChange = jest.fn(); + render(); + + const statisticElement = await screen.findByLabelText('Statistic'); + expect(statisticElement).toBeInTheDocument(); + + await userEvent.type(statisticElement, statistic); + fireEvent.keyDown(statisticElement, { keyCode: 13 }); + expect(onChange).not.toHaveBeenCalled(); + } + ); + + test.each([ + 'IQM', + 'tm90', + 'tm23.23', + 'TM(25%:75%)', + 'TM(0.005:0.030)', + 'TM(150:1000)', + 'TM(:0.5)', + 'TM(:95%)', + 'wm98', + 'wm23.23', + 'WM(25%:75%)', + 'WM(0.005:0.030)', + 'WM(150:1000)', + 'WM(:0.5)', + 'PR(10:)', + 'PR(:300)', + 'PR(100:20000)', + 'tc90', + 'tc23.23', + 'TC(0.005:0.030)', + 'TC(:0.5)', + 'TC(25%:75%)', + 'TC(150:1000)', + 'TC(:0.5)', + 'ts90', + 'ts23.23', + 'TS(80%:)', + ])('should accept other valid statistics syntax', async (statistic) => { + const onChange = jest.fn(); render(); const statisticElement = await screen.findByLabelText('Statistic'); @@ -63,7 +105,7 @@ describe('MetricStatEditor', () => { await userEvent.type(statisticElement, statistic); fireEvent.keyDown(statisticElement, { keyCode: 13 }); - expect(onChange).not.toHaveBeenCalled(); + expect(onChange).toHaveBeenCalledWith({ ...props.metricStat, statistic }); }); }); diff --git a/public/app/plugins/datasource/cloudwatch/components/MetricStatEditor/MetricStatEditor.tsx b/public/app/plugins/datasource/cloudwatch/components/MetricStatEditor/MetricStatEditor.tsx index abfe5b6f40c..f6daaafc528 100644 --- a/public/app/plugins/datasource/cloudwatch/components/MetricStatEditor/MetricStatEditor.tsx +++ b/public/app/plugins/datasource/cloudwatch/components/MetricStatEditor/MetricStatEditor.tsx @@ -21,6 +21,10 @@ export type Props = { onChange: (value: MetricStat) => void; }; +const percentileSyntaxRE = /^(p|tm|tc|ts|wm)\d{2}(?:\.\d{1,2})?$/; +const boundariesInnerParenthesesSyntax = `\\d*(\\.\\d+)?%?:\\d*(\\.\\d+)?%?`; +const boundariesSyntaxRE = new RegExp(`^(PR|TM|TC|TS|WM)\\((${boundariesInnerParenthesesSyntax})\\)$`); + export function MetricStatEditor({ refId, metricStat, @@ -116,7 +120,7 @@ export function MetricStatEditor({ if ( !statistic || (!standardStatistics.includes(statistic) && - !/^p\d{2}(?:\.\d{1,2})?$/.test(statistic) && + !(percentileSyntaxRE.test(statistic) || boundariesSyntaxRE.test(statistic)) && !datasource.templateSrv.containsTemplate(statistic)) ) { return; diff --git a/public/app/plugins/datasource/cloudwatch/standardStatistics.ts b/public/app/plugins/datasource/cloudwatch/standardStatistics.ts index 0d03476a07b..43a2e6796a2 100644 --- a/public/app/plugins/datasource/cloudwatch/standardStatistics.ts +++ b/public/app/plugins/datasource/cloudwatch/standardStatistics.ts @@ -1 +1 @@ -export const standardStatistics = ['Average', 'Maximum', 'Minimum', 'Sum', 'SampleCount']; +export const standardStatistics = ['Average', 'Maximum', 'Minimum', 'Sum', 'SampleCount', 'IQM']; diff --git a/public/app/plugins/datasource/cloudwatch/variables.test.ts b/public/app/plugins/datasource/cloudwatch/variables.test.ts index 88350e3e331..56ef68254ec 100644 --- a/public/app/plugins/datasource/cloudwatch/variables.test.ts +++ b/public/app/plugins/datasource/cloudwatch/variables.test.ts @@ -213,6 +213,7 @@ describe('variables', () => { { text: 'Minimum', value: 'Minimum', expandable: true }, { text: 'Sum', value: 'Sum', expandable: true }, { text: 'SampleCount', value: 'SampleCount', expandable: true }, + { text: 'IQM', value: 'IQM', expandable: true }, ]); });