Search in sources :

Example 1 with ApplicationTPS

use of org.apache.skywalking.apm.collector.storage.ui.overview.ApplicationTPS in project incubator-skywalking by apache.

the class ApplicationMetricEsUIDAO method getTopNApplicationThroughput.

@Override
public List<ApplicationTPS> getTopNApplicationThroughput(Step step, long startTimeBucket, long endTimeBucket, int betweenSecond, int topN, MetricSource metricSource) {
    String tableName = TimePyramidTableNameBuilder.build(step, ApplicationMetricTable.TABLE);
    SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(tableName);
    searchRequestBuilder.setTypes(ApplicationMetricTable.TABLE_TYPE);
    searchRequestBuilder.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
    BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
    boolQuery.must().add(QueryBuilders.rangeQuery(ApplicationMetricTable.COLUMN_TIME_BUCKET).gte(startTimeBucket).lte(endTimeBucket));
    boolQuery.must().add(QueryBuilders.termQuery(ApplicationMetricTable.COLUMN_SOURCE_VALUE, metricSource.getValue()));
    searchRequestBuilder.setQuery(boolQuery);
    searchRequestBuilder.setSize(0);
    TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms(ApplicationMetricTable.COLUMN_APPLICATION_ID).field(ApplicationMetricTable.COLUMN_APPLICATION_ID).size(2000);
    aggregationBuilder.subAggregation(AggregationBuilders.sum(ApplicationMetricTable.COLUMN_TRANSACTION_CALLS).field(ApplicationMetricTable.COLUMN_TRANSACTION_CALLS));
    searchRequestBuilder.addAggregation(aggregationBuilder);
    SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
    List<ApplicationTPS> applicationTPSs = new LinkedList<>();
    Terms applicationIdTerms = searchResponse.getAggregations().get(ApplicationMetricTable.COLUMN_APPLICATION_ID);
    applicationIdTerms.getBuckets().forEach(applicationIdTerm -> {
        int applicationId = applicationIdTerm.getKeyAsNumber().intValue();
        Sum callSum = applicationIdTerm.getAggregations().get(ApplicationMetricTable.COLUMN_TRANSACTION_CALLS);
        long calls = (long) callSum.getValue();
        int callsPerSec = (int) (betweenSecond == 0 ? 0 : calls / betweenSecond);
        ApplicationTPS applicationTPS = new ApplicationTPS();
        applicationTPS.setApplicationId(applicationId);
        applicationTPS.setCallsPerSec(callsPerSec);
        applicationTPSs.add(applicationTPS);
    });
    applicationTPSs.sort((first, second) -> first.getCallsPerSec() > second.getCallsPerSec() ? -1 : 1);
    if (applicationTPSs.size() <= topN) {
        return applicationTPSs;
    } else {
        List<ApplicationTPS> newCollection = new LinkedList<>();
        for (int i = 0; i < topN; i++) {
            newCollection.add(applicationTPSs.get(i));
        }
        return newCollection;
    }
}
Also used : TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) ApplicationTPS(org.apache.skywalking.apm.collector.storage.ui.overview.ApplicationTPS) Sum(org.elasticsearch.search.aggregations.metrics.sum.Sum) LinkedList(java.util.LinkedList) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

LinkedList (java.util.LinkedList)1 ApplicationTPS (org.apache.skywalking.apm.collector.storage.ui.overview.ApplicationTPS)1 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)1 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)1 TermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)1 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)1