/** * @param timeRange * @param apps * @param period in Second * @return */ public Iterable<ApplicationStatistic> selectApplicationStatistics( TimeRange timeRange, Iterable<Application> apps, Long period) { final long periodInMillis = period * 1000; List<Long> appIds = StreamSupport.stream(apps.spliterator(), false) .map(Application::getId) .collect(Collectors.toList()); QApplicationStatistic appStatistic = QApplicationStatistic.applicationStatistic; BooleanExpression timestampCondition = appStatistic.timestamp.between( toMillisSecond(timeRange.getFrom()), toMillisSecond(timeRange.getTo())); BooleanExpression appIdCondition = appStatistic.appId.in(appIds); BooleanExpression periodCondition = appStatistic.period.eq(periodInMillis); BooleanExpression whereCondition = timestampCondition.and(appIdCondition).and(periodCondition); return applicationStatisticRepository.findAll(whereCondition); }
/** * @param timeRange * @param instances * @param period in Second * @return */ public Iterable<InstanceStatistic> selectInstanceStatistics( TimeRange timeRange, List<Instance> instances, Long period) { final long periodInMillis = period * 1000; List<Long> instanceIds = StreamSupport.stream(instances.spliterator(), false) .map(Instance::getId) .collect(Collectors.toList()); QInstanceStatistic instanceStatistic = QInstanceStatistic.instanceStatistic; BooleanExpression timestampCondition = instanceStatistic.timestamp.between( toMillisSecond(timeRange.getFrom()), toMillisSecond(timeRange.getTo())); BooleanExpression instanceIdCondition = instanceStatistic.instanceId.in(instanceIds); BooleanExpression periodCondition = instanceStatistic.period.eq(periodInMillis); BooleanExpression whereCondition = instanceIdCondition.and(timestampCondition).and(periodCondition); return instanceStatisticRepository.findAll(whereCondition); }
/** * 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()); }