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;
}
}
Aggregations