protected void getExecute()

in src/main/java/org/opensearch/ad/transport/GetAnomalyDetectorTransportAction.java [153:264]


    protected void getExecute(GetAnomalyDetectorRequest request, ActionListener<GetAnomalyDetectorResponse> listener) {
        String detectorID = request.getDetectorID();
        String typesStr = request.getTypeStr();
        String rawPath = request.getRawPath();
        Entity entity = request.getEntity();
        boolean all = request.isAll();
        boolean returnJob = request.isReturnJob();
        boolean returnTask = request.isReturnTask();

        try {
            if (!Strings.isEmpty(typesStr) || rawPath.endsWith(PROFILE) || rawPath.endsWith(PROFILE + "/")) {
                if (entity != null) {
                    Set<EntityProfileName> entityProfilesToCollect = getEntityProfilesToCollect(typesStr, all);
                    EntityProfileRunner profileRunner = new EntityProfileRunner(
                        client,
                        xContentRegistry,
                        AnomalyDetectorSettings.NUM_MIN_SAMPLES
                    );
                    profileRunner
                        .profile(
                            detectorID,
                            entity,
                            entityProfilesToCollect,
                            ActionListener
                                .wrap(
                                    profile -> {
                                        listener
                                            .onResponse(
                                                new GetAnomalyDetectorResponse(
                                                    0,
                                                    null,
                                                    0,
                                                    0,
                                                    null,
                                                    null,
                                                    false,
                                                    null,
                                                    null,
                                                    false,
                                                    null,
                                                    null,
                                                    profile,
                                                    true
                                                )
                                            );
                                    },
                                    e -> listener.onFailure(e)
                                )
                        );
                } else {
                    Set<DetectorProfileName> profilesToCollect = getProfilesToCollect(typesStr, all);
                    AnomalyDetectorProfileRunner profileRunner = new AnomalyDetectorProfileRunner(
                        client,
                        xContentRegistry,
                        nodeFilter,
                        AnomalyDetectorSettings.NUM_MIN_SAMPLES,
                        transportService,
                        adTaskManager
                    );
                    profileRunner.profile(detectorID, getProfileActionListener(listener), profilesToCollect);
                }
            } else {
                if (returnTask) {
                    adTaskManager.getAndExecuteOnLatestADTasks(detectorID, null, null, ALL_DETECTOR_TASK_TYPES, (taskList) -> {
                        Optional<ADTask> realtimeAdTask = Optional.empty();
                        Optional<ADTask> historicalAdTask = Optional.empty();

                        if (taskList != null && taskList.size() > 0) {
                            Map<String, ADTask> adTasks = new HashMap<>();
                            List<ADTask> duplicateAdTasks = new ArrayList<>();
                            for (ADTask task : taskList) {
                                if (adTasks.containsKey(task.getTaskType())) {
                                    LOG
                                        .info(
                                            "Found duplicate latest task of detector {}, task id: {}, task type: {}",
                                            detectorID,
                                            task.getTaskType(),
                                            task.getTaskId()
                                        );
                                    duplicateAdTasks.add(task);
                                    continue;
                                }
                                adTasks.put(task.getTaskType(), task);
                            }
                            if (duplicateAdTasks.size() > 0) {
                                adTaskManager.resetLatestFlagAsFalse(duplicateAdTasks);
                            }

                            if (adTasks.containsKey(ADTaskType.REALTIME_HC_DETECTOR.name())) {
                                realtimeAdTask = Optional.ofNullable(adTasks.get(ADTaskType.REALTIME_HC_DETECTOR.name()));
                            } else if (adTasks.containsKey(ADTaskType.REALTIME_SINGLE_ENTITY.name())) {
                                realtimeAdTask = Optional.ofNullable(adTasks.get(ADTaskType.REALTIME_SINGLE_ENTITY.name()));
                            }
                            if (adTasks.containsKey(ADTaskType.HISTORICAL_HC_DETECTOR.name())) {
                                historicalAdTask = Optional.ofNullable(adTasks.get(ADTaskType.HISTORICAL_HC_DETECTOR.name()));
                            } else if (adTasks.containsKey(ADTaskType.HISTORICAL_SINGLE_ENTITY.name())) {
                                historicalAdTask = Optional.ofNullable(adTasks.get(ADTaskType.HISTORICAL_SINGLE_ENTITY.name()));
                            } else if (adTasks.containsKey(ADTaskType.HISTORICAL.name())) {
                                historicalAdTask = Optional.ofNullable(adTasks.get(ADTaskType.HISTORICAL.name()));
                            }
                        }
                        getDetectorAndJob(detectorID, returnJob, returnTask, realtimeAdTask, historicalAdTask, listener);
                    }, transportService, true, 2, listener);
                } else {
                    getDetectorAndJob(detectorID, returnJob, returnTask, Optional.empty(), Optional.empty(), listener);
                }
            }
        } catch (Exception e) {
            LOG.error(e);
            listener.onFailure(e);
        }
    }