Browse Source

优化计算逻辑

shaohongbo 3 years ago
parent
commit
a5238d0d5b

+ 62 - 44
src/main/java/com/persagy/apm/diagnose/indicatorrecord/service/impl/MonitorIndicatorRecordServiceImpl.java

@@ -138,14 +138,12 @@ public class MonitorIndicatorRecordServiceImpl implements IMonitorIndicatorRecor
 
     private AsyncResult<String> computeProjectIndicatorData(ProjectDTO projectDTO, ProjectIndicatorCompute projectIndicatorCompute) throws Exception {
         log.info(Thread.currentThread().getName() + "开始计算[{}]的设备运行诊断监测指标", projectDTO.getProjectId());
-        {
-            //判断当前计算轮次是否大于缓存中最新计算轮次,如果是,则证明已经被计算过,则略过
-            Object redisComputeRound = redisUtil.hget(DiAgnoseConst.RedisConstants.TargetComputeTime, projectDTO.getProjectId());
-            if (redisComputeRound != null && projectIndicatorCompute != null
-                    && projectIndicatorCompute.getComputeRound() < Long.parseLong(redisComputeRound.toString())) {
-                return new AsyncResult<>("【监测指标数据计算线程】当前项目:" + projectDTO.getProjectId() + "被占用");
-            }
+
+        //判断当前计算轮次是否大于缓存中最新计算轮次,如果是,则证明已经被计算过,则略过
+        if (checkProjectIsCalcd(projectDTO.getProjectId(), projectIndicatorCompute)) {
+            return new AsyncResult<>("【监测指标数据计算线程】当前项目:" + projectDTO.getProjectId() + "被占用");
         }
+
         //通过报警服务查询报警规则
         // 查询设备运行诊断的报警类型
         List<String> eqdxAlarmItemCodeList = energyAlarmService.queryEqdxAlarmItemCodes();
@@ -159,7 +157,7 @@ public class MonitorIndicatorRecordServiceImpl implements IMonitorIndicatorRecor
         }
 
         Set<String> objIdAndIndicatorId = getObjIdAndInfoCodeFromRules(projectAlarmRules);
-        log.info(Thread.currentThread().getName() + "共有{}个监测指标需要计算", objIdAndIndicatorId.size());
+        log.debug(Thread.currentThread().getName() + "共有{}个监测指标需要计算", objIdAndIndicatorId.size());
         if (CollectionUtils.isEmpty(objIdAndIndicatorId)) {
             return new AsyncResult<>("【监测指标数据计算线程】当前项目:" + projectDTO.getProjectId() + "报警规则未关联对象");
         }
@@ -167,37 +165,59 @@ public class MonitorIndicatorRecordServiceImpl implements IMonitorIndicatorRecor
         Map<String, CalculateMethodDTO> methodIdAndMethodDTO = energyAlarmService.queryCalculateMethods();
 
         for (String objIdIndicatorId : objIdAndIndicatorId) {
-            int indexOf = objIdIndicatorId.indexOf("_");
-            String objId = objIdIndicatorId.substring(0, indexOf);
-            String indicatorId = objIdIndicatorId.substring(indexOf + 1);
-            String methodId = energyAlarmService.queryObjIndicatorMethodId(projectDTO.getProjectId(), objId, indicatorId);
-            if (StringUtils.isBlank(methodId)) {
-                log.error("【监测指标数据计算线程】:项目监控指标未设置公式:" + projectDTO.getProjectId() + ";" + objId + ";" + indicatorId);
-                continue;
-            }
-            CalculateMethodDTO calculateMethodDTO = methodIdAndMethodDTO.get(methodId);
-            if (calculateMethodDTO == null) {
-                log.error("【监测指标数据计算线程】:项目监控指标设置公式为null:" + projectDTO.getProjectId() + ";" + objId + ";" + indicatorId);
-                continue;
-            }
-            Date lastComputeTime = objId_indicatorIdAndComputeTIme.get(objIdIndicatorId);
-            if (lastComputeTime == null) {
-                lastComputeTime = getDefultStartTime();
-            }
-            Date computeEndTime = DateUtils.addDays(lastComputeTime, computePropertiesConfig.getRoundDays());
-            String formula = calculateMethodDTO.getFormula();
-            if (StringUtils.isBlank(formula)) {
-                log.error("【监测指标数据计算线程】:项目监控指标设置公式为null:" + projectDTO.getProjectId() + ";" + objId + ";" + indicatorId);
-                continue;
-            }
-            Date computeTime = computeIndicatorData(projectDTO, objId, lastComputeTime, computeEndTime, indicatorId, formula);
-            saveOrUpdateComputeTime(projectDTO.getProjectId(), objId, indicatorId, computeTime);
+            computeObjIndicatorData(projectDTO, objId_indicatorIdAndComputeTIme.get(objIdIndicatorId), methodIdAndMethodDTO, objIdIndicatorId);
         }
         // 更新缓存计算轮次
         updateProjectComputeRound(projectDTO.getProjectId(), projectIndicatorCompute);
         return null;
     }
 
+    private void computeObjIndicatorData(ProjectDTO projectDTO, Date lastComputeTime, Map<String, CalculateMethodDTO> methodIdAndMethodDTO, String objIdIndicatorId) throws Exception {
+        int indexOf = objIdIndicatorId.indexOf("_");
+        String objId = objIdIndicatorId.substring(0, indexOf);
+        String indicatorId = objIdIndicatorId.substring(indexOf + 1);
+        String methodId = energyAlarmService.queryObjIndicatorMethodId(projectDTO.getProjectId(), objId, indicatorId);
+        if (StringUtils.isBlank(methodId)) {
+            log.error("【监测指标数据计算线程】:项目监控指标未设置公式:" + projectDTO.getProjectId() + ";" + objId + ";" + indicatorId);
+                return;
+        }
+        CalculateMethodDTO calculateMethodDTO = methodIdAndMethodDTO.get(methodId);
+        if (calculateMethodDTO == null) {
+            log.error("【监测指标数据计算线程】:项目监控指标设置公式为null:" + projectDTO.getProjectId() + ";" + objId + ";" + indicatorId);
+                return;
+        }
+        if (lastComputeTime == null) {
+            lastComputeTime = getDefultStartTime();
+        }
+        Date computeEndTime = DateUtils.addDays(lastComputeTime, computePropertiesConfig.getRoundDays());
+        String formula = calculateMethodDTO.getFormula();
+        if (StringUtils.isBlank(formula)) {
+            log.error("【监测指标数据计算线程】:项目监控指标设置公式为null:" + projectDTO.getProjectId() + ";" + objId + ";" + indicatorId);
+                return;
+        }
+        Date computeTime = computeIndicatorData(projectDTO, objId, lastComputeTime, computeEndTime, indicatorId, formula);
+        if (computeTime == null) {
+            return;
+        }
+        saveOrUpdateComputeTime(projectDTO.getProjectId(), objId, indicatorId, computeTime);
+    }
+
+    /**
+     * 判断项目是否已经被计算
+     *
+     * @param projectId
+     * @param projectIndicatorCompute
+     * @return
+     */
+    private Boolean checkProjectIsCalcd(String projectId, ProjectIndicatorCompute projectIndicatorCompute) {
+        Object redisComputeRound = redisUtil.hget(DiAgnoseConst.RedisConstants.TargetComputeTime, projectId);
+        if (redisComputeRound != null && projectIndicatorCompute != null
+                && projectIndicatorCompute.getComputeRound() < Long.parseLong(redisComputeRound.toString())) {
+            return true;
+        }
+        return false;
+    }
+
     /**
      * @throws
      * @title getObjIdAndInfoCodeFromRules
@@ -285,9 +305,7 @@ public class MonitorIndicatorRecordServiceImpl implements IMonitorIndicatorRecor
 
 
     private void saveOrUpdateComputeTime(String projectId, String objId, String indicatorId, Date computeTime) {
-        if (computeTime == null) {
-            return;
-        }
+
         CalculateTimeRecord update = new CalculateTimeRecord();
         update.setMonitorIndicatorId(indicatorId);
         update.setObjId(objId);
@@ -336,8 +354,8 @@ public class MonitorIndicatorRecordServiceImpl implements IMonitorIndicatorRecor
     }
 
     @Override
-    public void batchSaveMonitorIndicatorRecordList(List<MonitorIndicatorRecord> recordList)throws Exception {
-        if (!CollectionUtils.isEmpty(recordList) && recordList.size() > 0){
+    public void batchSaveMonitorIndicatorRecordList(List<MonitorIndicatorRecord> recordList) throws Exception {
+        if (!CollectionUtils.isEmpty(recordList) && recordList.size() > 0) {
             coredao.save(recordList);
         }
     }
@@ -403,13 +421,13 @@ public class MonitorIndicatorRecordServiceImpl implements IMonitorIndicatorRecor
      * @author lixing
      * @version V1.0 2021/12/18 12:31 下午
      */
-    private void cleanEqdxAlarmConditionState(){
+    private void cleanEqdxAlarmConditionState() {
         Cursor<Map.Entry<Object, Object>> entryCursor = redisTemplate.opsForHash().scan(
                 "ALARM_CONDITION_STATE", ScanOptions.scanOptions()       //绑定模糊查询的hash的key
-                .match("Eqdx*")                                 //模糊查询规则
-                .count(10000).build());
+                        .match("Eqdx*")                                 //模糊查询规则
+                        .count(10000).build());
 
-        while(entryCursor.hasNext()){
+        while (entryCursor.hasNext()) {
             Map.Entry next = entryCursor.next();
 
             String key = next.getKey().toString();
@@ -529,7 +547,7 @@ public class MonitorIndicatorRecordServiceImpl implements IMonitorIndicatorRecor
                     for (String objId : objIds) {
                         String key = objId + "_" + alarmItemCode;
                         if (!objIdAlarmItemCodeAndInfoCodes.containsKey(key)) {
-                            objIdAlarmItemCodeAndInfoCodes.put(key, new ArrayList<String>());
+                            objIdAlarmItemCodeAndInfoCodes.put(key, new ArrayList<>());
                         }
                         objIdAlarmItemCodeAndInfoCodes.get(key).add(infoCode);
                     }
@@ -821,7 +839,7 @@ public class MonitorIndicatorRecordServiceImpl implements IMonitorIndicatorRecor
 
     private List<Double> getObjStaticDataFromCenterData(String projectId, String objId, String computeCode) throws Exception {
         List<Double> arrayList = new ArrayList<>();
-        List<JSONObject> objectList = centerDataService.queryObjListByObjId(projectId, Arrays.asList(objId));
+        List<JSONObject> objectList = centerDataService.queryObjListByObjId(projectId, Collections.singletonList(objId));
         if (CollectionUtils.isEmpty(objectList)) {
             log.error("【指标数据计算线程】:查询数据中台对象不存在,:objId:" + objId + ";信息点:" + computeCode);
             arrayList.add(null);

+ 5 - 7
src/main/java/com/persagy/apm/diagnose/utils/RedisUtil.java

@@ -7,14 +7,11 @@ import java.util.concurrent.TimeUnit;
 
 import javax.annotation.Resource;
 
-import com.alibaba.fastjson.JSON;
-import lombok.extern.slf4j.Slf4j;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Component;
 import org.springframework.util.CollectionUtils;
 
 @Component
-@Slf4j
 public class RedisUtil {
 
     @Resource(name = "alarmRedisTemplate")
@@ -120,16 +117,17 @@ public class RedisUtil {
      * @return true成功 false失败
      */
 
-    public boolean set(String key, String value) {
+    public boolean set(String key, Object value) {
 
         try {
+
             redisTemplate.opsForValue().set(key, value);
 
             return true;
 
         } catch (Exception e) {
 
-            log.error("redis保存数据失败", e);
+            e.printStackTrace();
 
             return false;
 
@@ -146,7 +144,7 @@ public class RedisUtil {
      * @return true成功 false 失败
      */
 
-    public boolean set(String key, String value, long time) {
+    public boolean set(String key, Object value, long time) {
 
         try {
 
@@ -164,7 +162,7 @@ public class RedisUtil {
 
         } catch (Exception e) {
 
-            log.error("redis保存数据失败", e);
+            e.printStackTrace();
 
             return false;