提交 997ac7c1 编写于 作者: sq3536's avatar sq3536

提交

上级 cb3a98b0
......@@ -10,6 +10,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.stereotype.Repository;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
......@@ -125,7 +126,7 @@ public class ExecResultRepository {
public List<ExecResult> sum(List<String> ruleids, Integer retValue, List<String> dims, Timestamp start, Timestamp end) {
public List<ExecResult> sum(Collection<String> ruleids, Integer retValue, List<String> dims, Timestamp start, Timestamp end) {
LocalDate st = ExecResult.time2LocalDate(start);
LocalDate ed = ExecResult.time2LocalDate(end);
final ResultSet result = session.execute(select().column("ruleid").column("retvalue").column("dimfield").sum("metricfield").as("metricfield").
......@@ -137,7 +138,7 @@ public class ExecResultRepository {
return mapper.map(result).all();
}
public List<ExecResult> avg(List<String> ruleids, Integer retValue, List<String> dims, Timestamp start, Timestamp end) {
public List<ExecResult> avg(Collection<String> ruleids, Integer retValue, List<String> dims, Timestamp start, Timestamp end) {
LocalDate st = ExecResult.time2LocalDate(start);
LocalDate ed = ExecResult.time2LocalDate(end);
final ResultSet result = session.execute(select().column("ruleid").column("retvalue").column("dimfield").avg("metricfield").as("metricfield").
......@@ -149,7 +150,7 @@ public class ExecResultRepository {
return mapper.map(result).all();
}
public List<ExecResult> group(List<String> ruleids, Integer retValue, List<String> dims, String type, Timestamp start, Timestamp end) {
public List<ExecResult> group(Collection<String> ruleids, Integer retValue, List<String> dims, String type, Timestamp start, Timestamp end) {
if (type.equalsIgnoreCase("avg"))
return avg(ruleids, retValue, dims, start, end);
else
......
package cn.ibizlab.core.extensions.dto;
import cn.ibizlab.core.lite.extensions.domain.DataObj;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.math.BigDecimal;
@JsonIgnoreProperties(ignoreUnknown = true)
public class FetchItem extends DataObj {
// 表格展示类型:1. default(维度数据以行的形式展示),2. LIST_BOX(维度数据以列的形式展示)
public String getDisplayType() {
return this.getStringValue("displayType","default");
}
public FetchItem setDisplayType(String displayType) {
this.put("displayType", displayType);
return this;
}
public int getNo() {
return this.getIntegerValue("no",1);
}
public FetchItem setNo(int no) {
this.put("no",no);
return this;
}
public String getDimId() {
return this.getStringValue("dimId");
}
public FetchItem setDimId(String dimId) {
this.put("dimId", dimId);
this.put("itemId",dimId);
return this;
}
public String getDimName() {
return this.getStringValue("dimName");
}
public FetchItem setDimName(String dimName) {
this.put("dimName",dimName);
this.put("itemName",dimName);
return this;
}
public String getMetricId1() {
return this.getStringValue("metricId");
}
public FetchItem setMetricId(String metricId) {
this.put("metricId",metricId);
this.put("itemId",metricId);
return this;
}
public String getMetricName() {
return this.getStringValue("metricName");
}
public FetchItem setMetricName(String metricName) {
this.put("metricName",metricName);
this.put("itemName",metricName);
return this;
}
public BigDecimal getVal(String colId) {
return this.getBigDecimalValue(colId,BigDecimal.ZERO);
}
public FetchItem setVal(String colId, BigDecimal val) {
if(val==null)
val=BigDecimal.ZERO;
this.put(colId,val);
return this;
}
}
package cn.ibizlab.core.extensions.dto;
import cn.ibizlab.core.analysis.domain.DAMetric;
import cn.ibizlab.util.dict.CodeItem;
import cn.ibizlab.util.helper.DataObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
......@@ -12,10 +15,10 @@ import lombok.experimental.Accessors;
import org.springframework.util.StringUtils;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
......@@ -52,6 +55,8 @@ public class FetchParam {
private int page = 0;
private int size = 100;
private boolean filled = false;
public List<String> getDimValues()
{
if(dimValues == null&&(!includeChild)&&(!StringUtils.isEmpty(dimValue)))
......@@ -75,10 +80,28 @@ public class FetchParam {
@JsonIgnore
@JSONField(serialize = false)
private Map<String,List<String>> dimTables;
private CodeItem dimItem;
@JsonIgnore
@JSONField(serialize = false)
public CodeItem getDimItem()
{
return dimItem;
}
@JsonIgnore
@JSONField(serialize = false)
public Map<String,List<String>> getDimTables()
public FetchParam setDimItem(CodeItem dimItem)
{
this.dimItem = dimItem;
return this;
}
@JsonIgnore
@JSONField(serialize = false)
private Map<String,Set<String>> dimTables;
@JsonIgnore
@JSONField(serialize = false)
public Map<String,Set<String>> getDimTables()
{
if(dimTables==null)
dimTables = new LinkedHashMap<>();
......@@ -86,10 +109,125 @@ public class FetchParam {
}
@JsonIgnore
@JSONField(serialize = false)
public FetchParam getDimTables(Map<String,List<String>> dimTables)
public FetchParam setDimTables(Map<String, Set<String>> dimTables)
{
this.dimTables = dimTables;
return this;
}
@JsonIgnore
@JSONField(serialize = false)
private Map<String, DAMetric> metrics;
@JsonIgnore
@JSONField(serialize = false)
public Map<String,DAMetric> getMetrics()
{
if(metrics==null)
metrics = new LinkedHashMap<>();
return metrics;
}
@JsonIgnore
@JSONField(serialize = false)
public FetchParam setMetrics(Map<String,DAMetric> metrics)
{
this.metrics = metrics;
return this;
}
public Timestamp getStartTime() {
if(startTime!=null)
{
try {
return new Timestamp(DataObject.dayFormat.parse(DataObject.dayFormat.format(startTime)).getTime());
} catch (Exception exception) {
}
}
return getDefaultStartTimestamp();
}
public Timestamp getEndTime() {
if(endTime!=null)
{
try {
return new Timestamp(DataObject.dayFormat.parse(DataObject.dayFormat.format(endTime)+" 23:59:59").getTime());
} catch (Exception exception) {
}
}
return getDefaultEndTimestamp();
}
@JsonIgnore
@JSONField(serialize = false)
private List<VMConfig> vmConfigs;
@JsonIgnore
@JSONField(serialize = false)
private String group;
public static Timestamp getDefaultStartTimestamp() {
Calendar c = Calendar.getInstance();
Date d=new Date();
try {
d = DataObject.dayFormat.parse(DataObject.dayFormat.format(d));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
c.setTime(d);
c.add(Calendar.MONTH, -1);
c.set(Calendar.DAY_OF_MONTH, 1);
return new Timestamp(c.getTime().getTime());
}
public static Timestamp getDefaultEndTimestamp() {
Calendar c = Calendar.getInstance();
Date d=new Date();
try {
d = DataObject.dayFormat.parse(DataObject.dayFormat.format(d)+" 23:59:59");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
c.setTime(d);
int month=c.get(Calendar.MONTH);
c.set(Calendar.MONTH, month-1);
c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
return new Timestamp(c.getTime().getTime());
}
public static Timestamp getLastYear(Timestamp time)
{
Date date=new Date(time.getTime());
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
// 设置为当前时间
calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - 1);
// 设置为上一年
date = calendar.getTime();
Timestamp rt=new Timestamp(date.getTime());
return rt;
}
public static Timestamp getLastMonth(Timestamp time)
{
Date date=new Date(time.getTime());
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
// 设置为当前时间
calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
// 设置为上一个月
date = calendar.getTime();
Timestamp rt=new Timestamp(date.getTime());
return rt;
}
}
package cn.ibizlab.core.extensions.dto;
import cn.ibizlab.core.analysis.domain.DAMetric;
import cn.ibizlab.util.dict.CodeItem;
import cn.ibizlab.util.helper.DataObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.util.StringUtils;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.*;
@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
@Setter
@NoArgsConstructor
@Accessors(chain = true)
public class FetchResult {
// 表格展示类型:1. default(维度数据以行的形式展示),2. LIST_BOX(维度数据以列的形式展示)
private String displayType = "default";
private List<FetchItem> rows;
private List<VMConfig> headers;
public List<VMConfig> getHeaders() {
if(headers==null)
{
headers = new ArrayList<>();
headers.add(new VMConfig().setHeaderName("序号").setEntity("item").setField("no").setPinned("left"));
headers.add(new VMConfig().setHeaderName(getGroup()).setEntity("item").setField("itemName").setPinned("left"));
if(vmConfigs!=null)
headers.addAll(vmConfigs);
}
return headers;
}
public FetchResult trans2ListBox()
{
FetchResult listBox=new FetchResult();
List<VMConfig> vms=new ArrayList<>();
rows.forEach(row->{
VMConfig config=new VMConfig();
config.setHeaderName(row.getDimName()).setEntity("item").setField(row.getDimId()).setWidth(80);
vms.add(config);
});
this.setVmConfigs(vms);
return listBox;
}
@JsonIgnore
@JSONField(serialize = false)
private List<VMConfig> vmConfigs;
@JsonIgnore
@JSONField(serialize = false)
private String group;
public static FetchResult from(FetchParam fetchParam)
{
FetchResult fetchResult=new FetchResult();
fetchResult.setDisplayType(fetchParam.getDisplayType());
if(fetchParam.getVmConfigs()!=null)
{
fetchResult.setVmConfigs(fetchParam.getVmConfigs());
}
fetchResult.setGroup(StringUtils.isEmpty(fetchParam.getGroup())?"分项":fetchParam.getGroup());
return fetchResult;
}
}
......@@ -3,6 +3,7 @@ package cn.ibizlab.core.extensions.dto;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.NoArgsConstructor;
......@@ -13,19 +14,21 @@ import org.springframework.util.StringUtils;
import java.util.*;
@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
@Setter
@NoArgsConstructor
@Accessors(chain = true)
public class VMConfig {
private String headerName;
private String width;
private Integer width;
@JSONField(name = "entity")
@JsonProperty("entity")
private String buildId;
private String entity;
@JSONField(name = "field")
@JsonProperty("field")
private String metricId;
private String field;
private String pinned;
private List<VMConfig> children;
public Map<String, Set<String>> getBuilds(Map<String,Set<String>> builds)
......@@ -34,17 +37,17 @@ public class VMConfig {
{
builds = new LinkedHashMap<>();
}
if((!StringUtils.isEmpty(buildId)) && (!StringUtils.isEmpty(metricId)))
if((!StringUtils.isEmpty(entity)) && (!StringUtils.isEmpty(field)))
{
if(builds.containsKey(buildId))
if(builds.containsKey(entity))
{
builds.get(buildId).add(metricId);
builds.get(entity).add(field);
}
else
{
Set<String> metricIds = new LinkedHashSet<>();
metricIds.add(metricId);
builds.put(buildId,metricIds);
metricIds.add(field);
builds.put(entity,metricIds);
}
}
if(!ObjectUtils.isEmpty(children))
......
package cn.ibizlab.core.extensions.service;
import cn.ibizlab.core.analysis.domain.DABuild;
import cn.ibizlab.core.analysis.domain.DAChart;
import cn.ibizlab.core.analysis.domain.DAMetric;
import cn.ibizlab.core.analysis.domain.DAReport;
import cn.ibizlab.core.analysis.service.IDAChartService;
import cn.ibizlab.core.analysis.service.IDAMetricService;
import cn.ibizlab.core.analysis.service.impl.DAReportServiceImpl;
import cn.ibizlab.core.dict.extensions.service.DictDstService;
import cn.ibizlab.core.extensions.cql.ExecResult;
import cn.ibizlab.core.extensions.cql.ExecResultRepository;
import cn.ibizlab.core.extensions.dto.BuildResult;
import cn.ibizlab.core.extensions.dto.FetchParam;
import cn.ibizlab.core.extensions.dto.FetchItem;
import cn.ibizlab.core.extensions.dto.VMConfig;
import cn.ibizlab.core.rule.domain.ExecResult;
import cn.ibizlab.core.rule.service.IExecResultService;
import cn.ibizlab.util.dict.CodeItem;
import cn.ibizlab.util.dict.CodeList;
import cn.ibizlab.util.helper.Setting;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.datastax.driver.core.PagingState;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
......@@ -22,6 +34,7 @@ import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.*;
......@@ -51,142 +64,662 @@ public class BuildResultService {
@Lazy
private IDAChartService daChartService;
@Autowired
@Lazy
private IDAMetricService daMetricService;
@Autowired
@Lazy
private DABuildExService daBuildExService;
@Autowired
@Lazy
private DstDataSourceExService dstDataSourceService;
@Autowired
@Lazy
private IExecResultService execResultService;
private void prepareFetchParam(FetchParam fetchParam)
{
if( (!StringUtils.isEmpty(fetchParam.getDimDict())) && (!StringUtils.isEmpty(fetchParam.getDimValue())) && fetchParam.isIncludeChild() && fetchParam.getDimValues() == null)
{
List<String> dimValues = new ArrayList<>();
dimValues.add(fetchParam.getDimValue());
CodeList dict = dictDstService.getCodeListCatalog(fetchParam.getDimDict());
if(dict!=null)
{
CodeItem code= dict.findChildren(fetchParam.getDimValue());
if(code!=null && code.getChildren()!=null) {
code.getChildren().forEach(codeItem -> {
dimValues.add(codeItem.getValue().toString());
});
}
}
fetchParam.setDimValues(dimValues);
}
if(fetchParam.isFilled())
return;
Map<String, Set<String>> builds = null;
if(ObjectUtils.isEmpty(fetchParam.getMetricIds()))
{
String vmConfig="";
if(!StringUtils.isEmpty(fetchParam.getReportId()))
{
DAReport daReport = daReportService.getById(fetchParam.getReportId());
if(daReport != null)
if(daReport != null) {
vmConfig = daReport.getConfig();
if(!StringUtils.isEmpty(daReport.getDict()))
fetchParam.setDimDict(daReport.getDict());
if(!StringUtils.isEmpty(daReport.getGroup()))
fetchParam.setGroup(daReport.getGroup());
}
}
else if(!StringUtils.isEmpty(fetchParam.getChartId()))
{
DAChart daChart = daChartService.getById(fetchParam.getChartId());
if(daChart != null)
if(daChart != null) {
vmConfig = daChart.getConfig();
if(!StringUtils.isEmpty(daChart.getDict()))
fetchParam.setDimDict(daChart.getDict());
if(!StringUtils.isEmpty(daChart.getGroup()))
fetchParam.setGroup(daChart.getGroup());
}
}
Map<String, Set<String>> builds = null;
if(StringUtils.isEmpty(vmConfig))
if(!StringUtils.isEmpty(vmConfig)) {
builds = VMConfig.getBuilds(vmConfig);
fetchParam.setVmConfigs(JSONArray.parseArray(vmConfig,VMConfig.class));
Map<String, DAMetric> metricMap = new LinkedHashMap<>();
daMetricService.list(Wrappers.<DAMetric>lambdaQuery().in(DAMetric::getMetricId,fetchParam.getMetricIds())).forEach(item->{
metricMap.put(item.getMetricId(),item);
fetchParam.getMetricIds().add(item.getMetricId());
});
fetchParam.setMetrics(metricMap);
}
else
{
builds = new LinkedHashMap<>();
if(StringUtils.isEmpty(fetchParam.getBuildId())) {
builds.put(fetchParam.getBuildId(), new LinkedHashSet<>());
if(!StringUtils.isEmpty(fetchParam.getBuildId())) {
Map<String, DAMetric> metricMap = new LinkedHashMap<>();
daMetricService.selectByBuildId(fetchParam.getBuildId()).forEach(item->{
metricMap.put(item.getMetricId(),item);
fetchParam.getMetricIds().add(item.getMetricId());
});
fetchParam.setMetrics(metricMap);
Set set=new LinkedHashSet();
set.addAll(fetchParam.getMetricIds());
builds.put(fetchParam.getBuildId(), set);
}
}
builds.keySet().forEach(key->{
});
}
else// if(StringUtils.isEmpty(fetchParam.getReportId())&&StringUtils.isEmpty(fetchParam.getChartId())&&StringUtils.isEmpty(fetchParam.getBuildId()))
{
builds = new LinkedHashMap<>();
Map<String, DAMetric> metricMap = new LinkedHashMap<>();
List<DAMetric> list=daMetricService.list(Wrappers.<DAMetric>lambdaQuery().in(DAMetric::getMetricId,fetchParam.getMetricIds()));
for(DAMetric item:list){
metricMap.put(item.getMetricId(),item);
if(!builds.containsKey(item.getBuildId()))
builds.put(item.getBuildId(),new LinkedHashSet());
builds.get(item.getBuildId()).add(item.getMetricId());
}
fetchParam.setMetrics(metricMap);
}
if( (!StringUtils.isEmpty(fetchParam.getDimDict())) && (!StringUtils.isEmpty(fetchParam.getDimValue())) && fetchParam.isIncludeChild() && fetchParam.getDimValues() == null)
{
List<String> dimValues = new ArrayList<>();
CodeList dict = dictDstService.getCodeListCatalog(fetchParam.getDimDict());
if(StringUtils.isEmpty(fetchParam.getGroup()))
fetchParam.setGroup(dict.getName());
if(dict!=null)
{
CodeItem dimItem= dict.findChildren(fetchParam.getDimValue());
if(dimItem!=null) {
fetchParam.setDimItem(dimItem);
if(dimItem.getChildren()!=null)
{
dimItem.getChildren().forEach(codeItem -> {
dimValues.add(codeItem.getValue().toString());
});
}
}
}
dimValues.add(fetchParam.getDimValue());
fetchParam.setDimValues(dimValues);
}
if(builds!=null)
{
for(String key:builds.keySet())
{
DABuild daBuild = daBuildExService.getById(key);
if(daBuild!=null)
{
String resultDataSource = Setting.getValue(daBuild.getExtParams(), "resultDataSource");
String resultTableName = Setting.getValue(daBuild.getExtParams(), "resultTableName");
if(StringUtils.isEmpty(resultDataSource))
resultDataSource="default";
if(StringUtils.isEmpty(resultTableName))
resultTableName="IBZRULERESULT";
String table=resultDataSource+"|"+resultTableName;
if(!fetchParam.getDimTables().containsKey(table))
fetchParam.getDimTables().put(table,builds.get(key));
else
fetchParam.getDimTables().get(table).addAll(builds.get(key));
}
}
}
fetchParam.setFilled(true);
}
public List<BuildResult> sum(FetchParam fetchParam)
{
return sum(fetchParam,null,null,null);
}
public List<BuildResult> sum(FetchParam fetchParam,Set<String> subMetricIds,Timestamp start,Timestamp end) {
List<BuildResult> ret = new ArrayList<>();
Timestamp startTime=(start==null)?fetchParam.getStartTime():start;
Timestamp endTime=(end==null)?fetchParam.getEndTime():end;
if(subMetricIds==null)
{
subMetricIds=new LinkedHashSet<>();
for(DAMetric item:fetchParam.getMetrics().values()){
if(!"AVG".equals(item.getMetricType()))
subMetricIds.add(item.getMetricId());
}
}
if(!StringUtils.isEmpty(cassandraHost))
{
List<cn.ibizlab.core.extensions.cql.ExecResult> list=execResultRepository.sum(subMetricIds,1,fetchParam.getDimValues(),startTime,endTime);
list.forEach(item->{
ret.add(new BuildResult().setCqlResult(item));
});
}
else
{
//prepareFetchParam(fetchParam);
Map<String,Set<String>> tab = fetchParam.getDimTables();
for(String key:tab.keySet()){
String[] args = key.split("\\|");
String dsName = args[0];
String tableName = args[1];
public List<BuildResult> sum(List<String> ruleids, Integer retValue, List<String> dims, Timestamp start, Timestamp end) {
try {
if(!"default".equalsIgnoreCase(dsName)) {
dstDataSourceService.initDataSource(dsName);
DynamicDataSourceContextHolder.push(dsName);
}
Set<String> tmpSet=new LinkedHashSet<>();
for(String id:tab.get(key))
{
if(subMetricIds.contains(id))
tmpSet.add(id);
}
if(tmpSet.size()>0)
{
List<ExecResult> list = execResultService.list(Wrappers.<ExecResult>query()
.select("ruleid","1 as retvalue","dimfield","sum(metricfield) as metricfield")
.in("ruleid",tmpSet).eq("retvalue",1).in("dimfield",fetchParam.getDimValues())
.ge("timefield",startTime).le("timefield",endTime)
.groupBy("ruleid", "dimfield"));
list.forEach(item->{
ret.add(new BuildResult().setSqlResult(item));
});
}
} catch (Exception ex) {
log.error("汇总分析结果发生异常,详细错误信息:" + ex.getMessage());
} finally {
if(!"default".equalsIgnoreCase(dsName)) {
DynamicDataSourceContextHolder.poll();
}
}
}
}
return ret;
}
public List<BuildResult> avg(FetchParam fetchParam)
{
return avg(fetchParam,null,null,null);
}
public List<BuildResult> avg(FetchParam fetchParam,Set<String> subMetricIds,Timestamp start,Timestamp end) {
List<BuildResult> ret = new ArrayList<>();
Timestamp startTime=(start==null)?fetchParam.getStartTime():start;
Timestamp endTime=(end==null)?fetchParam.getEndTime():end;
if(subMetricIds==null)
{
subMetricIds=new LinkedHashSet<>();
for(DAMetric item:fetchParam.getMetrics().values()){
if("AVG".equals(item.getMetricType()))
subMetricIds.add(item.getMetricId());
}
}
if(!StringUtils.isEmpty(cassandraHost))
{
List<ExecResult> list=execResultRepository.sum(ruleids,retValue,dims,start,end);
List<cn.ibizlab.core.extensions.cql.ExecResult> list=execResultRepository.avg(fetchParam.getMetricIds(),1,fetchParam.getDimValues(),startTime,endTime);
list.forEach(item->{
ret.add(new BuildResult().setCqlResult(item));
});
}
else
{
//prepareFetchParam(fetchParam);
Map<String,Set<String>> tab = fetchParam.getDimTables();
for(String key:tab.keySet()){
String[] args = key.split("\\|");
String dsName = args[0];
String tableName = args[1];
try {
if(!"default".equalsIgnoreCase(dsName)) {
dstDataSourceService.initDataSource(dsName);
DynamicDataSourceContextHolder.push(dsName);
}
Set<String> tmpSet=new LinkedHashSet<>();
for(String id:tab.get(key))
{
if(subMetricIds.contains(id))
tmpSet.add(id);
}
if(tmpSet.size()>0)
{
List<ExecResult> list = execResultService.list(Wrappers.<ExecResult>query()
.select("ruleid","1 as retvalue","dimfield","avg(metricfield) as metricfield")
.in("ruleid",tab.get(key)).eq("retvalue",1).in("dimfield",fetchParam.getDimValues())
.ge("timefield",startTime).le("timefield",endTime)
.groupBy("ruleid", "dimfield"));
list.forEach(item->{
ret.add(new BuildResult().setSqlResult(item));
});
}
} catch (Exception ex) {
log.error("汇总分析结果发生异常,详细错误信息:" + ex.getMessage());
} finally {
if(!"default".equalsIgnoreCase(dsName)) {
DynamicDataSourceContextHolder.poll();
}
}
}
}
return ret;
// LocalDate st = ExecResult.time2LocalDate(start);
// LocalDate ed = ExecResult.time2LocalDate(end);
// final ResultSet result = session.execute(select().column("ruleid").column("retvalue").column("dimfield").sum("metricfield").as("metricfield").
// from(TABLE).
// where(in("ruleid", ruleids)).
// and(eq("retvalue", retValue)).
// and(in("dimfield", dims)).
// and(gte("timefield", st)).and(lte("timefield", ed)).groupBy("ruleid", "retvalue", "dimfield").limit(5000).setReadTimeoutMillis(200000));
// return mapper.map(result).all();
// }
//
// public List<ExecResult> avg(List<String> ruleids, Integer retValue, List<String> dims, Timestamp start, Timestamp end) {
// LocalDate st = ExecResult.time2LocalDate(start);
// LocalDate ed = ExecResult.time2LocalDate(end);
// final ResultSet result = session.execute(select().column("ruleid").column("retvalue").column("dimfield").avg("metricfield").as("metricfield").
// from(TABLE).
// where(in("ruleid", ruleids)).
// and(eq("retvalue", retValue)).
// and(in("dimfield", dims)).
// and(gte("timefield", st)).and(lte("timefield", ed)).groupBy("ruleid", "retvalue", "dimfield").limit(5000).setReadTimeoutMillis(200000));
// return mapper.map(result).all();
// }
//
// public List<ExecResult> group(List<String> ruleids, Integer retValue, List<String> dims, String type, Timestamp start, Timestamp end) {
// if (type.equalsIgnoreCase("avg"))
// return avg(ruleids, retValue, dims, start, end);
// else
// return sum(ruleids, retValue, dims, start, end);
// }
//
// public List<ExecResult> group(List<String> ruleids, Integer retValue, List<String> dims, Timestamp start, Timestamp end) {
//
// return group(ruleids, retValue, dims, "sum", start, end);
// }
//
// /**
// * 根据规则ID、单位和时间查询相应的规则结果数据
// *
// * @param ruleid
// * @param retValue
// * @param dims
// * @param start
// * @param end
// * @return
// */
// public ResultSet getPageData(String ruleid, PagingState pagingState, Integer retValue, String dims, Timestamp start, Timestamp end) {
// final int RESULTS_PER_PAGE = 1000;
// LocalDate st = ExecResult.time2LocalDate(start);
// LocalDate ed = ExecResult.time2LocalDate(end);
// Statement statement = select().column("keyvaluefield").
// from(TABLE).
// where(eq("ruleid", ruleid)).
// and(eq("retvalue", retValue)).
// and(eq("dimfield", dims)).
// and(gte("timefield", st)).and(lte("timefield", ed)).setReadTimeoutMillis(200000);
// statement.setFetchSize(RESULTS_PER_PAGE);
// if (pagingState != null) {
// statement.setPagingState(pagingState);
// }
// final ResultSet result = session.execute(statement);
// return result;
}
public List<String> lookup(FetchParam fetchParam) {
List<String> ret = new ArrayList<>();
if(!StringUtils.isEmpty(cassandraHost))
{
PagingState pagingState = null;
for (int i=0;i<=fetchParam.getPage();i++) {
if (fetchParam.getPage() == i) {
ResultSet rs=execResultRepository.getPageData(fetchParam.getMetricId(),pagingState,1,fetchParam.getDimValue(),fetchParam.getStartTime(),fetchParam.getEndTime());
int remaining = rs.getAvailableWithoutFetching();
for (Row row : rs) {
ret.add(row.getString("keyvaluefield"));
if (--remaining == 0) {
break;
}
}
break;
} else {
ResultSet rs=execResultRepository.getPageData(fetchParam.getMetricId(),pagingState,1,fetchParam.getDimValue(),fetchParam.getStartTime(),fetchParam.getEndTime());
pagingState = rs.getExecutionInfo().getPagingState();
}
}
}
else
{
//prepareFetchParam(fetchParam);
Map<String,Set<String>> tab = fetchParam.getDimTables();
tab.keySet().forEach(key->{
String[] args = key.split("\\|");
String dsName = args[0];
String tableName = args[1];
try {
if(!"default".equalsIgnoreCase(dsName)) {
dstDataSourceService.initDataSource(dsName);
DynamicDataSourceContextHolder.push(dsName);
}
Page<ExecResult> page = execResultService.page(new Page(fetchParam.getPage()+1,fetchParam.getSize()),Wrappers.<ExecResult>query()
.select("keyvaluefield")
.eq("ruleid",fetchParam.getMetricId()).eq("retvalue",1).eq("dimfield",fetchParam.getDimValue())
.ge("timefield",fetchParam.getStartTime()).le("timefield",fetchParam.getEndTime()));
page.getRecords().forEach(item->{
ret.add(item.getKeyValueField());
});
} catch (Exception ex) {
log.error("反查分析结果发生异常,详细错误信息:" + ex.getMessage());
} finally {
if(!"default".equalsIgnoreCase(dsName)) {
DynamicDataSourceContextHolder.poll();
}
}
});
}
return ret;
}
public List<FetchItem> getResult(FetchParam fetchParam)
{
this.prepareFetchParam(fetchParam);
Set<String> sum=new LinkedHashSet<>();
Set<String> avg=new LinkedHashSet<String>();
Set<String> sum_yoy=new LinkedHashSet<>();
Set<String> sum_mom=new LinkedHashSet<>();
Set<String> sum_y3=new LinkedHashSet<>();
Set<DAMetric> sum_per=new LinkedHashSet<>();
Set<String> avg_yoy=new LinkedHashSet<>();
Set<String> avg_mom=new LinkedHashSet<>();
Set<String> avg_y3=new LinkedHashSet<>();
List<DAMetric> avg_per=new ArrayList<>();
for(DAMetric metric:fetchParam.getMetrics().values())
{
if("AVG".equals(metric.getMetricType()))
{
avg.add(metric.getMetricId());
if("YOY".equals(metric.getExtOp()))
avg_yoy.add(metric.getMetricId());
else if("MOM".equals(metric.getExtOp()))
avg_mom.add(metric.getMetricId());
else if("Y3".equals(metric.getExtOp()))
avg_y3.add(metric.getMetricId());
else if("PER".equals(metric.getExtOp()) || "PER_ORI".equals(metric.getExtOp()))
avg_per.add(metric);
}
else
{
sum.add(metric.getMetricId());
if("YOY".equals(metric.getExtOp()))
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<FetchItem> dimsObj = new ArrayList<>();
Hashtable<String, FetchItem> dimsSet = new Hashtable<>();
int no=1;
if(fetchParam.isIncludeChild()&&fetchParam.getDimItem()!=null&&fetchParam.getDimItem().getChildren()!=null)
{
for(CodeItem item:fetchParam.getDimItem().getChildren())
{
dims.add(item.getValue().toString());
FetchItem obj=new FetchItem().setNo(no).setDimId(item.getValue().toString()).setDimName(item.getText()).setDisplayType(fetchParam.getDisplayType());
for(DAMetric metric:fetchParam.getMetrics().values())
{
obj.setVal(metric.getMetricId(),BigDecimal.ZERO);
}
dimsObj.add(obj);
dimsSet.put(obj.getDimId(), obj);
no++;
}
}
boolean bcalsum=false;
FetchItem sumobj=new FetchItem().setDisplayType(fetchParam.getDisplayType());
{
if(dims.size()==0)
dims.add(fetchParam.getDimValue());
else
bcalsum=true;
sumobj.setNo(no).setDimId(fetchParam.getDimValue()).setDimName("合计");
for(DAMetric metric:fetchParam.getMetrics().values())
{
sumobj.setVal(metric.getMetricId(),BigDecimal.ZERO);
}
dimsObj.add(sumobj);
dimsSet.put(sumobj.getDimId(), sumobj);
}
List<BuildResult> list=new ArrayList<>();
if(sum.size()>0)
{
List<BuildResult> listsum = sum(fetchParam, sum, null, null);
list.addAll(listsum);
}
if(avg.size()>0)
{
List<BuildResult> listavg = avg(fetchParam, avg, null, null);
list.addAll(listavg);
}
for(BuildResult res:list)
{
if(!dimsSet.containsKey(res.getDimField()))
continue;
FetchItem obj=dimsSet.get(res.getDimField());
obj.setVal(res.getRuleId(), res.getMetricField());
if(bcalsum)
{
Double d=0d;
if(res.getMetricField()!=null)
{
d=res.getMetricField().doubleValue();
}
Double sumd=0d;
if(sumobj.containsKey(res.getRuleId())&&sumobj.get(res.getRuleId())!=null)
{
sumd=sumobj.getVal(res.getRuleId()).doubleValue();
}
sumd+=d;
sumobj.setVal(res.getRuleId(), new BigDecimal(sumd).setScale(2, BigDecimal.ROUND_HALF_UP));
}
}
//同比
List<BuildResult> list_yoy=new ArrayList<>();
if(sum_yoy.size()>0)
{
List<BuildResult> listsum=sum(fetchParam, sum_yoy, FetchParam.getLastYear(fetchParam.getStartTime()), FetchParam.getLastYear(fetchParam.getEndTime()));
list_yoy.addAll(listsum);
}
if(avg_yoy.size()>0)
{
List<BuildResult> listavg=avg(fetchParam, avg_yoy, FetchParam.getLastYear(fetchParam.getStartTime()), FetchParam.getLastYear(fetchParam.getEndTime()));
list_yoy.addAll(listavg);
}
for(BuildResult res:list_yoy)
{
if(!dimsSet.containsKey(res.getDimField()))
continue;
FetchItem obj=dimsSet.get(res.getDimField());
Double now=obj.getVal(res.getRuleId()).doubleValue();
Double last=res.getMetricField()==null?0d:res.getMetricField().doubleValue();
if(last==0d)
{
last=1d;
if(now==0d)
now=1d;
}
Double d=(now-last)/last;
d=d*100;
obj.setVal(res.getRuleId(), new BigDecimal(d).setScale(2, BigDecimal.ROUND_HALF_UP));
}
if(bcalsum)
{
HashMap<String,Double> sumlastset=new HashMap<String,Double>();
for(BuildResult res:list_yoy)
{
Double sumlast=0d;
Double d=0d;
if(res.getMetricField()!=null)
{
d=res.getMetricField().doubleValue();
}
if(sumlastset.containsKey(res.getRuleId())&&sumlastset.get(res.getRuleId())!=null)
{
sumlast= sumlastset.get(res.getRuleId());
}
sumlast+=d;
sumlastset.put(res.getRuleId(), sumlast);
}
for(String str:sumlastset.keySet())
{
Double now=sumobj.getVal(str).doubleValue();
Double last=sumlastset.get(str);
if(last==0d)
{
last=1d;
if(now==0d)
now=1d;
}
Double d=(now-last)/last;
d=d*100;
sumobj.setVal(str, new BigDecimal(d).setScale(2, BigDecimal.ROUND_HALF_UP));
}
}
//环比
List<BuildResult> list_mom=new ArrayList<>();
if(sum_mom.size()>0)
{
List<BuildResult> listsum=sum(fetchParam, sum_mom, FetchParam.getLastMonth(fetchParam.getStartTime()), FetchParam.getLastMonth(fetchParam.getEndTime()));
list_mom.addAll(listsum);
}
if(avg_mom.size()>0)
{
List<BuildResult> listavg=avg(fetchParam, avg_mom, FetchParam.getLastMonth(fetchParam.getStartTime()), FetchParam.getLastMonth(fetchParam.getEndTime()));
list_mom.addAll(listavg);
}
for(BuildResult res:list_mom)
{
if(!dimsSet.containsKey(res.getDimField()))
continue;
FetchItem obj=dimsSet.get(res.getDimField());
Double now=obj.getVal(res.getRuleId()).doubleValue();
Double last=res.getMetricField()==null?0d:res.getMetricField().doubleValue();
if(last==0d)
{
last=1d;
if(now==0d)
now=1d;
}
Double d=(now-last)/last;
d=d*100;
obj.setVal(res.getRuleId(), new BigDecimal(d).setScale(2, BigDecimal.ROUND_HALF_UP));
}
if(bcalsum)
{
HashMap<String,Double> sumlastset=new HashMap<String,Double>();
for(BuildResult res:list_mom)
{
Double sumlast=0d;
Double d=0d;
if(res.getMetricField()!=null)
{
d=res.getMetricField().doubleValue();
}
if(sumlastset.containsKey(res.getRuleId())&&sumlastset.get(res.getRuleId())!=null)
{
sumlast= sumlastset.get(res.getRuleId());
}
sumlast+=d;
sumlastset.put(res.getRuleId(), sumlast);
}
for(String str:sumlastset.keySet())
{
Double now=sumobj.getVal(str).doubleValue();
Double last=sumlastset.get(str);
if(last==0d)
{
last=1d;
if(now==0d)
now=1d;
}
Double d=(now-last)/last;
d=d*100;
sumobj.put(str, new BigDecimal(d).setScale(2, BigDecimal.ROUND_HALF_UP));
}
}
//占比
sum_per.addAll(avg_per);
for(DAMetric res:sum_per)
{
boolean isPerOri = false;
if("PER_ORI".equalsIgnoreCase(res.getExtOp()))
isPerOri = true;
for(FetchItem obj:dimsObj)
{
if (StringUtils.isEmpty(obj.get(res.getMetricId())) || StringUtils.isEmpty(obj.get(res.getRefMetric()))) {
continue;
}
Double cur=obj.getVal(res.getMetricId()).doubleValue();
Double per=obj.getVal(res.getRefMetric()).doubleValue();
if(per==0d)
per=1d;
Double d=(cur)/per;
if(!isPerOri) {
d = d * 100;
}
obj.setVal(res.getMetricId(), new BigDecimal(d).setScale(2, BigDecimal.ROUND_HALF_UP));
}
}
return dimsObj;
}
}
......@@ -20,6 +20,7 @@ import cn.ibizlab.core.rule.service.IExecLogService;
import cn.ibizlab.core.rule.service.IRuleItemService;
import cn.ibizlab.util.errors.BadRequestAlertException;
import cn.ibizlab.util.helper.CachedBeanCopier;
import cn.ibizlab.util.helper.Setting;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
......@@ -174,6 +175,14 @@ public class DABuildExService extends DABuildServiceImpl {
msg.setBatch(engineMQMsg.getBatch());
msg.setSystemid(et.getSystemId());
msg.setEngineId(engineMQMsg.getEngineId());
String resultDataSource = Setting.getValue(et.getExtParams(), "resultDataSource");
String resultTableName = Setting.getValue(et.getExtParams(), "resultTableName");
if(!StringUtils.isEmpty(resultDataSource)){
msg.setResultDataSource(resultDataSource);
}
if(!StringUtils.isEmpty(resultTableName)){
msg.setResultTableName(resultTableName);
}
ExecLog execlog=new ExecLog();
execlog.setId(msg.getId());
......
......@@ -238,20 +238,20 @@ public class DataObject {
}
try {
if(objValue instanceof BigDecimal){
return (BigDecimal)(objValue);
return ((BigDecimal)(objValue)).stripTrailingZeros();
}
if(objValue instanceof Double){
return BigDecimal.valueOf((Double)objValue);
return BigDecimal.valueOf((Double)objValue).stripTrailingZeros();
}
if(objValue instanceof Long){
return BigDecimal.valueOf((Long)objValue);
return BigDecimal.valueOf((Long)objValue).stripTrailingZeros();
}
String strValue = objValue.toString();
if(StringUtils.isEmpty(strValue)) {
return fDefault;
}
strValue = strValue.replace(",", "");
return BigDecimal.valueOf(Double.parseDouble(strValue));
return BigDecimal.valueOf(Double.parseDouble(strValue)).stripTrailingZeros();
} catch (Exception ex) {
return fDefault;
}
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册