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`] = `
-
+
-
- + Perform action - -
- Per monitor execution + + +
+
+
+
+
+
+
+ +
+ +
+
+
+
+
+
+
+
+ +
+ +
+
+
-
-
+
-
- + Perform action - -
- Per monitor execution + + +
+
+
+
+
+
+
+ +
+ +
+
+
+
+
+
+
+
+ +
+ +
+
+
-
setDisplayPreview(e.target.checked); - const isBucketLevelMonitor = - _.get(context, 'ctx.monitor.monitor_type', MONITOR_TYPE.QUERY_LEVEL) === - MONITOR_TYPE.BUCKET_LEVEL; + const monitorType = _.get(context, 'ctx.monitor.monitor_type', MONITOR_TYPE.QUERY_LEVEL); + const editableActionExecutionPolicy = + monitorType === MONITOR_TYPE.BUCKET_LEVEL || MONITOR_TYPE.DOC_LEVEL; + const actionPath = `${fieldPath}actions.${index}`; - const actionExecutionPolicyPath = isBucketLevelMonitor + const actionExecutionPolicyPath = editableActionExecutionPolicy ? `${actionPath}.action_execution_policy` : actionPath; + const actionableAlertsSelectionsPath = `${actionExecutionPolicyPath}.action_execution_scope.${NOTIFY_OPTIONS_VALUES.PER_ALERT}.actionable_alerts`; - let actionExecutionScopeId = isBucketLevelMonitor + let actionExecutionScopeId = editableActionExecutionPolicy ? _.get( action, 'action_execution_policy.action_execution_scope', @@ -157,24 +159,31 @@ export default function Message( if (!_.isString(actionExecutionScopeId)) actionExecutionScopeId = _.keys(actionExecutionScopeId)[0]; - let actionableAlertsSelections = _.get( - values, - `${actionExecutionPolicyPath}.action_execution_scope.${NOTIFY_OPTIONS_VALUES.PER_ALERT}.actionable_alerts` - ); + let actionableAlertsSelections; + let displayActionableAlertsOptions; + let displayThrottlingSettings; + switch (monitorType) { + case MONITOR_TYPE.BUCKET_LEVEL: + displayActionableAlertsOptions = true; + actionableAlertsSelections = _.get(values, actionableAlertsSelectionsPath); + break; + case MONITOR_TYPE.DOC_LEVEL: + displayActionableAlertsOptions = false; + displayThrottlingSettings = false; + actionableAlertsSelections = []; + break; + default: + displayActionableAlertsOptions = false; + displayThrottlingSettings = actionExecutionScopeId !== NOTIFY_OPTIONS_VALUES.PER_EXECUTION; + } if (actionExecutionScopeId === NOTIFY_OPTIONS_VALUES.PER_ALERT) { - if (_.get(values, `${actionPath}.throttle.value`) === undefined) { + if (_.get(values, `${actionPath}.throttle.value`) === undefined) _.set(values, `${actionPath}.throttle.value`, 10); - } - if (actionableAlertsSelections === undefined) { - _.set( - values, - `${actionExecutionPolicyPath}.action_execution_scope.${NOTIFY_OPTIONS_VALUES.PER_ALERT}.actionable_alerts`, - DEFAULT_ACTIONABLE_ALERTS_SELECTIONS - ); + if (actionableAlertsSelections === undefined) actionableAlertsSelections = DEFAULT_ACTIONABLE_ALERTS_SELECTIONS; - } + _.set(values, actionableAlertsSelectionsPath, actionableAlertsSelections); } let preview = ''; @@ -237,7 +246,7 @@ export default function Message( {renderSendTestMessageButton( index, sendTestMessage, - isBucketLevelMonitor, + monitorType === MONITOR_TYPE.BUCKET_LEVEL, displayPreview, onDisplayPreviewChange, fieldPath @@ -264,7 +273,7 @@ export default function Message( - {isBucketLevelMonitor ? ( + {editableActionExecutionPolicy ? ( Perform action} style={{ maxWidth: '100%' }} @@ -279,9 +288,7 @@ export default function Message( value: NOTIFY_OPTIONS_VALUES.PER_EXECUTION, checked: actionExecutionScopeId === NOTIFY_OPTIONS_VALUES.PER_EXECUTION, label: NOTIFY_OPTIONS_LABELS.PER_EXECUTION, - onChange: (e, field, form) => { - field.onChange(e); - }, + onChange: (e, field, form) => field.onChange(e), }} /> @@ -294,63 +301,49 @@ export default function Message( value: NOTIFY_OPTIONS_VALUES.PER_ALERT, checked: actionExecutionScopeId === NOTIFY_OPTIONS_VALUES.PER_ALERT, label: NOTIFY_OPTIONS_LABELS.PER_ALERT, - onChange: (e, field, form) => { - field.onChange(e); - }, + onChange: (e, field, form) => field.onChange(e), }} /> - - {actionExecutionScopeId === NOTIFY_OPTIONS_VALUES.PER_ALERT ? ( - - + + - { - form.setFieldTouched( - `${actionExecutionPolicyPath}.action_execution_scope.${NOTIFY_OPTIONS_VALUES.PER_ALERT}.actionable_alerts`, - true - ); - }, - onChange: (options, field, form) => { - form.setFieldValue( - `${actionExecutionPolicyPath}.action_execution_scope.${NOTIFY_OPTIONS_VALUES.PER_ALERT}.actionable_alerts`, - options - ); - }, - isClearable: true, - selectedOptions: actionableAlertsSelections, - }} - /> - - - ) : null} - + inputProps={{ + placeholder: 'Select alert options', + options: ACTIONABLE_ALERTS_OPTIONS, + onBlur: (e, field, form) => { + form.setFieldTouched(actionableAlertsSelectionsPath, true); + }, + onChange: (options, field, form) => { + form.setFieldValue(actionableAlertsSelectionsPath, options); + }, + isClearable: true, + selectedOptions: actionableAlertsSelections, + }} + /> + + + ) : null} ) : ( @@ -360,7 +353,7 @@ export default function Message(
)} - {actionExecutionScopeId !== NOTIFY_OPTIONS_VALUES.PER_EXECUTION ? ( + {displayThrottlingSettings ? ( diff --git a/public/pages/CreateTrigger/components/Action/actions/__snapshots__/Message.test.js.snap b/public/pages/CreateTrigger/components/Action/actions/__snapshots__/Message.test.js.snap index 2f7ddbbc7..37749e638 100644 --- a/public/pages/CreateTrigger/components/Action/actions/__snapshots__/Message.test.js.snap +++ b/public/pages/CreateTrigger/components/Action/actions/__snapshots__/Message.test.js.snap @@ -165,24 +165,101 @@ exports[`Message renders 1`] = `
-
+
-
- + Perform action - -
- Per monitor execution + + +
+
+
+
+
+
+
+ +
+ +
+
+
+
+
+
+
+
+ +
+ +
+
+
-
{ const { monitorValues, triggerValues } = this.props; + const { triggerEmptyPrompt } = this.state; const hasTriggers = !_.isEmpty(_.get(triggerValues, 'triggerDefinitions')); const triggerContent = (arrayHelpers, index) => { @@ -331,18 +328,16 @@ class ConfigureTriggers extends React.Component { } }; - return hasTriggers ? ( - triggerValues.triggerDefinitions.map((trigger, index) => { - return ( -
- {triggerContent(triggerArrayHelpers, index)} - -
- ); - }) - ) : ( - - ); + return hasTriggers + ? triggerValues.triggerDefinitions.map((trigger, index) => { + return ( +
+ {triggerContent(triggerArrayHelpers, index)} + +
+ ); + }) + : triggerEmptyPrompt; }; render() { diff --git a/public/pages/CreateTrigger/containers/CreateTrigger/utils/constants.js b/public/pages/CreateTrigger/containers/CreateTrigger/utils/constants.js index 414fffd49..8f92c4636 100644 --- a/public/pages/CreateTrigger/containers/CreateTrigger/utils/constants.js +++ b/public/pages/CreateTrigger/containers/CreateTrigger/utils/constants.js @@ -73,6 +73,11 @@ export const FORMIK_INITIAL_TRIGGER_VALUES = { actions: undefined, }; +export const FORMIK_INITIAL_DOC_LEVEL_SCRIPT = { + lang: FORMIK_INITIAL_TRIGGER_VALUES.script.lang, + source: '(query[name=] || query[name=]) && query[tag=]', +}; + export const HITS_TOTAL_RESULTS_PATH = 'ctx.results[0].hits.total.value'; export const AGGREGATION_RESULTS_PATH = 'ctx.results[0].aggregations.metric.value'; export const ANOMALY_GRADE_RESULT_PATH = 'ctx.results[0].aggregations.max_anomaly_grade.value'; diff --git a/public/pages/CreateTrigger/containers/CreateTrigger/utils/formikToTrigger.js b/public/pages/CreateTrigger/containers/CreateTrigger/utils/formikToTrigger.js index e84e30ecc..7378261ee 100644 --- a/public/pages/CreateTrigger/containers/CreateTrigger/utils/formikToTrigger.js +++ b/public/pages/CreateTrigger/containers/CreateTrigger/utils/formikToTrigger.js @@ -73,7 +73,8 @@ export function formikToBucketLevelTrigger(values, monitorUiMetadata) { export function formikToDocumentLevelTrigger(values, monitorUiMetadata) { const condition = formikToDocumentLevelTriggerCondition(values, monitorUiMetadata); - const actions = formikToAction(values); + // const actions = formikToAction(values); // todo hurneyt + const actions = formikToBucketLevelTriggerAction(values); return { document_level_trigger: { id: values.id, diff --git a/public/pages/CreateTrigger/containers/CreateTrigger/utils/triggerToFormik.js b/public/pages/CreateTrigger/containers/CreateTrigger/utils/triggerToFormik.js index 6e0944be6..4793bc01f 100644 --- a/public/pages/CreateTrigger/containers/CreateTrigger/utils/triggerToFormik.js +++ b/public/pages/CreateTrigger/containers/CreateTrigger/utils/triggerToFormik.js @@ -175,7 +175,7 @@ export function bucketLevelTriggerToFormik(trigger, monitor) { severity, script, bucketSelector, - actions: getBucketLevelTriggerActions(actions), + actions: getExecutionPolicyActions(actions), triggerConditions, minTimeBetweenExecutions, rollingWindowSize, @@ -209,14 +209,14 @@ export function documentLevelTriggerToFormik(trigger, monitor) { name, severity, script, - actions, + actions: getExecutionPolicyActions(actions), minTimeBetweenExecutions, rollingWindowSize, triggerConditions: triggerUiMetadata, }; } -export function getBucketLevelTriggerActions(actions) { +export function getExecutionPolicyActions(actions) { const executionPolicyPath = 'action_execution_policy.action_execution_scope'; return _.cloneDeep(actions).map((action) => { const actionExecutionPolicy = _.get(action, `${executionPolicyPath}`); diff --git a/public/pages/CreateTrigger/utils/helper.js b/public/pages/CreateTrigger/utils/helper.js index d89523f76..c4a100ef6 100644 --- a/public/pages/CreateTrigger/utils/helper.js +++ b/public/pages/CreateTrigger/utils/helper.js @@ -3,8 +3,18 @@ * SPDX-License-Identifier: Apache-2.0 */ +import _ from 'lodash'; import { DESTINATION_TYPE } from '../../Destinations/utils/constants'; -import { BACKEND_CHANNEL_TYPE } from '../../../utils/constants'; +import { BACKEND_CHANNEL_TYPE, MONITOR_TYPE } from '../../../utils/constants'; +import { FORMIK_INITIAL_VALUES } from '../../CreateMonitor/containers/CreateMonitor/utils/constants'; +import { + API_TYPES, + DEFAULT_CLUSTER_METRICS_SCRIPT, +} from '../../CreateMonitor/components/ClusterMetricsMonitor/utils/clusterMetricsMonitorConstants'; +import { + FORMIK_INITIAL_DOC_LEVEL_SCRIPT, + FORMIK_INITIAL_TRIGGER_VALUES, +} from '../containers/CreateTrigger/utils/constants'; export const getChannelOptions = (channels, allowedTypes) => allowedTypes.map((type) => ({ @@ -21,3 +31,18 @@ export const toChannelType = (type) => { return type; }; + +export const getDefaultScript = (monitorValues) => { + const monitorType = _.get(monitorValues, 'monitor_type', FORMIK_INITIAL_VALUES.monitor_type); + switch (monitorType) { + case MONITOR_TYPE.BUCKET_LEVEL: + return FORMIK_INITIAL_TRIGGER_VALUES.bucketSelector; + case MONITOR_TYPE.CLUSTER_METRICS: + const apiType = _.get(monitorValues, 'uri.api_type'); + return _.get(API_TYPES, `${apiType}.defaultCondition`, DEFAULT_CLUSTER_METRICS_SCRIPT); + case MONITOR_TYPE.DOC_LEVEL: + return FORMIK_INITIAL_DOC_LEVEL_SCRIPT; + default: + return FORMIK_INITIAL_TRIGGER_VALUES.script; + } +}; diff --git a/public/pages/CreateTrigger/utils/helper.test.js b/public/pages/CreateTrigger/utils/helper.test.js new file mode 100644 index 000000000..24f9c0687 --- /dev/null +++ b/public/pages/CreateTrigger/utils/helper.test.js @@ -0,0 +1,84 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import _ from 'lodash'; +import { getDefaultScript } from './helper'; +import { MONITOR_TYPE } from '../../../utils/constants'; +import { + FORMIK_INITIAL_DOC_LEVEL_SCRIPT, + FORMIK_INITIAL_TRIGGER_VALUES, +} from '../containers/CreateTrigger/utils/constants'; +import { + API_TYPES, + DEFAULT_CLUSTER_METRICS_SCRIPT, +} from '../../CreateMonitor/components/ClusterMetricsMonitor/utils/clusterMetricsMonitorConstants'; + +describe('CreateTrigger/utils/helper', () => { + describe('getDefaultScript', () => { + test('when monitor_type is undefined', () => { + const monitorValues = undefined; + expect(getDefaultScript(monitorValues)).toEqual(FORMIK_INITIAL_TRIGGER_VALUES.script); + }); + + test(`when monitor_type is ${MONITOR_TYPE.BUCKET_LEVEL}`, () => { + const monitorValues = { monitor_type: MONITOR_TYPE.BUCKET_LEVEL }; + expect(getDefaultScript(monitorValues)).toEqual(FORMIK_INITIAL_TRIGGER_VALUES.bucketSelector); + }); + + test(`when monitor_type is ${MONITOR_TYPE.DOC_LEVEL}`, () => { + const monitorValues = { monitor_type: MONITOR_TYPE.DOC_LEVEL }; + expect(getDefaultScript(monitorValues)).toEqual(FORMIK_INITIAL_DOC_LEVEL_SCRIPT); + }); + + test(`when monitor_type is ${MONITOR_TYPE.QUERY_LEVEL}`, () => { + const monitorValues = { monitor_type: MONITOR_TYPE.QUERY_LEVEL }; + expect(getDefaultScript(monitorValues)).toEqual(FORMIK_INITIAL_TRIGGER_VALUES.script); + }); + + describe(`when monitor_type is ${MONITOR_TYPE.CLUSTER_METRICS}`, () => { + test('and api_type is undefined', () => { + const monitorValues = { + monitor_type: MONITOR_TYPE.CLUSTER_METRICS, + uri: undefined, + }; + expect(getDefaultScript(monitorValues)).toEqual(DEFAULT_CLUSTER_METRICS_SCRIPT); + }); + + test('and api_type is empty', () => { + const monitorValues = { + monitor_type: MONITOR_TYPE.CLUSTER_METRICS, + uri: { + api_type: '', + }, + }; + expect(getDefaultScript(monitorValues)).toEqual(DEFAULT_CLUSTER_METRICS_SCRIPT); + }); + + test('and api_type does not have a default condition', () => { + const monitorValues = { + monitor_type: MONITOR_TYPE.CLUSTER_METRICS, + uri: { + api_type: 'unknownApi', + }, + }; + expect(getDefaultScript(monitorValues)).toEqual(DEFAULT_CLUSTER_METRICS_SCRIPT); + }); + + _.keys(API_TYPES).forEach((apiType) => { + test(`and api_type is ${apiType}`, () => { + const monitorValues = { + monitor_type: MONITOR_TYPE.CLUSTER_METRICS, + uri: { + api_type: apiType, + }, + }; + const expectedOutput = _.get(API_TYPES, `${apiType}.defaultCondition`); + if (!_.isEmpty(expectedOutput)) + expect(getDefaultScript(monitorValues)).toEqual(expectedOutput); + }); + }); + }); + }); +});