Browse Source

Merge remote-tracking branch 'origin/v2.0.0' into v2.0.0

xujiaheng 3 years ago
parent
commit
17abb6b132

+ 12 - 12
src/main/java/com/persagy/apm/alarmservice/group/alarmrecord/controller/AlarmRecordController.java

@@ -388,18 +388,18 @@ public class AlarmRecordController {
 
     @PostMapping("/test")
     public void test() {
-//        AddAlarmRecordDTO addAlarmRecordDTO = new AddAlarmRecordDTO();
-//        addAlarmRecordDTO.setId("123");
-//        addAlarmRecordDTO.setTriggerTime(new Date());
-//        addAlarmRecordDTO.setObjId("123");
-//        addAlarmRecordDTO.setName("test");
-//        addAlarmRecordDTO.setItemCode("123");
-//        addAlarmRecordDTO.setLevel("1");
-//        try {
-//            alarmRecordService.create(addAlarmRecordDTO);
-//        } catch (Exception e) {
-//            e.printStackTrace();
-//        }
+        AddAlarmRecordDTO addAlarmRecordDTO = new AddAlarmRecordDTO();
+        addAlarmRecordDTO.setId("1234");
+        addAlarmRecordDTO.setTriggerTime(new Date());
+        addAlarmRecordDTO.setObjId("123");
+        addAlarmRecordDTO.setName("test");
+        addAlarmRecordDTO.setItemCode("123");
+        addAlarmRecordDTO.setLevel("1");
+        try {
+            alarmRecordService.create(addAlarmRecordDTO);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
     }
 }
 

+ 43 - 7
src/main/java/com/persagy/apm/alarmservice/project/alarmcondition/service/impl/AlarmConditionServiceImpl.java

@@ -31,6 +31,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 
+import java.math.BigDecimal;
 import java.util.*;
 import java.util.regex.Pattern;
 import java.util.stream.Collectors;
@@ -185,24 +186,30 @@ public class AlarmConditionServiceImpl extends ServiceImpl<AlarmConditionMapper,
             // 一条规则中包含的公式(中文)
             List<String> ruleDisplayFormulaList = new ArrayList<>();
             for (CalculateMethodDTO calculateMethodDTO : calculateMethodDtoList) {
-                infoCodesSet.add(calculateMethodDTO.getMonitorIndicatorId());
+                String monitorIndicatorId = calculateMethodDTO.getMonitorIndicatorId();
+//                MonitorIndicator monitorIndicator = monitorIndicatorService.queryMonitorIndicatorDetail(monitorIndicatorId);
+                infoCodesSet.add(monitorIndicatorId);
                 List<Formula> formulas = calculateMethodDTO.getFormula();
                 StringBuffer tempFormula = new StringBuffer();
                 StringBuffer displayFormula = new StringBuffer();
                 displayFormula.append(calculateMethodDTO.getMonitorIndicatorName()).append(calculateMethodDTO.getOperator());
                 for (Formula formula : formulas) {
                     displayFormula.append(formula.getLabel());
+                    String value = formula.getValue();
+//                    /* 对公式中各个参数进行特殊处理,例如:监测指标的单位是%,需要将公式中的数字都除以100 */
+//                    value = magicFormulaParam(value, monitorIndicator.getUnit());
+
                     if ("dic".equals(formula.getKey())) {
                         // 如果formula是字典选项
-                        tempFormula.append(formula.getValue());
+                        tempFormula.append(value);
                         continue;
                     }
-                    if (StringUtils.isNotBlank(formula.getValue())) {
+                    if (StringUtils.isNotBlank(value)) {
                         // 如果formula不是字典选项,且value值不为空,代表formula是个监测指标
-                        nullString.append(formula.getValue()).append("==NaN").append(" || ");
-                        notNullString.append(formula.getValue()).append("!=NaN").append(" && ");
-                        tempFormula.append(formula.getValue());
-                        infoCodesSet.add(formula.getValue());
+                        nullString.append(value).append("==NaN").append(" || ");
+                        notNullString.append(value).append("!=NaN").append(" && ");
+                        tempFormula.append(value);
+                        infoCodesSet.add(value);
                     } else {
                         // formula为数字或操作符
                         tempFormula.append(replace(formula.getLabel()));
@@ -237,6 +244,35 @@ public class AlarmConditionServiceImpl extends ServiceImpl<AlarmConditionMapper,
         return map;
     }
 
+    /**
+     *  对公式中各个参数进行特殊处理,例如:监测指标的单位是%,需要将公式中的数字都除以100
+     *  开发人员认为这里处理很诡异,因此将此方法成为对公式参数施法
+     *  处理不了下面这种情况,该方法废弃
+     *  指标1=(指标2+10)/2
+     *
+     * @param formulaParam 公式中的一个参数
+     * @param indicatorUnit 监测指标的单位
+     * @return 特殊处理后的参数
+     * @author lixing
+     * @version V1.0 2021/11/12 5:19 下午
+     */
+    @Deprecated
+    private String magicFormulaParam(String formulaParam, String indicatorUnit) {
+        // 如果参数能被转换为bigDecimal, 代表参数为数值型
+        BigDecimal paramValue;
+        try {
+            paramValue = new BigDecimal(formulaParam);
+        } catch (Exception e) {
+            // 如果参数不是数值型,参数不需要处理,直接返回
+            return formulaParam;
+        }
+
+        if ("%".equals(indicatorUnit)) {
+            paramValue = paramValue.divide(new BigDecimal(100));
+        }
+        return paramValue.toString();
+    }
+
     public String replace(String operator) {
         operator = operator.replace("=", "==")
                 .replace("≥", ">=")