Skip to content

Commit

Permalink
[ML] Transforms: Improve percentiles agg validation (elastic#197816)
Browse files Browse the repository at this point in the history
## Summary

Resolves elastic#138874

The Transforms percentiles aggregation now uses a ComboBox to define
percentiles.


https://github.com/user-attachments/assets/f30de65e-3555-4643-963b-821877e7b166

a few more validation cases:


https://github.com/user-attachments/assets/79339c4e-36d2-465c-bc93-c47e1f442a87



### Checklist

- [ ] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)
  • Loading branch information
rbrtj authored Nov 21, 2024
1 parent d3b9a9a commit 716375b
Show file tree
Hide file tree
Showing 14 changed files with 335 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe('getAggConfigFromEsAgg', () => {
field: 'products.base_price',
parentAgg: result,
aggConfig: {
percents: '1,5,25,50,75,95,99',
percents: [1, 5, 25, 50, 75, 95, 99],
},
});

Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/transform/public/app/common/pivot_aggs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ export interface PivotAggsConfigWithExtra<T, ESConfig extends { [key: string]: a
onChange: (arg: Partial<T>) => void;
selectedField: string;
isValid?: boolean;
errorMessages?: string[];
}>;
/** Aggregation specific configuration */
aggConfig: Partial<T>;
Expand All @@ -238,6 +239,8 @@ export interface PivotAggsConfigWithExtra<T, ESConfig extends { [key: string]: a
getAggName?: () => string | undefined;
/** Helper text for the aggregation reflecting some configuration info */
helperText?: () => string | undefined;
/** Returns validation error messages */
getErrorMessages?: () => string[] | undefined;
}

interface PivotAggsConfigPercentiles extends PivotAggsConfigWithUiBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ export const PopoverForm: React.FC<Props> = ({ defaultData, otherAggNames, onCha
});
}}
isValid={aggConfigDef.isValid()}
errorMessages={aggConfigDef.getErrorMessages?.()}
/>
) : null}
{isUnsupportedAgg && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe('Transform: Define Pivot Common', () => {
aggName: 'the-field.percentiles',
dropDownName: 'percentiles( the-f[i]e>ld )',
AggFormComponent: PercentilesAggForm,
aggConfig: { percents: '1,5,25,50,75,95,99' },
aggConfig: { percents: [1, 5, 25, 50, 75, 95, 99] },
},
'filter( the-f[i]e>ld )': {
agg: 'filter',
Expand Down Expand Up @@ -222,7 +222,7 @@ describe('Transform: Define Pivot Common', () => {
dropDownName: 'percentiles( the-f[i]e>ld )',
field: ' the-f[i]e>ld ',
AggFormComponent: PercentilesAggForm,
aggConfig: { percents: '1,5,25,50,75,95,99' },
aggConfig: { percents: [1, 5, 25, 50, 75, 95, 99] },
},
'sum( the-f[i]e>ld )': {
agg: 'sum',
Expand Down Expand Up @@ -292,7 +292,7 @@ describe('Transform: Define Pivot Common', () => {
dropDownName: 'percentiles(rt_bytes_bigger)',
field: 'rt_bytes_bigger',
AggFormComponent: PercentilesAggForm,
aggConfig: { percents: '1,5,25,50,75,95,99' },
aggConfig: { percents: [1, 5, 25, 50, 75, 95, 99] },
},
'sum(rt_bytes_bigger)': {
agg: 'sum',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { getPercentilesAggConfig } from './config';
import type { IPivotAggsConfigPercentiles } from './types';
import { PERCENTILES_AGG_DEFAULT_PERCENTS } from '../../../../../../common';

describe('percentiles agg config', () => {
let config: IPivotAggsConfigPercentiles;

beforeEach(() => {
config = getPercentilesAggConfig({
agg: 'percentiles',
aggName: 'test-agg',
field: ['test-field'],
dropDownName: 'test-agg',
});
});

describe('#setUiConfigFromEs', () => {
test('sets field and percents from ES config', () => {
// act
config.setUiConfigFromEs({
field: 'test-field',
percents: [10, 20, 30],
});

// assert
expect(config.field).toEqual('test-field');
expect(config.aggConfig).toEqual({ percents: [10, 20, 30] });
});
});

describe('#getEsAggConfig', () => {
test('returns null for invalid config', () => {
// arrange
config.aggConfig.percents = [150]; // invalid percentile value

// act and assert
expect(config.getEsAggConfig()).toBeNull();
});

test('returns valid config', () => {
// arrange
config.field = 'test-field';
config.aggConfig.percents = [10, 20, 30];

// act and assert
expect(config.getEsAggConfig()).toEqual({
field: 'test-field',
percents: [10, 20, 30],
});
});

test('returns default percents if none specified', () => {
// arrange
config.field = 'test-field';

// act and assert
expect(config.getEsAggConfig()).toEqual({
field: 'test-field',
percents: PERCENTILES_AGG_DEFAULT_PERCENTS,
});
});
});

describe('#isValid', () => {
test('returns false for percentiles out of range', () => {
// arrange
config.aggConfig.percents = [150];

// act and assert
expect(config.isValid()).toBeFalsy();
expect(config.aggConfig.errors).toContain('PERCENTILE_OUT_OF_RANGE');
});

test('returns false for invalid number format', () => {
// arrrange
config.aggConfig.pendingPercentileInput = 'invalid';

// act and assert
expect(config.isValid()).toBeFalsy();
expect(config.aggConfig.errors).toContain('INVALID_FORMAT');
});

test('returns true for valid percents', () => {
// arrange
config.aggConfig.percents = [10, 20, 30];

// act and assert
expect(config.isValid()).toBeTruthy();
expect(config.aggConfig.errors).toBeUndefined();
});

test('validates pending input along with existing percents', () => {
// arrange
config.aggConfig.percents = [10, 20, 30];
config.aggConfig.pendingPercentileInput = '50';

// act and assert
expect(config.isValid()).toBeTruthy();
expect(config.aggConfig.errors).toBeUndefined();
});
});

describe('#getErrorMessages', () => {
test('returns undefined when there are no errors', () => {
// arrange
config.aggConfig.errors = undefined;

// act and assert
expect(config.getErrorMessages?.()).toBeUndefined();
});

test('returns undefined when errors array is empty', () => {
// arrange
config.aggConfig.errors = [];

// act and assert
expect(config.getErrorMessages?.()).toBeUndefined();
});

test('returns translated messages for single error', () => {
// arrange
config.aggConfig.errors = ['PERCENTILE_OUT_OF_RANGE'];

// act and assert
expect(config.getErrorMessages?.()).toEqual(['Percentiles must be between 0 and 100']);
});

test('returns translated messages for multiple errors', () => {
// arrange
config.aggConfig.errors = ['PERCENTILE_OUT_OF_RANGE', 'INVALID_FORMAT'];

// act and assert
expect(config.getErrorMessages?.()).toEqual([
'Percentiles must be between 0 and 100',
'Percentile must be a valid number',
]);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,60 @@
* 2.0.
*/

import { i18n } from '@kbn/i18n';
import { PercentilesAggForm } from './percentiles_form_component';
import type { IPivotAggsConfigPercentiles } from './types';
import type {
IPivotAggsConfigPercentiles,
PercentilesAggConfig,
ValidationResult,
ValidationResultErrorType,
} from './types';
import type { PivotAggsConfigBase } from '../../../../../../common';
import {
isPivotAggsConfigWithUiBase,
PERCENTILES_AGG_DEFAULT_PERCENTS,
} from '../../../../../../common';
import type { PivotAggsConfigWithUiBase } from '../../../../../../common/pivot_aggs';
import { MAX_PERCENTILE_PRECISION, MAX_PERCENTILE_VALUE, MIN_PERCENTILE_VALUE } from './constants';

/**
* TODO this callback has been moved.
* The logic of parsing the string should be improved.
*/
function parsePercentsInput(inputValue: string | undefined) {
if (inputValue !== undefined) {
const strVals: string[] = inputValue.split(',');
const percents: number[] = [];
for (const str of strVals) {
if (str.trim().length > 0 && isNaN(str as any) === false) {
const val = Number(str);
if (val >= 0 && val <= 100) {
percents.push(val);
} else {
return [];
}
}
function validatePercentsInput(config: Partial<PercentilesAggConfig>): ValidationResult {
const allValues = [...(config.percents ?? [])];
const errors: ValidationResultErrorType[] = [];
// Combine existing percents with pending input for validation
if (config.pendingPercentileInput) {
// Replace comma with dot before converting to number
const normalizedInput = config.pendingPercentileInput.replace(',', '.');
const pendingValue = Number(normalizedInput);

if (allValues.includes(pendingValue)) {
errors.push('DUPLICATE_VALUE');
}

if (normalizedInput.replace('.', '').length > MAX_PERCENTILE_PRECISION) {
errors.push('NUMBER_TOO_PRECISE');
}

return percents;
allValues.push(pendingValue);
}

return [];
}
if (allValues.length === 0) {
return {
isValid: false,
errors: [],
};
}

if (allValues.some((value) => isNaN(value))) {
errors.push('INVALID_FORMAT');
}
if (allValues.some((value) => value < MIN_PERCENTILE_VALUE || value > MAX_PERCENTILE_VALUE)) {
errors.push('PERCENTILE_OUT_OF_RANGE');
}

// Input string should only include comma separated numbers
function isValidPercentsInput(inputValue: string) {
return /^[0-9]+(,[0-9]+)*$/.test(inputValue);
return {
isValid: errors.length === 0,
errors: errors.length > 0 ? errors : undefined,
};
}

export function getPercentilesAggConfig(
Expand All @@ -56,13 +73,13 @@ export function getPercentilesAggConfig(
AggFormComponent: PercentilesAggForm,
field,
aggConfig: {
percents: PERCENTILES_AGG_DEFAULT_PERCENTS.toString(),
percents: PERCENTILES_AGG_DEFAULT_PERCENTS,
},
setUiConfigFromEs(esAggDefinition) {
const { field: esField, percents } = esAggDefinition;

this.field = esField;
this.aggConfig.percents = percents.join(',');
this.aggConfig.percents = percents;
},
getEsAggConfig() {
if (!this.isValid()) {
Expand All @@ -71,13 +88,36 @@ export function getPercentilesAggConfig(

return {
field: this.field as string,
percents: parsePercentsInput(this.aggConfig.percents),
percents: this.aggConfig.percents ?? [],
};
},
isValid() {
return (
typeof this.aggConfig.percents === 'string' && isValidPercentsInput(this.aggConfig.percents)
);
const validationResult = validatePercentsInput(this.aggConfig);
this.aggConfig.errors = validationResult.errors;
return validationResult.isValid;
},
getErrorMessages() {
if (!this.aggConfig.errors?.length) return;

return this.aggConfig.errors.map((error) => ERROR_MESSAGES[error]);
},
};
}

const ERROR_MESSAGES: Record<ValidationResultErrorType, string> = {
INVALID_FORMAT: i18n.translate('xpack.transform.agg.popoverForm.invalidFormatError', {
defaultMessage: 'Percentile must be a valid number',
}),
PERCENTILE_OUT_OF_RANGE: i18n.translate(
'xpack.transform.agg.popoverForm.percentileOutOfRangeError',
{
defaultMessage: 'Percentiles must be between 0 and 100',
}
),
NUMBER_TOO_PRECISE: i18n.translate('xpack.transform.agg.popoverForm.numberTooPreciseError', {
defaultMessage: 'Value is too precise. Use fewer decimal places.',
}),
DUPLICATE_VALUE: i18n.translate('xpack.transform.agg.popoverForm.duplicateValueError', {
defaultMessage: 'Value already exists',
}),
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export const MAX_PERCENTILE_PRECISION = 17;
export const MAX_PERCENTILE_VALUE = 100;
export const MIN_PERCENTILE_VALUE = 0;
Loading

0 comments on commit 716375b

Please sign in to comment.