Pārlūkot izejas kodu

完善视图规则聚合。

zhangjinjun 6 gadi atpakaļ
vecāks
revīzija
b60db96bd3

+ 90 - 89
src/main/java/com/yihu/quota/service/view/ViewService.java

@ -305,98 +305,11 @@ public class ViewService extends BaseJpaService<View, ViewDao> {
        }
    }
    /**
     * 汇总指标的度量聚合
     *
     * @param viewQuotaList 视图的指标
     * @param viewDimension 视图的行/列维度
     * @return 指标的度量聚合
     * @throws IOException
     */
    private List<AbstractAggregationBuilder> gatherQuotaMetricsAgg(List<ViewQuota> viewQuotaList, ViewDimension viewDimension) throws IOException {
        List<AbstractAggregationBuilder> quotaAggList = new ArrayList<>();
        for (ViewQuota quota : viewQuotaList) {
            if ("basic".equals(quota.getFormulaMode())) {
                String aggNamePre = "";
                if (viewDimension == null) {
                    // 视图展示位数值类型的场合
                    aggNamePre += quota.getCode() + "-";
                } else {
                    // 合计维度场合
                    String dimensionCode = viewDimension.getDimensionCode();
                    aggNamePre = ViewConstant.TOTAL_DIMENSION_CODE.equals(dimensionCode) ? dimensionCode + "-" : "";
                }
                if ("sum".equals(quota.getBasicFormulaType())) {
                    // 求和
                    String aggName = aggNamePre + "sum-result";
                    SumBuilder sumAgg = AggregationBuilders.sum(aggName).field(quota.getDimensionCode());
                    quotaAggList.add(this.joinMetricAgg(sumAgg, quota));
                } else if ("count".equals(quota.getBasicFormulaType())) {
                    // 计数
                    String aggName = aggNamePre + "count-result";
                    ValueCountBuilder countAgg = AggregationBuilders.count(aggName).field(quota.getDimensionCode());
                    quotaAggList.add(this.joinMetricAgg(countAgg, quota));
                } else if ("avg".equals(quota.getBasicFormulaType())) {
                    // 均值
                    String aggName = aggNamePre + "avg-result";
                    AvgBuilder avgAgg = AggregationBuilders.avg(aggName).field(quota.getDimensionCode());
                    quotaAggList.add(this.joinMetricAgg(avgAgg, quota));
                } else if ("max".equals(quota.getBasicFormulaType())) {
                    // 最大值
                    String aggName = aggNamePre + "max-result";
                    MaxBuilder maxAgg = AggregationBuilders.max(aggName).field(quota.getDimensionCode());
                    quotaAggList.add(this.joinMetricAgg(maxAgg, quota));
                } else if ("min".equals(quota.getBasicFormulaType())) {
                    // 最小值
                    String aggName = aggNamePre + "min-result";
                    MinBuilder minAgg = AggregationBuilders.min(aggName).field(quota.getDimensionCode());
                    quotaAggList.add(this.joinMetricAgg(minAgg, quota));
                } else if ("cardinality".equals(quota.getBasicFormulaType())) {
                    // 去重计数
                    String aggName = aggNamePre + "cardinality-result";
                    CardinalityBuilder cardinalityAgg = AggregationBuilders.cardinality(aggName).field(quota.getDimensionCode());
                    quotaAggList.add(this.joinMetricAgg(cardinalityAgg, quota));
                }
            }
        }
        return quotaAggList;
    }
    /**
     * 组装度量聚合
     *
     * @param metricAgg ES度量聚合构建器
     * @param quota     视图指标
     * @throws IOException
     */
    private AbstractAggregationBuilder joinMetricAgg(MetricsAggregationBuilder metricAgg, ViewQuota quota) throws IOException {
        List<ViewQuotaFilter> quotaFilterList = quota.getQuotaFilterList();
        FilterAggregationBuilder filterAgg = null;
        if (quotaFilterList != null && quotaFilterList.size() > 0) {
            String quotaFilterStr = objectMapper.writeValueAsString(quotaFilterList);
            List<ViewQuotaFilterModel> quotaFilterModelList = objectMapper.readValue(quotaFilterStr, new TypeReference<List<ViewQuotaFilterModel>>() {
            });
            BoolQueryBuilder quotaBoolQueryBuilder = QueryBuilders.boolQuery();
            joinFilters(quotaBoolQueryBuilder, quotaFilterModelList);
            String aggName = quota.getDimensionCode() + "-filter";
            filterAgg = AggregationBuilders.filter(aggName).filter(quotaBoolQueryBuilder);
            filterAgg.subAggregation(metricAgg);
        }
        if (filterAgg == null) {
            return metricAgg;
        } else {
            return filterAgg;
        }
    }
    /**
     * 组装行/列维度的聚合
     *
     * @param viewDimension 视图行/列维度
     * @return 行/列维度的聚合
     * @return 行/列维度的聚合构建器
     */
    private AggregationBuilder joinDimensionAgg(ViewDimension viewDimension) {
        AggregationBuilder dimensionAgg = null;
@ -467,7 +380,7 @@ public class ViewService extends BaseJpaService<View, ViewDao> {
    /**
     * 汇总列维度的聚合及嵌套其指标聚合,并列嵌套于上层聚合
     *
     * @param upperAgg         上层聚合对象
     * @param upperAgg         上层维度的聚合构建器
     * @param rowViewDimension 视图的行维度
     * @param colDimensionList 视图的列维度集合
     * @param viewQuotaList    视图的指标集合
@ -497,4 +410,92 @@ public class ViewService extends BaseJpaService<View, ViewDao> {
        }
    }
    /**
     * 汇总指标的度量聚合
     *
     * @param viewQuotaList 视图的指标
     * @param viewDimension 视图的行/列维度
     * @return 指标的度量聚合构建器集合
     * @throws IOException
     */
    private List<AbstractAggregationBuilder> gatherQuotaMetricsAgg(List<ViewQuota> viewQuotaList,
                                                                   ViewDimension viewDimension) throws IOException {
        List<AbstractAggregationBuilder> quotaAggList = new ArrayList<>();
        for (ViewQuota quota : viewQuotaList) {
            if ("basic".equals(quota.getFormulaMode())) {
                String aggNamePre = "";
                String dimensionCode = viewDimension.getDimensionCode();
                if (viewDimension == null) {
                    // 视图展示为数值类型的场合
                    aggNamePre += quota.getCode() + "-";
                } else if (ViewConstant.TOTAL_DIMENSION_CODE.equals(dimensionCode)) {
                    // 合计维度场合
                    aggNamePre = ViewConstant.TOTAL_DIMENSION_CODE + "-";
                }
                if ("sum".equals(quota.getBasicFormulaType())) {
                    // 求和
                    String aggName = aggNamePre + "sum-result";
                    SumBuilder sumAgg = AggregationBuilders.sum(aggName).field(quota.getDimensionCode());
                    quotaAggList.add(this.joinMetricAgg(sumAgg, quota));
                } else if ("count".equals(quota.getBasicFormulaType())) {
                    // 计数
                    String aggName = aggNamePre + "count-result";
                    ValueCountBuilder countAgg = AggregationBuilders.count(aggName).field(quota.getDimensionCode());
                    quotaAggList.add(this.joinMetricAgg(countAgg, quota));
                } else if ("avg".equals(quota.getBasicFormulaType())) {
                    // 均值
                    String aggName = aggNamePre + "avg-result";
                    AvgBuilder avgAgg = AggregationBuilders.avg(aggName).field(quota.getDimensionCode());
                    quotaAggList.add(this.joinMetricAgg(avgAgg, quota));
                } else if ("max".equals(quota.getBasicFormulaType())) {
                    // 最大值
                    String aggName = aggNamePre + "max-result";
                    MaxBuilder maxAgg = AggregationBuilders.max(aggName).field(quota.getDimensionCode());
                    quotaAggList.add(this.joinMetricAgg(maxAgg, quota));
                } else if ("min".equals(quota.getBasicFormulaType())) {
                    // 最小值
                    String aggName = aggNamePre + "min-result";
                    MinBuilder minAgg = AggregationBuilders.min(aggName).field(quota.getDimensionCode());
                    quotaAggList.add(this.joinMetricAgg(minAgg, quota));
                } else if ("cardinality".equals(quota.getBasicFormulaType())) {
                    // 去重计数
                    String aggName = aggNamePre + "cardinality-result";
                    CardinalityBuilder cardinalityAgg = AggregationBuilders.cardinality(aggName).field(quota.getDimensionCode());
                    quotaAggList.add(this.joinMetricAgg(cardinalityAgg, quota));
                }
            }
        }
        return quotaAggList;
    }
    /**
     * 组装度量聚合
     *
     * @param metricAgg ES度量聚合构建器
     * @param quota     视图指标
     * @throws IOException
     */
    private AbstractAggregationBuilder joinMetricAgg(MetricsAggregationBuilder metricAgg, ViewQuota quota) throws IOException {
        List<ViewQuotaFilter> quotaFilterList = quota.getQuotaFilterList();
        FilterAggregationBuilder filterAgg = null;
        if (quotaFilterList != null && quotaFilterList.size() > 0) {
            String quotaFilterStr = objectMapper.writeValueAsString(quotaFilterList);
            List<ViewQuotaFilterModel> quotaFilterModelList = objectMapper.readValue(quotaFilterStr, new TypeReference<List<ViewQuotaFilterModel>>() {
            });
            BoolQueryBuilder quotaBoolQueryBuilder = QueryBuilders.boolQuery();
            joinFilters(quotaBoolQueryBuilder, quotaFilterModelList);
            String aggName = quota.getDimensionCode() + "-filter";
            filterAgg = AggregationBuilders.filter(aggName).filter(quotaBoolQueryBuilder);
            filterAgg.subAggregation(metricAgg);
        }
        if (filterAgg == null) {
            return metricAgg;
        } else {
            return filterAgg;
        }
    }
}