async function getBucketizedAnomalyResults()

in public/pages/DetectorResults/containers/AnomalyHistory.tsx [195:322]


  async function getBucketizedAnomalyResults(
    entityLists: Entity[][] | undefined = undefined,
    modelId?: string
  ) {
    try {
      setIsLoadingAnomalyResults(true);

      let allPureAnomalies = [] as AnomalyData[][];
      let allBucketizedAnomalyResults = [] as Anomalies[];

      // If entityLists exists: then fetch results for each set of entity lists. There may be
      // multiple if the user has selected multiple entities to view.
      if (!isEmpty(entityLists)) {
        // First, get all anomaly summaries, aggregate into a single anomaly summary
        const anomalySummaryPromises =
          entityLists !== undefined
            ? entityLists.map(async (entityList: Entity[]) => {
                const params = getAnomalySummaryQuery(
                  dateRange.startDate,
                  dateRange.endDate,
                  props.detector.id,
                  entityList,
                  props.isHistorical,
                  taskId.current,
                  modelId
                );
                return dispatch(searchResults(params, resultIndex, true));
              })
            : [];

        const allAnomalySummaryResponses = await Promise.all(
          anomalySummaryPromises
        ).catch((error) => {
          const errorMessage = `Error getting all anomaly summaries for all entities: ${error}`;
          console.error(errorMessage);
          core.notifications.toasts.addDanger(
            prettifyErrorMessage(errorMessage)
          );
        });

        //@ts-ignore
        allAnomalySummaryResponses.forEach((anomalySummaryResponse) => {
          allPureAnomalies.push(parsePureAnomalies(anomalySummaryResponse));
        });
      } else if (!isHCDetector) {
        const anomalySummaryQuery = getAnomalySummaryQuery(
          dateRange.startDate,
          dateRange.endDate,
          props.detector.id,
          undefined,
          props.isHistorical,
          taskId.current,
          modelId
        );
        const anomalySummaryResponse = await dispatch(
          searchResults(anomalySummaryQuery, resultIndex, true)
        );
        allPureAnomalies.push(parsePureAnomalies(anomalySummaryResponse));
      }

      setPureAnomalies(allPureAnomalies);

      // If entityLists exists: then fetch results for each set of entity lists. There may be
      // multiple if the user has selected multiple entities to view.
      if (!isEmpty(entityLists)) {
        // Next, get all bucketized anomaly results - one for each entity list
        //@ts-ignore
        const bucketizedAnomalyResultPromises = entityLists.map(
          async (entityList: Entity[]) => {
            const params = getBucketizedAnomalyResultsQuery(
              dateRange.startDate,
              dateRange.endDate,
              props.detector.id,
              entityList,
              props.isHistorical,
              taskId.current,
              modelId
            );
            return dispatch(searchResults(params, resultIndex, true));
          }
        );

        const allBucketizedAnomalyResultResponses = await Promise.all(
          bucketizedAnomalyResultPromises
        ).catch((error) => {
          const errorMessage = `Error getting bucketized anomaly results for all entities: ${error}`;
          console.error(errorMessage);
          core.notifications.toasts.addDanger(
            prettifyErrorMessage(errorMessage)
          );
        });

        //@ts-ignore
        allBucketizedAnomalyResultResponses.forEach(
          (bucketizedAnomalyResultResponse: any) => {
            allBucketizedAnomalyResults.push(
              parseBucketizedAnomalyResults(bucketizedAnomalyResultResponse)
            );
          }
        );
      } else if (!isHCDetector) {
        const bucketizedAnomalyResultsQuery = getBucketizedAnomalyResultsQuery(
          dateRange.startDate,
          dateRange.endDate,
          props.detector.id,
          undefined,
          props.isHistorical,
          taskId.current,
          modelId
        );
        const bucketizedAnomalyResultResponse = await dispatch(
          searchResults(bucketizedAnomalyResultsQuery, resultIndex, true)
        );
        allBucketizedAnomalyResults.push(
          parseBucketizedAnomalyResults(bucketizedAnomalyResultResponse)
        );
      }

      setBucketizedAnomalyResults(allBucketizedAnomalyResults);
    } catch (err) {
      console.error(
        `Failed to get anomaly results for ${props.detector?.id}`,
        err
      );
    } finally {
      setIsLoadingAnomalyResults(false);
    }
  }