|
@ -7,15 +7,19 @@ 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.ViewDimension;
|
|
|
import com.yihu.quota.model.view.ViewQuota;
|
|
|
import com.yihu.quota.model.view.ViewQuotaFilter;
|
|
|
import com.yihu.quota.service.cube.CubeService;
|
|
|
import org.elasticsearch.action.search.SearchRequestBuilder;
|
|
|
import org.elasticsearch.action.search.SearchResponse;
|
|
|
import org.elasticsearch.client.transport.TransportClient;
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
import org.elasticsearch.index.query.QueryBuilder;
|
|
|
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
|
|
|
import org.elasticsearch.search.aggregations.Aggregation;
|
|
|
import org.elasticsearch.search.aggregations.AggregationBuilders;
|
|
|
import org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder;
|
|
|
import org.elasticsearch.search.aggregations.bucket.terms.*;
|
|
|
import org.elasticsearch.search.aggregations.metrics.avg.InternalAvg;
|
|
|
import org.elasticsearch.search.aggregations.metrics.max.InternalMax;
|
|
@ -26,6 +30,7 @@ import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
@ -45,6 +50,8 @@ public class AggregationBuildHandler {
|
|
|
private ViewQuotaDao viewQuotaDao;
|
|
|
@Autowired
|
|
|
private ViewQuotaFilterDao viewQuotaFilterDao;
|
|
|
@Autowired
|
|
|
private CubeService cubeService;
|
|
|
|
|
|
/**
|
|
|
* 简单聚合
|
|
@ -101,8 +108,8 @@ public class AggregationBuildHandler {
|
|
|
* @return
|
|
|
*/
|
|
|
public TermsBuilder addTermsBuilder(String termName, String fieldName){
|
|
|
TermsBuilder termsBuilder= AggregationBuilders.terms(termName).field(fieldName);
|
|
|
return termsBuilder;
|
|
|
TermsBuilder termBuilder = AggregationBuilders.terms(termName).field(fieldName);
|
|
|
return termBuilder;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@ -143,38 +150,24 @@ public class AggregationBuildHandler {
|
|
|
*/
|
|
|
public List<Map<String, Object>> tableDataParsing(String viewCode ,Map<String, Aggregation> map){
|
|
|
List<String> rowList = new LinkedList<>();
|
|
|
rowList.add("区县_terms");
|
|
|
rowList.add("town_terms");
|
|
|
rowList.add("total");
|
|
|
List<String> cloumnList = new ArrayList<>();
|
|
|
cloumnList.add("total_count_result");
|
|
|
cloumnList.add("性别_terms");
|
|
|
cloumnList.add("sex_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());
|
|
|
// if(view == null){
|
|
|
// logger.debug("视图不存在");
|
|
|
// return null;
|
|
|
// }
|
|
|
// rowList = getRowDimensionList(view.getId());
|
|
|
// cloumnList = getColumnDimensionList(view.getId());
|
|
|
|
|
|
List<Map<String, Object>> resultList = new LinkedList<Map<String, Object>>();
|
|
|
//合计行数据处理
|
|
|
Map<String, Object> sumResultMap = new HashMap<>();
|
|
|
sumResultMap.put(rowList.get(0),"total");//默认第一个行维度
|
|
|
//合计行 数据处理 开始
|
|
|
Map<String, Object> totalResultMap = new HashMap<>();
|
|
|
totalResultMap.put(rowList.get(0),"total");//默认第一个行维度
|
|
|
for(String rowAggCode: rowList) {
|
|
|
if (rowAggCode.contains("total")) {
|
|
|
for (String cloumnAggCode : cloumnList) {
|
|
@ -185,17 +178,17 @@ public class AggregationBuildHandler {
|
|
|
if (subDataMap == null || subDataMap.size() == 0) {
|
|
|
Terms subTerm = (Terms) aggregation;
|
|
|
Map<String, Object> data = getTermAggregationData(subTerm.getBuckets().iterator(), cloumnAggCode);
|
|
|
sumResultMap.putAll(data);
|
|
|
totalResultMap.putAll(data);
|
|
|
} else {
|
|
|
sumResultMap.putAll(subDataMap);
|
|
|
totalResultMap.putAll(subDataMap);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
resultList.add(sumResultMap);
|
|
|
|
|
|
resultList.add(totalResultMap);
|
|
|
//合计行 数据处理 接收
|
|
|
|
|
|
//非合计数据处理
|
|
|
for(String rowAggCode: rowList){
|
|
@ -210,7 +203,7 @@ public class AggregationBuildHandler {
|
|
|
List<Terms.Bucket> buckets = terms.getBuckets();
|
|
|
for(Terms.Bucket bucket : buckets){
|
|
|
Map<String, Object> resultMap = new HashMap<>();
|
|
|
key = bucket.getKey().toString();
|
|
|
key = bucket.getKeyAsString() != null ? bucket.getKeyAsString() : bucket.getKey().toString();
|
|
|
resultMap.put(termNmae,key);
|
|
|
for(String cloumnAggCode: cloumnList){
|
|
|
List<Aggregation> aggregationList = bucket.getAggregations().asList();
|
|
@ -244,39 +237,43 @@ public class AggregationBuildHandler {
|
|
|
* @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> resultMap = 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(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);
|
|
|
// }
|
|
|
quotaMap.put("total_total","noFilter");
|
|
|
quotaMap.put("total_sex-1","noFilter");
|
|
|
for(String aggCode:map.keySet()) {
|
|
|
Aggregation aggregation = map.get(aggCode);
|
|
|
for(String quotaCode :quotaMap.keySet()){
|
|
|
String quotaAggCode = "";
|
|
|
String quotaAggCode = quotaCode;
|
|
|
if(quotaMap.get(quotaCode).equals("filter")){
|
|
|
quotaAggCode = quotaCode + "-filter" ;
|
|
|
}
|
|
|
if(aggCode.equals(quotaAggCode)){
|
|
|
dataMap = getInternalAggValue(quotaCode, aggregation);
|
|
|
if(!StringUtils.isEmpty(quotaAggCode) && aggCode.contains(quotaAggCode)){
|
|
|
Map<String, Object> dataMap = getInternalAggValue(quotaCode, aggregation);
|
|
|
resultMap.putAll(dataMap);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return dataMap;
|
|
|
return resultMap;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 图表型 - 数据结果解析
|
|
|
* 一个展示维度,多个指标组成
|
|
|
* @param
|
|
|
* @param map 聚合查询结果
|
|
|
* @return
|
|
@ -284,103 +281,103 @@ public class AggregationBuildHandler {
|
|
|
public Option ehartDataParsing(String viewCode ,Map<String, Aggregation> map){
|
|
|
Option option = null;
|
|
|
ReportOption reportOption = new ReportOption();
|
|
|
View view = viewDao.findByCode(viewCode);
|
|
|
List<String> xDataList = new LinkedList<>();
|
|
|
List<String> charTypes = new LinkedList<>();
|
|
|
List<String> lineNames = new LinkedList<>();
|
|
|
List<List<Object>> lineDataList = new LinkedList<>();
|
|
|
|
|
|
List<String> rowList = new LinkedList<>();
|
|
|
rowList.add("区县_terms");
|
|
|
rowList.add("合计");
|
|
|
List<String> cloumnList = new ArrayList<>();
|
|
|
cloumnList.add("合计_count_result");
|
|
|
cloumnList.add("性别_terms");
|
|
|
|
|
|
// // 视图各组组内顶层行维度(升序)
|
|
|
// 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),"合计");//默认第一个行维度
|
|
|
for(String rowAggCode: rowList) {
|
|
|
if (rowAggCode.contains("合计")) {
|
|
|
for (String cloumnAggCode : cloumnList) {
|
|
|
for(String aggCode:map.keySet()) {
|
|
|
Aggregation aggregation = map.get(aggCode);
|
|
|
if (aggregation.getName().equals("合计_" + 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);
|
|
|
rowList.add("town_terms");
|
|
|
View view = viewDao.findByCode(viewCode);
|
|
|
// rowList = getRowDimensionList(view.getId());
|
|
|
if(rowList.size() != 1){
|
|
|
logger.debug("图表型指标维度配置有误");
|
|
|
return null;
|
|
|
}
|
|
|
String rowAggCode = rowList.get(0);
|
|
|
|
|
|
List<String> quotaList = new LinkedList<>();
|
|
|
Map<String,List<Object>> quotasDataMap = new LinkedHashMap<>();
|
|
|
List<ViewQuota> viewQuotas = new ArrayList<>();
|
|
|
ViewQuota vq = new ViewQuota();
|
|
|
vq.setChartType("1");
|
|
|
vq.setCode("total");
|
|
|
vq.setName("各区县总人数");
|
|
|
viewQuotas.add(vq);
|
|
|
// viewQuotas = viewQuotaDao.findByViewId(view.getId());
|
|
|
for(ViewQuota viewQuota :viewQuotas){
|
|
|
quotaList.add(viewQuota.getCode());
|
|
|
charTypes.add(viewQuota.getChartType());
|
|
|
lineNames.add(viewQuota.getName());
|
|
|
}
|
|
|
Aggregation aggregation = map.get(rowAggCode);
|
|
|
if(rowAggCode.equals(aggregation.getName())){
|
|
|
Terms terms = (Terms)aggregation;
|
|
|
String key = "";
|
|
|
List<Terms.Bucket> buckets = terms.getBuckets();
|
|
|
for(String quotaCode: quotaList){
|
|
|
List<Object> quotaData = new ArrayList<>();
|
|
|
for(Terms.Bucket bucket : buckets){
|
|
|
key = bucket.getKeyAsString() != null ? bucket.getKeyAsString() : bucket.getKey().toString();
|
|
|
xDataList.add(key);
|
|
|
List<Aggregation> aggregationList = bucket.getAggregations().asList();
|
|
|
for(Aggregation subAgg : aggregationList){
|
|
|
if(subAgg.getName().contains(quotaCode)){
|
|
|
Map<String, Object> subDataMap = getInternalAggValue(subAgg.getName(),subAgg);
|
|
|
if(subDataMap != null || subDataMap.size() > 0){
|
|
|
quotaData.add(subDataMap.get(subAgg.getName()));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
quotasDataMap.put(quotaCode, quotaData);
|
|
|
}
|
|
|
}
|
|
|
resultList.add(sumResultMap);
|
|
|
for (Map.Entry<String,List<Object>> entry : quotasDataMap.entrySet()) {
|
|
|
lineDataList.add(entry.getValue());
|
|
|
}
|
|
|
option = reportOption.getLineEchartOptionMoreChart(view.getName(), "xName", "yName", xDataList.toArray(), lineDataList , lineNames, charTypes);
|
|
|
return option;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取列维度
|
|
|
* @param viewId
|
|
|
* @return
|
|
|
*/
|
|
|
private List<String> getColumnDimensionList(String viewId){
|
|
|
List<String> cloumnList = new ArrayList<>();
|
|
|
List<ViewDimension> colDimensionList = viewDimensionDao.getColDimensionList(viewId);
|
|
|
for (ViewDimension colViewDimension : colDimensionList) {
|
|
|
cloumnList.add(colViewDimension.getDimensionCode());
|
|
|
}
|
|
|
return cloumnList;
|
|
|
}
|
|
|
|
|
|
//非合计数据处理
|
|
|
for(String rowAggCode: rowList){
|
|
|
if( !rowAggCode.contains("合计")){
|
|
|
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);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
/**
|
|
|
* 获取行维度
|
|
|
* @param viewId
|
|
|
* @return
|
|
|
*/
|
|
|
private List<String> getRowDimensionList(String viewId){
|
|
|
List<String> rowList = new ArrayList<>();
|
|
|
// 视图各组组内顶层行维度(升序)
|
|
|
List<ViewDimension> groupTopRowDimensionList = viewDimensionDao.getGroupTopRowDimensionList(viewId);
|
|
|
for (ViewDimension viewDimension : groupTopRowDimensionList) {
|
|
|
String dataType = cubeService.findDimensionDataType("cubeCode:1", viewDimension.getDimensionCode());
|
|
|
String aggType = "-terms";
|
|
|
if(dataType.equals("date")){
|
|
|
aggType = "-date_histogram";
|
|
|
}
|
|
|
rowList.add(viewDimension.getDimensionCode() + aggType);
|
|
|
List<ViewDimension> otherRowDimensionList = viewDimensionDao.getGroupOtherRowDimensionList(viewId, viewDimension.getGroupRow());
|
|
|
for (ViewDimension otherViewDimension : otherRowDimensionList) {
|
|
|
rowList.add(otherViewDimension.getDimensionCode() + aggType);;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// option = reportOption.getLineEchartOptionMoreChart(view.getName(), "xName", "yName", lineData , lineNames, charTypes);
|
|
|
return option;
|
|
|
return rowList;
|
|
|
}
|
|
|
|
|
|
|
|
|
private List<Aggregation> getAggregationList(Iterator<Terms.Bucket> gradeBucket){
|
|
|
List<Aggregation> aggregationList = new ArrayList<>();
|
|
|
while (gradeBucket.hasNext()) {
|