componentDidUpdate()

in public/pages/CreateMonitor/containers/DefineMonitor/DefineMonitor.js [103:165]


  componentDidUpdate(prevProps) {
    const {
      searchType: prevSearchType,
      index: prevIndex,
      timeField: prevTimeField,
      monitor_type: prevMonitorType,
      groupBy: prevGroupBy,
      aggregations: prevAggregations,
      bucketValue: prevBucketValue,
      bucketUnitOfTime: prevBucketUnitOfTime,
      where: prevWhere,
    } = prevProps.values;
    const {
      searchType,
      index,
      timeField,
      monitor_type,
      groupBy,
      aggregations,
      bucketValue,
      bucketUnitOfTime,
      where,
    } = this.props.values;
    const isGraph = searchType === SEARCH_TYPE.GRAPH;
    const hasIndices = !!index.length;
    // If customer is defining query through extraction query, then they are manually running their own queries
    // Below logic is for customers defining queries through graph/visual way.
    if (isGraph && hasIndices) {
      // If current query type is graph and there are indices selected, then we want to query new index mappings if
      // a) previous query type was query (to get the first load of mappings)
      // b) different indices, to get new mappings
      const wasQuery = prevSearchType === SEARCH_TYPE.QUERY;
      const diffIndices = prevIndex !== index;
      if (wasQuery || diffIndices) {
        this.onQueryMappings();
      }
      // If there is a timeField selected, then we want to run the query if
      // a) previous query type was query (to get first run executed)
      // b) different indices, to get new data
      // c) different time fields, to aggregate on new data/axis
      const diffTimeFields = prevTimeField !== timeField;
      const hasTimeField = !!timeField;
      const wasQueryType =
        prevMonitorType === MONITOR_TYPE.QUERY_LEVEL && monitor_type === MONITOR_TYPE.BUCKET_LEVEL;
      if (hasTimeField) {
        if (wasQuery || diffIndices || diffTimeFields || wasQueryType) this.onRunQuery();
      }
    }
    const groupByCleared = prevGroupBy && !groupBy;

    if (
      prevAggregations !== aggregations ||
      prevBucketValue !== bucketValue ||
      prevBucketUnitOfTime !== bucketUnitOfTime ||
      prevWhere !== where ||
      prevGroupBy !== groupBy
    )
      this.onRunQuery();

    // Reset response when monitor type or definition method is changed
    if (prevSearchType !== searchType || prevMonitorType !== monitor_type || groupByCleared)
      this.resetResponse();
  }