提交 9616956e 编写于 作者: xuhui961310148's avatar xuhui961310148

update:报表查询支持忽略时间范围,使用默认时间查询结果,报表导出支持计算表达式列导出

上级 aa3c45d6
...@@ -40,6 +40,8 @@ import org.springframework.util.Assert; ...@@ -40,6 +40,8 @@ import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.io.File; import java.io.File;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.nio.file.Files; import java.nio.file.Files;
...@@ -263,6 +265,15 @@ public class BuildResultService { ...@@ -263,6 +265,15 @@ public class BuildResultService {
fetchParam.getDimTables().put(table,builds.get(key)); fetchParam.getDimTables().put(table,builds.get(key));
else else
fetchParam.getDimTables().get(table).addAll(builds.get(key)); fetchParam.getDimTables().get(table).addAll(builds.get(key));
String ignoreTime = Setting.getValue(daBuild.getExtParams(), "ignoreTime");
if("Y".equals(ignoreTime)){
// 是否忽略查询时间范围(Y:是,N:否),使用默认时间范围
fetchParam.getMetrics().forEach((metricId, daMetric) ->{
if (daBuild.getBuildId().equals(daMetric.getBuildId())){
daMetric.set("ignoreTime","Y");
}
});
}
} }
} }
...@@ -507,6 +518,7 @@ public class BuildResultService { ...@@ -507,6 +518,7 @@ public class BuildResultService {
Set<String> sum=new LinkedHashSet<>(); Set<String> sum=new LinkedHashSet<>();
Set<String> avg=new LinkedHashSet<String>(); Set<String> avg=new LinkedHashSet<String>();
Set<String> countDistExt1=new LinkedHashSet<String>(); Set<String> countDistExt1=new LinkedHashSet<String>();
Set<String> ignoreTime = new LinkedHashSet<>();
Set<String> sum_yoy=new LinkedHashSet<>(); Set<String> sum_yoy=new LinkedHashSet<>();
Set<String> sum_mom=new LinkedHashSet<>(); Set<String> sum_mom=new LinkedHashSet<>();
...@@ -554,15 +566,22 @@ public class BuildResultService { ...@@ -554,15 +566,22 @@ public class BuildResultService {
} }
else else
{ {
sum.add(metric.getMetricId()); if ("Y".equals(metric.get("ignoreTime")))
if("YOY".equals(metric.getExtOp())) {
sum_yoy.add(metric.getMetricId()); ignoreTime.add(metric.getMetricId());
else if("MOM".equals(metric.getExtOp())) }
sum_mom.add(metric.getMetricId()); else
else if("Y3".equals(metric.getExtOp())) {
sum_y3.add(metric.getMetricId()); sum.add(metric.getMetricId());
else if("PER".equals(metric.getExtOp()) || "PER_ORI".equals(metric.getExtOp())) if("YOY".equals(metric.getExtOp()))
sum_per.add(metric); sum_yoy.add(metric.getMetricId());
else if("MOM".equals(metric.getExtOp()))
sum_mom.add(metric.getMetricId());
else if("Y3".equals(metric.getExtOp()))
sum_y3.add(metric.getMetricId());
else if("PER".equals(metric.getExtOp()) || "PER_ORI".equals(metric.getExtOp()))
sum_per.add(metric);
}
} }
} }
List<String> dims = new ArrayList<>(); List<String> dims = new ArrayList<>();
...@@ -624,6 +643,13 @@ public class BuildResultService { ...@@ -624,6 +643,13 @@ public class BuildResultService {
List<BuildResult> listcountDistExt1 = countDistExt1(fetchParam, countDistExt1, null, null); List<BuildResult> listcountDistExt1 = countDistExt1(fetchParam, countDistExt1, null, null);
list.addAll(listcountDistExt1); list.addAll(listcountDistExt1);
} }
if (ignoreTime.size()>0)
{
Timestamp start = Timestamp.valueOf("1970-01-01 00:00:00");
Timestamp end = new Timestamp(System.currentTimeMillis());
List<BuildResult> listignoreTime = sum(fetchParam, ignoreTime, start, end);
list.addAll(listignoreTime);
}
for(BuildResult res:list) for(BuildResult res:list)
{ {
if(!dimsSet.containsKey(res.getDimField())) if(!dimsSet.containsKey(res.getDimField()))
...@@ -934,7 +960,7 @@ public class BuildResultService { ...@@ -934,7 +960,7 @@ public class BuildResultService {
titleMap.put("content", DataObject.dayFormat.format(result.getStartTime()).concat("到").concat(DataObject.dayFormat.format(result.getEndTime()))); titleMap.put("content", DataObject.dayFormat.format(result.getStartTime()).concat("到").concat(DataObject.dayFormat.format(result.getEndTime())));
titleMap.put("fieldName", "filter"); titleMap.put("fieldName", "filter");
titleList.add(titleMap); titleList.add(titleMap);
this.onHandleColumn(result.getHeaders(),titleList,"filter"); this.onHandleColumn(result.getHeaders(),titleList,"filter",result.getRows());
} }
...@@ -943,7 +969,7 @@ public class BuildResultService { ...@@ -943,7 +969,7 @@ public class BuildResultService {
} }
public List<Map<String,String>> onHandleColumn(List<VMConfig> headers,List<Map<String,String>> columnDataList,String pid) { public List<Map<String,String>> onHandleColumn(List<VMConfig> headers, List<Map<String, String>> columnDataList, String pid, List<FetchItem> rows) {
if(columnDataList==null) columnDataList = new ArrayList<Map<String,String>>(); if(columnDataList==null) columnDataList = new ArrayList<Map<String,String>>();
if(StringUtils.isEmpty(pid)) pid="0"; if(StringUtils.isEmpty(pid)) pid="0";
for(VMConfig col:headers){ for(VMConfig col:headers){
...@@ -955,8 +981,11 @@ public class BuildResultService { ...@@ -955,8 +981,11 @@ public class BuildResultService {
titleMap.put("content",col.getHeaderName()); titleMap.put("content",col.getHeaderName());
titleMap.put("fieldName", field); titleMap.put("fieldName", field);
columnDataList.add(titleMap); columnDataList.add(titleMap);
if (StringUtils.isEmpty(col.getField()) && !StringUtils.isEmpty(col.getValueGetter())){
processExpression(field,col.getValueGetter(),rows);
}
if(!ObjectUtils.isEmpty(col.getChildren())) if(!ObjectUtils.isEmpty(col.getChildren()))
onHandleColumn(col.getChildren(),columnDataList,field); onHandleColumn(col.getChildren(),columnDataList,field, rows);
} }
return columnDataList; return columnDataList;
} }
...@@ -983,4 +1012,54 @@ public class BuildResultService { ...@@ -983,4 +1012,54 @@ public class BuildResultService {
return null; return null;
} }
/**
* 处理计算表达式并赋值到对应行数据列字段
* @param field 报表计算表达式列id
* @param valueGetter 计算表达式 示例:parseFloat((data['a31f863b14c6299bc4ed1b98badbb6d0']/((data['0fdd75f26e5d8a6c5c7fb0eb2a9e1f84'])>0?(data['0fdd75f26e5d8a6c5c7fb0eb2a9e1f84']):1)).toFixed(2))
* @param rows 报表结果行数据
*/
private void processExpression(String field, String valueGetter, List<FetchItem> rows){
if (valueGetter.indexOf("data['") >= 0){
String[] split = valueGetter.split("data\\['");
if (split.length > 0){
Map<String,Object> fieldSet = new LinkedHashMap<>();
for (int i = 0; i < split.length; i++) {
String str = split[i];
// 获取表达式中的列id
if (!StringUtils.isEmpty(str) && str.indexOf("']") > 0){
String strField = str.substring(0, str.indexOf("']"));
fieldSet.put(strField,"1");
}
}
if (fieldSet.size() > 0){
// 创建ScriptEngineManager对象
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
// 获取名为“JavaScript(或者js)”的ScriptEngine对象
ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("JavaScript");
for (FetchItem fetchItem : rows) {
String script = valueGetter;
try{
// 将表达式中的列id取值内容替换为对应列id的属性值,如:data['a31f863b14c6299bc4ed1b98badbb6d0'] => 16.0
for (String key : fieldSet.keySet()) {
script = script.replaceAll("data\\['" + key + "']",fetchItem.getVal(key) != null ? fetchItem.getVal(key).toString() : "0");
}
// 执行替换完成后的js计算表达式,如:parseFloat((16.0/13.0).toFixed(2))
Object eval = scriptEngine.eval(script);
// 将计算表达式的值赋值到行数据中的:field属性。
if (!StringUtils.isEmpty(eval)){
BigDecimal bigDecimaVal = new BigDecimal(eval.toString()).setScale(2, BigDecimal.ROUND_HALF_UP);
fetchItem.setVal(field,bigDecimaVal);
}else {
fetchItem.setVal(field,new BigDecimal(0));
}
}catch (Exception e){
e.printStackTrace();
log.error("执行计算表达式异常,原始表达式:" + valueGetter + ",替换后计算表达式:" + script + ",原因:" + e.getMessage());
}
}
}
}
}
}
} }
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册