org.elasticsearch.search.aggregations.metrics.stats.StatsBuilder Java Examples

The following examples show how to use org.elasticsearch.search.aggregations.metrics.stats.StatsBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AggregationBuilders.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Create a new {@link Stats} aggregation with the given name.
 */
public static StatsBuilder stats(String name) {
    return new StatsBuilder(name);
}
 
Example #2
Source File: AnalyticsServiceElasticsearch.java    From hawkular-apm with Apache License 2.0 4 votes vote down vote up
@Override
public List<TimeseriesStatistics> getTraceCompletionTimeseriesStatistics(String tenantId, Criteria criteria, long interval) {
    String index = client.getIndex(tenantId);
    if (!refresh(index)) {
        return null;
    }

    StatsBuilder statsBuilder = AggregationBuilders
            .stats("stats")
            .field(ElasticsearchUtil.DURATION_FIELD);

    // TODO: HWKAPM-679 (related to HWKAPM-675), faults now recorded as properties. However this
    // current results in the fault count being an actual count of fault properties, where
    // the original intention of the fault count is the number of txns that have been affected
    // by a fault.
    FilterAggregationBuilder faultCountBuilder = AggregationBuilders
            .filter("faults")
            .filter(FilterBuilders.queryFilter(QueryBuilders.boolQuery()
                    .must(QueryBuilders.matchQuery(ElasticsearchUtil.PROPERTIES_NAME_FIELD, Constants.PROP_FAULT))));

    NestedBuilder nestedFaultCountBuilder = AggregationBuilders
            .nested("nested")
            .path(ElasticsearchUtil.PROPERTIES_FIELD)
            .subAggregation(faultCountBuilder);

    DateHistogramBuilder histogramBuilder = AggregationBuilders
            .dateHistogram("histogram")
            .interval(interval)
            .field(ElasticsearchUtil.TIMESTAMP_FIELD)
            .subAggregation(statsBuilder)
            .subAggregation(nestedFaultCountBuilder);

    BoolQueryBuilder query = buildQuery(criteria, ElasticsearchUtil.TRANSACTION_FIELD, CompletionTime.class);
    SearchRequestBuilder request = getTraceCompletionRequest(index, criteria, query, 0)
            .addAggregation(histogramBuilder);

    SearchResponse response = getSearchResponse(request);
    DateHistogram histogram = response.getAggregations().get("histogram");

    return histogram.getBuckets().stream()
            .map(AnalyticsServiceElasticsearch::toTimeseriesStatistics)
            .collect(Collectors.toList());
}
 
Example #3
Source File: AnalyticsServiceElasticsearch.java    From hawkular-apm with Apache License 2.0 4 votes vote down vote up
/**
 * This method builds a map of communication summary stats related to the supplied
 * criteria.
 *
 * @param stats The map of communication summary stats
 * @param index The index
 * @param criteria The criteria
 * @param addMetrics Whether to add metrics on the nodes/links
 */
private void buildCommunicationSummaryStatistics(Map<String, CommunicationSummaryStatistics> stats, String index,
                                                 Criteria criteria, boolean addMetrics) {
    if (!refresh(index)) {
        return;
    }

    // Don't specify target class, so that query provided that can be used with
    // CommunicationDetails and CompletionTime
    BoolQueryBuilder query = buildQuery(criteria, ElasticsearchUtil.TRANSACTION_FIELD, null);

    // Only want external communications
    query = query.mustNot(QueryBuilders.matchQuery("internal", "true"));

    StatsBuilder latencyBuilder = AggregationBuilders
            .stats("latency")
            .field(ElasticsearchUtil.LATENCY_FIELD);

    TermsBuilder targetBuilder = AggregationBuilders
            .terms("target")
            .field(ElasticsearchUtil.TARGET_FIELD)
            .size(criteria.getMaxResponseSize())
            .subAggregation(latencyBuilder);

    TermsBuilder sourceBuilder = AggregationBuilders
            .terms("source")
            .field(ElasticsearchUtil.SOURCE_FIELD)
            .size(criteria.getMaxResponseSize())
            .subAggregation(targetBuilder);

    SearchRequestBuilder request = getBaseSearchRequestBuilder(COMMUNICATION_DETAILS_TYPE, index, criteria, query, 0)
            .addAggregation(sourceBuilder);
    SearchResponse response = getSearchResponse(request);

    for (Terms.Bucket sourceBucket : response.getAggregations().<Terms>get("source").getBuckets()) {
        Terms targets = sourceBucket.getAggregations().get("target");

        CommunicationSummaryStatistics css = stats.get(sourceBucket.getKey());

        if (css == null) {
            css = new CommunicationSummaryStatistics();
            css.setId(sourceBucket.getKey());
            css.setUri(EndpointUtil.decodeEndpointURI(css.getId()));
            css.setOperation(EndpointUtil.decodeEndpointOperation(css.getId(), true));
            stats.put(css.getId(), css);
        }

        if (addMetrics) {
            css.setCount(sourceBucket.getDocCount());
        }

        for (Terms.Bucket targetBucket : targets.getBuckets()) {
            Stats latency = targetBucket.getAggregations().get("latency");

            String linkId = targetBucket.getKey();
            ConnectionStatistics con = css.getOutbound().get(linkId);

            if (con == null) {
                con = new ConnectionStatistics();
                css.getOutbound().put(linkId, con);
            }

            if (addMetrics) {
                con.setMinimumLatency((long)latency.getMin());
                con.setAverageLatency((long)latency.getAvg());
                con.setMaximumLatency((long)latency.getMax());
                con.setCount(targetBucket.getDocCount());
            }
        }
    }

    addNodeInformation(stats, index, criteria, addMetrics, false);
    addNodeInformation(stats, index, criteria, addMetrics, true);
}
 
Example #4
Source File: AnalyticsServiceElasticsearch.java    From hawkular-apm with Apache License 2.0 4 votes vote down vote up
@Override
public List<TimeseriesStatistics> getEndpointResponseTimeseriesStatistics(String tenantId, Criteria criteria, long interval) {
    String index = client.getIndex(tenantId);
    if (!refresh(index)) {
        return null;
    }

    StatsBuilder statsBuilder = AggregationBuilders
            .stats("stats")
            .field(ElasticsearchUtil.ELAPSED_FIELD);

    // TODO: HWKAPM-679 (related to HWKAPM-675), faults now recorded as properties. However this
    // current results in the fault count being an actual count of fault properties, where
    // the original intention of the fault count is the number of txns that have been affected
    // by a fault.
    FilterAggregationBuilder faultCountBuilder = AggregationBuilders
            .filter("faults")
            .filter(FilterBuilders.queryFilter(QueryBuilders.boolQuery()
                    .must(QueryBuilders.matchQuery(ElasticsearchUtil.PROPERTIES_NAME_FIELD, Constants.PROP_FAULT))));

    NestedBuilder nestedFaultCountBuilder = AggregationBuilders
            .nested("nested")
            .path(ElasticsearchUtil.PROPERTIES_FIELD)
            .subAggregation(faultCountBuilder);

    DateHistogramBuilder histogramBuilder = AggregationBuilders
            .dateHistogram("histogram")
            .interval(interval)
            .field(ElasticsearchUtil.TIMESTAMP_FIELD)
            .subAggregation(statsBuilder)
            .subAggregation(nestedFaultCountBuilder);

    BoolQueryBuilder query = buildQuery(criteria, ElasticsearchUtil.TRANSACTION_FIELD, NodeDetails.class);
    // Only interested in service endpoints, so just Consumer nodes
    query.must(QueryBuilders.termQuery(ElasticsearchUtil.TYPE_FIELD, "Consumer"));

    SearchRequestBuilder request = getNodeDetailsRequest(index, criteria, query, 0)
            .addAggregation(histogramBuilder);

    SearchResponse response = getSearchResponse(request);
    DateHistogram histogram = response.getAggregations().get("histogram");

    return histogram.getBuckets().stream()
            .map(AnalyticsServiceElasticsearch::toTimeseriesStatistics)
            .collect(Collectors.toList());
}