|
@ -1,10 +1,14 @@
|
|
|
|
|
|
package com.yihu.quota.util;
|
|
package com.yihu.quota.util;
|
|
|
|
|
|
|
|
import com.github.abel533.echarts.Option;
|
|
import com.yihu.quota.dao.view.ViewDao;
|
|
import com.yihu.quota.dao.view.ViewDao;
|
|
import com.yihu.quota.dao.view.ViewDimensionDao;
|
|
import com.yihu.quota.dao.view.ViewDimensionDao;
|
|
|
|
import com.yihu.quota.dao.view.ViewQuotaDao;
|
|
|
|
import com.yihu.quota.dao.view.ViewQuotaFilterDao;
|
|
import com.yihu.quota.model.view.View;
|
|
import com.yihu.quota.model.view.View;
|
|
import com.yihu.quota.model.view.ViewDimension;
|
|
|
|
|
|
import com.yihu.quota.model.view.ViewQuota;
|
|
|
|
import com.yihu.quota.model.view.ViewQuotaFilter;
|
|
import org.elasticsearch.action.search.SearchRequestBuilder;
|
|
import org.elasticsearch.action.search.SearchRequestBuilder;
|
|
import org.elasticsearch.action.search.SearchResponse;
|
|
import org.elasticsearch.action.search.SearchResponse;
|
|
import org.elasticsearch.client.transport.TransportClient;
|
|
import org.elasticsearch.client.transport.TransportClient;
|
|
@ -18,6 +22,8 @@ import org.elasticsearch.search.aggregations.metrics.max.InternalMax;
|
|
import org.elasticsearch.search.aggregations.metrics.min.InternalMin;
|
|
import org.elasticsearch.search.aggregations.metrics.min.InternalMin;
|
|
import org.elasticsearch.search.aggregations.metrics.sum.InternalSum;
|
|
import org.elasticsearch.search.aggregations.metrics.sum.InternalSum;
|
|
import org.elasticsearch.search.aggregations.metrics.valuecount.InternalValueCount;
|
|
import org.elasticsearch.search.aggregations.metrics.valuecount.InternalValueCount;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
@ -29,10 +35,16 @@ import java.util.*;
|
|
@Component
|
|
@Component
|
|
public class AggregationBuildHandler {
|
|
public class AggregationBuildHandler {
|
|
|
|
|
|
|
|
Logger logger = LoggerFactory.getLogger(AggregationBuildHandler.class);
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
private ViewDimensionDao viewDimensionDao;
|
|
private ViewDimensionDao viewDimensionDao;
|
|
@Autowired
|
|
@Autowired
|
|
private ViewDao viewDao;
|
|
private ViewDao viewDao;
|
|
|
|
@Autowired
|
|
|
|
private ViewQuotaDao viewQuotaDao;
|
|
|
|
@Autowired
|
|
|
|
private ViewQuotaFilterDao viewQuotaFilterDao;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 简单聚合
|
|
* 简单聚合
|
|
@ -124,12 +136,156 @@ public class AggregationBuildHandler {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* es bucket 数据结果解析
|
|
|
|
|
|
* 表格型 - 数据结果解析
|
|
|
|
* @param
|
|
|
|
* @param map 聚合查询结果
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public List<Map<String, Object>> tableDataParsing(String viewCode ,Map<String, Aggregation> map){
|
|
|
|
List<String> rowList = new LinkedList<>();
|
|
|
|
rowList.add("区县_terms");
|
|
|
|
rowList.add("total");
|
|
|
|
List<String> cloumnList = new ArrayList<>();
|
|
|
|
cloumnList.add("total_count_result");
|
|
|
|
cloumnList.add("性别_terms");
|
|
|
|
|
|
|
|
View view = viewDao.findByCode(viewCode);
|
|
|
|
if(view == null){
|
|
|
|
logger.debug("视图不存在");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// // 视图各组组内顶层行维度(升序)
|
|
|
|
// List<ViewDimension> groupTopRowDimensionList = viewDimensionDao.getGroupTopRowDimensionList(view.getId());
|
|
|
|
// // 视图各组组内其他行维度(升序)
|
|
|
|
// Map<Integer, List<ViewDimension>> groupOtherRowDimensionsMap = new HashMap<>(16);
|
|
|
|
// for (ViewDimension viewDimension : groupTopRowDimensionList) {
|
|
|
|
// rowList.add(viewDimension.getDimensionCode());
|
|
|
|
// List<ViewDimension> otherRowDimensionList = viewDimensionDao.getGroupOtherRowDimensionList(view.getId(), viewDimension.getGroupRow());
|
|
|
|
// for (ViewDimension otherViewDimension : otherRowDimensionList) {
|
|
|
|
// rowList.add(otherViewDimension.getDimensionCode());
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// // 视图列维度(升序)
|
|
|
|
// List<ViewDimension> colDimensionList = viewDimensionDao.getColDimensionList(view.getId());
|
|
|
|
// for (ViewDimension colViewDimension : colDimensionList) {
|
|
|
|
// cloumnList.add(colViewDimension.getDimensionCode());
|
|
|
|
// }
|
|
|
|
|
|
|
|
List<Map<String, Object>> resultList = new LinkedList<Map<String, Object>>();
|
|
|
|
//合计行数据处理
|
|
|
|
Map<String, Object> sumResultMap = new HashMap<>();
|
|
|
|
sumResultMap.put(rowList.get(0),"total");//默认第一个行维度
|
|
|
|
for(String rowAggCode: rowList) {
|
|
|
|
if (rowAggCode.contains("total")) {
|
|
|
|
for (String cloumnAggCode : cloumnList) {
|
|
|
|
for(String aggCode:map.keySet()) {
|
|
|
|
Aggregation aggregation = map.get(aggCode);
|
|
|
|
if (aggregation.getName().equals("total_" + cloumnAggCode)) {
|
|
|
|
Map<String, Object> subDataMap = getInternalAggValue(cloumnAggCode, aggregation);
|
|
|
|
if (subDataMap == null || subDataMap.size() == 0) {
|
|
|
|
Terms subTerm = (Terms) aggregation;
|
|
|
|
Map<String, Object> data = getTermAggregationData(subTerm.getBuckets().iterator(), cloumnAggCode);
|
|
|
|
sumResultMap.putAll(data);
|
|
|
|
} else {
|
|
|
|
sumResultMap.putAll(subDataMap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resultList.add(sumResultMap);
|
|
|
|
|
|
|
|
|
|
|
|
//非合计数据处理
|
|
|
|
for(String rowAggCode: rowList){
|
|
|
|
if( !rowAggCode.contains("total")){
|
|
|
|
for(String aggCode:map.keySet()){
|
|
|
|
Aggregation aggregation = map.get(aggCode);
|
|
|
|
String aggName = aggregation.getName();
|
|
|
|
if(rowAggCode.equals(aggName)){
|
|
|
|
Terms terms = (Terms)aggregation;
|
|
|
|
String termNmae = terms.getName();
|
|
|
|
String key = "";
|
|
|
|
List<Terms.Bucket> buckets = terms.getBuckets();
|
|
|
|
for(Terms.Bucket bucket : buckets){
|
|
|
|
Map<String, Object> resultMap = new HashMap<>();
|
|
|
|
key = bucket.getKey().toString();
|
|
|
|
resultMap.put(termNmae,key);
|
|
|
|
for(String cloumnAggCode: cloumnList){
|
|
|
|
List<Aggregation> aggregationList = bucket.getAggregations().asList();
|
|
|
|
for(Aggregation subAgg : aggregationList){
|
|
|
|
if(subAgg.getName().equals(cloumnAggCode)){
|
|
|
|
Map<String, Object> subDataMap = getInternalAggValue(subAgg.getName(),subAgg);
|
|
|
|
if(subDataMap == null || subDataMap.size() == 0){
|
|
|
|
Terms subTerm = (Terms)subAgg;
|
|
|
|
Map<String, Object> data = getTermAggregationData(subTerm.getBuckets().iterator(), cloumnAggCode);
|
|
|
|
resultMap.putAll(data);
|
|
|
|
}else {
|
|
|
|
resultMap.putAll(subDataMap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resultList.add(resultMap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return resultList;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 数值型 - 数据结果解析
|
|
|
|
* @param
|
|
|
|
* @param map 聚合查询结果
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public Map<String, Object> numericalDataParsing(String viewCode ,Map<String, Aggregation> map){
|
|
|
|
Map<String, Object> dataMap = new HashMap<>();
|
|
|
|
View view = viewDao.findByCode(viewCode);
|
|
|
|
if(view == null){
|
|
|
|
logger.debug("视图不存在");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
List<ViewQuota> viewQuotas = viewQuotaDao.findByViewId(view.getId());
|
|
|
|
Map<String, Object> quotaMap = new HashMap<>();
|
|
|
|
for(ViewQuota viewQuota :viewQuotas){
|
|
|
|
List<ViewQuotaFilter> viewQuotaFilters = viewQuotaFilterDao.findByRelationId(viewQuota.getId());
|
|
|
|
String filter = "noFilter";
|
|
|
|
if(viewQuotaFilters != null && viewQuotaFilters.size() > 0){
|
|
|
|
quotaMap.put(viewQuota.getCode(),"filter");
|
|
|
|
}
|
|
|
|
quotaMap.put(viewQuota.getCode(),filter);
|
|
|
|
}
|
|
|
|
for(String aggCode:map.keySet()) {
|
|
|
|
Aggregation aggregation = map.get(aggCode);
|
|
|
|
for(String quotaCode :quotaMap.keySet()){
|
|
|
|
String quotaAggCode = "";
|
|
|
|
if(quotaMap.get(quotaCode).equals("filter")){
|
|
|
|
quotaAggCode = quotaCode + "-filter" ;
|
|
|
|
}
|
|
|
|
if(aggCode.equals(quotaAggCode)){
|
|
|
|
dataMap = getInternalAggValue(quotaCode, aggregation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dataMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 图表型 - 数据结果解析
|
|
* @param
|
|
* @param
|
|
* @param map 聚合查询结果
|
|
* @param map 聚合查询结果
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public List<Map<String, Object>> dataParsing(String viewCode ,Map<String, Aggregation> map){
|
|
|
|
|
|
public Option ehartDataParsing(String viewCode ,Map<String, Aggregation> map){
|
|
|
|
Option option = null;
|
|
|
|
ReportOption reportOption = new ReportOption();
|
|
|
|
View view = viewDao.findByCode(viewCode);
|
|
|
|
|
|
List<String> rowList = new LinkedList<>();
|
|
List<String> rowList = new LinkedList<>();
|
|
rowList.add("区县_terms");
|
|
rowList.add("区县_terms");
|
|
rowList.add("合计");
|
|
rowList.add("合计");
|
|
@ -137,7 +293,6 @@ public class AggregationBuildHandler {
|
|
cloumnList.add("合计_count_result");
|
|
cloumnList.add("合计_count_result");
|
|
cloumnList.add("性别_terms");
|
|
cloumnList.add("性别_terms");
|
|
|
|
|
|
// View view = viewDao.findByCode(viewCode);
|
|
|
|
// // 视图各组组内顶层行维度(升序)
|
|
// // 视图各组组内顶层行维度(升序)
|
|
// List<ViewDimension> groupTopRowDimensionList = viewDimensionDao.getGroupTopRowDimensionList(view.getId());
|
|
// List<ViewDimension> groupTopRowDimensionList = viewDimensionDao.getGroupTopRowDimensionList(view.getId());
|
|
// // 视图各组组内其他行维度(升序)
|
|
// // 视图各组组内其他行维度(升序)
|
|
@ -217,9 +372,15 @@ public class AggregationBuildHandler {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return resultList;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// option = reportOption.getLineEchartOptionMoreChart(view.getName(), "xName", "yName", lineData , lineNames, charTypes);
|
|
|
|
return option;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<Aggregation> getAggregationList(Iterator<Terms.Bucket> gradeBucket){
|
|
private List<Aggregation> getAggregationList(Iterator<Terms.Bucket> gradeBucket){
|
|
List<Aggregation> aggregationList = new ArrayList<>();
|
|
List<Aggregation> aggregationList = new ArrayList<>();
|
|
while (gradeBucket.hasNext()) {
|
|
while (gradeBucket.hasNext()) {
|