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);
}
Aggregations