From 8657caaffdef9b5b782710f3794e235478a21b52 Mon Sep 17 00:00:00 2001 From: Gidi Meir Morris Date: Tue, 24 Mar 2020 14:51:15 +0000 Subject: [PATCH] allow users to unset the throttle of an alert (#60964) --- .../sections/alert_form/alert_form.tsx | 22 +++- .../apps/triggers_actions_ui/alerts.ts | 102 ++++++++++++++++++ 2 files changed, 121 insertions(+), 3 deletions(-) diff --git a/x-pack/plugins/triggers_actions_ui/public/application/sections/alert_form/alert_form.tsx b/x-pack/plugins/triggers_actions_ui/public/application/sections/alert_form/alert_form.tsx index c6346ba002a7..2c601eeb7564 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/sections/alert_form/alert_form.tsx +++ b/x-pack/plugins/triggers_actions_ui/public/application/sections/alert_form/alert_form.tsx @@ -24,6 +24,8 @@ import { EuiButtonIcon, EuiHorizontalRule, } from '@elastic/eui'; +import { some, filter, map, fold } from 'fp-ts/lib/Option'; +import { pipe } from 'fp-ts/lib/pipeable'; import { getDurationNumberInItsUnit, getDurationUnitValue, @@ -408,9 +410,23 @@ export const AlertForm = ({ name="throttle" data-test-subj="throttleInput" onChange={e => { - const throttle = e.target.value !== '' ? parseInt(e.target.value, 10) : null; - setAlertThrottle(throttle); - setAlertProperty('throttle', `${e.target.value}${alertThrottleUnit}`); + pipe( + some(e.target.value.trim()), + filter(value => value !== ''), + map(value => parseInt(value, 10)), + filter(value => !isNaN(value)), + fold( + () => { + // unset throttle + setAlertThrottle(null); + setAlertProperty('throttle', null); + }, + throttle => { + setAlertThrottle(throttle); + setAlertProperty('throttle', `${throttle}${alertThrottleUnit}`); + } + ) + ); }} /> diff --git a/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/alerts.ts b/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/alerts.ts index 7e5825d88ec1..beedd6390388 100644 --- a/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/alerts.ts +++ b/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/alerts.ts @@ -185,6 +185,108 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => { ]); }); + it('should set an alert throttle', async () => { + const alertName = `edit throttle ${generateUniqueKey()}`; + const createdAlert = await createAlert({ + alertTypeId: '.index-threshold', + name: alertName, + params: { + aggType: 'count', + termSize: 5, + thresholdComparator: '>', + timeWindowSize: 5, + timeWindowUnit: 'm', + groupBy: 'all', + threshold: [1000, 5000], + index: ['.kibana_1'], + timeField: 'alert', + }, + }); + await pageObjects.common.navigateToApp('triggersActions'); + await pageObjects.triggersActionsUI.searchAlerts(createdAlert.name); + + const searchResults = await pageObjects.triggersActionsUI.getAlertsList(); + expect(searchResults).to.eql([ + { + name: createdAlert.name, + tagsText: 'foo, bar', + alertType: 'Index Threshold', + interval: '1m', + }, + ]); + + const editLink = await testSubjects.findAll('alertsTableCell-editLink'); + await editLink[0].click(); + + const throttleInputToSetInitialValue = await testSubjects.find('throttleInput'); + await throttleInputToSetInitialValue.click(); + await throttleInputToSetInitialValue.clearValue(); + await throttleInputToSetInitialValue.type('1'); + + await find.clickByCssSelector('[data-test-subj="saveEditedAlertButton"]:not(disabled)'); + + expect(await pageObjects.common.closeToast()).to.eql(`Updated '${createdAlert.name}'`); + + await pageObjects.common.navigateToApp('triggersActions'); + await pageObjects.triggersActionsUI.searchAlerts(createdAlert.name); + await (await testSubjects.findAll('alertsTableCell-editLink'))[0].click(); + const throttleInput = await testSubjects.find('throttleInput'); + expect(await throttleInput.getAttribute('value')).to.eql('1'); + }); + + it('should unset an alert throttle', async () => { + const alertName = `edit throttle ${generateUniqueKey()}`; + const createdAlert = await createAlert({ + alertTypeId: '.index-threshold', + name: alertName, + throttle: '10m', + params: { + aggType: 'count', + termSize: 5, + thresholdComparator: '>', + timeWindowSize: 5, + timeWindowUnit: 'm', + groupBy: 'all', + threshold: [1000, 5000], + index: ['.kibana_1'], + timeField: 'alert', + }, + }); + await pageObjects.common.navigateToApp('triggersActions'); + await pageObjects.triggersActionsUI.searchAlerts(createdAlert.name); + + const searchResults = await pageObjects.triggersActionsUI.getAlertsList(); + expect(searchResults).to.eql([ + { + name: createdAlert.name, + tagsText: 'foo, bar', + alertType: 'Index Threshold', + interval: '1m', + }, + ]); + + const editLink = await testSubjects.findAll('alertsTableCell-editLink'); + await editLink[0].click(); + + const throttleInputToUnsetValue = await testSubjects.find('throttleInput'); + + expect(await throttleInputToUnsetValue.getAttribute('value')).to.eql('10'); + await throttleInputToUnsetValue.click(); + await throttleInputToUnsetValue.clearValueWithKeyboard(); + + expect(await throttleInputToUnsetValue.getAttribute('value')).to.eql(''); + + await find.clickByCssSelector('[data-test-subj="saveEditedAlertButton"]:not(disabled)'); + + expect(await pageObjects.common.closeToast()).to.eql(`Updated '${createdAlert.name}'`); + + await pageObjects.common.navigateToApp('triggersActions'); + await pageObjects.triggersActionsUI.searchAlerts(createdAlert.name); + await (await testSubjects.findAll('alertsTableCell-editLink'))[0].click(); + const throttleInput = await testSubjects.find('throttleInput'); + expect(await throttleInput.getAttribute('value')).to.eql(''); + }); + it('should reset alert when canceling an edit', async () => { const createdAlert = await createAlert({ alertTypeId: '.index-threshold',