CloudWatch: Update metric stat editor to match aws statistics (#66532)

This commit is contained in:
Sladyn 2023-05-10 01:20:41 -07:00 committed by GitHub
parent 356eabc24c
commit 26d33d8fd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 6 deletions

View File

@ -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(<MetricStatEditor {...props} onChange={onChange} />);
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(<MetricStatEditor {...props} onChange={onChange} />);
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(<MetricStatEditor {...props} onChange={onChange} />);
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 });
});
});

View File

@ -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;

View File

@ -1 +1 @@
export const standardStatistics = ['Average', 'Maximum', 'Minimum', 'Sum', 'SampleCount'];
export const standardStatistics = ['Average', 'Maximum', 'Minimum', 'Sum', 'SampleCount', 'IQM'];

View File

@ -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 },
]);
});