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

提交

上级 46ca7a2a
......@@ -174,6 +174,20 @@ public class ExecResultRepository {
*/
public ResultSet getPageData(String ruleid, PagingState pagingState, Integer retValue, String dims, Timestamp start, Timestamp end) {
final int RESULTS_PER_PAGE = 1000;
return getPageData(ruleid, pagingState, RESULTS_PER_PAGE, retValue, dims, start, end);
}
/**
* 根据规则ID、单位和时间查询相应的规则结果数据
*
* @param ruleid
* @param retValue
* @param dims
* @param start
* @param end
* @return
*/
public ResultSet getPageData(String ruleid, PagingState pagingState, int pageSize, Integer retValue, String dims, Timestamp start, Timestamp end) {
LocalDate st = ExecResult.time2LocalDate(start);
LocalDate ed = ExecResult.time2LocalDate(end);
Statement statement = select().column("keyvaluefield").
......@@ -182,7 +196,7 @@ public class ExecResultRepository {
and(eq("retvalue", retValue)).
and(eq("dimfield", dims)).
and(gte("timefield", st)).and(lte("timefield", ed)).setReadTimeoutMillis(200000);
statement.setFetchSize(RESULTS_PER_PAGE);
statement.setFetchSize(pageSize);
if (pagingState != null) {
statement.setPagingState(pagingState);
}
......
......@@ -53,7 +53,8 @@ public class FetchParam {
private Timestamp endTime;
private int page = 0;
private int size = 100;
private int size = 1000;
private int total = 0;
private boolean filled = false;
......
......@@ -3,8 +3,11 @@ package cn.ibizlab.core.extensions.mapper;
import cn.ibizlab.core.lite.extensions.domain.EntityObj;
import cn.ibizlab.core.rule.domain.ExecResult;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import java.sql.Timestamp;
import java.util.List;
public interface ExecResultExMapper extends BaseMapper<EntityObj>{
......@@ -14,5 +17,9 @@ public interface ExecResultExMapper extends BaseMapper<EntityObj>{
int replaceBatchByPG(@Param("list") List<ExecResult> var1 , @Param("resultTableName") String tableName);
int clearBatch(@Param("list")List<ExecResult> var1, @Param("resultTableName") String tableName);
List<ExecResult> sumResult(@Param("resultTableName") String tableName, @Param("ruleids")List<String> ruleids, @Param("dimfields")List<String> dimfields, @Param("from")Timestamp from,@Param("to")Timestamp to);
List<ExecResult> avgResult(@Param("resultTableName") String tableName, @Param("ruleids")List<String> ruleids, @Param("dimfields")List<String> dimfields, @Param("from")Timestamp from,@Param("to")Timestamp to);
Page<ExecResult> lookupResult(IPage page, @Param("resultTableName") String tableName, @Param("columns")String columns,@Param("ruleid")String ruleid, @Param("dimfield")String dimfield, @Param("from")Timestamp from, @Param("to")Timestamp to);
}
\ No newline at end of file
......@@ -21,7 +21,6 @@ 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;
......@@ -30,6 +29,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
......@@ -78,9 +78,13 @@ public class BuildResultService {
@Autowired
@Lazy
private IExecResultService execResultService;
private ExecResultExService execResultExService;
@Value("${defaultResultDataSource:default}")
private String Default_ResultDataSource;
@Value("${defaultResultTableName:IBZRULERESULT}")
private String Default_ResultTableName;
private void prepareFetchParam(FetchParam fetchParam)
......@@ -194,9 +198,9 @@ public class BuildResultService {
String resultDataSource = Setting.getValue(daBuild.getExtParams(), "resultDataSource");
String resultTableName = Setting.getValue(daBuild.getExtParams(), "resultTableName");
if(StringUtils.isEmpty(resultDataSource))
resultDataSource="default";
resultDataSource=Default_ResultDataSource;
if(StringUtils.isEmpty(resultTableName))
resultTableName="IBZRULERESULT";
resultTableName=Default_ResultTableName;
String table=resultDataSource+"|"+resultTableName;
if(!fetchParam.getDimTables().containsKey(table))
fetchParam.getDimTables().put(table,builds.get(key));
......@@ -246,11 +250,6 @@ public class BuildResultService {
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))
{
......@@ -259,22 +258,11 @@ public class BuildResultService {
}
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<ExecResult> list = execResultExService.avgResult(dsName,tableName,new ArrayList<>(tmpSet),fetchParam.getDimValues(),startTime,endTime);
list.forEach(item->{
ret.add(new BuildResult().setSqlResult(item));
});
}
} catch (Exception ex) {
log.error("汇总分析结果发生异常,详细错误信息:" + ex.getMessage());
} finally {
if(!"default".equalsIgnoreCase(dsName)) {
DynamicDataSourceContextHolder.poll();
}
}
}
......@@ -318,11 +306,7 @@ public class BuildResultService {
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))
{
......@@ -331,22 +315,12 @@ public class BuildResultService {
}
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<ExecResult> list = execResultExService.sumResult(dsName,tableName,new ArrayList<>(tmpSet),fetchParam.getDimValues(),startTime,endTime);
list.forEach(item->{
ret.add(new BuildResult().setSqlResult(item));
});
}
} catch (Exception ex) {
log.error("汇总分析结果发生异常,详细错误信息:" + ex.getMessage());
} finally {
if(!"default".equalsIgnoreCase(dsName)) {
DynamicDataSourceContextHolder.poll();
}
}
}
......@@ -363,7 +337,7 @@ public class BuildResultService {
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());
ResultSet rs=execResultRepository.getPageData(fetchParam.getMetricId(),pagingState,fetchParam.getSize(), 1,fetchParam.getDimValue(),fetchParam.getStartTime(),fetchParam.getEndTime());
int remaining = rs.getAvailableWithoutFetching();
for (Row row : rs) {
ret.add(row.getString("keyvaluefield"));
......@@ -373,7 +347,7 @@ public class BuildResultService {
}
break;
} else {
ResultSet rs=execResultRepository.getPageData(fetchParam.getMetricId(),pagingState,1,fetchParam.getDimValue(),fetchParam.getStartTime(),fetchParam.getEndTime());
ResultSet rs=execResultRepository.getPageData(fetchParam.getMetricId(),pagingState,fetchParam.getSize(),1,fetchParam.getDimValue(),fetchParam.getStartTime(),fetchParam.getEndTime());
pagingState = rs.getExecutionInfo().getPagingState();
}
}
......@@ -388,25 +362,10 @@ public class BuildResultService {
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->{
Page<ExecResult> page = execResultExService.lookupResult(fetchParam.getPage(), fetchParam.getSize(), dsName, tableName, "keyvaluefield", fetchParam.getMetricId(), fetchParam.getDimValue(), fetchParam.getStartTime(), fetchParam.getEndTime());
page.getContent().forEach(item->{
ret.add(item.getKeyValueField());
});
} catch (Exception ex) {
log.error("反查分析结果发生异常,详细错误信息:" + ex.getMessage());
} finally {
if(!"default".equalsIgnoreCase(dsName)) {
DynamicDataSourceContextHolder.poll();
}
}
});
......
......@@ -7,17 +7,23 @@ import cn.ibizlab.core.rule.service.impl.ExecResultServiceImpl;
import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import javax.sql.DataSource;
import java.sql.Timestamp;
import java.util.*;
/**
......@@ -246,5 +252,75 @@ public class ExecResultExService extends ExecResultServiceImpl {
return result;
}
public List<ExecResult> sumResult(String dsName ,String tableName, List<String> ruleids, List<String> dimfields, Timestamp from, Timestamp to)
{
List<ExecResult> result = null;
try {
if(!Default_ResultDataSource.equalsIgnoreCase(dsName)){
dstDataSourceService.initDataSource(dsName);
DynamicDataSourceContextHolder.push(dsName);
}
result = execResultExMapper.sumResult(tableName,ruleids,dimfields,from,to);
} catch (Exception ex) {
result = new ArrayList<>();
log.error("存储规则结果发生异常,详细错误信息:" + ex.getMessage());
} finally {
if(!Default_ResultDataSource.equalsIgnoreCase(dsName)) {
DynamicDataSourceContextHolder.poll();
}
}
return result;
}
public List<ExecResult> avgResult(String dsName ,String tableName, List<String> ruleids, List<String> dimfields, Timestamp from, Timestamp to)
{
List<ExecResult> result = null;
try {
if(!Default_ResultDataSource.equalsIgnoreCase(dsName)){
dstDataSourceService.initDataSource(dsName);
DynamicDataSourceContextHolder.push(dsName);
}
result = execResultExMapper.avgResult(tableName,ruleids,dimfields,from,to);
} catch (Exception ex) {
result = new ArrayList<>();
log.error("存储规则结果发生异常,详细错误信息:" + ex.getMessage());
} finally {
if(!Default_ResultDataSource.equalsIgnoreCase(dsName)) {
DynamicDataSourceContextHolder.poll();
}
}
return result;
}
public Page<ExecResult> lookupResult(int current, int size, String dsName, String tableName, String columns,String ruleid, String dimfield, Timestamp from, Timestamp to) {
if(StringUtils.isEmpty(columns))
columns="*";
List<ExecResult> result = null;
long total=0;
try {
if(!Default_ResultDataSource.equalsIgnoreCase(dsName)){
dstDataSourceService.initDataSource(dsName);
DynamicDataSourceContextHolder.push(dsName);
}
com.baomidou.mybatisplus.extension.plugins.pagination.Page<ExecResult> pages=execResultExMapper.lookupResult(new com.baomidou.mybatisplus.extension.plugins.pagination.Page(current+1,size), tableName, columns, ruleid, dimfield, from, to);
result = pages.getRecords();
total=pages.getTotal();
} catch (Exception ex) {
result = new ArrayList<>();
log.error("存储规则结果发生异常,详细错误信息:" + ex.getMessage());
} finally {
if(!Default_ResultDataSource.equalsIgnoreCase(dsName)) {
DynamicDataSourceContextHolder.poll();
}
}
return new PageImpl<ExecResult>(result, PageRequest.of(current,size), total);
}
}
......@@ -86,5 +86,43 @@
</delete>
<select id="sumResult" >
select ruleid, 1 as retvalue,dimfield,sum(metricfield) as metricfield from ${resultTableName} where ruleid in
<foreach item="id" index="index" collection="ruleids" open="(" separator="," close=")">
#{id}
</foreach>
and RETVALUE=1 and DIMFIELD in
<foreach item="id" index="index" collection="dimfields" open="(" separator="," close=")">
#{id}
</foreach>
and TIMEFIELD<![CDATA[ >= ]]>#{from} and TIMEFIELD<![CDATA[ <= ]]>#{to}
group by ruleid,dimfield
</select>
<select id="avgResult" >
select ruleid, 1 as retvalue,dimfield,avg(metricfield) as metricfield from ${resultTableName} where ruleid in
<foreach item="id" index="index" collection="ruleids" open="(" separator="," close=")">
#{id}
</foreach>
and RETVALUE=1 and DIMFIELD in
<foreach item="id" index="index" collection="dimfields" open="(" separator="," close=")">
#{id}
</foreach>
and TIMEFIELD<![CDATA[ >= ]]>#{from} and TIMEFIELD<![CDATA[ <= ]]>#{to}
group by ruleid,dimfield
</select>
<select id="lookupResult" >
select ${columns} from ${resultTableName} where ruleid = #{ruleid}
and RETVALUE=1 and DIMFIELD = #{dimfield}
and TIMEFIELD<![CDATA[ >= ]]>#{from} and TIMEFIELD<![CDATA[ <= ]]>#{to}
</select>
</mapper>
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册