Search in sources :

Example 21 with ADTaskProfile

use of org.opensearch.ad.model.ADTaskProfile in project anomaly-detection by opensearch-project.

the class HistoricalAnalysisRestApiIT method startHistoricalAnalysis.

@SuppressWarnings("unchecked")
private List<String> startHistoricalAnalysis(int categoryFieldSize, String resultIndex) throws Exception {
    AnomalyDetector detector = createAnomalyDetector(categoryFieldSize, resultIndex);
    String detectorId = detector.getDetectorId();
    // start historical detector
    String taskId = startHistoricalAnalysis(detectorId);
    // get task profile
    ADTaskProfile adTaskProfile = waitUntilGetTaskProfile(detectorId);
    if (categoryFieldSize > 0) {
        if (!ADTaskState.RUNNING.name().equals(adTaskProfile.getAdTask().getState())) {
            adTaskProfile = (ADTaskProfile) waitUntilTaskReachState(detectorId, ImmutableSet.of(ADTaskState.RUNNING.name())).get(0);
        }
        assertEquals((int) Math.pow(categoryFieldDocCount, categoryFieldSize), adTaskProfile.getTotalEntitiesCount().intValue());
        assertTrue(adTaskProfile.getPendingEntitiesCount() > 0);
        assertTrue(adTaskProfile.getRunningEntitiesCount() > 0);
    }
    ADTask adTask = adTaskProfile.getAdTask();
    assertEquals(taskId, adTask.getTaskId());
    assertTrue(TestHelpers.HISTORICAL_ANALYSIS_RUNNING_STATS.contains(adTask.getState()));
    // get task stats
    Response statsResponse = TestHelpers.makeRequest(client(), "GET", AD_BASE_STATS_URI, ImmutableMap.of(), "", null);
    String statsResult = EntityUtils.toString(statsResponse.getEntity());
    Map<String, Object> stringObjectMap = TestHelpers.parseStatsResult(statsResult);
    String detectorCountState = categoryFieldSize > 0 ? MULTI_ENTITY_DETECTOR_COUNT.getName() : SINGLE_ENTITY_DETECTOR_COUNT.getName();
    assertTrue((long) stringObjectMap.get(detectorCountState) > 0);
    Map<String, Object> nodes = (Map<String, Object>) stringObjectMap.get("nodes");
    long totalBatchTaskExecution = 0;
    for (String key : nodes.keySet()) {
        Map<String, Object> nodeStats = (Map<String, Object>) nodes.get(key);
        totalBatchTaskExecution += (long) nodeStats.get(AD_TOTAL_BATCH_TASK_EXECUTION_COUNT.getName());
    }
    assertTrue(totalBatchTaskExecution > 0);
    // get detector with AD task
    ToXContentObject[] result = getHistoricalAnomalyDetector(detectorId, true, client());
    AnomalyDetector parsedDetector = (AnomalyDetector) result[0];
    AnomalyDetectorJob parsedJob = (AnomalyDetectorJob) result[1];
    ADTask parsedADTask = (ADTask) result[2];
    assertNull(parsedJob);
    assertNotNull(parsedDetector);
    assertNotNull(parsedADTask);
    assertEquals(taskId, parsedADTask.getTaskId());
    return ImmutableList.of(detectorId, taskId);
}
Also used : Response(org.opensearch.client.Response) ADTaskProfile(org.opensearch.ad.model.ADTaskProfile) ADTask(org.opensearch.ad.model.ADTask) ToXContentObject(org.opensearch.common.xcontent.ToXContentObject) AnomalyDetectorJob(org.opensearch.ad.model.AnomalyDetectorJob) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) ToXContentObject(org.opensearch.common.xcontent.ToXContentObject)

Aggregations

ADTaskProfile (org.opensearch.ad.model.ADTaskProfile)21 ADTask (org.opensearch.ad.model.ADTask)7 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 AnomalyDetector (org.opensearch.ad.model.AnomalyDetector)5 ImmutableList (com.google.common.collect.ImmutableList)4 List (java.util.List)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 Version (org.opensearch.Version)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Instant (java.time.Instant)3 Map (java.util.Map)3 XContentParser (org.opensearch.common.xcontent.XContentParser)3 RandomCutForest (com.amazon.randomcutforest.RandomCutForest)2 ThresholdedRandomCutForest (com.amazon.randomcutforest.parkservices.ThresholdedRandomCutForest)2 ChronoUnit (java.time.temporal.ChronoUnit)2 Locale (java.util.Locale)2 Optional (java.util.Optional)2 ExecutorService (java.util.concurrent.ExecutorService)2 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)2