Search in sources :

Example 1 with ADStatsResponse

use of org.opensearch.ad.stats.ADStatsResponse in project anomaly-detection by opensearch-project.

the class StatsAnomalyDetectorTransportAction method getClusterStats.

/**
 * Make async request to get the number of detectors in AnomalyDetector.ANOMALY_DETECTORS_INDEX if necessary
 * and, onResponse, gather the cluster statistics
 *
 * @param client Client
 * @param listener MultiResponsesDelegateActionListener to be used once both requests complete
 * @param adStatsRequest Request containing stats to be retrieved
 */
private void getClusterStats(Client client, MultiResponsesDelegateActionListener<ADStatsResponse> listener, ADStatsRequest adStatsRequest) {
    ADStatsResponse adStatsResponse = new ADStatsResponse();
    if ((adStatsRequest.getStatsToBeRetrieved().contains(StatNames.DETECTOR_COUNT.getName()) || adStatsRequest.getStatsToBeRetrieved().contains(StatNames.SINGLE_ENTITY_DETECTOR_COUNT.getName()) || adStatsRequest.getStatsToBeRetrieved().contains(StatNames.MULTI_ENTITY_DETECTOR_COUNT.getName())) && clusterService.state().getRoutingTable().hasIndex(AnomalyDetector.ANOMALY_DETECTORS_INDEX)) {
        TermsAggregationBuilder termsAgg = AggregationBuilders.terms(DETECTOR_TYPE_AGG).field(AnomalyDetector.DETECTOR_TYPE_FIELD);
        SearchRequest request = new SearchRequest().indices(AnomalyDetector.ANOMALY_DETECTORS_INDEX).source(new SearchSourceBuilder().aggregation(termsAgg).size(0).trackTotalHits(true));
        client.search(request, ActionListener.wrap(r -> {
            StringTerms aggregation = r.getAggregations().get(DETECTOR_TYPE_AGG);
            List<StringTerms.Bucket> buckets = aggregation.getBuckets();
            long totalDetectors = r.getHits().getTotalHits().value;
            long totalSingleEntityDetectors = 0;
            long totalMultiEntityDetectors = 0;
            for (StringTerms.Bucket b : buckets) {
                if (AnomalyDetectorType.SINGLE_ENTITY.name().equals(b.getKeyAsString()) || AnomalyDetectorType.REALTIME_SINGLE_ENTITY.name().equals(b.getKeyAsString()) || AnomalyDetectorType.HISTORICAL_SINGLE_ENTITY.name().equals(b.getKeyAsString())) {
                    totalSingleEntityDetectors += b.getDocCount();
                }
                if (AnomalyDetectorType.MULTI_ENTITY.name().equals(b.getKeyAsString()) || AnomalyDetectorType.REALTIME_MULTI_ENTITY.name().equals(b.getKeyAsString()) || AnomalyDetectorType.HISTORICAL_MULTI_ENTITY.name().equals(b.getKeyAsString())) {
                    totalMultiEntityDetectors += b.getDocCount();
                }
            }
            if (adStatsRequest.getStatsToBeRetrieved().contains(StatNames.DETECTOR_COUNT.getName())) {
                adStats.getStat(StatNames.DETECTOR_COUNT.getName()).setValue(totalDetectors);
            }
            if (adStatsRequest.getStatsToBeRetrieved().contains(StatNames.SINGLE_ENTITY_DETECTOR_COUNT.getName())) {
                adStats.getStat(StatNames.SINGLE_ENTITY_DETECTOR_COUNT.getName()).setValue(totalSingleEntityDetectors);
            }
            if (adStatsRequest.getStatsToBeRetrieved().contains(StatNames.MULTI_ENTITY_DETECTOR_COUNT.getName())) {
                adStats.getStat(StatNames.MULTI_ENTITY_DETECTOR_COUNT.getName()).setValue(totalMultiEntityDetectors);
            }
            adStatsResponse.setClusterStats(getClusterStatsMap(adStatsRequest));
            listener.onResponse(adStatsResponse);
        }, e -> listener.onFailure(e)));
    } else {
        adStatsResponse.setClusterStats(getClusterStatsMap(adStatsRequest));
        listener.onResponse(adStatsResponse);
    }
}
Also used : HandledTransportAction(org.opensearch.action.support.HandledTransportAction) StringTerms(org.opensearch.search.aggregations.bucket.terms.StringTerms) ADStats(org.opensearch.ad.stats.ADStats) OpenSearchStatusException(org.opensearch.OpenSearchStatusException) HashMap(java.util.HashMap) ADStatsResponse(org.opensearch.ad.stats.ADStatsResponse) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) AnomalyDetectorType(org.opensearch.ad.model.AnomalyDetectorType) Map(java.util.Map) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) SearchRequest(org.opensearch.action.search.SearchRequest) Inject(org.opensearch.common.inject.Inject) ActionListener(org.opensearch.action.ActionListener) RestHandlerUtils.wrapRestActionListener(org.opensearch.ad.util.RestHandlerUtils.wrapRestActionListener) Client(org.opensearch.client.Client) Set(java.util.Set) Task(org.opensearch.tasks.Task) RestStatus(org.opensearch.rest.RestStatus) TransportService(org.opensearch.transport.TransportService) FAIL_TO_GET_STATS(org.opensearch.ad.constant.CommonErrorMessages.FAIL_TO_GET_STATS) MultiResponsesDelegateActionListener(org.opensearch.ad.util.MultiResponsesDelegateActionListener) ActionFilters(org.opensearch.action.support.ActionFilters) AggregationBuilders(org.opensearch.search.aggregations.AggregationBuilders) List(java.util.List) Logger(org.apache.logging.log4j.Logger) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) ClusterService(org.opensearch.cluster.service.ClusterService) StatNames(org.opensearch.ad.stats.StatNames) LogManager(org.apache.logging.log4j.LogManager) SearchRequest(org.opensearch.action.search.SearchRequest) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) StringTerms(org.opensearch.search.aggregations.bucket.terms.StringTerms) ADStatsResponse(org.opensearch.ad.stats.ADStatsResponse) List(java.util.List) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Example 2 with ADStatsResponse

use of org.opensearch.ad.stats.ADStatsResponse in project anomaly-detection by opensearch-project.

the class StatsAnomalyDetectorActionTests method testStatsResponse.

@Test
public void testStatsResponse() throws IOException {
    ADStatsResponse adStatsResponse = new ADStatsResponse();
    Map<String, Object> testClusterStats = new HashMap<>();
    testClusterStats.put("test_response", 1);
    adStatsResponse.setClusterStats(testClusterStats);
    List<ADStatsNodeResponse> responses = Collections.emptyList();
    List<FailedNodeException> failures = Collections.emptyList();
    ADStatsNodesResponse adStatsNodesResponse = new ADStatsNodesResponse(ClusterName.DEFAULT, responses, failures);
    adStatsResponse.setADStatsNodesResponse(adStatsNodesResponse);
    StatsAnomalyDetectorResponse response = new StatsAnomalyDetectorResponse(adStatsResponse);
    BytesStreamOutput out = new BytesStreamOutput();
    response.writeTo(out);
    StreamInput input = out.bytes().streamInput();
    StatsAnomalyDetectorResponse newResponse = new StatsAnomalyDetectorResponse(input);
    assertNotNull(newResponse);
    XContentBuilder builder = XContentFactory.jsonBuilder();
    builder = newResponse.toXContent(builder, ToXContent.EMPTY_PARAMS);
    XContentParser parser = createParser(builder);
    assertEquals(1, parser.map().get("test_response"));
}
Also used : HashMap(java.util.HashMap) ADStatsResponse(org.opensearch.ad.stats.ADStatsResponse) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) StreamInput(org.opensearch.common.io.stream.StreamInput) FailedNodeException(org.opensearch.action.FailedNodeException) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser) Test(org.junit.Test)

Example 3 with ADStatsResponse

use of org.opensearch.ad.stats.ADStatsResponse in project anomaly-detection by opensearch-project.

the class EntityAnomalyResultTests method testMerge_otherClass.

@Test
public void testMerge_otherClass() {
    ADStatsResponse adStatsResponse = new ADStatsResponse();
    AnomalyResult anomalyResult = randomHCADAnomalyDetectResult(0.25, 0.25, "error");
    EntityAnomalyResult entityAnomalyResult = new EntityAnomalyResult(new ArrayList<AnomalyResult>() {

        {
            add(anomalyResult);
        }
    });
    entityAnomalyResult.merge(adStatsResponse);
    assertEquals(asList(anomalyResult), entityAnomalyResult.getAnomalyResults());
}
Also used : ADStatsResponse(org.opensearch.ad.stats.ADStatsResponse) Test(org.junit.Test)

Aggregations

ADStatsResponse (org.opensearch.ad.stats.ADStatsResponse)3 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 OpenSearchStatusException (org.opensearch.OpenSearchStatusException)1 ActionListener (org.opensearch.action.ActionListener)1 FailedNodeException (org.opensearch.action.FailedNodeException)1 SearchRequest (org.opensearch.action.search.SearchRequest)1 ActionFilters (org.opensearch.action.support.ActionFilters)1 HandledTransportAction (org.opensearch.action.support.HandledTransportAction)1 FAIL_TO_GET_STATS (org.opensearch.ad.constant.CommonErrorMessages.FAIL_TO_GET_STATS)1 AnomalyDetector (org.opensearch.ad.model.AnomalyDetector)1 AnomalyDetectorType (org.opensearch.ad.model.AnomalyDetectorType)1 ADStats (org.opensearch.ad.stats.ADStats)1 StatNames (org.opensearch.ad.stats.StatNames)1 MultiResponsesDelegateActionListener (org.opensearch.ad.util.MultiResponsesDelegateActionListener)1