Skip to content

Commit

Permalink
Fixed an issue that was causing the results of the ctx object to be e…
Browse files Browse the repository at this point in the history
…mpty. (#334) (#411)

* Fixed an issue that was causing the results of the ctx object to be empty.

Signed-off-by: AWSHurneyt <[email protected]>

* Fixed an issue that was causing the results of the ctx object to be empty.

Signed-off-by: AWSHurneyt <[email protected]>

* Consolidated duplicate code.

Signed-off-by: AWSHurneyt <[email protected]>

Signed-off-by: AWSHurneyt <[email protected]>
(cherry picked from commit e34fa02)

Co-authored-by: AWSHurneyt <[email protected]>
  • Loading branch information
opensearch-trigger-bot[bot] and AWSHurneyt authored Dec 8, 2022
1 parent f9e19f2 commit d3047e9
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import { getPathsPerDataType } from '../../../CreateMonitor/containers/DefineMon
import monitorToFormik from '../../../CreateMonitor/containers/CreateMonitor/utils/monitorToFormik';
import { buildRequest } from '../../../CreateMonitor/containers/DefineMonitor/utils/searchRequests';
import { backendErrorNotification, inputLimitText } from '../../../../utils/helpers';
import moment from 'moment';
import { formikToTrigger } from '../CreateTrigger/utils/formikToTrigger';
import DefineDocumentLevelTrigger from '../DefineDocumentLevelTrigger/DefineDocumentLevelTrigger';
import {
buildClusterMetricsRequest,
Expand All @@ -34,9 +32,6 @@ class ConfigureTriggers extends React.Component {
this.state = {
dataTypes: {},
executeResponse: null,
isBucketLevelMonitor:
_.get(props, 'monitor.monitor_type', MONITOR_TYPE.QUERY_LEVEL) ===
MONITOR_TYPE.BUCKET_LEVEL,
triggerDeleted: false,
addTriggerButton: this.prepareAddTriggerButton(),
triggerEmptyPrompt: this.prepareTriggerEmptyPrompt(),
Expand All @@ -49,21 +44,10 @@ class ConfigureTriggers extends React.Component {
}

componentDidMount() {
const {
monitorValues: { searchType, uri },
} = this.props;
const { isBucketLevelMonitor } = this.state;
if (searchType === SEARCH_TYPE.CLUSTER_METRICS && canExecuteClusterMetricsMonitor(uri))
this.onRunExecute();
if (isBucketLevelMonitor) this.onQueryMappings();
this.monitorSetupByType();
}

componentDidUpdate(prevProps) {
const prevMonitorType = _.get(prevProps, 'monitor.monitor_type', MONITOR_TYPE.QUERY_LEVEL);
const currMonitorType = _.get(this.props, 'monitor.monitor_type', MONITOR_TYPE.QUERY_LEVEL);
if (prevMonitorType !== currMonitorType)
_.set(this.state, 'isBucketLevelMonitor', currMonitorType === MONITOR_TYPE.BUCKET_LEVEL);

const prevSearchType = _.get(
prevProps,
'monitorValues.searchType',
Expand Down Expand Up @@ -91,12 +75,24 @@ class ConfigureTriggers extends React.Component {

const prevInputs = prevProps.monitor.inputs[0];
const currInputs = this.props.monitor.inputs[0];
if (!_.isEqual(prevInputs, currInputs)) {
const { isBucketLevelMonitor } = this.state;
if (isBucketLevelMonitor) this.onQueryMappings();
}
if (!_.isEqual(prevInputs, currInputs)) this.monitorSetupByType();
}

monitorSetupByType = () => {
const {
monitor: { monitor_type },
monitorValues: { uri },
} = this.props;
switch (monitor_type) {
case MONITOR_TYPE.BUCKET_LEVEL:
this.onQueryMappings();
break;
case MONITOR_TYPE.CLUSTER_METRICS:
if (canExecuteClusterMetricsMonitor(uri)) this.onRunExecute();
break;
}
};

prepareAddTriggerButton = () => {
const { monitorValues, triggerArrayHelpers, triggerValues } = this.props;
const disableAddTriggerButton =
Expand Down Expand Up @@ -186,18 +182,6 @@ class ConfigureTriggers extends React.Component {
}
}

getTriggerContext = (executeResponse, monitor, values) => {
return {
periodStart: moment.utc(_.get(executeResponse, 'period_start', Date.now())).format(),
periodEnd: moment.utc(_.get(executeResponse, 'period_end', Date.now())).format(),
results: [_.get(executeResponse, 'input_results.results[0]')].filter((result) => !!result),
trigger: formikToTrigger(values, _.get(this.props.monitor, 'ui_metadata', {})),
alert: null,
error: null,
monitor: monitor,
};
};

renderDefineTrigger = (triggerArrayHelpers, index) => {
const {
edit,
Expand All @@ -212,13 +196,11 @@ class ConfigureTriggers extends React.Component {
notificationService,
plugins,
} = this.props;

const { executeResponse } = this.state;
return (
<DefineTrigger
edit={edit}
triggerArrayHelpers={triggerArrayHelpers}
context={this.getTriggerContext(executeResponse, monitor, triggerValues)}
executeResponse={executeResponse}
monitor={monitor}
monitorValues={monitorValues}
Expand Down Expand Up @@ -255,7 +237,6 @@ class ConfigureTriggers extends React.Component {
<DefineBucketLevelTrigger
edit={edit}
triggerArrayHelpers={triggerArrayHelpers}
context={this.getTriggerContext(executeResponse, monitor, triggerValues)}
executeResponse={executeResponse}
monitor={monitor}
monitorValues={monitorValues}
Expand Down Expand Up @@ -293,7 +274,6 @@ class ConfigureTriggers extends React.Component {
<DefineDocumentLevelTrigger
edit={edit}
triggerArrayHelpers={triggerArrayHelpers}
context={this.getTriggerContext(executeResponse, monitor, triggerValues)}
executeResponse={executeResponse}
monitor={monitor}
monitorValues={monitorValues}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { FieldArray } from 'formik';
import ConfigureActions from '../ConfigureActions';
import { inputLimitText } from '../../../../utils/helpers';
import { DEFAULT_TRIGGER_NAME, SEVERITY_OPTIONS } from '../../utils/constants';
import { getTriggerContext } from '../../utils/helper';

const defaultRowProps = {
label: 'Trigger name',
Expand Down Expand Up @@ -49,7 +50,6 @@ const selectInputProps = {
};

const propTypes = {
context: PropTypes.object.isRequired,
executeResponse: PropTypes.object,
monitorValues: PropTypes.object.isRequired,
onRun: PropTypes.func.isRequired,
Expand Down Expand Up @@ -205,7 +205,6 @@ class DefineBucketLevelTrigger extends Component {
const {
edit,
triggerArrayHelpers,
context,
executeResponse,
monitor,
monitorValues,
Expand All @@ -222,6 +221,7 @@ class DefineBucketLevelTrigger extends Component {
plugins,
} = this.props;
const fieldPath = triggerIndex !== undefined ? `triggerDefinitions[${triggerIndex}].` : '';
const context = getTriggerContext(executeResponse, monitor, triggerValues, triggerIndex);
const isGraph = _.get(monitorValues, 'searchType') === SEARCH_TYPE.GRAPH;
const response = _.get(executeResponse, 'input_results.results[0]');
const triggerName = _.get(triggerValues, `${fieldPath}name`, DEFAULT_TRIGGER_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import DocumentLevelTriggerExpression from './DocumentLevelTriggerExpression';
import { backendErrorNotification, inputLimitText } from '../../../../utils/helpers';
import monitorToFormik from '../../../CreateMonitor/containers/CreateMonitor/utils/monitorToFormik';
import { buildRequest } from '../../../CreateMonitor/containers/DefineMonitor/utils/searchRequests';
import { getTriggerContext } from '../../utils/helper';

const MAX_TRIGGER_CONDITIONS = 10;

Expand Down Expand Up @@ -58,7 +59,6 @@ const selectInputProps = {
};

const propTypes = {
context: PropTypes.object.isRequired,
executeResponse: PropTypes.object,
monitorValues: PropTypes.object.isRequired,
onRun: PropTypes.func.isRequired,
Expand Down Expand Up @@ -166,7 +166,6 @@ class DefineDocumentLevelTrigger extends Component {
const {
edit,
triggerArrayHelpers,
context,
monitor,
monitorValues,
onRun,
Expand All @@ -181,6 +180,7 @@ class DefineDocumentLevelTrigger extends Component {
plugins,
} = this.props;
const executeResponse = _.get(this.state, 'executeResponse', this.props.executeResponse);
const context = getTriggerContext(executeResponse, monitor, triggerValues, triggerIndex);
const fieldPath = triggerIndex !== undefined ? `triggerDefinitions[${triggerIndex}].` : '';
const isGraph = _.get(monitorValues, 'searchType') === SEARCH_TYPE.GRAPH;
const response = _.get(executeResponse, 'input_results.results[0]');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
canExecuteClusterMetricsMonitor,
} from '../../../CreateMonitor/components/ClusterMetricsMonitor/utils/clusterMetricsMonitorHelpers';
import { DEFAULT_TRIGGER_NAME, SEVERITY_OPTIONS } from '../../utils/constants';
import { getTriggerContext } from '../../utils/helper';

const defaultRowProps = {
label: 'Trigger name',
Expand Down Expand Up @@ -59,7 +60,6 @@ const selectInputProps = {
};

const propTypes = {
context: PropTypes.object.isRequired,
executeResponse: PropTypes.object,
monitorValues: PropTypes.object.isRequired,
onRun: PropTypes.func.isRequired,
Expand Down Expand Up @@ -134,7 +134,7 @@ class DefineTrigger extends Component {
const {
edit,
triggerArrayHelpers,
context,
monitor,
monitorValues,
onRun,
setFlyout,
Expand All @@ -148,6 +148,7 @@ class DefineTrigger extends Component {
plugins,
} = this.props;
const executeResponse = _.get(this.state, 'executeResponse', this.props.executeResponse);
const context = getTriggerContext(executeResponse, monitor, triggerValues, triggerIndex);
const fieldPath = triggerIndex !== undefined ? `triggerDefinitions[${triggerIndex}].` : '';
const isGraph = _.get(monitorValues, 'searchType') === SEARCH_TYPE.GRAPH;
const isAd = _.get(monitorValues, 'searchType') === SEARCH_TYPE.AD;
Expand Down
16 changes: 16 additions & 0 deletions public/pages/CreateTrigger/utils/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
FORMIK_INITIAL_DOC_LEVEL_SCRIPT,
FORMIK_INITIAL_TRIGGER_VALUES,
} from '../containers/CreateTrigger/utils/constants';
import moment from 'moment';
import { formikToTrigger } from '../containers/CreateTrigger/utils/formikToTrigger';

export const getChannelOptions = (channels, allowedTypes) =>
allowedTypes.map((type) => ({
Expand Down Expand Up @@ -46,3 +48,17 @@ export const getDefaultScript = (monitorValues) => {
return FORMIK_INITIAL_TRIGGER_VALUES.script;
}
};

export const getTriggerContext = (executeResponse, monitor, values, triggerIndex) => {
let trigger = formikToTrigger(values, _.get(monitor, 'ui_metadata', {}));
if (_.isArray(trigger) && triggerIndex >= 0) trigger = trigger[triggerIndex];
return {
periodStart: moment.utc(_.get(executeResponse, 'period_start', Date.now())).format(),
periodEnd: moment.utc(_.get(executeResponse, 'period_end', Date.now())).format(),
results: [_.get(executeResponse, 'input_results.results[0]')].filter((result) => !!result),
trigger: trigger,
alert: null,
error: null,
monitor: monitor,
};
};

0 comments on commit d3047e9

Please sign in to comment.