Search in sources :

Example 36 with AnomalyDetector

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

the class AnomalyDetectorRestApiIT method testValidateAnomalyDetectorWithNoIssue.

public void testValidateAnomalyDetectorWithNoIssue() throws Exception {
    AnomalyDetector detector = createIndexAndGetAnomalyDetector(INDEX_NAME);
    Response resp = TestHelpers.makeRequest(client(), "POST", TestHelpers.AD_BASE_DETECTORS_URI + "/_validate/detector", ImmutableMap.of(), TestHelpers.toHttpEntity(detector), null);
    Map<String, Object> responseMap = entityAsMap(resp);
    assertEquals("no issue, empty response body", new HashMap<String, Object>(), responseMap);
}
Also used : Response(org.opensearch.client.Response) ToXContentObject(org.opensearch.common.xcontent.ToXContentObject) Matchers.containsString(org.hamcrest.Matchers.containsString) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector)

Example 37 with AnomalyDetector

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

the class HistoricalAnalysisRestApiIT method testDeleteHistoricalAnalysis.

// TODO: fix delete
public void testDeleteHistoricalAnalysis() throws IOException, IllegalAccessException {
    // create historical detector
    AnomalyDetector detector = createAnomalyDetector();
    String detectorId = detector.getDetectorId();
    // delete detector
    Response response = TestHelpers.makeRequest(client(), "DELETE", TestHelpers.AD_BASE_DETECTORS_URI + "/" + detectorId, ImmutableMap.of(), "", null);
    assertEquals(RestStatus.OK, TestHelpers.restStatus(response));
}
Also used : Response(org.opensearch.client.Response) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector)

Example 38 with AnomalyDetector

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

the class HistoricalAnalysisRestApiIT method testSearchTasks.

public void testSearchTasks() throws IOException, InterruptedException, IllegalAccessException {
    // create historical detector
    AnomalyDetector detector = createAnomalyDetector();
    String detectorId = detector.getDetectorId();
    // start historical detector
    String taskId = startHistoricalAnalysis(detectorId);
    waitUntilTaskDone(detectorId);
    String query = String.format("{\"query\":{\"term\":{\"detector_id\":{\"value\":\"%s\"}}}}", detectorId);
    Response response = TestHelpers.makeRequest(client(), "POST", TestHelpers.AD_BASE_DETECTORS_URI + "/tasks/_search", ImmutableMap.of(), query, null);
    String searchResult = EntityUtils.toString(response.getEntity());
    assertTrue(searchResult.contains(taskId));
    assertTrue(searchResult.contains(detector.getDetectorId()));
}
Also used : Response(org.opensearch.client.Response) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector)

Example 39 with AnomalyDetector

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

the class HistoricalAnalysisRestApiIT method testStopHistoricalAnalysis.

@SuppressWarnings("unchecked")
public void testStopHistoricalAnalysis() throws Exception {
    // create historical detector
    AnomalyDetector detector = createAnomalyDetector();
    String detectorId = detector.getDetectorId();
    // start historical detector
    String taskId = startHistoricalAnalysis(detectorId);
    waitUntilGetTaskProfile(detectorId);
    // stop historical detector
    Response stopDetectorResponse = stopAnomalyDetector(detectorId, client(), false);
    assertEquals(RestStatus.OK, TestHelpers.restStatus(stopDetectorResponse));
    // get task profile
    checkIfTaskCanFinishCorrectly(detectorId, taskId, ImmutableSet.of(ADTaskState.STOPPED.name()));
    updateClusterSettings(BATCH_TASK_PIECE_INTERVAL_SECONDS.getKey(), 1);
    waitUntilTaskDone(detectorId);
    // get AD 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);
    assertTrue((long) stringObjectMap.get("single_entity_detector_count") > 0);
    Map<String, Object> nodes = (Map<String, Object>) stringObjectMap.get("nodes");
    long cancelledTaskCount = 0;
    for (String key : nodes.keySet()) {
        Map<String, Object> nodeStats = (Map<String, Object>) nodes.get(key);
        cancelledTaskCount += (long) nodeStats.get("ad_canceled_batch_task_count");
    }
    assertTrue(cancelledTaskCount >= 1);
}
Also used : Response(org.opensearch.client.Response) ToXContentObject(org.opensearch.common.xcontent.ToXContentObject) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector)

Example 40 with AnomalyDetector

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

the class SecureADRestIT method testValidateAnomalyDetectorWithNoBackendRole.

public void testValidateAnomalyDetectorWithNoBackendRole() throws IOException {
    AnomalyDetector detector = TestHelpers.randomAnomalyDetector(null, Instant.now());
    enableFilterBy();
    // User Dog has AD full access, but has no backend role
    // When filter by is enabled, we block validating Detectors
    Exception exception = expectThrows(IOException.class, () -> {
        validateAnomalyDetector(detector, dogClient);
    });
    Assert.assertTrue(exception.getMessage().contains("Filter by backend roles is enabled and User dog does not have backend roles configured"));
}
Also used : AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) IOException(java.io.IOException)

Aggregations

AnomalyDetector (org.opensearch.ad.model.AnomalyDetector)226 IOException (java.io.IOException)71 ActionListener (org.opensearch.action.ActionListener)54 Response (org.opensearch.client.Response)45 Map (java.util.Map)43 List (java.util.List)41 Client (org.opensearch.client.Client)38 SearchRequest (org.opensearch.action.search.SearchRequest)37 LogManager (org.apache.logging.log4j.LogManager)35 Logger (org.apache.logging.log4j.Logger)35 ArrayList (java.util.ArrayList)33 Optional (java.util.Optional)33 Test (org.junit.Test)32 SearchResponse (org.opensearch.action.search.SearchResponse)32 Instant (java.time.Instant)30 TimeValue (org.opensearch.common.unit.TimeValue)30 HashMap (java.util.HashMap)29 Entity (org.opensearch.ad.model.Entity)29 NamedXContentRegistry (org.opensearch.common.xcontent.NamedXContentRegistry)29 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)29