diff --git a/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_alert_types/threshold/expression.tsx b/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_alert_types/threshold/expression.tsx index 5c7f48de81f75..728418bf3c336 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_alert_types/threshold/expression.tsx +++ b/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_alert_types/threshold/expression.tsx @@ -50,7 +50,7 @@ const DEFAULT_VALUES = { THRESHOLD_COMPARATOR: COMPARATORS.GREATER_THAN, TIME_WINDOW_SIZE: 5, TIME_WINDOW_UNIT: 'm', - THRESHOLD: [1000, 5000], + THRESHOLD: [1000], GROUP_BY: 'all', }; diff --git a/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_alert_types/threshold/index.ts b/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_alert_types/threshold/index.ts index a94d2319d6e4d..b8ad623e23424 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_alert_types/threshold/index.ts +++ b/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_alert_types/threshold/index.ts @@ -7,7 +7,11 @@ import { i18n } from '@kbn/i18n'; import { AlertTypeModel, ValidationResult } from '../../../../types'; import { IndexThresholdAlertTypeExpression } from './expression'; import { IndexThresholdAlertParams } from './types'; -import { builtInGroupByTypes, builtInAggregationTypes } from '../../../../common/constants'; +import { + builtInGroupByTypes, + builtInAggregationTypes, + builtInComparators, +} from '../../../../common/constants'; export function getAlertType(): AlertTypeModel { return { @@ -26,6 +30,7 @@ export function getAlertType(): AlertTypeModel { termField, threshold, timeWindowSize, + thresholdComparator, } = alertParams; const validationResult = { errors: {} }; const errors = { @@ -84,20 +89,32 @@ export function getAlertType(): AlertTypeModel { ) ); } - if (threshold && threshold.length > 0 && !threshold[0]) { + if (!threshold || threshold.length === 0 || (threshold.length === 1 && !threshold[0])) { errors.threshold0.push( i18n.translate('xpack.triggersActionsUI.sections.addAlert.error.requiredThreshold0Text', { defaultMessage: 'Threshold0, is required.', }) ); } - if (threshold && threshold.length > 1 && !threshold[1]) { + if ( + thresholdComparator && + builtInComparators[thresholdComparator].requiredValues > 1 && + (!threshold || + (threshold && threshold.length < builtInComparators[thresholdComparator!].requiredValues)) + ) { errors.threshold1.push( i18n.translate('xpack.triggersActionsUI.sections.addAlert.error.requiredThreshold1Text', { defaultMessage: 'Threshold1 is required.', }) ); } + if (threshold && threshold.length === 2 && threshold[0] > threshold[1]) { + errors.threshold1.push( + i18n.translate('xpack.triggersActionsUI.sections.addAlert.error.requiredThreshold1Text', { + defaultMessage: 'Threshold1 should be > Threshold0.', + }) + ); + } return validationResult; }, defaultActionMessage: 'Alert [{{ctx.metadata.name}}] has exceeded the threshold', diff --git a/x-pack/plugins/triggers_actions_ui/public/common/expression_items/threshold.test.tsx b/x-pack/plugins/triggers_actions_ui/public/common/expression_items/threshold.test.tsx index bd3c7383d4b9c..92880bd124507 100644 --- a/x-pack/plugins/triggers_actions_ui/public/common/expression_items/threshold.test.tsx +++ b/x-pack/plugins/triggers_actions_ui/public/common/expression_items/threshold.test.tsx @@ -15,7 +15,7 @@ describe('threshold expression', () => { const wrapper = shallow( @@ -59,7 +59,7 @@ describe('threshold expression', () => { const wrapper = shallow( diff --git a/x-pack/plugins/triggers_actions_ui/public/common/expression_items/threshold.tsx b/x-pack/plugins/triggers_actions_ui/public/common/expression_items/threshold.tsx index ecbf0aee63e2d..2dda0a576cea5 100644 --- a/x-pack/plugins/triggers_actions_ui/public/common/expression_items/threshold.tsx +++ b/x-pack/plugins/triggers_actions_ui/public/common/expression_items/threshold.tsx @@ -105,6 +105,11 @@ export const ThresholdExpression = ({ value={thresholdComparator} onChange={e => { onChangeSelectedThresholdComparator(e.target.value); + const thresholdValues: number[] | undefined = []; + Array.from(Array(comparators[e.target.value].requiredValues)).map((_notUsed, i) => { + thresholdValues.push(threshold[i] ?? 0); + }); + onChangeSelectedThreshold(thresholdValues); }} options={Object.values(comparators).map(({ text, value }) => { return { text, value }; @@ -123,12 +128,14 @@ export const ThresholdExpression = ({ ) : null} - + 0 || !threshold[i]} + error={errors[`threshold${i}`]} + > 0 || !threshold[i]} onChange={e => { const { value } = e.target; const thresholdVal = value !== '' ? parseFloat(value) : undefined;