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