|
@@ -17,15 +17,18 @@ import com.persagy.apm.alarmservice.alarmcondition.model.dto.QueryAlarmCondition
|
|
|
import com.persagy.apm.alarmservice.alarmcondition.model.dto.UpdateAlarmConditionDTO;
|
|
|
import com.persagy.apm.alarmservice.alarmcondition.service.IAlarmConditionService;
|
|
|
import com.persagy.apm.alarmservice.calculatemethod.model.dto.CalculateMethodDTO;
|
|
|
-import com.persagy.apm.common.configuration.DataUtils;
|
|
|
import com.persagy.apm.common.constant.enums.ValidEnum;
|
|
|
import com.persagy.apm.common.context.poems.PoemsContext;
|
|
|
import com.persagy.apm.common.model.dto.Sort;
|
|
|
+import com.persagy.apm.common.response.CommonResult;
|
|
|
+import com.persagy.apm.common.utils.ResultHelper;
|
|
|
+import com.persagy.common.enums.ResponseCode;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import java.util.*;
|
|
|
+import java.util.regex.Pattern;
|
|
|
|
|
|
/**
|
|
|
* 报警条件(AlarmCondition) service层
|
|
@@ -49,11 +52,11 @@ public class AlarmConditionServiceImpl extends ServiceImpl<AlarmConditionMapper,
|
|
|
AlarmCondition alarmCondition = ConvertAlarmConditionTool.INSTANCE.convertAddDto2Entity(addAlarmConditionDTO);
|
|
|
List<List<CalculateMethodDTO>> frontend = addAlarmConditionDTO.getFrontend();
|
|
|
Object json = JSON.toJSON(frontend);
|
|
|
- alarmCondition.setFrontend(json);
|
|
|
+ alarmCondition.setFrontend(frontend);
|
|
|
//将前端公式转换成后台公式
|
|
|
Map<String, Object> stringMap = frontend2Backend(frontend);
|
|
|
alarmCondition.setBackend(stringMap.get("backend"));
|
|
|
- alarmCondition.setInfoCodes(stringMap.get("infoCode"));
|
|
|
+ alarmCondition.setInfoCodes((List<String>) stringMap.get("infoCode"));
|
|
|
// 设置默认值
|
|
|
setDefaultValue(alarmCondition);
|
|
|
save(alarmCondition);
|
|
@@ -64,16 +67,15 @@ public class AlarmConditionServiceImpl extends ServiceImpl<AlarmConditionMapper,
|
|
|
private Map<String, Object> frontend2Backend(List<List<CalculateMethodDTO>> frontend) {
|
|
|
StringBuffer allNotNull = new StringBuffer();
|
|
|
StringBuffer allNull = new StringBuffer();
|
|
|
- Set<String> infoCodesList = new HashSet<>();
|
|
|
+ Set<String> infoCodesSet = new HashSet<>();
|
|
|
Set<String> displayFormulaList = new HashSet<>();
|
|
|
for (List<CalculateMethodDTO> calculateMethodDTOS : frontend) {
|
|
|
StringBuffer notNullString = new StringBuffer();
|
|
|
StringBuffer nullString = new StringBuffer();
|
|
|
StringBuffer calculateString = new StringBuffer();
|
|
|
for (CalculateMethodDTO calculateMethodDTO : calculateMethodDTOS) {
|
|
|
- infoCodesList.add(calculateMethodDTO.getMonitorIndicatorId());
|
|
|
- String formular = calculateMethodDTO.getFormula();
|
|
|
- List<Formula> formulas = JSONObject.parseArray(formular, Formula.class);
|
|
|
+ infoCodesSet.add(calculateMethodDTO.getMonitorIndicatorId());
|
|
|
+ List<Formula> formulas = calculateMethodDTO.getFormula();
|
|
|
StringBuffer tempFormula = new StringBuffer();
|
|
|
StringBuffer displayFormula = new StringBuffer();
|
|
|
displayFormula.append(calculateMethodDTO.getMonitorIndicatorName()).append(calculateMethodDTO.getOperator());
|
|
@@ -82,7 +84,7 @@ public class AlarmConditionServiceImpl extends ServiceImpl<AlarmConditionMapper,
|
|
|
nullString.append(formula.getValue()).append("==NaN").append(" || ");
|
|
|
notNullString.append(formula.getValue()).append("!=NaN").append(" && ");
|
|
|
tempFormula.append(formula.getValue());
|
|
|
- infoCodesList.add(formula.getValue());
|
|
|
+ infoCodesSet.add(formula.getValue());
|
|
|
}else{
|
|
|
tempFormula.append(formula.getLabel());
|
|
|
}
|
|
@@ -103,11 +105,13 @@ public class AlarmConditionServiceImpl extends ServiceImpl<AlarmConditionMapper,
|
|
|
allNull.append("(").append("(").append(nullString.substring(0, nullString.length() - 4)).append(")").append("?true:").append("(").append(calculateString.substring(0, calculateString.length() - 4)).append(")").append(")").append(" || ");
|
|
|
|
|
|
}
|
|
|
- String infoCode = JSON.toJSONString(infoCodesList);
|
|
|
+ //String infoCode = JSON.toJSONString(infoCodesList);
|
|
|
StringBuffer substring = new StringBuffer();
|
|
|
substring.append("(").append(allNotNull.substring(0, allNotNull.length() - 4)).append(")").append(" && ").append("(").append(allNull.substring(0, allNull.length() - 4)).append(")").toString();
|
|
|
Map<String, Object> map = new HashMap();
|
|
|
- map.put("infoCode",infoCode);
|
|
|
+ List<String> infoCodesList = new ArrayList<>();
|
|
|
+ infoCodesList.addAll(infoCodesSet);
|
|
|
+ map.put("infoCode",infoCodesList);
|
|
|
map.put("backend",substring.toString());
|
|
|
map.put("displayFormula",displayFormulaList);
|
|
|
return map;
|
|
@@ -125,7 +129,6 @@ public class AlarmConditionServiceImpl extends ServiceImpl<AlarmConditionMapper,
|
|
|
alarmCondition.setCreationTime(new Date());
|
|
|
alarmCondition.setModifiedTime(new Date());
|
|
|
alarmCondition.setModifier(PoemsContext.getContext().getUserId());
|
|
|
- alarmCondition.setId(DataUtils.getUUID());
|
|
|
// todo 其他默认的属性
|
|
|
|
|
|
}
|
|
@@ -161,11 +164,11 @@ public class AlarmConditionServiceImpl extends ServiceImpl<AlarmConditionMapper,
|
|
|
if (updateAlarmConditionDTO.getFrontend() != null) {
|
|
|
List<List<CalculateMethodDTO>> frontend = updateAlarmConditionDTO.getFrontend();
|
|
|
String frontJson = JSON.toJSONString(frontend);
|
|
|
- alarmCondition.setFrontend(frontJson);
|
|
|
+ alarmCondition.setFrontend(frontend);
|
|
|
//将前端公式转换成后台公式
|
|
|
Map<String, Object> stringMap = frontend2Backend(frontend);
|
|
|
alarmCondition.setBackend(stringMap.get("backend"));
|
|
|
- alarmCondition.setInfoCodes(stringMap.get("infoCode"));
|
|
|
+ alarmCondition.setInfoCodes((List<String>) stringMap.get("infoCode"));
|
|
|
}
|
|
|
updateById(alarmCondition);
|
|
|
}
|
|
@@ -298,4 +301,99 @@ public class AlarmConditionServiceImpl extends ServiceImpl<AlarmConditionMapper,
|
|
|
|
|
|
return getBaseMapper().selectPage(pageParam, queryWrapper);
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public CommonResult<Boolean> validate(List<Formula> formulas) {
|
|
|
+ StringBuffer displayFormula = new StringBuffer();
|
|
|
+ Set<String> infoCodesList = new HashSet<>();
|
|
|
+ for (Formula formula : formulas) {
|
|
|
+ if (formula.getValue() != null) {
|
|
|
+ String value = formula.getValue();
|
|
|
+ infoCodesList.add(value);
|
|
|
+ displayFormula.append(value);
|
|
|
+ }else {
|
|
|
+ displayFormula.append(formula.getLabel());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ List<String> infoCodes = new ArrayList<>();
|
|
|
+ infoCodes.addAll(infoCodesList);
|
|
|
+ return this.validate(displayFormula.toString(), infoCodes);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 使用正则来校验数学公式
|
|
|
+ *
|
|
|
+ * @param expression 数学公式,包含变量
|
|
|
+ * @param variables 内置变量集合
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static CommonResult validate(String expression, List<String> variables) {
|
|
|
+// if (variables == null || variables.isEmpty()) {
|
|
|
+// return ResultHelper.failure(ResponseCode.C0134.getCode(), "内置变量为空");
|
|
|
+// }
|
|
|
+ // 去空格
|
|
|
+ expression = expression.replaceAll(" ", "");
|
|
|
+ // 连续运算符处理
|
|
|
+ if (expression.split("[\\+\\-\\*\\/]{2,}").length > 1) {
|
|
|
+ return ResultHelper.failure(ResponseCode.C0134.getCode(), "公式不合法,包含连续运算符");
|
|
|
+ }
|
|
|
+ if (org.apache.commons.lang3.StringUtils.contains(expression, "()")) {
|
|
|
+ return ResultHelper.failure(ResponseCode.C0134.getCode(), "公式不合法,包含空括号");
|
|
|
+ }
|
|
|
+// expression = expression.replaceAll("\\)\\(", "\\)*\\(");
|
|
|
+// expression = expression.replaceAll("\\(\\-", "\\(0-");
|
|
|
+// expression = expression.replaceAll("\\(\\+", "\\(0+");
|
|
|
+ // 校验变量
|
|
|
+ String[] splits = expression.split("\\+|\\-|\\*|\\/|\\(|\\)");
|
|
|
+ for (String split : splits) {
|
|
|
+ if (org.apache.commons.lang3.StringUtils.isBlank(split) || Pattern.matches("[0-9]+", split)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (!variables.contains(split)) {
|
|
|
+ return ResultHelper.failure(ResponseCode.C0134.getCode(), "公式不合法,包含非法变量或字符");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 校验括号
|
|
|
+ Character preChar = null;
|
|
|
+ Stack<Character> stack = new Stack<>();
|
|
|
+ String resultExpression = expression;
|
|
|
+ for (int i = 0; i < expression.length(); i++) {
|
|
|
+ char currChar = expression.charAt(i);
|
|
|
+ if (i == 0) {
|
|
|
+ if (Pattern.matches("\\*|\\/", String.valueOf(currChar))) {
|
|
|
+ return ResultHelper.failure(ResponseCode.C0134.getCode(), "公式不合法,以错误运算符开头");
|
|
|
+ }
|
|
|
+ if (currChar == '+') {
|
|
|
+ resultExpression = expression.substring(1);
|
|
|
+ }
|
|
|
+ if (currChar == '-') {
|
|
|
+ resultExpression = "0" + expression;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ('(' == currChar) {
|
|
|
+ stack.push('(');
|
|
|
+ } else if (')' == currChar) {
|
|
|
+ if (stack.size() > 0) {
|
|
|
+ stack.pop();
|
|
|
+ } else {
|
|
|
+ return ResultHelper.failure(ResponseCode.C0134.getCode(), "公式不合法,括号不配对");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (preChar != null && preChar == '(' && Pattern.matches("[\\+\\-\\*\\/]+", String.valueOf(currChar))) {
|
|
|
+ return ResultHelper.failure(ResponseCode.C0134.getCode(), "公式不合法,左括号后是运算符");
|
|
|
+ }
|
|
|
+ if (preChar != null && preChar == ')' && !Pattern.matches("[\\+\\-\\*\\/]+", String.valueOf(currChar))) {
|
|
|
+ return ResultHelper.failure(ResponseCode.C0134.getCode(), "公式不合法,右括号后面不是运算符");
|
|
|
+ }
|
|
|
+ if (i == expression.length() - 1) {
|
|
|
+ if (Pattern.matches("\\+|\\-|\\*|\\/", String.valueOf(currChar)))
|
|
|
+ return ResultHelper.failure(ResponseCode.C0134.getCode(), "公式不合法,以运算符结尾");
|
|
|
+ }
|
|
|
+ preChar = currChar;
|
|
|
+ }
|
|
|
+ if (stack.size() > 0) {
|
|
|
+ return ResultHelper.failure(ResponseCode.C0134.getCode(), "公式不合法,括号不配对");
|
|
|
+ }
|
|
|
+ return ResultHelper.success();
|
|
|
+ }
|
|
|
}
|