Browse Source

新增 压测接口

miaolijing 3 years ago
parent
commit
9a46151c9e

+ 88 - 0
src/main/java/com/persagy/apm/alarmservice/group/alarmrule/controller/PressureMeasurementController.java

@@ -0,0 +1,88 @@
+package com.persagy.apm.alarmservice.group.alarmrule.controller;
+
+import com.persagy.apm.alarmservice.group.alarmrule.model.dto.AddGroupAlarmRuleDTO;
+import com.persagy.apm.alarmservice.group.alarmrule.service.IGroupAlarmRuleService;
+import com.persagy.apm.alarmservice.project.alarmcondition.model.dto.QueryAlarmConditionByObjDTO;
+import com.persagy.apm.alarmservice.project.alarmrule.service.IProjectAlarmRuleService;
+import com.persagy.apm.alarmservice.project.alarmruleobjrel.model.vo.ProjectVO;
+import com.persagy.apm.common.response.ApmResponseUpsertVO;
+import com.persagy.apm.common.response.CommonResult;
+import com.persagy.apm.common.utils.ResultHelper;
+import com.persagy.common.enums.ResponseCode;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.validation.Valid;
+import java.util.List;
+
+@Api(tags = "压力测试接口")
+@Validated
+@RestController
+@RequestMapping("pressure_measurement")
+public class PressureMeasurementController {
+
+    @Autowired
+    private IGroupAlarmRuleService groupAlarmRuleService;
+    @Autowired
+    private IProjectAlarmRuleService projectAlarmRuleService;
+
+    @ApiOperation(value = "创建集团报警规则")
+    @PostMapping("/create_alarm_project")
+    public void createAlarmProject() {
+        try {
+            List<ProjectVO> projects = projectAlarmRuleService.getProject();
+            if (projects.size() < 200){
+                StringBuffer sb = new StringBuffer();
+                for (int i = projects.size(); i < 210; i++) {
+                   int num = 1000+i;
+                   String projectId = "Pm202112"+num;
+                   String projectName = projectId+"广场";
+                    sb.append("(");
+                    sb.append("'"+num+"',");
+                    sb.append("'"+projectId+"',");
+                    sb.append("'"+projectName+"',");
+                    sb.append("'WD'");
+                    sb.append("),");
+                }
+                String substring = sb.substring(0, sb.length() - 1);
+                projectAlarmRuleService.insertProject(substring);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    @ApiOperation(value = "创建集团报警规则")
+    @PostMapping("/create")
+    public CommonResult<ApmResponseUpsertVO> createGroupAlarmRule(
+            @Valid @RequestBody AddGroupAlarmRuleDTO addGroupAlarmRuleDTO) {
+        String id = null;
+        try {
+            id = groupAlarmRuleService.createGroupAlarmRulePM(addGroupAlarmRuleDTO);
+        } catch (Exception e) {
+            e.printStackTrace();
+            return ResultHelper.failure(ResponseCode.B0001.getCode(), e.getMessage());
+        }
+        return ResultHelper.single(new ApmResponseUpsertVO(id));
+    }
+
+    @ApiOperation(value = "查询同步项目列表")
+    @PostMapping("/query_project")
+    public CommonResult<List<ProjectVO>> queryProject(@RequestBody QueryAlarmConditionByObjDTO alarmConditionByObjDTO) {
+        List<ProjectVO> projects = null;
+        try {
+            projects = projectAlarmRuleService.getProject();
+        } catch (Exception e) {
+            e.printStackTrace();
+            return ResultHelper.failure(ResponseCode.B0001.getCode(), e.getMessage());
+        }
+        return ResultHelper.single(projects);
+    }
+
+}

+ 14 - 0
src/main/java/com/persagy/apm/alarmservice/group/alarmrule/service/IGroupAlarmRuleService.java

@@ -85,4 +85,18 @@ public interface IGroupAlarmRuleService  {
     List<GroupAlarmRule> getExistAlarmItem(String groupId);
 
     void deleteEquipCondition(String condition, String classCode, String alarmItemCode) throws Exception;
+
+
+    /**
+     * TODO 压力测试
+     * 创建集团报警规则
+     *
+     * @param addGroupAlarmRuleDTO addDTO
+     * @return 集团报警规则主键
+     * @author lixing
+     * @version V1.0 2021-09-08 22:35:45
+     */
+    public String createGroupAlarmRulePM(AddGroupAlarmRuleDTO addGroupAlarmRuleDTO) throws Exception;
+
+
 }

+ 86 - 0
src/main/java/com/persagy/apm/alarmservice/group/alarmrule/service/impl/GroupAlarmRuleServiceImpl.java

@@ -474,4 +474,90 @@ public class GroupAlarmRuleServiceImpl extends ServiceImpl<GroupAlarmRuleMapper,
         queryWrapper.eq(GroupAlarmRule.PROP_VALID, ValidEnum.TRUE.getType());
         return list(queryWrapper);
     }
+
+    /**
+     * 创建集团报警规则
+     *
+     * @return 集团报警规则主键
+     * @author lixing
+     * @version V1.0 2021-09-08 22:35:45
+     */
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public String createGroupAlarmRulePM(AddGroupAlarmRuleDTO addGroupAlarmRuleDTO) throws Exception {
+        //1、给某个设备分组创建集团报警
+        GroupAlarmRule groupAlarmRule = ConvertGroupAlarmRuleTool.INSTANCE.convertAddDto2Entity(addGroupAlarmRuleDTO);
+        AddAlarmConditionDTO alarmCondition = addGroupAlarmRuleDTO.getAlarmCondition();
+        Map<String, Object> conditionMap = alarmConditionService.createAlarmCondition(alarmCondition);
+        //创建集团报警
+        groupAlarmRule.setAlarmConditionId((String) conditionMap.get("id"));
+        setDefaultValue(groupAlarmRule);
+        save(groupAlarmRule);
+        //2、异步创建项目报警
+        String userId = PoemsContext.getContext().getUserId();
+        String groupCode = PoemsContext.getContext().getGroupCode();
+        String projectId = PoemsContext.getContext().getProjectId();
+        String pd = PoemsContext.getContext().getPd();
+        String loginDevice = PoemsContext.getContext().getLoginDevice();
+        executor.execute(() -> {
+            // 切换线程需要重新设置线程中的通用变量
+            PoemsContext.setContext(userId, loginDevice, pd, groupCode, projectId);
+            projectAlarmRuleService.addProjectAlarmRulesPM(addGroupAlarmRuleDTO);
+        });
+
+        // 勿删,后续有可能恢复 lixing
+//        List<ProjectVO> projects = projectAlarmRuleService.getProject();
+//        if (projects.size() == 0) {
+//            throw new Exception("获取项目为空");
+//        }
+//        //3、同步创建设备报警条件
+//        List<AddAlarmConfigDTO> alarmConfigs = new ArrayList<>();
+//        String condition = addGroupAlarmRuleDTO.getCondition();
+//        String classCode = addGroupAlarmRuleDTO.getClassCode();
+//        for (ProjectVO project : projects) {
+//            QueryEquipDTO queryEquipDTO = new QueryEquipDTO();
+//            queryEquipDTO.setProjectId(projectId);
+//            queryEquipDTO.setClassCode(classCode);
+//            queryEquipDTO.setCondition(condition);
+//            List<EquipItem> record = groupManagementService.getEquipList(queryEquipDTO);
+//            for (EquipItem rec : record) {
+//                AddAlarmConfigDTO addAlarmConfigDTO = new AddAlarmConfigDTO();
+//                String equipId = rec.getId();
+//                addAlarmConfigDTO.setObjId(equipId);
+//                addAlarmConfigDTO.setItemCode(addGroupAlarmRuleDTO.getAlarmItemCode());
+//                addAlarmConfigDTO.setClassCode(addGroupAlarmRuleDTO.getClassCode());
+//                addAlarmConfigDTO.setLevel(alarmCondition.getAlarmLevel());
+//                addAlarmConfigDTO.setCategory("Eqdx");
+//                addAlarmConfigDTO.setOpen(1);
+//                AlarmCondition cond = new AlarmCondition();
+//                List<String> infoCodeList = (List<String>) conditionMap.get("infoCode");
+//                List<String> infoCodeNew = new ArrayList<>();
+//                for (String infoCode : infoCodeList) {
+//                    String replacement = equipId + "_" + infoCode;
+//                    infoCodeNew.add(infoCode.replaceAll(infoCode, replacement));
+//                }
+//                cond.setInfoCode(infoCodeNew);
+//                String backend = (String) conditionMap.get("backend");
+//                for (String infoCode : infoCodeList) {
+//                    String replacement = equipId + "_" + infoCode;
+//                    backend = backend.replaceAll(infoCode, replacement);
+//                }
+//                cond.setEnd("!" + backend);
+//                cond.setEndUphold(0);
+//                cond.setTrigger(backend);
+//                cond.setTriggerUphold(alarmCondition.getTriggerUphold() * 60);
+//                cond.setRules((List<String>) conditionMap.get("displayFormula"));
+//                JSONArray configs = JSONArray.parseArray(JSON.toJSONString(alarmCondition.getFrontend()));
+//                cond.setConfigs(configs);
+//                AlarmEffectTime effectTime = new AlarmEffectTime();
+//                effectTime.setType("always");
+//                cond.setEffectTime(effectTime);
+//                addAlarmConfigDTO.setCondition(cond);
+//                alarmConfigs.add(addAlarmConfigDTO);
+//            }
+//            projectAlarmRuleService.sendBatchCreateAlarmConfigMsg(alarmConfigs, projectId);
+//        }
+        return groupAlarmRule.getId();
+    }
+
 }

+ 7 - 0
src/main/java/com/persagy/apm/alarmservice/project/alarmrule/service/IProjectAlarmRuleService.java

@@ -104,4 +104,11 @@ public interface IProjectAlarmRuleService  {
 //    void sendBatchCreateAlarmConfigMsg(List<AddAlarmConfigDTO> alarmConfigs, String projectId);
 
     void deleteBatchByGroupRule(GroupAlarmRule groupAlarmRule) throws Exception;
+
+    /**
+     * TODO 压测  新增项目规则
+     * @param addGroupAlarmRuleDTO
+     */
+    void addProjectAlarmRulesPM(AddGroupAlarmRuleDTO addGroupAlarmRuleDTO);
+
 }

+ 54 - 4
src/main/java/com/persagy/apm/alarmservice/project/alarmrule/service/impl/ProjectAlarmRuleServiceImpl.java

@@ -47,10 +47,7 @@ import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.CollectionUtils;
 
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 import java.util.stream.Collectors;
 
 import static org.apache.commons.lang3.math.NumberUtils.INTEGER_ONE;
@@ -561,4 +558,57 @@ public class ProjectAlarmRuleServiceImpl extends ServiceImpl<ProjectAlarmRuleMap
         }
         return list.subList((currentPage - 1) * pagesize, Math.min(currentPage * pagesize, list.size()));
     }
+
+    @Override
+    public void addProjectAlarmRulesPM(AddGroupAlarmRuleDTO addGroupAlarmRuleDTO) {
+        String condition = addGroupAlarmRuleDTO.getCondition();
+        String classCode = addGroupAlarmRuleDTO.getClassCode();
+        //查询所有项目
+        List<ProjectVO> projects = this.getProject();
+        //遍历项目,组好对象进行插入
+        List<ProjectAlarmRule> projectAlarmRuleList;
+        String alarmItemCode = addGroupAlarmRuleDTO.getAlarmItemCode();
+        String alarmItemName = addGroupAlarmRuleDTO.getAlarmItemName();
+        List<AddProjectAlarmRuleObjRelDTO> addRelationList;
+        log.info("开始创建项目报警规则,并添加项目规则与设备的关联关系");
+        log.info("共有{}个项目需要创建报警规则", projects.size());
+        int count = 0;
+        for (ProjectVO project : projects) {
+            projectAlarmRuleList = new ArrayList<>();
+            addRelationList = new ArrayList<>();
+
+            ProjectAlarmRule projectAlarmRule = new ProjectAlarmRule();
+            projectAlarmRule.setProjectId(project.getProjectId());
+            projectAlarmRule.setAlarmItemCode(alarmItemCode);
+            projectAlarmRule.setAlarmItemName(alarmItemName);
+            projectAlarmRule.setBelong(INTEGER_ONE);
+            projectAlarmRule.setGroupId(addGroupAlarmRuleDTO.getGroupId());
+            projectAlarmRule.setId(DataUtils.getUUID());
+            String conditionId = getConditionId(alarmItemCode);
+            projectAlarmRule.setAlarmConditionId(conditionId);
+            setDefaultValue(projectAlarmRule);
+            projectAlarmRuleList.add(projectAlarmRule);
+            //新增关联关系
+            QueryEquipDTO queryEquipDTO = new QueryEquipDTO();
+            queryEquipDTO.setProjectId(project.getProjectId());
+            queryEquipDTO.setClassCode(classCode);
+            queryEquipDTO.setCondition(condition);
+            List<EquipItem> records = groupManagementService.getEquipList(queryEquipDTO);
+            for (int i = 100; i < 1500; i++) {
+                String objId = i+"-"+UUID.randomUUID().toString().replace("-","");
+                AddProjectAlarmRuleObjRelDTO addProjectAlarmRuleObjRelDTO = new AddProjectAlarmRuleObjRelDTO();
+                addProjectAlarmRuleObjRelDTO.setProjectAlarmRuleId(projectAlarmRule.getId());
+                addProjectAlarmRuleObjRelDTO.setProjectId(project.getProjectId());
+                addProjectAlarmRuleObjRelDTO.setObjId(objId);
+                addProjectAlarmRuleObjRelDTO.setObjName("设备名称AAA");
+                addProjectAlarmRuleObjRelDTO.setOpen(INTEGER_ONE);
+                addRelationList.add(addProjectAlarmRuleObjRelDTO);
+            }
+            // 按项目批量保存
+            saveBatch(projectAlarmRuleList);
+            projectAlarmRuleObjRelService.createBatchRelationAndSendMsg(addRelationList);
+            count++;
+            log.info("已完成{}个项目的报警规则创建, 剩余{}个项目的报警规则待创建", count, projects.size() - count);
+        }
+    }
 }