Browse Source

优化创建压测数据接口

lixing 3 years ago
parent
commit
1804d43f64

+ 33 - 30
src/main/java/com/persagy/apm/diagnose/maintenance/controller/MaintenanceController.java

@@ -4,16 +4,13 @@ import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.persagy.apm.diagnose.config.RabbitConfig;
 import com.persagy.apm.diagnose.constant.AlarmConstants;
-import com.persagy.apm.diagnose.indicatorrecord.model.dto.QueryMonitorIndicatorRecordDTO;
 import com.persagy.apm.diagnose.indicatorrecord.service.IMonitorIndicatorRecordService;
 import com.persagy.apm.diagnose.maintenance.constant.EnumAlarmItem;
 import com.persagy.apm.diagnose.maintenance.constant.EnumMaintenanceIndicatorType;
 import com.persagy.apm.diagnose.maintenance.model.dto.AlarmProjectDTO;
 import com.persagy.apm.diagnose.maintenance.model.dto.CreatePMDataDTO;
 import com.persagy.apm.diagnose.service.EnergyAlarmServiceImpl;
-import com.persagy.apm.diagnose.service.SaasWebServiceImpl;
 import com.persagy.apm.diagnose.service.dto.AlarmRuleDTO;
-import com.persagy.apm.diagnose.service.dto.ProjectDTO;
 import com.persagy.apm.diagnose.utils.DateUtils;
 import com.persagy.apm.diagnose.utils.RedisLock;
 import com.persagy.apm.diagnose.utils.RedisUtil;
@@ -47,36 +44,40 @@ public class MaintenanceController {
     private EnergyAlarmServiceImpl energyAlarmService;
     @Autowired
     private IMonitorIndicatorRecordService monitorIndicatorRecordService;
+
     //创建压测 数据
     @PostMapping("/createDataPM")
     public void createPMData(@Valid @RequestBody CreatePMDataDTO createPMDataDTO) throws Exception {
         String startTime = createPMDataDTO.getStartTime();
         String endTime = createPMDataDTO.getEndTime();
-        Date startDate = DateUtils.str2Date(startTime,DateUtils.SDF_SECOND);
-        Date endDate = DateUtils.str2Date(endTime,DateUtils.SDF_SECOND);
-        log.info("开始创造压测测试数据,时间范围:{}~~~{}",startTime,endTime);
+        Date startDate = DateUtils.str2Date(startTime, DateUtils.SDF_SECOND);
+        Date endDate = DateUtils.str2Date(endTime, DateUtils.SDF_SECOND);
+        log.info("开始创造压测测试数据,时间范围:{}~~~{}", startTime, endTime);
         //查询项目列表
         List<AlarmProjectDTO> alarmProjectDTOList = energyAlarmService.getAlarmProjectDTOList();
         int count = 0;
-        if (!CollectionUtils.isEmpty(alarmProjectDTOList)){
+        if (!CollectionUtils.isEmpty(alarmProjectDTOList)) {
             log.info("共有{}个项目需要创建压测测试数据", alarmProjectDTOList.size());
-            for (AlarmProjectDTO projectDTO:alarmProjectDTOList) {
-                //通过报警服务查询报警规则
+            // 先按项目、再按时间、再按
+            for (AlarmProjectDTO projectDTO : alarmProjectDTOList) {
+                log.info("添加项目{}的监测值", projectDTO.getProjectId());
                 List<AlarmRuleDTO> projectAlarmRules = energyAlarmService.getProjectAlarmRules(projectDTO.getProjectId());
-                for (AlarmRuleDTO projectAlarmRule : projectAlarmRules) {
-                    List<String> objIds = projectAlarmRule.getObjIds();
-                    if (CollectionUtils.isEmpty(objIds)) {
-                        continue;
-                    }
-                    List<String> infoCodes = projectAlarmRule.getInfoCodes();
-                    if (CollectionUtils.isEmpty(infoCodes)) {
-                        continue;
-                    }
-                    for (String infoCode : infoCodes) {
-                        List<MonitorIndicatorRecord> recordList = new ArrayList<>();
-                        for (String objId : objIds) {
-                            for (Date start = startDate; start.before(endDate);start = DateUtils.addMinutes(start,15)) {
-                                int num = (int) (Math.random()*100);
+                for (Date start = startDate; start.before(endDate); start = DateUtils.addMinutes(start, 15)) {
+                    //通过报警服务查询报警规则
+                    List<MonitorIndicatorRecord> recordList = new ArrayList<>();
+                    for (AlarmRuleDTO projectAlarmRule : projectAlarmRules) {
+                        List<String> objIds = projectAlarmRule.getObjIds();
+                        if (CollectionUtils.isEmpty(objIds)) {
+                            continue;
+                        }
+                        List<String> infoCodes = projectAlarmRule.getInfoCodes();
+                        if (CollectionUtils.isEmpty(infoCodes)) {
+                            continue;
+                        }
+                        for (String infoCode : infoCodes) {
+                            for (String objId : objIds) {
+
+                                int num = (int) (Math.random() * 100);
                                 //构造实体对象
                                 MonitorIndicatorRecord monitorIndicatorRecord = new MonitorIndicatorRecord();
                                 monitorIndicatorRecord.setProject(projectDTO.getProjectId());
@@ -87,13 +88,15 @@ public class MaintenanceController {
                                 recordList.add(monitorIndicatorRecord);
                             }
                         }
-                        monitorIndicatorRecordService.batchSaveMonitorIndicatorRecordList(recordList);
-                        Thread.sleep(5);
                     }
+                    long saveIndicatorStartTime = System.currentTimeMillis();
+                    monitorIndicatorRecordService.batchSaveMonitorIndicatorRecordList(recordList);
+                    log.info("添加{}的监测值", startDate);
+                    log.info("共添加{}个监测值,共用时{}ms", recordList.size(), System.currentTimeMillis() - saveIndicatorStartTime);
                 }
                 count++;
-                Thread.sleep(5);
                 log.info("已完成{}个项目的压测测试数据创建, 剩余{}个项目的压测测试数据待创建", count, alarmProjectDTOList.size() - count);
+                Thread.sleep(5);
             }
         }
     }
@@ -116,9 +119,9 @@ public class MaintenanceController {
         String objId = "Eq1101060001f00f2ed121f9407783ac0f0a520a9616", alarmItemCode = EnumAlarmItem.EvapAbnormal.getCode();
         String lockKey = AlarmConstants.alarmDataLockPrefix + objId + "-" + alarmItemCode;
         for (String time : iotDataMap.keySet()) {
-            while (true){
+            while (true) {
                 Object isLock = redisUtil.get(lockKey);
-                if (isLock == null){
+                if (isLock == null) {
                     break;
                 }
                 Thread.sleep(100);
@@ -127,9 +130,9 @@ public class MaintenanceController {
             lockUtil.sendDataLock(lockKey, String.valueOf(timeLong));
             JSONArray array = new JSONArray();
             if (iotDataMap.get(time)) {
-                array.add(getTriggerData(time,objId,alarmItemCode));
+                array.add(getTriggerData(time, objId, alarmItemCode));
             } else {
-                array.add(getNormalData(time,objId,alarmItemCode));
+                array.add(getNormalData(time, objId, alarmItemCode));
             }
             //推送
             rabbitConfig.sendMsg(array);