From 988ddad14880886ac7880aca22675276e411b7a5 Mon Sep 17 00:00:00 2001 From: Thomas Hurney Date: Thu, 1 Apr 2021 11:17:46 -0700 Subject: [PATCH 1/5] Implemented support for dropdown options --- .../components/LocalUriInput/LocalUriInput.js | 62 ++++++++++++++++ .../components/LocalUriInput/index.js | 18 +++++ .../MonitorDefinition/MonitorDefinition.js | 3 +- .../CreateMonitor/utils/constants.js | 8 ++ .../CreateMonitor/utils/formikToMonitor.js | 24 +++++- .../CreateMonitor/utils/monitorToFormik.js | 35 +++++++-- .../containers/DefineMonitor/DefineMonitor.js | 73 +++++++++++++++++-- .../DefineMonitor/utils/localUriRequests.js | 23 ++++++ .../containers/CreateTrigger/CreateTrigger.js | 35 ++++++++- .../CreateTrigger/utils/formikToTrigger.js | 4 +- .../MonitorOverview/utils/getOverviewStats.js | 20 ++++- public/utils/constants.js | 1 + 12 files changed, 288 insertions(+), 18 deletions(-) create mode 100644 public/pages/CreateMonitor/components/LocalUriInput/LocalUriInput.js create mode 100644 public/pages/CreateMonitor/components/LocalUriInput/index.js create mode 100644 public/pages/CreateMonitor/containers/DefineMonitor/utils/localUriRequests.js diff --git a/public/pages/CreateMonitor/components/LocalUriInput/LocalUriInput.js b/public/pages/CreateMonitor/components/LocalUriInput/LocalUriInput.js new file mode 100644 index 00000000..95a50eec --- /dev/null +++ b/public/pages/CreateMonitor/components/LocalUriInput/LocalUriInput.js @@ -0,0 +1,62 @@ +/* + * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +import React, { Fragment } from 'react'; +import { EuiSpacer, EuiFlexItem, EuiFlexGroup, EuiCodeEditor, EuiFormRow } from '@elastic/eui'; +import FormikSelect from '../../../../components/FormControls/FormikSelect'; + +const supportedClusterApis = [ + { value: '', text: '' }, + { value: '/_cluster/health', text: 'Define using ClusterHealth endpoint' }, + { value: '/_cluster/stats', text: 'Define using ClusterStats endpoint' }, +]; + +const LocalUriInput = ({ isDarkMode, response, values }) => ( + + + + + { + form.setFieldValue('apiType', e.target.value); + }, + }} + /> + + + + + + + + +); + +export default LocalUriInput; diff --git a/public/pages/CreateMonitor/components/LocalUriInput/index.js b/public/pages/CreateMonitor/components/LocalUriInput/index.js new file mode 100644 index 00000000..799e96cb --- /dev/null +++ b/public/pages/CreateMonitor/components/LocalUriInput/index.js @@ -0,0 +1,18 @@ +/* + * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +import LocalUriImport from './LocalUriInput'; + +export default LocalUriImport; diff --git a/public/pages/CreateMonitor/components/MonitorDefinition/MonitorDefinition.js b/public/pages/CreateMonitor/components/MonitorDefinition/MonitorDefinition.js index 314f0df2..5a427190 100644 --- a/public/pages/CreateMonitor/components/MonitorDefinition/MonitorDefinition.js +++ b/public/pages/CreateMonitor/components/MonitorDefinition/MonitorDefinition.js @@ -20,6 +20,7 @@ import { ES_AD_PLUGIN } from '../../../../utils/constants'; const defaultSelectDefinitions = [ { value: 'graph', text: 'Define using visual graph' }, { value: 'query', text: 'Define using extraction query' }, + { value: 'clusterApi', text: 'Define using Cluster API endpoint' }, ]; const onChangeDefinition = (e, form, resetResponse) => { @@ -28,7 +29,7 @@ const onChangeDefinition = (e, form, resetResponse) => { form.setFieldValue('searchType', type); }; -const selectDefinitions = plugins => { +const selectDefinitions = (plugins) => { return plugins === undefined || plugins.indexOf(ES_AD_PLUGIN) == -1 ? defaultSelectDefinitions : [...defaultSelectDefinitions, { value: 'ad', text: 'Define using anomaly detector' }]; diff --git a/public/pages/CreateMonitor/containers/CreateMonitor/utils/constants.js b/public/pages/CreateMonitor/containers/CreateMonitor/utils/constants.js index 55d159be..fe97884e 100644 --- a/public/pages/CreateMonitor/containers/CreateMonitor/utils/constants.js +++ b/public/pages/CreateMonitor/containers/CreateMonitor/utils/constants.js @@ -33,6 +33,14 @@ export const FORMIK_INITIAL_VALUES = { /* DEFINE MONITOR */ searchType: 'graph', + apiType: '', + uri: { + scheme: 'http', + host: '127.0.0.1', + port: '9200', + path: '', + url: '', + }, index: [], timeField: '', query: MATCH_ALL_QUERY, diff --git a/public/pages/CreateMonitor/containers/CreateMonitor/utils/formikToMonitor.js b/public/pages/CreateMonitor/containers/CreateMonitor/utils/formikToMonitor.js index cecebdc7..b3a2dfba 100644 --- a/public/pages/CreateMonitor/containers/CreateMonitor/utils/formikToMonitor.js +++ b/public/pages/CreateMonitor/containers/CreateMonitor/utils/formikToMonitor.js @@ -28,7 +28,7 @@ export function formikToMonitor(values) { type: 'monitor', enabled: !values.disabled, schedule, - inputs: [formikToSearch(values)], + inputs: [formikToInputs(values)], triggers: [], ui_metadata: { schedule: uiSchedule, @@ -38,8 +38,14 @@ export function formikToMonitor(values) { } export function formikToInputs(values) { - const isAD = values.searchType === SEARCH_TYPE.AD; - return [isAD ? formikToAd(values) : formikToSearch(values)]; + switch (values.searchType) { + case SEARCH_TYPE.AD: + return formikToAd(values); + case SEARCH_TYPE.CLUSTER_API: + return formikToLocalUri(values); + default: + return formikToSearch(values); + } } export function formikToSearch(values) { @@ -99,6 +105,18 @@ export function formikToAd(values) { }, }; } + +export function formikToLocalUri(values) { + return { + uri: { + scheme: 'http', + host: 'localhost', + port: '9200', + path: values.apiType, + }, + }; +} + export function formikToUiSearch(values) { const { searchType, diff --git a/public/pages/CreateMonitor/containers/CreateMonitor/utils/monitorToFormik.js b/public/pages/CreateMonitor/containers/CreateMonitor/utils/monitorToFormik.js index 67fe7e0a..91960fc8 100644 --- a/public/pages/CreateMonitor/containers/CreateMonitor/utils/monitorToFormik.js +++ b/public/pages/CreateMonitor/containers/CreateMonitor/utils/monitorToFormik.js @@ -30,8 +30,35 @@ export default function monitorToFormik(monitor) { } = monitor; // Default searchType to query, because if there is no ui_metadata or search then it was created through API or overwritten by API // In that case we don't want to guess on the UI what selections a user made, so we will default to just showing the extraction query - const { searchType = 'query', fieldName } = search; - const isAD = searchType === SEARCH_TYPE.AD; + let { searchType = 'query', fieldName } = search; + if (_.isEmpty(search) && 'uri' in inputs[0]) searchType = SEARCH_TYPE.CLUSTER_API; + + function inputsToFormik() { + if (searchType === SEARCH_TYPE.CLUSTER_API) { + console.log('HURNEYT: monitorToFormik inputsToFormik inputs = ' + JSON.stringify(inputs)); + console.log('HURNEYT: monitorToFormik inputsToFormik uri = ' + JSON.stringify(inputs[0].uri)); + return { + uri: inputs[0].uri, + }; + } else { + const { + search: { indices, query }, + } = inputs[0]; + if (searchType === SEARCH_TYPE.AD) { + return { + detectorId: _.get(inputs, INPUTS_DETECTOR_ID), + index: indices.map((index) => ({ label: index })), + query: JSON.stringify(query, null, 4), + }; + } else { + // when searchType is Query or Graph + return { + index: indices.map((index) => ({ label: index })), + query: JSON.stringify(query, null, 4), + }; + } + } + } return { /* INITIALIZE WITH DEFAULTS */ @@ -51,8 +78,6 @@ export default function monitorToFormik(monitor) { fieldName: fieldName ? [{ label: fieldName }] : [], timezone: timezone ? [{ label: timezone }] : [], - detectorId: isAD ? _.get(inputs, INPUTS_DETECTOR_ID) : undefined, - index: inputs[0].search.indices.map(index => ({ label: index })), - query: JSON.stringify(inputs[0].search.query, null, 4), + ...inputsToFormik(), }; } diff --git a/public/pages/CreateMonitor/containers/DefineMonitor/DefineMonitor.js b/public/pages/CreateMonitor/containers/DefineMonitor/DefineMonitor.js index 70707057..12c1100d 100644 --- a/public/pages/CreateMonitor/containers/DefineMonitor/DefineMonitor.js +++ b/public/pages/CreateMonitor/containers/DefineMonitor/DefineMonitor.js @@ -31,6 +31,8 @@ import { buildSearchRequest } from './utils/searchRequests'; import { SEARCH_TYPE, ES_AD_PLUGIN } from '../../../../utils/constants'; import AnomalyDetectors from '../AnomalyDetectors/AnomalyDetectors'; import { backendErrorNotification } from '../../../../utils/helpers'; +import LocalUriInput from '../../components/LocalUriInput'; +import { buildLocalUriRequest } from './utils/localUriRequests'; function renderEmptyMessage(message) { return ( @@ -74,6 +76,7 @@ class DefineMonitor extends Component { this.renderVisualMonitor = this.renderVisualMonitor.bind(this); this.renderExtractionQuery = this.renderExtractionQuery.bind(this); this.renderAnomalyDetector = this.renderAnomalyDetector.bind(this); + this.renderLocalUriInput = this.renderLocalUriInput.bind(this); this.getMonitorContent = this.getMonitorContent.bind(this); this.getPlugins = this.getPlugins.bind(this); this.showPluginWarning = this.showPluginWarning.bind(this); @@ -168,31 +171,64 @@ class DefineMonitor extends Component { const { httpClient, values, notifications } = this.props; const formikSnapshot = _.cloneDeep(values); + console.log('HURNEYT: Starting onRunQuery'); + // If we are running a visual graph query, then we need to run two separate queries // 1. The actual query that will be saved on the monitor, to get accurate query performance stats // 2. The UI generated query that gets [BUCKET_COUNT] times the aggregated buckets to show past history of query // If the query is an extraction query, we can use the same query for results and query performance - const searchRequests = [buildSearchRequest(values)]; - if (values.searchType === SEARCH_TYPE.GRAPH) { - searchRequests.push(buildSearchRequest(values, false)); + const searchType = values.searchType; + let requests; + switch (searchType) { + case SEARCH_TYPE.QUERY: + console.log('HURNEYT: Starting onRunQuery - query check'); + requests = [buildSearchRequest(values)]; + break; + case SEARCH_TYPE.GRAPH: + // If we are running a visual graph query, then we need to run two separate queries + // 1. The actual query that will be saved on the monitor, to get accurate query performance stats + // 2. The UI generated query that gets [BUCKET_COUNT] times the aggregated buckets to show past history of query + // If the query is an extraction query, we can use the same query for results and query performance + console.log('HURNEYT: Starting onRunQuery - graph check'); + requests = [buildSearchRequest(values)]; + requests.push(buildSearchRequest(values, false)); + break; + case SEARCH_TYPE.CLUSTER_API: + console.log('HURNEYT: Starting onRunQuery - buildLocalUriRequest'); + requests = [buildLocalUriRequest(values)]; + console.log('HURNEYT: Finishing onRunQuery - buildLocalUriRequest'); + break; } try { - const promises = searchRequests.map((searchRequest) => { + const promises = requests.map((request) => { // Fill in monitor name in case it's empty (in create workflow) // Set triggers to empty array so they are not executed (if in edit workflow) // Set input search to query/graph query and then use execute API to fill in period_start/period_end const monitor = formikToMonitor(values); + console.log('HURNEYT: values.apiType - ' + values.apiType); + console.log('HURNEYT: monitor - ' + JSON.stringify(monitor.inputs[0].search)); _.set(monitor, 'name', 'TEMP_MONITOR'); _.set(monitor, 'triggers', []); - _.set(monitor, 'inputs[0].search', searchRequest); + if (searchType === SEARCH_TYPE.QUERY || searchType === SEARCH_TYPE.GRAPH) { + console.log('HURNEYT: Starting onRunQuery - monitor query or graph check'); + _.set(monitor, 'inputs[0].search', request); + } else if (searchType === SEARCH_TYPE.CLUSTER_API) { + console.log('HURNEYT: Starting onRunQuery - monitor API check'); + _.set(monitor, 'inputs[0].uri', request); + } + console.log('HURNEYT: Starting onRunQuery - httpClient.post'); + console.log('HURNEYT: requests JSON - ' + JSON.stringify(monitor)); return httpClient.post('../api/alerting/monitors/_execute', { body: JSON.stringify(monitor), }); }); + console.log('HURNEYT: Finishing onRunQuery - httpClient.post'); const [queryResponse, optionalResponse] = await Promise.all(promises); + console.log('HURNEYT: DefineMonitor queryResponse = ' + JSON.stringify(queryResponse)); + if (queryResponse.ok) { const response = _.get(queryResponse.resp, 'input_results.results[0]'); // If there is an optionalResponse use it's results, otherwise use the original response @@ -321,6 +357,31 @@ class DefineMonitor extends Component { }; } + renderLocalUriInput() { + const { values } = this.props; + const { response } = this.state; + // Definition of when the "run" button should be disabled for LocalUri type. + console.log('HURNEYT: DefineMonitor renderLocalUriInput = ' + values.apiType); + const runIsDisabled = !values.apiType; + console.log('HURNEYT: DefineMonitor runIsDisabled = ' + runIsDisabled); + return { + actions: [ + + Run + , + ], + content: ( + + + + ), + }; + } + getMonitorContent() { const { values } = this.props; switch (values.searchType) { @@ -328,6 +389,8 @@ class DefineMonitor extends Component { return this.renderAnomalyDetector(); case SEARCH_TYPE.GRAPH: return this.renderVisualMonitor(); + case SEARCH_TYPE.CLUSTER_API: + return this.renderLocalUriInput(); default: return this.renderExtractionQuery(); } diff --git a/public/pages/CreateMonitor/containers/DefineMonitor/utils/localUriRequests.js b/public/pages/CreateMonitor/containers/DefineMonitor/utils/localUriRequests.js new file mode 100644 index 00000000..3593710e --- /dev/null +++ b/public/pages/CreateMonitor/containers/DefineMonitor/utils/localUriRequests.js @@ -0,0 +1,23 @@ +/* + * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +export function buildLocalUriRequest(values) { + return { + scheme: 'http', + host: '127.0.0.1', + port: '9200', + path: values.apiType ? values.apiType : values.uri.path, + }; +} diff --git a/public/pages/CreateTrigger/containers/CreateTrigger/CreateTrigger.js b/public/pages/CreateTrigger/containers/CreateTrigger/CreateTrigger.js index ed749cc1..c60283ef 100644 --- a/public/pages/CreateTrigger/containers/CreateTrigger/CreateTrigger.js +++ b/public/pages/CreateTrigger/containers/CreateTrigger/CreateTrigger.js @@ -42,6 +42,7 @@ import { FORMIK_INITIAL_VALUES } from './utils/constants'; import { SEARCH_TYPE } from '../../../../utils/constants'; import { SubmitErrorHandler } from '../../../../utils/SubmitErrorHandler'; import { backendErrorNotification } from '../../../../utils/helpers'; +import { buildLocalUriRequest } from '../../../CreateMonitor/containers/DefineMonitor/utils/localUriRequests'; export default class CreateTrigger extends Component { constructor(props) { @@ -121,12 +122,29 @@ export default class CreateTrigger extends Component { onRunExecute = (triggers = []) => { const { httpClient, monitor, notifications } = this.props; const formikValues = monitorToFormik(monitor); + const searchType = formikValues.searchType; const monitorToExecute = _.cloneDeep(monitor); _.set(monitorToExecute, 'triggers', triggers); - if (formikValues.searchType !== SEARCH_TYPE.AD) { + + if (searchType === SEARCH_TYPE.QUERY || searchType === SEARCH_TYPE.GRAPH) { const searchRequest = buildSearchRequest(formikValues); _.set(monitorToExecute, 'inputs[0].search', searchRequest); } + if (searchType === SEARCH_TYPE.CLUSTER_API) { + const localUriRequest = buildLocalUriRequest(formikValues); + console.log('HURNEYT: CreateTrigger onRunExecute monitor = ' + JSON.stringify(monitor)); + console.log( + 'HURNEYT: CreateTrigger onRunExecute monitor clone = ' + JSON.stringify(monitorToExecute) + ); + console.log( + 'HURNEYT: CreateTrigger onRunExecute formikValues = ' + JSON.stringify(formikValues) + ); + console.log( + 'HURNEYT: CreateTrigger onRunExecute request = ' + JSON.stringify(localUriRequest) + ); + _.set(monitorToExecute, 'inputs[0].uri', localUriRequest); + } + httpClient .post('../api/alerting/monitors/_execute', { body: JSON.stringify(monitorToExecute) }) .then((resp) => { @@ -190,6 +208,21 @@ export default class CreateTrigger extends Component { monitor: monitor, }); + // overrideInitialValues = () => { + // const { monitor, edit, triggerToEdit } = this.props; + // const { initialValues, executeResponse } = this.state; + // const useTriggerToFormik = edit && triggerToEdit; + // + // // When searchType of the monitor is 'clusterApi', override the default trigger + // // condition with the first name of the name-value pairs in the response + // if (!useTriggerToFormik && 'uri' in monitor.inputs[0]) { + // const response = _.get(executeResponse, 'input_results.results[0]'); + // console.log("HURNEYT: CreateTrigger overrideInitialValues response = " + JSON.stringify(response)) + // _.set(initialValues, 'script.source', 'ctx.results[0].' + _.keys(response)[0] + ' != null'); + // this.setState({ initialValues: initialValues }); + // } + // }; + render() { const { monitor, onCloseTrigger, setFlyout, edit, httpClient, notifications } = this.props; const { initialValues, executeResponse } = this.state; diff --git a/public/pages/CreateTrigger/containers/CreateTrigger/utils/formikToTrigger.js b/public/pages/CreateTrigger/containers/CreateTrigger/utils/formikToTrigger.js index 5b01ba4b..a51cfa7a 100644 --- a/public/pages/CreateTrigger/containers/CreateTrigger/utils/formikToTrigger.js +++ b/public/pages/CreateTrigger/containers/CreateTrigger/utils/formikToTrigger.js @@ -75,8 +75,10 @@ export function formikToCondition(values, monitorUiMetadata = {}) { const searchType = _.get(monitorUiMetadata, 'search.searchType', 'query'); const aggregationType = _.get(monitorUiMetadata, 'search.aggregationType', 'count'); - if (searchType === SEARCH_TYPE.QUERY) return { script: values.script }; + if (searchType === SEARCH_TYPE.QUERY || searchType === SEARCH_TYPE.CLUSTER_API) + return { script: values.script }; if (searchType === SEARCH_TYPE.AD) return getADCondition(values); + const isCount = aggregationType === 'count'; const resultsPath = getResultsPath(isCount); const operator = getOperator(thresholdEnum); diff --git a/public/pages/MonitorDetails/components/MonitorOverview/utils/getOverviewStats.js b/public/pages/MonitorDetails/components/MonitorOverview/utils/getOverviewStats.js index f2f9918d..1a00caaf 100644 --- a/public/pages/MonitorDetails/components/MonitorOverview/utils/getOverviewStats.js +++ b/public/pages/MonitorDetails/components/MonitorOverview/utils/getOverviewStats.js @@ -27,8 +27,24 @@ function getTime(time) { return DEFAULT_EMPTY_DATA; } +function getMonitorType(searchType) { + switch (searchType) { + case SEARCH_TYPE.GRAPH: + return 'Visual Graph'; + case SEARCH_TYPE.AD: + return 'Anomaly Detector'; + case SEARCH_TYPE.CLUSTER_API: + return 'Cluster API'; + default: + return 'Extraction Query'; + } +} + export default function getOverviewStats(monitor, monitorId, monitorVersion, activeCount) { - const searchType = _.get(monitor, 'ui_metadata.search.searchType', 'query'); + let searchType = _.get(monitor, 'ui_metadata.search.searchType', 'query'); + if (_.has(monitor, 'inputs[0].uri')) { + searchType = SEARCH_TYPE.CLUSTER_API; + } return [ { header: 'State', @@ -36,7 +52,7 @@ export default function getOverviewStats(monitor, monitorId, monitorVersion, act }, { header: 'Monitor definition type', - value: searchType === SEARCH_TYPE.QUERY ? 'Extraction Query' : 'Visual graph', + value: getMonitorType(searchType), }, { header: 'Total active alerts', diff --git a/public/utils/constants.js b/public/utils/constants.js index dc8a7650..29a4f6df 100644 --- a/public/utils/constants.js +++ b/public/utils/constants.js @@ -32,6 +32,7 @@ export const SEARCH_TYPE = { GRAPH: 'graph', QUERY: 'query', AD: 'ad', + CLUSTER_API: 'clusterApi', }; export const DESTINATION_ACTIONS = { From 41eb635f67bafd484fbb635796e5b9390b74179b Mon Sep 17 00:00:00 2001 From: Thomas Hurney Date: Fri, 2 Apr 2021 17:18:57 -0700 Subject: [PATCH 2/5] Implemented LocalUriInput monitor creation via path entry field --- .../components/LocalUriInput/LocalUriInput.js | 21 +++++++++---------- .../MonitorDefinition/MonitorDefinition.js | 2 +- .../CreateMonitor/utils/constants.js | 1 + .../CreateMonitor/utils/formikToMonitor.js | 4 ++-- .../CreateMonitor/utils/monitorToFormik.js | 4 ++-- .../containers/DefineMonitor/DefineMonitor.js | 10 ++++----- .../containers/CreateTrigger/CreateTrigger.js | 2 +- .../CreateTrigger/utils/formikToTrigger.js | 2 +- .../MonitorOverview/utils/getOverviewStats.js | 6 +++--- public/utils/constants.js | 2 +- 10 files changed, 27 insertions(+), 27 deletions(-) diff --git a/public/pages/CreateMonitor/components/LocalUriInput/LocalUriInput.js b/public/pages/CreateMonitor/components/LocalUriInput/LocalUriInput.js index 95a50eec..89f41397 100644 --- a/public/pages/CreateMonitor/components/LocalUriInput/LocalUriInput.js +++ b/public/pages/CreateMonitor/components/LocalUriInput/LocalUriInput.js @@ -16,29 +16,28 @@ import React, { Fragment } from 'react'; import { EuiSpacer, EuiFlexItem, EuiFlexGroup, EuiCodeEditor, EuiFormRow } from '@elastic/eui'; import FormikSelect from '../../../../components/FormControls/FormikSelect'; - -const supportedClusterApis = [ - { value: '', text: '' }, - { value: '/_cluster/health', text: 'Define using ClusterHealth endpoint' }, - { value: '/_cluster/stats', text: 'Define using ClusterStats endpoint' }, -]; +import { hasError, isInvalid } from '../../../../utils/validate'; +import { FormikFieldText } from '../../../../components/FormControls'; const LocalUriInput = ({ isDarkMode, response, values }) => ( - { - form.setFieldValue('apiType', e.target.value); + form.setFieldValue('uri.path', e.target.value); }, }} /> diff --git a/public/pages/CreateMonitor/components/MonitorDefinition/MonitorDefinition.js b/public/pages/CreateMonitor/components/MonitorDefinition/MonitorDefinition.js index 5a427190..d66397a3 100644 --- a/public/pages/CreateMonitor/components/MonitorDefinition/MonitorDefinition.js +++ b/public/pages/CreateMonitor/components/MonitorDefinition/MonitorDefinition.js @@ -20,7 +20,7 @@ import { ES_AD_PLUGIN } from '../../../../utils/constants'; const defaultSelectDefinitions = [ { value: 'graph', text: 'Define using visual graph' }, { value: 'query', text: 'Define using extraction query' }, - { value: 'clusterApi', text: 'Define using Cluster API endpoint' }, + { value: 'localUri', text: 'Define using Local URI endpoint' }, ]; const onChangeDefinition = (e, form, resetResponse) => { diff --git a/public/pages/CreateMonitor/containers/CreateMonitor/utils/constants.js b/public/pages/CreateMonitor/containers/CreateMonitor/utils/constants.js index fe97884e..b116700c 100644 --- a/public/pages/CreateMonitor/containers/CreateMonitor/utils/constants.js +++ b/public/pages/CreateMonitor/containers/CreateMonitor/utils/constants.js @@ -33,6 +33,7 @@ export const FORMIK_INITIAL_VALUES = { /* DEFINE MONITOR */ searchType: 'graph', + path: '', apiType: '', uri: { scheme: 'http', diff --git a/public/pages/CreateMonitor/containers/CreateMonitor/utils/formikToMonitor.js b/public/pages/CreateMonitor/containers/CreateMonitor/utils/formikToMonitor.js index b3a2dfba..24188940 100644 --- a/public/pages/CreateMonitor/containers/CreateMonitor/utils/formikToMonitor.js +++ b/public/pages/CreateMonitor/containers/CreateMonitor/utils/formikToMonitor.js @@ -41,7 +41,7 @@ export function formikToInputs(values) { switch (values.searchType) { case SEARCH_TYPE.AD: return formikToAd(values); - case SEARCH_TYPE.CLUSTER_API: + case SEARCH_TYPE.LOCAL_URI: return formikToLocalUri(values); default: return formikToSearch(values); @@ -112,7 +112,7 @@ export function formikToLocalUri(values) { scheme: 'http', host: 'localhost', port: '9200', - path: values.apiType, + path: values.apiType ? values.apiType : values.uri.path, }, }; } diff --git a/public/pages/CreateMonitor/containers/CreateMonitor/utils/monitorToFormik.js b/public/pages/CreateMonitor/containers/CreateMonitor/utils/monitorToFormik.js index 91960fc8..11b8777c 100644 --- a/public/pages/CreateMonitor/containers/CreateMonitor/utils/monitorToFormik.js +++ b/public/pages/CreateMonitor/containers/CreateMonitor/utils/monitorToFormik.js @@ -31,10 +31,10 @@ export default function monitorToFormik(monitor) { // Default searchType to query, because if there is no ui_metadata or search then it was created through API or overwritten by API // In that case we don't want to guess on the UI what selections a user made, so we will default to just showing the extraction query let { searchType = 'query', fieldName } = search; - if (_.isEmpty(search) && 'uri' in inputs[0]) searchType = SEARCH_TYPE.CLUSTER_API; + if (_.isEmpty(search) && 'uri' in inputs[0]) searchType = SEARCH_TYPE.LOCAL_URI; function inputsToFormik() { - if (searchType === SEARCH_TYPE.CLUSTER_API) { + if (searchType === SEARCH_TYPE.LOCAL_URI) { console.log('HURNEYT: monitorToFormik inputsToFormik inputs = ' + JSON.stringify(inputs)); console.log('HURNEYT: monitorToFormik inputsToFormik uri = ' + JSON.stringify(inputs[0].uri)); return { diff --git a/public/pages/CreateMonitor/containers/DefineMonitor/DefineMonitor.js b/public/pages/CreateMonitor/containers/DefineMonitor/DefineMonitor.js index 12c1100d..5c0f677e 100644 --- a/public/pages/CreateMonitor/containers/DefineMonitor/DefineMonitor.js +++ b/public/pages/CreateMonitor/containers/DefineMonitor/DefineMonitor.js @@ -193,7 +193,7 @@ class DefineMonitor extends Component { requests = [buildSearchRequest(values)]; requests.push(buildSearchRequest(values, false)); break; - case SEARCH_TYPE.CLUSTER_API: + case SEARCH_TYPE.LOCAL_URI: console.log('HURNEYT: Starting onRunQuery - buildLocalUriRequest'); requests = [buildLocalUriRequest(values)]; console.log('HURNEYT: Finishing onRunQuery - buildLocalUriRequest'); @@ -213,7 +213,7 @@ class DefineMonitor extends Component { if (searchType === SEARCH_TYPE.QUERY || searchType === SEARCH_TYPE.GRAPH) { console.log('HURNEYT: Starting onRunQuery - monitor query or graph check'); _.set(monitor, 'inputs[0].search', request); - } else if (searchType === SEARCH_TYPE.CLUSTER_API) { + } else if (searchType === SEARCH_TYPE.LOCAL_URI) { console.log('HURNEYT: Starting onRunQuery - monitor API check'); _.set(monitor, 'inputs[0].uri', request); } @@ -361,8 +361,8 @@ class DefineMonitor extends Component { const { values } = this.props; const { response } = this.state; // Definition of when the "run" button should be disabled for LocalUri type. - console.log('HURNEYT: DefineMonitor renderLocalUriInput = ' + values.apiType); - const runIsDisabled = !values.apiType; + console.log('HURNEYT: DefineMonitor renderLocalUriInput = ' + values.uri.path); + const runIsDisabled = !values.uri.path; console.log('HURNEYT: DefineMonitor runIsDisabled = ' + runIsDisabled); return { actions: [ @@ -389,7 +389,7 @@ class DefineMonitor extends Component { return this.renderAnomalyDetector(); case SEARCH_TYPE.GRAPH: return this.renderVisualMonitor(); - case SEARCH_TYPE.CLUSTER_API: + case SEARCH_TYPE.LOCAL_URI: return this.renderLocalUriInput(); default: return this.renderExtractionQuery(); diff --git a/public/pages/CreateTrigger/containers/CreateTrigger/CreateTrigger.js b/public/pages/CreateTrigger/containers/CreateTrigger/CreateTrigger.js index c60283ef..321cc37f 100644 --- a/public/pages/CreateTrigger/containers/CreateTrigger/CreateTrigger.js +++ b/public/pages/CreateTrigger/containers/CreateTrigger/CreateTrigger.js @@ -130,7 +130,7 @@ export default class CreateTrigger extends Component { const searchRequest = buildSearchRequest(formikValues); _.set(monitorToExecute, 'inputs[0].search', searchRequest); } - if (searchType === SEARCH_TYPE.CLUSTER_API) { + if (searchType === SEARCH_TYPE.LOCAL_URI) { const localUriRequest = buildLocalUriRequest(formikValues); console.log('HURNEYT: CreateTrigger onRunExecute monitor = ' + JSON.stringify(monitor)); console.log( diff --git a/public/pages/CreateTrigger/containers/CreateTrigger/utils/formikToTrigger.js b/public/pages/CreateTrigger/containers/CreateTrigger/utils/formikToTrigger.js index a51cfa7a..b7c79f73 100644 --- a/public/pages/CreateTrigger/containers/CreateTrigger/utils/formikToTrigger.js +++ b/public/pages/CreateTrigger/containers/CreateTrigger/utils/formikToTrigger.js @@ -75,7 +75,7 @@ export function formikToCondition(values, monitorUiMetadata = {}) { const searchType = _.get(monitorUiMetadata, 'search.searchType', 'query'); const aggregationType = _.get(monitorUiMetadata, 'search.aggregationType', 'count'); - if (searchType === SEARCH_TYPE.QUERY || searchType === SEARCH_TYPE.CLUSTER_API) + if (searchType === SEARCH_TYPE.QUERY || searchType === SEARCH_TYPE.LOCAL_URI) return { script: values.script }; if (searchType === SEARCH_TYPE.AD) return getADCondition(values); diff --git a/public/pages/MonitorDetails/components/MonitorOverview/utils/getOverviewStats.js b/public/pages/MonitorDetails/components/MonitorOverview/utils/getOverviewStats.js index 1a00caaf..55a2b273 100644 --- a/public/pages/MonitorDetails/components/MonitorOverview/utils/getOverviewStats.js +++ b/public/pages/MonitorDetails/components/MonitorOverview/utils/getOverviewStats.js @@ -33,8 +33,8 @@ function getMonitorType(searchType) { return 'Visual Graph'; case SEARCH_TYPE.AD: return 'Anomaly Detector'; - case SEARCH_TYPE.CLUSTER_API: - return 'Cluster API'; + case SEARCH_TYPE.LOCAL_URI: + return 'Local URI'; default: return 'Extraction Query'; } @@ -43,7 +43,7 @@ function getMonitorType(searchType) { export default function getOverviewStats(monitor, monitorId, monitorVersion, activeCount) { let searchType = _.get(monitor, 'ui_metadata.search.searchType', 'query'); if (_.has(monitor, 'inputs[0].uri')) { - searchType = SEARCH_TYPE.CLUSTER_API; + searchType = SEARCH_TYPE.LOCAL_URI; } return [ { diff --git a/public/utils/constants.js b/public/utils/constants.js index 29a4f6df..faeeee42 100644 --- a/public/utils/constants.js +++ b/public/utils/constants.js @@ -32,7 +32,7 @@ export const SEARCH_TYPE = { GRAPH: 'graph', QUERY: 'query', AD: 'ad', - CLUSTER_API: 'clusterApi', + LOCAL_URI: 'localUri', }; export const DESTINATION_ACTIONS = { From 46231796390a6eea90d3da965673f3e8a82e76d9 Mon Sep 17 00:00:00 2001 From: Thomas Hurney Date: Tue, 6 Apr 2021 19:10:00 -0700 Subject: [PATCH 3/5] Implemented frontend tests --- .../components/LocalUriInput/LocalUriInput.js | 1 - .../LocalUriInput/LocalUriInput.test.js | 30 +++++ .../__snapshots__/LocalUriInput.test.js.snap | 106 ++++++++++++++++++ .../MonitorDefinition.test.js.snap | 5 + .../AnomalyDetector.test.js.snap | 42 +++++++ .../__snapshots__/CreateMonitor.test.js.snap | 7 ++ .../formikToMonitor.test.js.snap | 22 ++++ .../CreateMonitor/utils/constants.js | 4 +- .../CreateMonitor/utils/formikToMonitor.js | 2 +- .../utils/formikToMonitor.test.js | 17 +++ .../CreateMonitor/utils/monitorToFormik.js | 2 - .../utils/monitorToFormik.test.js | 16 ++- .../containers/DefineMonitor/DefineMonitor.js | 17 --- .../__snapshots__/DefineMonitor.test.js.snap | 7 ++ .../DefineMonitor/utils/localUriRequests.js | 2 +- .../__snapshots__/MonitorIndex.test.js.snap | 35 ++++++ .../containers/CreateTrigger/CreateTrigger.js | 39 +++---- .../utils/formikToTrigger.test.js | 7 ++ 18 files changed, 310 insertions(+), 51 deletions(-) create mode 100644 public/pages/CreateMonitor/components/LocalUriInput/LocalUriInput.test.js create mode 100644 public/pages/CreateMonitor/components/LocalUriInput/__snapshots__/LocalUriInput.test.js.snap diff --git a/public/pages/CreateMonitor/components/LocalUriInput/LocalUriInput.js b/public/pages/CreateMonitor/components/LocalUriInput/LocalUriInput.js index 89f41397..a5b79f54 100644 --- a/public/pages/CreateMonitor/components/LocalUriInput/LocalUriInput.js +++ b/public/pages/CreateMonitor/components/LocalUriInput/LocalUriInput.js @@ -15,7 +15,6 @@ import React, { Fragment } from 'react'; import { EuiSpacer, EuiFlexItem, EuiFlexGroup, EuiCodeEditor, EuiFormRow } from '@elastic/eui'; -import FormikSelect from '../../../../components/FormControls/FormikSelect'; import { hasError, isInvalid } from '../../../../utils/validate'; import { FormikFieldText } from '../../../../components/FormControls'; diff --git a/public/pages/CreateMonitor/components/LocalUriInput/LocalUriInput.test.js b/public/pages/CreateMonitor/components/LocalUriInput/LocalUriInput.test.js new file mode 100644 index 00000000..b02bcb00 --- /dev/null +++ b/public/pages/CreateMonitor/components/LocalUriInput/LocalUriInput.test.js @@ -0,0 +1,30 @@ +/* + * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ +import React from 'react'; +import { render } from 'enzyme'; +import { Formik } from 'formik'; + +import LocalUriInput from './LocalUriInput'; + +describe('LocalUriInput', () => { + test('renders', () => { + const component = ( + + + + ); + expect(render(component)).toMatchSnapshot(); + }); +}); diff --git a/public/pages/CreateMonitor/components/LocalUriInput/__snapshots__/LocalUriInput.test.js.snap b/public/pages/CreateMonitor/components/LocalUriInput/__snapshots__/LocalUriInput.test.js.snap new file mode 100644 index 00000000..62d6e06e --- /dev/null +++ b/public/pages/CreateMonitor/components/LocalUriInput/__snapshots__/LocalUriInput.test.js.snap @@ -0,0 +1,106 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`LocalUriInput renders 1`] = ` +
+
+
+
+
+ +
+
+
+
+ +
+
+
+ Example path: "/_cluster/health/" +
+
+
+
+
+
+
+ +
+
+
+
+

+ Press Enter to start editing. +

+

+ When you're done, press Escape to stop editing. +

+
+
+
+
+
+
+
+`; diff --git a/public/pages/CreateMonitor/components/MonitorDefinition/__snapshots__/MonitorDefinition.test.js.snap b/public/pages/CreateMonitor/components/MonitorDefinition/__snapshots__/MonitorDefinition.test.js.snap index 45264816..2d0e3b2f 100644 --- a/public/pages/CreateMonitor/components/MonitorDefinition/__snapshots__/MonitorDefinition.test.js.snap +++ b/public/pages/CreateMonitor/components/MonitorDefinition/__snapshots__/MonitorDefinition.test.js.snap @@ -40,6 +40,11 @@ exports[`MonitorDefinition renders 1`] = ` > Define using extraction query +
{ }); }); +describe('formikToInputs', () => { + const formikValues = _.cloneDeep(FORMIK_INITIAL_VALUES); + test('can call formikToLocalUri', () => { + formikValues.searchType = 'localUri'; + expect(formikToInputs(formikValues)).toMatchSnapshot(); + }); +}); + describe('formikToDetector', () => { const formikValues = _.cloneDeep(FORMIK_INITIAL_VALUES); formikValues.detectorId = 'temp_detector'; @@ -59,6 +69,13 @@ describe('formikToDetector', () => { }); }); +describe('formikToLocalUri', () => { + test('can build a LocalUriInput request', () => { + const formikValues = _.cloneDeep(FORMIK_INITIAL_VALUES); + expect(formikToLocalUri(formikValues)).toMatchSnapshot(); + }); +}); + describe('formikToUiSearch', () => { const formikValues = _.cloneDeep(FORMIK_INITIAL_VALUES); formikValues.fieldName = [{ label: 'bytes' }]; diff --git a/public/pages/CreateMonitor/containers/CreateMonitor/utils/monitorToFormik.js b/public/pages/CreateMonitor/containers/CreateMonitor/utils/monitorToFormik.js index 11b8777c..999afaeb 100644 --- a/public/pages/CreateMonitor/containers/CreateMonitor/utils/monitorToFormik.js +++ b/public/pages/CreateMonitor/containers/CreateMonitor/utils/monitorToFormik.js @@ -35,8 +35,6 @@ export default function monitorToFormik(monitor) { function inputsToFormik() { if (searchType === SEARCH_TYPE.LOCAL_URI) { - console.log('HURNEYT: monitorToFormik inputsToFormik inputs = ' + JSON.stringify(inputs)); - console.log('HURNEYT: monitorToFormik inputsToFormik uri = ' + JSON.stringify(inputs[0].uri)); return { uri: inputs[0].uri, }; diff --git a/public/pages/CreateMonitor/containers/CreateMonitor/utils/monitorToFormik.test.js b/public/pages/CreateMonitor/containers/CreateMonitor/utils/monitorToFormik.test.js index b3d7ac56..bd8e2bd3 100644 --- a/public/pages/CreateMonitor/containers/CreateMonitor/utils/monitorToFormik.test.js +++ b/public/pages/CreateMonitor/containers/CreateMonitor/utils/monitorToFormik.test.js @@ -90,7 +90,7 @@ describe('monitorToFormik', () => { JSON.stringify(exampleMonitor.inputs[0].search.query, null, 4) ); expect(formikValues.index).toEqual( - exampleMonitor.inputs[0].search.indices.map(index => ({ label: index })) + exampleMonitor.inputs[0].search.indices.map((index) => ({ label: index })) ); }); @@ -161,4 +161,18 @@ describe('monitorToFormik', () => { expect(formikValues.query).toContain('zIqG0nABwoJjo1UZKHnL'); }); }); + + test('can build LocalUriInput monitor', () => { + const localUriMonitor = _.cloneDeep(exampleMonitor); + localUriMonitor.ui_metadata.search.searchType = 'localUri'; + localUriMonitor.inputs = [ + { + uri: { + path: '/_cluster/health', + }, + }, + ]; + const formikValues = monitorToFormik(localUriMonitor); + expect(formikValues.uri.path).toBe('/_cluster/health'); + }); }); diff --git a/public/pages/CreateMonitor/containers/DefineMonitor/DefineMonitor.js b/public/pages/CreateMonitor/containers/DefineMonitor/DefineMonitor.js index 5c0f677e..213cb4e7 100644 --- a/public/pages/CreateMonitor/containers/DefineMonitor/DefineMonitor.js +++ b/public/pages/CreateMonitor/containers/DefineMonitor/DefineMonitor.js @@ -171,8 +171,6 @@ class DefineMonitor extends Component { const { httpClient, values, notifications } = this.props; const formikSnapshot = _.cloneDeep(values); - console.log('HURNEYT: Starting onRunQuery'); - // If we are running a visual graph query, then we need to run two separate queries // 1. The actual query that will be saved on the monitor, to get accurate query performance stats // 2. The UI generated query that gets [BUCKET_COUNT] times the aggregated buckets to show past history of query @@ -181,7 +179,6 @@ class DefineMonitor extends Component { let requests; switch (searchType) { case SEARCH_TYPE.QUERY: - console.log('HURNEYT: Starting onRunQuery - query check'); requests = [buildSearchRequest(values)]; break; case SEARCH_TYPE.GRAPH: @@ -189,14 +186,11 @@ class DefineMonitor extends Component { // 1. The actual query that will be saved on the monitor, to get accurate query performance stats // 2. The UI generated query that gets [BUCKET_COUNT] times the aggregated buckets to show past history of query // If the query is an extraction query, we can use the same query for results and query performance - console.log('HURNEYT: Starting onRunQuery - graph check'); requests = [buildSearchRequest(values)]; requests.push(buildSearchRequest(values, false)); break; case SEARCH_TYPE.LOCAL_URI: - console.log('HURNEYT: Starting onRunQuery - buildLocalUriRequest'); requests = [buildLocalUriRequest(values)]; - console.log('HURNEYT: Finishing onRunQuery - buildLocalUriRequest'); break; } @@ -206,29 +200,20 @@ class DefineMonitor extends Component { // Set triggers to empty array so they are not executed (if in edit workflow) // Set input search to query/graph query and then use execute API to fill in period_start/period_end const monitor = formikToMonitor(values); - console.log('HURNEYT: values.apiType - ' + values.apiType); - console.log('HURNEYT: monitor - ' + JSON.stringify(monitor.inputs[0].search)); _.set(monitor, 'name', 'TEMP_MONITOR'); _.set(monitor, 'triggers', []); if (searchType === SEARCH_TYPE.QUERY || searchType === SEARCH_TYPE.GRAPH) { - console.log('HURNEYT: Starting onRunQuery - monitor query or graph check'); _.set(monitor, 'inputs[0].search', request); } else if (searchType === SEARCH_TYPE.LOCAL_URI) { - console.log('HURNEYT: Starting onRunQuery - monitor API check'); _.set(monitor, 'inputs[0].uri', request); } - console.log('HURNEYT: Starting onRunQuery - httpClient.post'); - console.log('HURNEYT: requests JSON - ' + JSON.stringify(monitor)); return httpClient.post('../api/alerting/monitors/_execute', { body: JSON.stringify(monitor), }); }); - console.log('HURNEYT: Finishing onRunQuery - httpClient.post'); const [queryResponse, optionalResponse] = await Promise.all(promises); - console.log('HURNEYT: DefineMonitor queryResponse = ' + JSON.stringify(queryResponse)); - if (queryResponse.ok) { const response = _.get(queryResponse.resp, 'input_results.results[0]'); // If there is an optionalResponse use it's results, otherwise use the original response @@ -361,9 +346,7 @@ class DefineMonitor extends Component { const { values } = this.props; const { response } = this.state; // Definition of when the "run" button should be disabled for LocalUri type. - console.log('HURNEYT: DefineMonitor renderLocalUriInput = ' + values.uri.path); const runIsDisabled = !values.uri.path; - console.log('HURNEYT: DefineMonitor runIsDisabled = ' + runIsDisabled); return { actions: [ diff --git a/public/pages/CreateMonitor/containers/DefineMonitor/__snapshots__/DefineMonitor.test.js.snap b/public/pages/CreateMonitor/containers/DefineMonitor/__snapshots__/DefineMonitor.test.js.snap index 78d409d8..ad838952 100644 --- a/public/pages/CreateMonitor/containers/DefineMonitor/__snapshots__/DefineMonitor.test.js.snap +++ b/public/pages/CreateMonitor/containers/DefineMonitor/__snapshots__/DefineMonitor.test.js.snap @@ -129,6 +129,13 @@ exports[`DefineMonitor should show warning in case of Ad monitor and plugin is n "searchType": "ad", "timeField": "", "timezone": Array [], + "uri": Object { + "host": "localhost", + "path": "", + "port": "9200", + "scheme": "http", + "url": "", + }, "weekly": Object { "fri": false, "mon": false, diff --git a/public/pages/CreateMonitor/containers/DefineMonitor/utils/localUriRequests.js b/public/pages/CreateMonitor/containers/DefineMonitor/utils/localUriRequests.js index 3593710e..8543b80d 100644 --- a/public/pages/CreateMonitor/containers/DefineMonitor/utils/localUriRequests.js +++ b/public/pages/CreateMonitor/containers/DefineMonitor/utils/localUriRequests.js @@ -18,6 +18,6 @@ export function buildLocalUriRequest(values) { scheme: 'http', host: '127.0.0.1', port: '9200', - path: values.apiType ? values.apiType : values.uri.path, + path: values.uri.path, }; } diff --git a/public/pages/CreateMonitor/containers/MonitorIndex/__snapshots__/MonitorIndex.test.js.snap b/public/pages/CreateMonitor/containers/MonitorIndex/__snapshots__/MonitorIndex.test.js.snap index f7ce8dce..67f18f94 100644 --- a/public/pages/CreateMonitor/containers/MonitorIndex/__snapshots__/MonitorIndex.test.js.snap +++ b/public/pages/CreateMonitor/containers/MonitorIndex/__snapshots__/MonitorIndex.test.js.snap @@ -35,6 +35,13 @@ exports[`MonitorIndex renders 1`] = ` "searchType": "graph", "timeField": "", "timezone": Array [], + "uri": Object { + "host": "localhost", + "path": "", + "port": "9200", + "scheme": "http", + "url": "", + }, "weekly": Object { "fri": false, "mon": false, @@ -161,6 +168,13 @@ exports[`MonitorIndex renders 1`] = ` "searchType": "graph", "timeField": "", "timezone": Array [], + "uri": Object { + "host": "localhost", + "path": "", + "port": "9200", + "scheme": "http", + "url": "", + }, "weekly": Object { "fri": false, "mon": false, @@ -234,6 +248,13 @@ exports[`MonitorIndex renders 1`] = ` "searchType": "graph", "timeField": "", "timezone": Array [], + "uri": Object { + "host": "localhost", + "path": "", + "port": "9200", + "scheme": "http", + "url": "", + }, "weekly": Object { "fri": false, "mon": false, @@ -371,6 +392,13 @@ exports[`MonitorIndex renders 1`] = ` "searchType": "graph", "timeField": "", "timezone": Array [], + "uri": Object { + "host": "localhost", + "path": "", + "port": "9200", + "scheme": "http", + "url": "", + }, "weekly": Object { "fri": false, "mon": false, @@ -444,6 +472,13 @@ exports[`MonitorIndex renders 1`] = ` "searchType": "graph", "timeField": "", "timezone": Array [], + "uri": Object { + "host": "localhost", + "path": "", + "port": "9200", + "scheme": "http", + "url": "", + }, "weekly": Object { "fri": false, "mon": false, diff --git a/public/pages/CreateTrigger/containers/CreateTrigger/CreateTrigger.js b/public/pages/CreateTrigger/containers/CreateTrigger/CreateTrigger.js index 321cc37f..e0adbac9 100644 --- a/public/pages/CreateTrigger/containers/CreateTrigger/CreateTrigger.js +++ b/public/pages/CreateTrigger/containers/CreateTrigger/CreateTrigger.js @@ -132,16 +132,6 @@ export default class CreateTrigger extends Component { } if (searchType === SEARCH_TYPE.LOCAL_URI) { const localUriRequest = buildLocalUriRequest(formikValues); - console.log('HURNEYT: CreateTrigger onRunExecute monitor = ' + JSON.stringify(monitor)); - console.log( - 'HURNEYT: CreateTrigger onRunExecute monitor clone = ' + JSON.stringify(monitorToExecute) - ); - console.log( - 'HURNEYT: CreateTrigger onRunExecute formikValues = ' + JSON.stringify(formikValues) - ); - console.log( - 'HURNEYT: CreateTrigger onRunExecute request = ' + JSON.stringify(localUriRequest) - ); _.set(monitorToExecute, 'inputs[0].uri', localUriRequest); } @@ -149,7 +139,7 @@ export default class CreateTrigger extends Component { .post('../api/alerting/monitors/_execute', { body: JSON.stringify(monitorToExecute) }) .then((resp) => { if (resp.ok) { - this.setState({ executeResponse: resp.resp }); + this.setState({ executeResponse: resp.resp }, this.overrideInitialValues); } else { // TODO: need a notification system to show errors or banners at top console.error('err:', resp); @@ -208,20 +198,19 @@ export default class CreateTrigger extends Component { monitor: monitor, }); - // overrideInitialValues = () => { - // const { monitor, edit, triggerToEdit } = this.props; - // const { initialValues, executeResponse } = this.state; - // const useTriggerToFormik = edit && triggerToEdit; - // - // // When searchType of the monitor is 'clusterApi', override the default trigger - // // condition with the first name of the name-value pairs in the response - // if (!useTriggerToFormik && 'uri' in monitor.inputs[0]) { - // const response = _.get(executeResponse, 'input_results.results[0]'); - // console.log("HURNEYT: CreateTrigger overrideInitialValues response = " + JSON.stringify(response)) - // _.set(initialValues, 'script.source', 'ctx.results[0].' + _.keys(response)[0] + ' != null'); - // this.setState({ initialValues: initialValues }); - // } - // }; + overrideInitialValues = () => { + const { monitor, edit, triggerToEdit } = this.props; + const { initialValues, executeResponse } = this.state; + const useTriggerToFormik = edit && triggerToEdit; + + // When searchType of the monitor is 'localUri', override the default trigger + // condition with the first name of the name-value pairs in the response + if (!useTriggerToFormik && 'uri' in monitor.inputs[0]) { + const response = _.get(executeResponse, 'input_results.results[0]'); + _.set(initialValues, 'script.source', 'ctx.results[0].' + _.keys(response)[0] + ' != null'); + this.setState({ initialValues: initialValues }); + } + }; render() { const { monitor, onCloseTrigger, setFlyout, edit, httpClient, notifications } = this.props; diff --git a/public/pages/CreateTrigger/containers/CreateTrigger/utils/formikToTrigger.test.js b/public/pages/CreateTrigger/containers/CreateTrigger/utils/formikToTrigger.test.js index 873f7f7e..3031b137 100644 --- a/public/pages/CreateTrigger/containers/CreateTrigger/utils/formikToTrigger.test.js +++ b/public/pages/CreateTrigger/containers/CreateTrigger/utils/formikToTrigger.test.js @@ -78,6 +78,13 @@ describe('formikToCondition', () => { }); }); + test('can return condition when searchType is localUri', () => { + const formikValues = _.cloneDeep(FORMIK_INITIAL_VALUES); + expect(formikToCondition(formikValues, { search: { searchType: 'localUri' } })).toEqual({ + script: formikValues.script, + }); + }); + test('can return condition when searchType is ad', () => { const formikValues = _.cloneDeep(FORMIK_INITIAL_VALUES); expect(formikToCondition(formikValues, { search: { searchType: 'ad' } })).toEqual({ From 997a231fb187f1e8c7f649d051b0c3bae15e5fdd Mon Sep 17 00:00:00 2001 From: Thomas Hurney Date: Fri, 16 Apr 2021 10:09:56 -0700 Subject: [PATCH 4/5] Refactored default LocalUriInput host value to localhost --- .../containers/DefineMonitor/utils/localUriRequests.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/pages/CreateMonitor/containers/DefineMonitor/utils/localUriRequests.js b/public/pages/CreateMonitor/containers/DefineMonitor/utils/localUriRequests.js index 8543b80d..c711172a 100644 --- a/public/pages/CreateMonitor/containers/DefineMonitor/utils/localUriRequests.js +++ b/public/pages/CreateMonitor/containers/DefineMonitor/utils/localUriRequests.js @@ -16,7 +16,7 @@ export function buildLocalUriRequest(values) { return { scheme: 'http', - host: '127.0.0.1', + host: 'localhost', port: '9200', path: values.uri.path, }; From 94b93a90e785c4bdb49847f02c35c15376105126 Mon Sep 17 00:00:00 2001 From: Thomas Hurney Date: Mon, 19 Apr 2021 16:38:25 -0700 Subject: [PATCH 5/5] Updated copyright year in modified files, removed some duplicated comments, and refactored help text based on PR feedback. --- .../CreateMonitor/components/LocalUriInput/LocalUriInput.js | 5 +++-- .../components/LocalUriInput/LocalUriInput.test.js | 2 +- .../LocalUriInput/__snapshots__/LocalUriInput.test.js.snap | 2 +- .../components/MonitorDefinition/MonitorDefinition.js | 2 +- .../containers/CreateMonitor/utils/constants.js | 2 +- .../containers/CreateMonitor/utils/formikToMonitor.js | 2 +- .../containers/CreateMonitor/utils/monitorToFormik.js | 2 +- .../containers/CreateMonitor/utils/monitorToFormik.test.js | 2 +- .../CreateMonitor/containers/DefineMonitor/DefineMonitor.js | 6 +----- .../containers/DefineMonitor/utils/localUriRequests.js | 2 +- .../CreateTrigger/containers/CreateTrigger/CreateTrigger.js | 2 +- .../containers/CreateTrigger/utils/formikToTrigger.js | 2 +- .../containers/CreateTrigger/utils/formikToTrigger.test.js | 2 +- .../components/MonitorOverview/utils/getOverviewStats.js | 2 +- public/utils/constants.js | 2 +- 15 files changed, 17 insertions(+), 20 deletions(-) diff --git a/public/pages/CreateMonitor/components/LocalUriInput/LocalUriInput.js b/public/pages/CreateMonitor/components/LocalUriInput/LocalUriInput.js index a5b79f54..118f8c4f 100644 --- a/public/pages/CreateMonitor/components/LocalUriInput/LocalUriInput.js +++ b/public/pages/CreateMonitor/components/LocalUriInput/LocalUriInput.js @@ -1,5 +1,5 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. @@ -28,7 +28,8 @@ const LocalUriInput = ({ isDarkMode, response, values }) => ( formRow rowProps={{ label: 'Path', - helpText: 'Example path: "/_cluster/health/"', + helpText: + 'The path associated with the REST API the monitor should call (e.g., "/_cluster/health").', style: { paddingLeft: '10px' }, isInvalid, error: hasError, diff --git a/public/pages/CreateMonitor/components/LocalUriInput/LocalUriInput.test.js b/public/pages/CreateMonitor/components/LocalUriInput/LocalUriInput.test.js index b02bcb00..60537ab9 100644 --- a/public/pages/CreateMonitor/components/LocalUriInput/LocalUriInput.test.js +++ b/public/pages/CreateMonitor/components/LocalUriInput/LocalUriInput.test.js @@ -1,5 +1,5 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. diff --git a/public/pages/CreateMonitor/components/LocalUriInput/__snapshots__/LocalUriInput.test.js.snap b/public/pages/CreateMonitor/components/LocalUriInput/__snapshots__/LocalUriInput.test.js.snap index 62d6e06e..476234f0 100644 --- a/public/pages/CreateMonitor/components/LocalUriInput/__snapshots__/LocalUriInput.test.js.snap +++ b/public/pages/CreateMonitor/components/LocalUriInput/__snapshots__/LocalUriInput.test.js.snap @@ -46,7 +46,7 @@ exports[`LocalUriInput renders 1`] = ` class="euiFormHelpText euiFormRow__text" id="localUri.path-form-row-help" > - Example path: "/_cluster/health/" + The path associated with the REST API the monitor should call (e.g., "/_cluster/health").
diff --git a/public/pages/CreateMonitor/components/MonitorDefinition/MonitorDefinition.js b/public/pages/CreateMonitor/components/MonitorDefinition/MonitorDefinition.js index d66397a3..2a3ba69b 100644 --- a/public/pages/CreateMonitor/components/MonitorDefinition/MonitorDefinition.js +++ b/public/pages/CreateMonitor/components/MonitorDefinition/MonitorDefinition.js @@ -1,5 +1,5 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. diff --git a/public/pages/CreateMonitor/containers/CreateMonitor/utils/constants.js b/public/pages/CreateMonitor/containers/CreateMonitor/utils/constants.js index bc41bfd8..796c96f7 100644 --- a/public/pages/CreateMonitor/containers/CreateMonitor/utils/constants.js +++ b/public/pages/CreateMonitor/containers/CreateMonitor/utils/constants.js @@ -1,7 +1,7 @@ import { OPERATORS_MAP } from '../../../components/MonitorExpressions/expressions/utils/constants'; /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. diff --git a/public/pages/CreateMonitor/containers/CreateMonitor/utils/formikToMonitor.js b/public/pages/CreateMonitor/containers/CreateMonitor/utils/formikToMonitor.js index 23c33e3a..8802b4e9 100644 --- a/public/pages/CreateMonitor/containers/CreateMonitor/utils/formikToMonitor.js +++ b/public/pages/CreateMonitor/containers/CreateMonitor/utils/formikToMonitor.js @@ -1,5 +1,5 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. diff --git a/public/pages/CreateMonitor/containers/CreateMonitor/utils/monitorToFormik.js b/public/pages/CreateMonitor/containers/CreateMonitor/utils/monitorToFormik.js index 999afaeb..97ffaa29 100644 --- a/public/pages/CreateMonitor/containers/CreateMonitor/utils/monitorToFormik.js +++ b/public/pages/CreateMonitor/containers/CreateMonitor/utils/monitorToFormik.js @@ -1,5 +1,5 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. diff --git a/public/pages/CreateMonitor/containers/CreateMonitor/utils/monitorToFormik.test.js b/public/pages/CreateMonitor/containers/CreateMonitor/utils/monitorToFormik.test.js index bd8e2bd3..879d5d04 100644 --- a/public/pages/CreateMonitor/containers/CreateMonitor/utils/monitorToFormik.test.js +++ b/public/pages/CreateMonitor/containers/CreateMonitor/utils/monitorToFormik.test.js @@ -1,5 +1,5 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. diff --git a/public/pages/CreateMonitor/containers/DefineMonitor/DefineMonitor.js b/public/pages/CreateMonitor/containers/DefineMonitor/DefineMonitor.js index 213cb4e7..6209ab60 100644 --- a/public/pages/CreateMonitor/containers/DefineMonitor/DefineMonitor.js +++ b/public/pages/CreateMonitor/containers/DefineMonitor/DefineMonitor.js @@ -1,5 +1,5 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. @@ -171,10 +171,6 @@ class DefineMonitor extends Component { const { httpClient, values, notifications } = this.props; const formikSnapshot = _.cloneDeep(values); - // If we are running a visual graph query, then we need to run two separate queries - // 1. The actual query that will be saved on the monitor, to get accurate query performance stats - // 2. The UI generated query that gets [BUCKET_COUNT] times the aggregated buckets to show past history of query - // If the query is an extraction query, we can use the same query for results and query performance const searchType = values.searchType; let requests; switch (searchType) { diff --git a/public/pages/CreateMonitor/containers/DefineMonitor/utils/localUriRequests.js b/public/pages/CreateMonitor/containers/DefineMonitor/utils/localUriRequests.js index c711172a..0e4050e7 100644 --- a/public/pages/CreateMonitor/containers/DefineMonitor/utils/localUriRequests.js +++ b/public/pages/CreateMonitor/containers/DefineMonitor/utils/localUriRequests.js @@ -1,5 +1,5 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. diff --git a/public/pages/CreateTrigger/containers/CreateTrigger/CreateTrigger.js b/public/pages/CreateTrigger/containers/CreateTrigger/CreateTrigger.js index e0adbac9..b000d1c2 100644 --- a/public/pages/CreateTrigger/containers/CreateTrigger/CreateTrigger.js +++ b/public/pages/CreateTrigger/containers/CreateTrigger/CreateTrigger.js @@ -1,5 +1,5 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. diff --git a/public/pages/CreateTrigger/containers/CreateTrigger/utils/formikToTrigger.js b/public/pages/CreateTrigger/containers/CreateTrigger/utils/formikToTrigger.js index b7c79f73..63cb6eaa 100644 --- a/public/pages/CreateTrigger/containers/CreateTrigger/utils/formikToTrigger.js +++ b/public/pages/CreateTrigger/containers/CreateTrigger/utils/formikToTrigger.js @@ -1,5 +1,5 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. diff --git a/public/pages/CreateTrigger/containers/CreateTrigger/utils/formikToTrigger.test.js b/public/pages/CreateTrigger/containers/CreateTrigger/utils/formikToTrigger.test.js index 3031b137..2368a964 100644 --- a/public/pages/CreateTrigger/containers/CreateTrigger/utils/formikToTrigger.test.js +++ b/public/pages/CreateTrigger/containers/CreateTrigger/utils/formikToTrigger.test.js @@ -1,5 +1,5 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. diff --git a/public/pages/MonitorDetails/components/MonitorOverview/utils/getOverviewStats.js b/public/pages/MonitorDetails/components/MonitorOverview/utils/getOverviewStats.js index 55a2b273..2099ef2d 100644 --- a/public/pages/MonitorDetails/components/MonitorOverview/utils/getOverviewStats.js +++ b/public/pages/MonitorDetails/components/MonitorOverview/utils/getOverviewStats.js @@ -1,5 +1,5 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. diff --git a/public/utils/constants.js b/public/utils/constants.js index faeeee42..59fc6c7f 100644 --- a/public/utils/constants.js +++ b/public/utils/constants.js @@ -1,5 +1,5 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License.