Skip to content

Commit 4081635

Browse files
committed
Fixed default message for index threshold includes both threshold values even if not used
1 parent fb81758 commit 4081635

4 files changed

Lines changed: 34 additions & 10 deletions

File tree

x-pack/plugins/triggers_actions_ui/public/application/components/builtin_alert_types/threshold/expression.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const DEFAULT_VALUES = {
5050
THRESHOLD_COMPARATOR: COMPARATORS.GREATER_THAN,
5151
TIME_WINDOW_SIZE: 5,
5252
TIME_WINDOW_UNIT: 'm',
53-
THRESHOLD: [1000, 5000],
53+
THRESHOLD: [1000],
5454
GROUP_BY: 'all',
5555
};
5656

x-pack/plugins/triggers_actions_ui/public/application/components/builtin_alert_types/threshold/index.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import { i18n } from '@kbn/i18n';
77
import { AlertTypeModel, ValidationResult } from '../../../../types';
88
import { IndexThresholdAlertTypeExpression } from './expression';
99
import { IndexThresholdAlertParams } from './types';
10-
import { builtInGroupByTypes, builtInAggregationTypes } from '../../../../common/constants';
10+
import {
11+
builtInGroupByTypes,
12+
builtInAggregationTypes,
13+
builtInComparators,
14+
} from '../../../../common/constants';
1115

1216
export function getAlertType(): AlertTypeModel {
1317
return {
@@ -26,6 +30,7 @@ export function getAlertType(): AlertTypeModel {
2630
termField,
2731
threshold,
2832
timeWindowSize,
33+
thresholdComparator,
2934
} = alertParams;
3035
const validationResult = { errors: {} };
3136
const errors = {
@@ -84,20 +89,32 @@ export function getAlertType(): AlertTypeModel {
8489
)
8590
);
8691
}
87-
if (threshold && threshold.length > 0 && !threshold[0]) {
92+
if (!threshold || threshold.length === 0 || (threshold.length === 1 && !threshold[0])) {
8893
errors.threshold0.push(
8994
i18n.translate('xpack.triggersActionsUI.sections.addAlert.error.requiredThreshold0Text', {
9095
defaultMessage: 'Threshold0, is required.',
9196
})
9297
);
9398
}
94-
if (threshold && threshold.length > 1 && !threshold[1]) {
99+
if (
100+
thresholdComparator &&
101+
builtInComparators[thresholdComparator].requiredValues > 1 &&
102+
(!threshold ||
103+
(threshold && threshold.length < builtInComparators[thresholdComparator!].requiredValues))
104+
) {
95105
errors.threshold1.push(
96106
i18n.translate('xpack.triggersActionsUI.sections.addAlert.error.requiredThreshold1Text', {
97107
defaultMessage: 'Threshold1 is required.',
98108
})
99109
);
100110
}
111+
if (threshold && threshold.length === 2 && threshold[0] > threshold[1]) {
112+
errors.threshold1.push(
113+
i18n.translate('xpack.triggersActionsUI.sections.addAlert.error.requiredThreshold1Text', {
114+
defaultMessage: 'Threshold1 should be > Threshold0.',
115+
})
116+
);
117+
}
101118
return validationResult;
102119
},
103120
defaultActionMessage: 'Alert [{{ctx.metadata.name}}] has exceeded the threshold',

x-pack/plugins/triggers_actions_ui/public/common/expression_items/threshold.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('threshold expression', () => {
1515
const wrapper = shallow(
1616
<ThresholdExpression
1717
thresholdComparator={'between'}
18-
errors={{ threshold0: [] }}
18+
errors={{ threshold0: [], threshold1: [] }}
1919
onChangeSelectedThreshold={onChangeSelectedThreshold}
2020
onChangeSelectedThresholdComparator={onChangeSelectedThresholdComparator}
2121
/>
@@ -59,7 +59,7 @@ describe('threshold expression', () => {
5959
const wrapper = shallow(
6060
<ThresholdExpression
6161
thresholdComparator={'between'}
62-
errors={{ threshold0: [] }}
62+
errors={{ threshold0: [], threshold1: [] }}
6363
onChangeSelectedThreshold={onChangeSelectedThreshold}
6464
onChangeSelectedThresholdComparator={onChangeSelectedThresholdComparator}
6565
/>

x-pack/plugins/triggers_actions_ui/public/common/expression_items/threshold.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ export const ThresholdExpression = ({
105105
value={thresholdComparator}
106106
onChange={e => {
107107
onChangeSelectedThresholdComparator(e.target.value);
108+
const thresholdValues: number[] | undefined = [];
109+
Array.from(Array(comparators[e.target.value].requiredValues)).map((_notUsed, i) => {
110+
thresholdValues.push(threshold[i] ?? 0);
111+
});
112+
onChangeSelectedThreshold(thresholdValues);
108113
}}
109114
options={Object.values(comparators).map(({ text, value }) => {
110115
return { text, value };
@@ -123,12 +128,14 @@ export const ThresholdExpression = ({
123128
</EuiFlexItem>
124129
) : null}
125130
<EuiFlexItem grow={false}>
126-
<EuiFormRow>
131+
<EuiFormRow
132+
isInvalid={errors[`threshold${i}`].length > 0 || !threshold[i]}
133+
error={errors[`threshold${i}`]}
134+
>
127135
<EuiFieldNumber
128136
data-test-subj="alertThresholdInput"
129-
value={!threshold || threshold[i] === null ? 0 : threshold[i]}
130-
min={0}
131-
step={0.1}
137+
value={!threshold || !threshold[i] ? 0 : threshold[i]}
138+
isInvalid={errors[`threshold${i}`].length > 0 || !threshold[i]}
132139
onChange={e => {
133140
const { value } = e.target;
134141
const thresholdVal = value !== '' ? parseFloat(value) : undefined;

0 commit comments

Comments
 (0)