/**
   * Get the top n external Services of the given services.
   *
   * @param statistics
   * @param externalServices
   * @param timeRange
   * @param limit
   * @return
   */
  public static List<ExternalServiceVo> topByAvgResponseTime(
      Iterable<ExternalServiceStatistic> statistics,
      Iterable<ExternalService> externalServices,
      TimeRange timeRange,
      Integer limit) {
    Map<Long, ExternalService> rpcTransactionMap = new HashMap<>();

    StreamSupport.stream(externalServices.spliterator(), false)
        .forEach(transaction -> rpcTransactionMap.put(transaction.getId(), transaction));

    Map<TransactionGroup, List<ExternalServiceStatistic>> groups =
        StreamSupport.stream(statistics.spliterator(), false)
            .collect(
                Collectors.groupingBy(
                    new Function<ExternalServiceStatistic, TransactionGroup>() {
                      @Override
                      public TransactionGroup apply(ExternalServiceStatistic statistic) {
                        Long transactionId = statistic.getExternalServiceId();
                        ExternalService webTransaction = rpcTransactionMap.get(transactionId);
                        TransactionGroup group = new TransactionGroup();
                        group.setAppId(webTransaction.getAppId());
                        group.setDisplayName(webTransaction.getUrl());
                        return group;
                      }
                    }));
    List<ExternalServiceVo> result = new ArrayList<>();
    groups.forEach(
        (group, webTransactionStatistics) -> {
          DoubleSummaryStatistics responseSummaryStatistics =
              webTransactionStatistics
                  .stream()
                  .filter(statistic -> statistic.getSumResponseTime() != null)
                  .mapToDouble(ExternalServiceStatistic::getSumResponseTime)
                  .summaryStatistics();
          DoubleSummaryStatistics maxSummaryStatistics =
              webTransactionStatistics
                  .stream()
                  .filter(statistic -> statistic.getMaxResponseTime() != null)
                  .mapToDouble(ExternalServiceStatistic::getMaxResponseTime)
                  .summaryStatistics();
          DoubleSummaryStatistics minSummaryStatistics =
              webTransactionStatistics
                  .stream()
                  .filter(statistic -> statistic.getMinResponseTime() != null)
                  .mapToDouble(ExternalServiceStatistic::getMinResponseTime)
                  .summaryStatistics();
          LongSummaryStatistics pvSummaryStatistics =
              webTransactionStatistics
                  .stream()
                  .filter(statistic -> statistic.getPv() != null)
                  .mapToLong(ExternalServiceStatistic::getPv)
                  .summaryStatistics();

          ExternalServiceVo transaction = new ExternalServiceVo();
          transaction.setAppId(group.getAppId());
          transaction.setDestination(group.getDisplayName());

          transaction.setPv(pvSummaryStatistics.getSum());
          transaction.setCpm(
              format(
                  calculateRate(
                      pvSummaryStatistics.getSum(), timeRange.getDuration(ChronoUnit.MINUTES))));
          transaction.setMaxResponseTime(format(maxSummaryStatistics.getMax()));
          transaction.setMinResponseTime(format(minSummaryStatistics.getMin()));
          transaction.setResponseTime(
              format(
                  calculateRate(responseSummaryStatistics.getSum(), pvSummaryStatistics.getSum())));

          result.add(transaction);
        });

    return result
        .stream()
        .sorted(Comparator.comparing(ExternalServiceVo::getResponseTime))
        .limit(limit)
        .collect(Collectors.toList());
  }