export function bucketLevelTriggerToFormik()

in public/pages/CreateTrigger/containers/CreateTrigger/utils/triggerToFormik.js [126:199]


export function bucketLevelTriggerToFormik(trigger, monitor) {
  const {
    id,
    name,
    severity,
    condition,
    condition: { script, composite_agg_filter },
    actions,
    min_time_between_executions: minTimeBetweenExecutions,
    rolling_window_size: rollingWindowSize,
  } = trigger.bucket_level_trigger;

  const bucketSelector = JSON.stringify(condition, null, 4);
  const triggerConditions = getBucketLevelTriggerConditions(condition);
  const where = getWhereExpression(composite_agg_filter);

  const thresholdEnum = _.get(
    monitor,
    `ui_metadata.triggers[${name}].enum`,
    FORMIK_INITIAL_TRIGGER_VALUES.thresholdEnum
  );
  const thresholdValue = _.get(
    monitor,
    `ui_metadata.triggers[${name}].value`,
    FORMIK_INITIAL_TRIGGER_VALUES.thresholdValue
  );
  const anomalyConfidenceThresholdValue = _.get(
    monitor,
    `ui_metadata.triggers[${name}].adTriggerMetadata.anomalyConfidence.value`,
    FORMIK_INITIAL_TRIGGER_VALUES.anomalyDetector.anomalyConfidenceThresholdValue
  );
  const anomalyConfidenceThresholdEnum = _.get(
    monitor,
    `ui_metadata.triggers[${name}].adTriggerMetadata.anomalyConfidence.enum`,
    FORMIK_INITIAL_TRIGGER_VALUES.anomalyDetector.anomalyConfidenceThresholdEnum
  );
  const anomalyGradeThresholdValue = _.get(
    monitor,
    `ui_metadata.triggers[${name}].adTriggerMetadata.anomalyGrade.value`,
    FORMIK_INITIAL_TRIGGER_VALUES.anomalyDetector.anomalyGradeThresholdValue
  );
  const anomalyGradeThresholdEnum = _.get(
    monitor,
    `ui_metadata.triggers[${name}].adTriggerMetadata.anomalyGrade.enum`,
    FORMIK_INITIAL_TRIGGER_VALUES.anomalyDetector.anomalyGradeThresholdEnum
  );
  const triggerType = _.get(monitor, `ui_metadata.triggers[${name}].adTriggerMetadata.triggerType`);
  return {
    ..._.cloneDeep(FORMIK_INITIAL_TRIGGER_VALUES),
    id,
    name,
    severity,
    script,
    bucketSelector,
    actions: getBucketLevelTriggerActions(actions),
    triggerConditions,
    minTimeBetweenExecutions,
    rollingWindowSize,
    thresholdEnum,
    thresholdValue,
    where,
    anomalyDetector: {
      /*If trigger type doesn't exist fallback to query trigger with following reasons
        1. User has changed monitory type from normal monitor to AD monitor.
        2. User has created / updated from API and visiting OpenSearch Dashboards to do other operations.
      */
      triggerType: triggerType ? triggerType : TRIGGER_TYPE.ALERT_TRIGGER,
      anomalyGradeThresholdValue,
      anomalyGradeThresholdEnum,
      anomalyConfidenceThresholdValue,
      anomalyConfidenceThresholdEnum,
    },
  };
}