Skip to content

Commit

Permalink
[ResponseOps][Alerting] Elasticsearch query rule type allows SIZE: 0,…
Browse files Browse the repository at this point in the history
… but flags as error on re-edit (#142225)

* Fixing es query bug

* Updating threshold

* Adding isNil to another check
  • Loading branch information
doakalexi authored Oct 10, 2022
1 parent c7b742c commit b542f7b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,20 @@ describe('expression params validation', () => {
);
});

test('if size property is 0 should not return error message', () => {
const initialParams: EsQueryAlertParams<SearchType.esQuery> = {
index: ['test'],
esQuery: `{\n \"query\":{\n \"match_all\" : {}\n }\n`,
size: 0,
timeWindowSize: 1,
timeWindowUnit: 's',
threshold: [0],
timeField: '',
excludeHitsFromPreviousRun: true,
};
expect(validateExpression(initialParams).errors.size.length).toBe(0);
});

test('if size property is > 10000 should return proper error message', () => {
const initialParams: EsQueryAlertParams<SearchType.esQuery> = {
index: ['test'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { defaultsDeep } from 'lodash';
import { defaultsDeep, isNil } from 'lodash';
import { i18n } from '@kbn/i18n';
import { ValidationResult, builtInComparators } from '@kbn/triggers-actions-ui-plugin/public';
import { EsQueryAlertParams, ExpressionErrors } from './types';
Expand Down Expand Up @@ -63,7 +63,7 @@ export const validateExpression = (ruleParams: EsQueryAlertParams): ValidationRe
);
}

if (!size) {
if (isNil(size)) {
errors.size.push(
i18n.translate('xpack.stackAlerts.esQuery.ui.validation.error.requiredSizeText', {
defaultMessage: 'Size is required.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,27 @@ describe('threshold expression', () => {
wrapper.update();
expect(wrapper.find('input[data-test-subj="alertThresholdInput"]').length).toEqual(1);
});

it('is valid when the threshold value is 0', () => {
const onChangeSelectedThreshold = jest.fn();
const onChangeSelectedThresholdComparator = jest.fn();
const wrapper = shallow(
<ThresholdExpression
thresholdComparator={'>'}
threshold={[0]}
errors={{ threshold0: [], threshold1: [] }}
onChangeSelectedThreshold={onChangeSelectedThreshold}
onChangeSelectedThresholdComparator={onChangeSelectedThresholdComparator}
/>
);
expect(wrapper.find('[data-test-subj="alertThresholdInput"]')).toMatchInlineSnapshot(`
<EuiFieldNumber
data-test-subj="alertThresholdInput"
isInvalid={false}
min={0}
onChange={[Function]}
value={0}
/>
`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
EuiFieldNumber,
EuiText,
} from '@elastic/eui';
import { isNil } from 'lodash';
import { builtInComparators } from '../constants';
import { Comparator } from '../types';
import { IErrorObject } from '../../types';
Expand Down Expand Up @@ -145,14 +146,14 @@ export const ThresholdExpression = ({
) : null}
<EuiFlexItem grow={false}>
<EuiFormRow
isInvalid={errors[`threshold${i}`]?.length > 0 || !threshold[i]}
isInvalid={errors[`threshold${i}`]?.length > 0 || isNil(threshold[i])}
error={errors[`threshold${i}`]}
>
<EuiFieldNumber
data-test-subj="alertThresholdInput"
min={0}
value={!threshold || threshold[i] === undefined ? '' : threshold[i]}
isInvalid={errors[`threshold${i}`]?.length > 0 || !threshold[i]}
isInvalid={errors[`threshold${i}`]?.length > 0 || isNil(threshold[i])}
onChange={(e) => {
const { value } = e.target;
const thresholdVal = value !== '' ? parseFloat(value) : undefined;
Expand Down

0 comments on commit b542f7b

Please sign in to comment.