diff --git a/public/pages/CreateMonitor/components/ClusterMetricsMonitor/utils/clusterMetricsMonitorHelpers.js b/public/pages/CreateMonitor/components/ClusterMetricsMonitor/utils/clusterMetricsMonitorHelpers.js index 929f64aad..55ea7752d 100644 --- a/public/pages/CreateMonitor/components/ClusterMetricsMonitor/utils/clusterMetricsMonitorHelpers.js +++ b/public/pages/CreateMonitor/components/ClusterMetricsMonitor/utils/clusterMetricsMonitorHelpers.js @@ -6,16 +6,13 @@ import _ from 'lodash'; import { formikToClusterMetricsInput } from '../../../containers/CreateMonitor/utils/formikToMonitor'; import { - DEFAULT_CLUSTER_METRICS_SCRIPT, + API_TYPES, + GET_API_TYPE_DEBUG_TEXT, ILLEGAL_PATH_PARAMETER_CHARACTERS, + NO_PATH_PARAMS_PLACEHOLDER_TEXT, PATH_PARAMETER_ILLEGAL_CHARACTER_TEXT, PATH_PARAMETERS_REQUIRED_TEXT, - API_TYPES, - NO_PATH_PARAMS_PLACEHOLDER_TEXT, - GET_API_TYPE_DEBUG_TEXT, } from './clusterMetricsMonitorConstants'; -import { SEARCH_TYPE } from '../../../../../utils/constants'; -import { FORMIK_INITIAL_TRIGGER_VALUES } from '../../../../CreateTrigger/containers/CreateTrigger/utils/constants'; import { FORMIK_INITIAL_VALUES } from '../../../containers/CreateMonitor/utils/constants'; export function buildClusterMetricsRequest(values) { @@ -66,17 +63,6 @@ export const getApiTypesRequiringPathParams = () => { return apiList; }; -export const getDefaultScript = (monitorValues) => { - const searchType = _.get(monitorValues, 'searchType', FORMIK_INITIAL_VALUES.searchType); - switch (searchType) { - case SEARCH_TYPE.CLUSTER_METRICS: - const apiType = _.get(monitorValues, 'uri.api_type'); - return _.get(API_TYPES, `${apiType}.defaultCondition`, DEFAULT_CLUSTER_METRICS_SCRIPT); - default: - return FORMIK_INITIAL_TRIGGER_VALUES.script; - } -}; - export const getExamplePathParams = (apiType) => { if (_.isEmpty(apiType)) return NO_PATH_PARAMS_PLACEHOLDER_TEXT; let exampleText = _.get(API_TYPES, `${apiType}.exampleText`, ''); diff --git a/public/pages/CreateMonitor/components/ClusterMetricsMonitor/utils/clusterMetricsMonitorHelpers.test.js b/public/pages/CreateMonitor/components/ClusterMetricsMonitor/utils/clusterMetricsMonitorHelpers.test.js index f2c2cf35f..ee48e8a1e 100644 --- a/public/pages/CreateMonitor/components/ClusterMetricsMonitor/utils/clusterMetricsMonitorHelpers.test.js +++ b/public/pages/CreateMonitor/components/ClusterMetricsMonitor/utils/clusterMetricsMonitorHelpers.test.js @@ -6,7 +6,6 @@ import _ from 'lodash'; import { API_TYPES, - DEFAULT_CLUSTER_METRICS_SCRIPT, GET_API_TYPE_DEBUG_TEXT, ILLEGAL_PATH_PARAMETER_CHARACTERS, NO_PATH_PARAMS_PLACEHOLDER_TEXT, @@ -19,15 +18,12 @@ import { getApiPath, getApiType, getApiTypesRequiringPathParams, - getDefaultScript, getExamplePathParams, isInvalidApiPathParameter, pathParamsContainIllegalCharacters, validateApiPathParameter, } from './clusterMetricsMonitorHelpers'; import { FORMIK_INITIAL_VALUES } from '../../../containers/CreateMonitor/utils/constants'; -import { SEARCH_TYPE } from '../../../../../utils/constants'; -import { FORMIK_INITIAL_TRIGGER_VALUES } from '../../../../CreateTrigger/containers/CreateTrigger/utils/constants'; describe('clusterMetricsMonitorHelpers', () => { describe('buildClusterMetricsRequest', () => { @@ -273,61 +269,6 @@ describe('clusterMetricsMonitorHelpers', () => { }); }); - describe('getDefaultScript', () => { - test('when searchType is undefined', () => { - const monitorValues = undefined; - expect(getDefaultScript(monitorValues)).toEqual(FORMIK_INITIAL_TRIGGER_VALUES.script); - }); - test('when searchType is clusterMetrics and api_type is undefined', () => { - const monitorValues = { - searchType: SEARCH_TYPE.CLUSTER_METRICS, - uri: undefined, - }; - expect(getDefaultScript(monitorValues)).toEqual(DEFAULT_CLUSTER_METRICS_SCRIPT); - }); - test('when searchType is clusterMetrics and api_type is empty', () => { - const monitorValues = { - searchType: SEARCH_TYPE.CLUSTER_METRICS, - uri: { - api_type: '', - }, - }; - expect(getDefaultScript(monitorValues)).toEqual(DEFAULT_CLUSTER_METRICS_SCRIPT); - }); - test('when searchType is clusterMetrics and api_type does not have a default condition', () => { - const monitorValues = { - searchType: SEARCH_TYPE.CLUSTER_METRICS, - uri: { - api_type: 'unknownApi', - }, - }; - expect(getDefaultScript(monitorValues)).toEqual(DEFAULT_CLUSTER_METRICS_SCRIPT); - }); - - _.keys(SEARCH_TYPE).forEach((searchType) => { - test(`when searchType is ${searchType}`, () => { - if (SEARCH_TYPE[searchType] !== SEARCH_TYPE.CLUSTER_METRICS) { - const monitorValues = { searchType: searchType }; - expect(getDefaultScript(monitorValues)).toEqual(FORMIK_INITIAL_TRIGGER_VALUES.script); - } - }); - }); - - _.keys(API_TYPES).forEach((apiType) => { - test(`when searchType is clusterMetrics and api_type is ${apiType}`, () => { - const monitorValues = { - searchType: SEARCH_TYPE.CLUSTER_METRICS, - uri: { - api_type: apiType, - }, - }; - const expectedOutput = _.get(API_TYPES, `${apiType}.defaultCondition`); - if (!_.isEmpty(expectedOutput)) - expect(getDefaultScript(monitorValues)).toEqual(expectedOutput); - }); - }); - }); - describe('getExamplePathParams', () => { test('when apiType has no example text', () => { const apiType = 'apiTypeWithoutExampleText'; diff --git a/public/pages/CreateMonitor/components/MonitorType/MonitorType.js b/public/pages/CreateMonitor/components/MonitorType/MonitorType.js index 983b3472f..1ba2a1ef9 100644 --- a/public/pages/CreateMonitor/components/MonitorType/MonitorType.js +++ b/public/pages/CreateMonitor/components/MonitorType/MonitorType.js @@ -4,7 +4,6 @@ */ import React from 'react'; -import _ from 'lodash'; import { EuiFlexGrid, EuiFlexItem, EuiText } from '@elastic/eui'; import FormikCheckableCard from '../../../../components/FormControls/FormikCheckableCard'; import { MONITOR_TYPE, SEARCH_TYPE } from '../../../../utils/constants'; @@ -23,8 +22,12 @@ const onChangeDefinition = (e, form) => { // Clearing various form fields when changing monitor types. // TODO: Implement modal that confirms the change before clearing. form.setFieldValue('index', FORMIK_INITIAL_VALUES.index); + form.setFieldValue('searchType', FORMIK_INITIAL_VALUES.searchType); form.setFieldValue('triggerDefinitions', FORMIK_INITIAL_TRIGGER_VALUES.triggerConditions); switch (type) { + case MONITOR_TYPE.CLUSTER_METRICS: + form.setFieldValue('searchType', SEARCH_TYPE.CLUSTER_METRICS); + break; case MONITOR_TYPE.DOC_LEVEL: form.setFieldValue('query', DEFAULT_DOCUMENT_LEVEL_QUERY); break; @@ -71,9 +74,7 @@ const MonitorType = ({ values }) => ( label: 'Per query monitor', checked: values.monitor_type === MONITOR_TYPE.QUERY_LEVEL, value: MONITOR_TYPE.QUERY_LEVEL, - onChange: (e, field, form) => { - onChangeDefinition(e, form); - }, + onChange: (e, field, form) => onChangeDefinition(e, form), children: queryLevelDescription, 'data-test-subj': 'queryLevelMonitorRadioCard', }} @@ -89,14 +90,7 @@ const MonitorType = ({ values }) => ( label: 'Per bucket monitor', checked: values.monitor_type === MONITOR_TYPE.BUCKET_LEVEL, value: MONITOR_TYPE.BUCKET_LEVEL, - onChange: (e, field, form) => { - const searchType = _.get(values, 'searchType'); - // Setting search type to graph when changing monitor type from query-level to bucket-level, - // and the search type is not supported by bucket-level monitors. - if (searchType !== SEARCH_TYPE.GRAPH || searchType !== SEARCH_TYPE.QUERY) - form.setFieldValue('searchType', SEARCH_TYPE.GRAPH); - onChangeDefinition(e, form); - }, + onChange: (e, field, form) => onChangeDefinition(e, form), children: bucketLevelDescription, 'data-test-subj': 'bucketLevelMonitorRadioCard', }} @@ -112,10 +106,7 @@ const MonitorType = ({ values }) => ( label: 'Per cluster metrics monitor', checked: values.monitor_type === MONITOR_TYPE.CLUSTER_METRICS, value: MONITOR_TYPE.CLUSTER_METRICS, - onChange: (e, field, form) => { - form.setFieldValue('searchType', SEARCH_TYPE.CLUSTER_METRICS); - onChangeDefinition(e, form); - }, + onChange: (e, field, form) => onChangeDefinition(e, form), children: clusterMetricsDescription, 'data-test-subj': 'clusterMetricsMonitorRadioCard', }} @@ -131,9 +122,7 @@ const MonitorType = ({ values }) => ( label: 'Per document monitor', checked: values.monitor_type === MONITOR_TYPE.DOC_LEVEL, value: MONITOR_TYPE.DOC_LEVEL, - onChange: (e, field, form) => { - onChangeDefinition(e, form); - }, + onChange: (e, field, form) => onChangeDefinition(e, form), children: documentLevelDescription, 'data-test-subj': 'docLevelMonitorRadioCard', }} diff --git a/public/pages/CreateTrigger/components/Action/__snapshots__/Action.test.js.snap b/public/pages/CreateTrigger/components/Action/__snapshots__/Action.test.js.snap index e5ec21cde..64ec296b8 100644 --- a/public/pages/CreateTrigger/components/Action/__snapshots__/Action.test.js.snap +++ b/public/pages/CreateTrigger/components/Action/__snapshots__/Action.test.js.snap @@ -389,24 +389,101 @@ exports[`Action renders with Notifications plugin installed 1`] = `
-