暂态高级算法异步
This commit is contained in:
@@ -42,11 +42,14 @@ import org.springframework.transaction.annotation.Transactional;
|
|||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -69,7 +72,8 @@ public class EventDetailServiceImpl extends ServiceImpl<RmpEventDetailMapper, Rm
|
|||||||
private final DeptLineFeignClient deptLineFeignClient;
|
private final DeptLineFeignClient deptLineFeignClient;
|
||||||
private final DeptFeignClient deptFeignClient;
|
private final DeptFeignClient deptFeignClient;
|
||||||
private final UserFeignClient userFeignClient;
|
private final UserFeignClient userFeignClient;
|
||||||
|
@Resource(name="asyncExecutor")
|
||||||
|
private Executor executor;
|
||||||
private final EventCauseFeignClient eventCauseFeignClient;
|
private final EventCauseFeignClient eventCauseFeignClient;
|
||||||
@Override
|
@Override
|
||||||
public List<EventDetail> getEventDetailData(String id, String startTime, String endTime) {
|
public List<EventDetail> getEventDetailData(String id, String startTime, String endTime) {
|
||||||
@@ -173,49 +177,7 @@ public class EventDetailServiceImpl extends ServiceImpl<RmpEventDetailMapper, Rm
|
|||||||
|
|
||||||
rmpEventDetailPO.setEventDescribe(" ");
|
rmpEventDetailPO.setEventDescribe(" ");
|
||||||
//如果不为空,说明是二次上传波形文件了;
|
//如果不为空,说明是二次上传波形文件了;
|
||||||
String reason,type;
|
|
||||||
if(!StringUtils.isEmpty(rmpEventDetailPO.getWavePath())){
|
|
||||||
try {
|
|
||||||
LineDetailDataVO lineDetailData = lineFeignClient.getLineDetailData(rmpEventDetailPO.getLineId()).getData();
|
|
||||||
String ip = lineDetailData.getIp();
|
|
||||||
EventAnalysisDTO eventAnalysisDTO = new EventAnalysisDTO();
|
|
||||||
eventAnalysisDTO.setIp(ip);
|
|
||||||
eventAnalysisDTO.setWaveName(rmpEventDetailPO.getWavePath());
|
|
||||||
|
|
||||||
EventAnalysisDTO result = eventCauseFeignClient.analysisCauseAndType(eventAnalysisDTO).getData();
|
|
||||||
if(Objects.isNull(result.getCause())){
|
|
||||||
reason =reasonReflection(0);
|
|
||||||
|
|
||||||
}else {
|
|
||||||
reason =reasonReflection(result.getCause());
|
|
||||||
|
|
||||||
}
|
|
||||||
if(Objects.isNull(result.getType())){
|
|
||||||
type =advanceTypeReflection(10);
|
|
||||||
|
|
||||||
}else {
|
|
||||||
type =advanceTypeReflection(result.getType());
|
|
||||||
|
|
||||||
}
|
|
||||||
DictData advancereason = dicDataFeignClient.getDicDataByCode(reason).getData();
|
|
||||||
DictData advanceType = dicDataFeignClient.getDicDataByCode(type).getData();
|
|
||||||
if(Objects.equals(result.getCauseFlag(),1)&&Objects.equals(result.getTypeFlag(),1)){
|
|
||||||
rmpEventDetailPO.setDealFlag(1);
|
|
||||||
}else {
|
|
||||||
rmpEventDetailPO.setDealFlag(0);
|
|
||||||
}
|
|
||||||
rmpEventDetailPO.setAdvanceReason(advancereason.getId());
|
|
||||||
rmpEventDetailPO.setAdvanceType(advanceType.getId());
|
|
||||||
}catch (Exception e){
|
|
||||||
rmpEventDetailPO.setDealFlag(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//默认都是其他
|
|
||||||
// DictData reason = dicDataFeignClient.getDicDataByCode(DicDataEnum.RESON_REST.getCode()).getData();
|
|
||||||
// DictData advanceType = dicDataFeignClient.getDicDataByCode(DicDataEnum.TYPE_REST.getCode()).getData();
|
|
||||||
//
|
|
||||||
// rmpEventDetailPO.setAdvanceReason(reason.getId());
|
|
||||||
// rmpEventDetailPO.setAdvanceType(advanceType.getId());
|
|
||||||
|
|
||||||
|
|
||||||
String severity = EventUtil.getYzd(deatilDTO.getDuration().floatValue(),(deatilDTO.getAmplitude().floatValue()/100));
|
String severity = EventUtil.getYzd(deatilDTO.getDuration().floatValue(),(deatilDTO.getAmplitude().floatValue()/100));
|
||||||
@@ -236,10 +198,73 @@ public class EventDetailServiceImpl extends ServiceImpl<RmpEventDetailMapper, Rm
|
|||||||
pushEvent(rmpEventDetailPO);
|
pushEvent(rmpEventDetailPO);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//异步调用高级算法
|
||||||
|
if (!StringUtils.isEmpty(rmpEventDetailPO.getWavePath())) {
|
||||||
|
CompletableFuture.runAsync(() -> {
|
||||||
|
// 异步任务内执行分析及更新
|
||||||
|
analyzeAndUpdateEvent(rmpEventDetailPO.getEventId(), rmpEventDetailPO.getLineId(), rmpEventDetailPO.getWavePath());
|
||||||
|
}, executor).exceptionally(ex -> {
|
||||||
|
// exceptionally 也可以捕获异常,但内部已 try-catch,这里仅做兜底日志
|
||||||
|
log.error("异步任务未预期的异常, eventId: {}", rmpEventDetailPO.getEventId(), ex);
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void analyzeAndUpdateEvent(String eventId, String lineId, String wavePath) {
|
||||||
|
// 重新查询实体,避免跨线程持久化对象问题
|
||||||
|
RmpEventDetailPO po = this.getById(eventId);
|
||||||
|
if (po == null) {
|
||||||
|
log.warn("异步分析时事件记录不存在, eventId: {}", eventId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Feign 调用链
|
||||||
|
LineDetailDataVO lineDetailData = lineFeignClient.getLineDetailData(lineId).getData();
|
||||||
|
String ip = lineDetailData.getIp();
|
||||||
|
EventAnalysisDTO eventAnalysisDTO = new EventAnalysisDTO();
|
||||||
|
eventAnalysisDTO.setIp(ip);
|
||||||
|
eventAnalysisDTO.setWaveName(wavePath);
|
||||||
|
|
||||||
|
EventAnalysisDTO result = eventCauseFeignClient.analysisCauseAndType(eventAnalysisDTO).getData();
|
||||||
|
|
||||||
|
// 转换结果
|
||||||
|
String reasonCode, typeCode;
|
||||||
|
if (Objects.isNull(result.getCause())) {
|
||||||
|
reasonCode = reasonReflection(0);
|
||||||
|
} else {
|
||||||
|
reasonCode = reasonReflection(result.getCause());
|
||||||
|
}
|
||||||
|
if (Objects.isNull(result.getType())) {
|
||||||
|
typeCode = advanceTypeReflection(10);
|
||||||
|
} else {
|
||||||
|
typeCode = advanceTypeReflection(result.getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
DictData advancereason = dicDataFeignClient.getDicDataByCode(reasonCode).getData();
|
||||||
|
DictData advanceType = dicDataFeignClient.getDicDataByCode(typeCode).getData();
|
||||||
|
|
||||||
|
po.setAdvanceReason(advancereason.getId());
|
||||||
|
po.setAdvanceType(advanceType.getId());
|
||||||
|
if (Objects.equals(result.getCauseFlag(), 1) && Objects.equals(result.getTypeFlag(), 1)) {
|
||||||
|
po.setDealFlag(1);
|
||||||
|
} else {
|
||||||
|
po.setDealFlag(0);
|
||||||
|
}
|
||||||
|
// 更新数据库
|
||||||
|
this.updateById(po);
|
||||||
|
} catch (Exception e) {
|
||||||
|
// 关键点:所有异常被捕获并记录日志,不会抛出到主线程
|
||||||
|
log.error("异步分析失败, eventId={}, wavePath={}", eventId, wavePath, e);
|
||||||
|
// 可选:设置一个错误标志,避免一直处于未分析状态
|
||||||
|
po.setDealFlag(0);
|
||||||
|
this.updateById(po);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private String advanceTypeReflection(Integer type) {
|
private String advanceTypeReflection(Integer type) {
|
||||||
String result = DicDataEnum.TYPE_REST.getCode();
|
String result = DicDataEnum.TYPE_REST.getCode();
|
||||||
switch (type) {
|
switch (type) {
|
||||||
@@ -356,5 +381,15 @@ public class EventDetailServiceImpl extends ServiceImpl<RmpEventDetailMapper, Rm
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
try{
|
||||||
|
System.out.println(1/0);
|
||||||
|
}catch (Exception e){
|
||||||
|
System.out.println(1);
|
||||||
|
}
|
||||||
|
System.out.println(1111);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user