feat:Analysis Warn模板
This commit is contained in:
parent
71a4860c63
commit
73f1dea4e0
|
@ -1,13 +1,20 @@
|
|||
package org.jeecg.common.util;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.ReUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import org.jeecg.common.api.dto.message.MessageDTO;
|
||||
import org.jeecg.common.util.dynamic.db.FreemarkerParseFactory;
|
||||
import org.jeecg.modules.base.entity.postgre.SysMessageTemplate;
|
||||
import org.jeecg.modules.base.service.ISysMessageTemplateService;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/*
|
||||
* 模板工具类
|
||||
|
@ -37,6 +44,30 @@ public class TemplateUtil {
|
|||
return messageDTO;
|
||||
}
|
||||
|
||||
public static MessageDTO parse1(String code, Map<String, Object> data){
|
||||
MessageDTO messageDTO = new MessageDTO();
|
||||
SysMessageTemplate template = templateService.getOne(code);
|
||||
// 如果没有消息模板
|
||||
if(ObjectUtil.isNull(template))
|
||||
return messageDTO;
|
||||
String templateName = template.getTemplateName();
|
||||
String templateContent = template.getTemplateContent();
|
||||
messageDTO.setTitle(templateName);
|
||||
if (MapUtil.isEmpty(data))
|
||||
return messageDTO;
|
||||
Set<String> keys = data.keySet();
|
||||
String pattern = "\\<([^<>]*{}[^<>]*)\\>";
|
||||
List<String> contents = new ArrayList<>();
|
||||
for (String key : keys) {
|
||||
contents.add(ReUtil.getGroup1(StrUtil.format(pattern, key), templateContent));
|
||||
}
|
||||
templateContent = CollUtil.join(contents, "#");
|
||||
String content = FreemarkerParseFactory
|
||||
.parseTemplateContent(templateContent, data, true);
|
||||
messageDTO.setContent(content);
|
||||
return messageDTO;
|
||||
}
|
||||
|
||||
public static MessageDTO parse(String title, String code, Map<String, Object> data) {
|
||||
MessageDTO messageDTO = new MessageDTO();
|
||||
SysMessageTemplate template = templateService.getOne(code);
|
||||
|
@ -53,4 +84,28 @@ public class TemplateUtil {
|
|||
messageDTO.setContent(content);
|
||||
return messageDTO;
|
||||
}
|
||||
|
||||
public static MessageDTO parse1(String title, String code, Map<String, Object> data) {
|
||||
MessageDTO messageDTO = new MessageDTO();
|
||||
SysMessageTemplate template = templateService.getOne(code);
|
||||
// 如果没有消息模板
|
||||
if(ObjectUtil.isNull(template))
|
||||
return messageDTO;
|
||||
String templateName = template.getTemplateName();
|
||||
String templateContent = template.getTemplateContent();
|
||||
messageDTO.setTitle(StrUtil.isBlank(title) ? templateName : title);
|
||||
if (MapUtil.isEmpty(data))
|
||||
return messageDTO;
|
||||
Set<String> keys = data.keySet();
|
||||
String pattern = "\\<([^<>]*{}[^<>]*)\\>";
|
||||
List<String> contents = new ArrayList<>();
|
||||
for (String key : keys) {
|
||||
contents.add(ReUtil.getGroup1(StrUtil.format(pattern, key), templateContent));
|
||||
}
|
||||
templateContent = CollUtil.join(contents, "#");
|
||||
String content = FreemarkerParseFactory
|
||||
.parseTemplateContent(templateContent, data, true);
|
||||
messageDTO.setContent(content);
|
||||
return messageDTO;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,8 +5,10 @@ import cn.hutool.core.collection.CollUtil;
|
|||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.text.StrBuilder;
|
||||
import cn.hutool.core.util.NumberUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.ReUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
@ -16,8 +18,10 @@ import org.jeecg.common.config.mqtoken.UserTokenContext;
|
|||
import org.jeecg.common.constant.CommonConstant;
|
||||
import org.jeecg.common.constant.SymbolConstant;
|
||||
import org.jeecg.common.constant.enums.SampleType;
|
||||
import org.jeecg.common.util.DataTool;
|
||||
import org.jeecg.common.util.RedisStreamUtil;
|
||||
import org.jeecg.common.util.SpringContextUtils;
|
||||
import org.jeecg.common.util.TemplateUtil;
|
||||
import org.jeecg.modules.base.dto.NuclideInfo;
|
||||
import org.jeecg.modules.base.dto.Info;
|
||||
import org.jeecg.modules.base.entity.postgre.AlarmAnalysisLog;
|
||||
|
@ -36,9 +40,13 @@ import org.springframework.stereotype.Component;
|
|||
|
||||
import static org.jeecg.common.constant.enums.MessageTypeEnum.*;
|
||||
import static org.jeecg.common.util.TokenUtils.getTempToken;
|
||||
import static org.jeecg.modules.base.enums.Template.ANALYSIS_NUCLIDE;
|
||||
import static org.jeecg.modules.base.enums.Template.MONITOR_EMAIL;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Data
|
||||
|
@ -143,71 +151,61 @@ public class AnalysisConsumer implements StreamListener<String, ObjectRecord<Str
|
|||
|
||||
private void judge(Info info, Map<String,String> nuclidesCross){
|
||||
Set<String> nuclideNames = nuclidesCross.keySet();
|
||||
StringBuilder alarmInfo = new StringBuilder();
|
||||
List<String> firstDetected;
|
||||
List<NuclideInfo> moreThanAvg = new ArrayList<>();
|
||||
String conditionStr = info.getConditions();
|
||||
String betaOrGamma = info.getBetaOrGamma();
|
||||
String datasource = info.getDatasource();
|
||||
List<String> conditions = ListUtil.toList(conditionStr.split(COMMA));
|
||||
List<String> first = new ArrayList<>();
|
||||
List<String> average = new ArrayList<>();
|
||||
List<String> meanwhile = new ArrayList<>();
|
||||
List<String> firstDetected = new ArrayList<>(); // 首次发现
|
||||
List<NuclideInfo> moreThanAvg = new ArrayList<>(); // 超浓度均值
|
||||
List<String> meanwhile = new ArrayList<>(); // 同时出现两种及以上核素
|
||||
for (String con : conditions) {
|
||||
Condition condition = Condition.valueOf1(con);
|
||||
if (ObjectUtil.isNotNull(condition)){
|
||||
switch (condition){
|
||||
case FIRST_FOUND: // 首次发现该元素
|
||||
firstDetected = firstDetected(betaOrGamma, datasource, nuclideNames);
|
||||
if (CollUtil.isNotEmpty(firstDetected)){
|
||||
String message = "First discovery of nuclides: [" + StrUtil.join(COMMA, firstDetected) + "]";
|
||||
alarmInfo.append(message);
|
||||
}
|
||||
break;
|
||||
case ABOVE_AVERAGE: // 元素浓度高于均值
|
||||
moreThanAvg = moreThanAvg(datasource,nuclidesCross);
|
||||
if (CollUtil.isNotEmpty(moreThanAvg)){
|
||||
for (NuclideInfo nuclideInfo : moreThanAvg) {
|
||||
String nuclide = nuclideInfo.getNuclide();
|
||||
String threshold = nuclideInfo.getThreshold();
|
||||
String message = "Nuclide " + nuclide + " is above average: " + threshold;
|
||||
alarmInfo.append(COMMA).append(message);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case MEANWHILE: // 同时出现两种及以上核素
|
||||
if (nuclideNames.size() >= 2){
|
||||
String message = "Simultaneously detecting nuclides: [" + StrUtil.join(COMMA, nuclideNames) + "]";
|
||||
alarmInfo.append(COMMA).append(message);
|
||||
}
|
||||
if (CollUtil.isNotEmpty(nuclideNames) && nuclideNames.size() >= 2)
|
||||
meanwhile.addAll(nuclideNames);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (StrUtil.isNotBlank(alarmInfo.toString())){
|
||||
// 保存报警日志
|
||||
AlarmAnalysisLog logInfo = new AlarmAnalysisLog();
|
||||
BeanUtil.copyProperties(info,logInfo);
|
||||
SampleType sampleType = SampleType.typeOf(betaOrGamma);
|
||||
if (ObjectUtil.isNotNull(sampleType))
|
||||
logInfo.setSampleType(sampleType.getValue());
|
||||
if (alarmInfo.toString().startsWith(COMMA))
|
||||
alarmInfo = new StringBuilder(StrUtil.sub(alarmInfo.toString(), 1, alarmInfo.length()));
|
||||
logInfo.setAlarmInfo(alarmInfo.toString());
|
||||
if (CollUtil.isNotEmpty(moreThanAvg))
|
||||
logInfo.setNuclideInfoList(moreThanAvg);
|
||||
logService.saveLog(logInfo);
|
||||
// 发送报警信息
|
||||
String groupId = info.getGroupId();
|
||||
MessageDTO messageDTO = new MessageDTO();
|
||||
messageDTO.setTitle("Nuclied Warn Info").setContent(alarmInfo.toString());
|
||||
if (StrUtil.isNotBlank(groupId)) {
|
||||
systemClient.sendMessage(messageDTO, groupId, ALL.getValue());
|
||||
systemClient.pushMessageToSingle(messageDTO, groupId);
|
||||
}
|
||||
// 构建预警信息
|
||||
DataTool dataTool = DataTool.getInstance();
|
||||
if (CollUtil.isNotEmpty(firstDetected))
|
||||
dataTool.put("firstDetected", CollUtil.join(firstDetected, StrUtil.COMMA + StrUtil.SPACE));
|
||||
if (CollUtil.isNotEmpty(moreThanAvg)){
|
||||
String above = moreThanAvg.stream()
|
||||
.map(item -> item.getNuclide() + "(" + item.getValue() + ")" + " > " + item.getThreshold())
|
||||
.collect(Collectors.joining(StrUtil.COMMA + StrUtil.SPACE));
|
||||
dataTool.put("moreThanAvg", above);
|
||||
}
|
||||
if (CollUtil.isNotEmpty(meanwhile))
|
||||
dataTool.put("meanwhile", CollUtil.join(meanwhile, StrUtil.COMMA + StrUtil.SPACE));
|
||||
// 如果报警数据为空 则不需要发送报警信息和生成报警日志
|
||||
if (MapUtil.isEmpty(dataTool.get())) return;
|
||||
MessageDTO messageDTO = TemplateUtil.parse1(ANALYSIS_NUCLIDE.getCode(), dataTool.get());
|
||||
// 保存报警日志
|
||||
AlarmAnalysisLog logInfo = new AlarmAnalysisLog();
|
||||
BeanUtil.copyProperties(info, logInfo);
|
||||
SampleType sampleType = SampleType.typeOf(betaOrGamma);
|
||||
if (ObjectUtil.isNotNull(sampleType))
|
||||
logInfo.setSampleType(sampleType.getValue());
|
||||
logInfo.setAlarmInfo(messageDTO.getContent());
|
||||
if (CollUtil.isNotEmpty(moreThanAvg))
|
||||
logInfo.setNuclideInfoList(moreThanAvg);
|
||||
logService.saveLog(logInfo);
|
||||
// 发送报警信息
|
||||
String groupId = info.getGroupId();
|
||||
systemClient.sendMessage(messageDTO, groupId, ALL.getValue());
|
||||
systemClient.pushMessageToSingle(messageDTO, groupId);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue
Block a user