refactor: jenkins 通知发送

This commit is contained in:
Captain.B 2021-08-27 16:57:21 +08:00 committed by 刘瑞斌
parent 58ef571fdd
commit 812f0c9147
4 changed files with 58 additions and 39 deletions

View File

@ -83,8 +83,11 @@ public class NoticeSendService {
} }
} }
/**
* jenkins 和定时任务触发的发送
*/
public void send(String triggerMode, String taskType, NoticeModel noticeModel) { public void send(String triggerMode, String taskType, NoticeModel noticeModel) {
// api和定时任务调用排除自己 // api和定时任务调用排除自己
noticeModel.setExcludeSelf(false); noticeModel.setExcludeSelf(false);
try { try {
List<MessageDetail> messageDetails = new ArrayList<>(); List<MessageDetail> messageDetails = new ArrayList<>();
@ -117,9 +120,24 @@ public class NoticeSendService {
} }
} }
/**
* 后台触发的发送没有session
*/
public void send(Organization organization, String taskType, NoticeModel noticeModel) { public void send(Organization organization, String taskType, NoticeModel noticeModel) {
try { try {
List<MessageDetail> messageDetails = noticeService.searchMessageByTypeAndOrganizationId(taskType, organization.getId()); List<MessageDetail> messageDetails;
// switch (taskType) {
// case NoticeConstants.Mode.API:
// String projectId = (String) noticeModel.getParamMap().get("projectId");
// messageDetails = noticeService.searchMessageByTypeBySend(NoticeConstants.TaskType.JENKINS_TASK, projectId);
// break;
// case NoticeConstants.Mode.SCHEDULE:
// messageDetails = noticeService.searchMessageByTestId(noticeModel.getTestId());
// break;
// default:
// break;
// }
messageDetails = noticeService.searchMessageByTypeAndOrganizationId(taskType, organization.getId());
// 异步发送通知 // 异步发送通知
messageDetails.stream() messageDetails.stream()

View File

@ -31,6 +31,7 @@ import io.metersphere.track.request.report.QueryTestPlanReportRequest;
import io.metersphere.track.request.report.TestPlanReportSaveRequest; import io.metersphere.track.request.report.TestPlanReportSaveRequest;
import io.metersphere.track.request.testcase.QueryTestPlanRequest; import io.metersphere.track.request.testcase.QueryTestPlanRequest;
import io.metersphere.track.request.testplan.LoadCaseRequest; import io.metersphere.track.request.testplan.LoadCaseRequest;
import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils; import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -138,18 +139,18 @@ public class TestPlanReportService {
String planReportId = UUID.randomUUID().toString(); String planReportId = UUID.randomUUID().toString();
Map<String, String> apiCaseInfoMap = new HashMap<>(); Map<String, String> apiCaseInfoMap = new HashMap<>();
if(!apiTestCaseIdMap.isEmpty()){ if (!apiTestCaseIdMap.isEmpty()) {
ApiTestCaseExample apiTestCaseExample = new ApiTestCaseExample(); ApiTestCaseExample apiTestCaseExample = new ApiTestCaseExample();
apiTestCaseExample.createCriteria().andIdIn(new ArrayList<>(apiTestCaseIdMap.keySet())); apiTestCaseExample.createCriteria().andIdIn(new ArrayList<>(apiTestCaseIdMap.keySet()));
List<ApiTestCaseWithBLOBs> apiCaseList = apiTestCaseMapper.selectByExampleWithBLOBs(apiTestCaseExample); List<ApiTestCaseWithBLOBs> apiCaseList = apiTestCaseMapper.selectByExampleWithBLOBs(apiTestCaseExample);
Map<String,ApiTestCaseWithBLOBs> apiCaseDataMap = new HashMap<>(); Map<String, ApiTestCaseWithBLOBs> apiCaseDataMap = new HashMap<>();
if(!apiCaseList.isEmpty()){ if (!apiCaseList.isEmpty()) {
apiCaseDataMap = apiCaseList.stream().collect(Collectors.toMap(ApiTestCaseWithBLOBs::getId, k -> k)); apiCaseDataMap = apiCaseList.stream().collect(Collectors.toMap(ApiTestCaseWithBLOBs::getId, k -> k));
for (String id : apiCaseDataMap.keySet()) { for (String id : apiCaseDataMap.keySet()) {
apiCaseInfoMap.put(id,TestPlanApiExecuteStatus.PREPARE.name()); apiCaseInfoMap.put(id, TestPlanApiExecuteStatus.PREPARE.name());
String testPlanApiCaseId = apiTestCaseIdMap.get(id); String testPlanApiCaseId = apiTestCaseIdMap.get(id);
if(StringUtils.isNotEmpty(testPlanApiCaseId)){ if (StringUtils.isNotEmpty(testPlanApiCaseId)) {
apiTestCaseDataMap.put(apiCaseDataMap.get(id),testPlanApiCaseId); apiTestCaseDataMap.put(apiCaseDataMap.get(id), testPlanApiCaseId);
} }
} }
} }
@ -195,7 +196,8 @@ public class TestPlanReportService {
testPlanReport.setUpdateTime(System.currentTimeMillis()); testPlanReport.setUpdateTime(System.currentTimeMillis());
try { try {
testPlanReport.setName(testPlan.getName() + "-" + DateUtils.getTimeString(new Date())); testPlanReport.setName(testPlan.getName() + "-" + DateUtils.getTimeString(new Date()));
} catch (Exception ignored) {} } catch (Exception ignored) {
}
testPlanReport.setTriggerMode(saveRequest.getTriggerMode()); testPlanReport.setTriggerMode(saveRequest.getTriggerMode());
testPlanReport.setCreator(saveRequest.getUserId()); testPlanReport.setCreator(saveRequest.getUserId());
@ -246,7 +248,7 @@ public class TestPlanReportService {
performanceInfoMap = saveRequest.getPerformanceIdMap(); performanceInfoMap = saveRequest.getPerformanceIdMap();
} }
TestPlanReportExecuteCatch.addApiTestPlanExecuteInfo(testPlanReportID,apiCaseInfoMap,scenarioInfoMap,performanceInfoMap); TestPlanReportExecuteCatch.addApiTestPlanExecuteInfo(testPlanReportID, apiCaseInfoMap, scenarioInfoMap, performanceInfoMap);
testPlanReport.setPrincipal(testPlan.getPrincipal()); testPlanReport.setPrincipal(testPlan.getPrincipal());
if (testPlanReport.getIsScenarioExecuting() || testPlanReport.getIsApiCaseExecuting() || testPlanReport.getIsPerformanceExecuting()) { if (testPlanReport.getIsScenarioExecuting() || testPlanReport.getIsApiCaseExecuting() || testPlanReport.getIsPerformanceExecuting()) {
@ -510,7 +512,7 @@ public class TestPlanReportService {
return returnDTO; return returnDTO;
} }
public TestPlanReport updateReport(TestPlanReport testPlanReport,TestPlanReportContentWithBLOBs reportContent,boolean updateTime,TestPlanExecuteInfo executeInfo) { public TestPlanReport updateReport(TestPlanReport testPlanReport, TestPlanReportContentWithBLOBs reportContent, boolean updateTime, TestPlanExecuteInfo executeInfo) {
if (testPlanReport == null || executeInfo == null) { if (testPlanReport == null || executeInfo == null) {
return null; return null;
@ -538,7 +540,7 @@ public class TestPlanReportService {
testPlanReport.setIsPerformanceExecuting(false); testPlanReport.setIsPerformanceExecuting(false);
} }
if(apiCaseIsOk && scenarioIsOk && performanceIsOk){ if (apiCaseIsOk && scenarioIsOk && performanceIsOk) {
TestPlanReportExecuteCatch.remove(testPlanReport.getId()); TestPlanReportExecuteCatch.remove(testPlanReport.getId());
} }
@ -560,7 +562,7 @@ public class TestPlanReportService {
Map<String, Map<String, String>> testPlanExecuteResult = executeInfo.getExecutedResult(); Map<String, Map<String, String>> testPlanExecuteResult = executeInfo.getExecutedResult();
testPlanLog.info("ReportId[" + testPlanReport.getId() + "] COUNT OVER. COUNT RESULT :" + JSONObject.toJSONString(testPlanExecuteResult)); testPlanLog.info("ReportId[" + testPlanReport.getId() + "] COUNT OVER. COUNT RESULT :" + JSONObject.toJSONString(testPlanExecuteResult));
TestPlanSimpleReportDTO reportDTO = testPlanService.buildPlanReport(executeInfo, testPlanReport.getTestPlanId(),false); TestPlanSimpleReportDTO reportDTO = testPlanService.buildPlanReport(executeInfo, testPlanReport.getTestPlanId(), false);
//更新执行时间 //更新执行时间
reportDTO.setStartTime(testPlanReport.getStartTime()); reportDTO.setStartTime(testPlanReport.getStartTime());
reportDTO.setEndTime(System.currentTimeMillis()); reportDTO.setEndTime(System.currentTimeMillis());
@ -762,7 +764,7 @@ public class TestPlanReportService {
} }
if (testPlan != null && StringUtils.equalsAny(report.getTriggerMode(), ReportTriggerMode.API.name(), ReportTriggerMode.SCHEDULE.name())) { if (testPlan != null && StringUtils.equalsAny(report.getTriggerMode(), ReportTriggerMode.API.name(), ReportTriggerMode.SCHEDULE.name())) {
//发送通知 //发送通知
sendMessage(report,testPlan.getProjectId()); sendMessage(report, testPlan.getProjectId());
} }
} catch (Exception e) { } catch (Exception e) {
@ -774,7 +776,7 @@ public class TestPlanReportService {
return report; return report;
} }
public void sendMessage(TestPlanReport testPlanReport,String projectId) { public void sendMessage(TestPlanReport testPlanReport, String projectId) {
TestPlan testPlan = testPlanMapper.selectByPrimaryKey(testPlanReport.getTestPlanId()); TestPlan testPlan = testPlanMapper.selectByPrimaryKey(testPlanReport.getTestPlanId());
assert testPlan != null; assert testPlan != null;
SystemParameterService systemParameterService = CommonBeanFactory.getBean(SystemParameterService.class); SystemParameterService systemParameterService = CommonBeanFactory.getBean(SystemParameterService.class);
@ -803,12 +805,11 @@ public class TestPlanReportService {
} else { } else {
event = NoticeConstants.Event.EXECUTE_SUCCESSFUL; event = NoticeConstants.Event.EXECUTE_SUCCESSFUL;
} }
Map<String, Object> paramMap = new HashMap<>(); Map paramMap = new HashMap();
paramMap.put("testName", testPlan.getName());
paramMap.put("id", testPlanReport.getId());
paramMap.put("type", "testPlan"); paramMap.put("type", "testPlan");
paramMap.put("url", url); paramMap.put("url", url);
paramMap.put("status", testPlanReport.getStatus()); paramMap.put("projectId", projectId);
paramMap.putAll(new BeanMap(testPlanReport));
String successfulMailTemplate = ""; String successfulMailTemplate = "";
String errfoMailTemplate = ""; String errfoMailTemplate = "";
@ -831,8 +832,8 @@ public class TestPlanReportService {
.paramMap(paramMap) .paramMap(paramMap)
.build(); .build();
// noticeSendService.send(testPlanReport.getTriggerMode(), noticeModel); // noticeSendService.send(testPlanReport.getTriggerMode(), noticeModel);
Organization organization = projectService.getOrganizationByProjectId(projectId); // Organization organization = projectService.getOrganizationByProjectId(projectId);
noticeSendService.send(organization, testPlanReport.getTriggerMode(), noticeModel); noticeSendService.send(testPlanReport.getTriggerMode(), noticeModel);
} }
public TestPlanReport getTestPlanReport(String planId) { public TestPlanReport getTestPlanReport(String planId) {
@ -851,7 +852,7 @@ public class TestPlanReportService {
* 同时如果执行过程中报告删除那么此时也应当记为失败 * 同时如果执行过程中报告删除那么此时也应当记为失败
*/ */
Map<String, String> finishLoadTestId = new HashMap<>(); Map<String, String> finishLoadTestId = new HashMap<>();
Map<String,String> caseReportMap = new HashMap<>(); Map<String, String> caseReportMap = new HashMap<>();
executorService.submit(() -> { executorService.submit(() -> {
//错误数据检查集合 如果错误数据出现超过20次则取消该条数据的检查 //错误数据检查集合 如果错误数据出现超过20次则取消该条数据的检查
Map<String, Integer> errorDataCheckMap = new HashMap<>(); Map<String, Integer> errorDataCheckMap = new HashMap<>();
@ -906,8 +907,8 @@ public class TestPlanReportService {
this.updatePerformanceTestStatus(eventDTO); this.updatePerformanceTestStatus(eventDTO);
} }
} }
TestPlanReportExecuteCatch.updateApiTestPlanExecuteInfo(testPlanReport.getId(),null,null,finishLoadTestId); TestPlanReportExecuteCatch.updateApiTestPlanExecuteInfo(testPlanReport.getId(), null, null, finishLoadTestId);
TestPlanReportExecuteCatch.updateTestPlanExecuteResultInfo(testPlanReport.getId(),null,null,caseReportMap); TestPlanReportExecuteCatch.updateTestPlanExecuteResultInfo(testPlanReport.getId(), null, null, caseReportMap);
} else { } else {
try { try {
//查询定时任务是否关闭 //查询定时任务是否关闭
@ -1006,9 +1007,9 @@ public class TestPlanReportService {
TestPlanExecuteInfo executeInfo = TestPlanReportExecuteCatch.getTestPlanExecuteInfo(planReportId); TestPlanExecuteInfo executeInfo = TestPlanReportExecuteCatch.getTestPlanExecuteInfo(planReportId);
Map<String,String> executeApiCaseIdMap = executeInfo.getApiCaseExecInfo(); Map<String, String> executeApiCaseIdMap = executeInfo.getApiCaseExecInfo();
Map<String,String> executeScenarioCaseIdMap = executeInfo.getApiScenarioCaseExecInfo(); Map<String, String> executeScenarioCaseIdMap = executeInfo.getApiScenarioCaseExecInfo();
Map<String,String> executePerformanceIdMap = executeInfo.getLoadCaseExecInfo(); Map<String, String> executePerformanceIdMap = executeInfo.getLoadCaseExecInfo();
if (executeApiCaseIdMap == null) { if (executeApiCaseIdMap == null) {
executeApiCaseIdMap = new HashMap<>(); executeApiCaseIdMap = new HashMap<>();
} }
@ -1029,12 +1030,12 @@ public class TestPlanReportService {
TestPlanReport report = null; TestPlanReport report = null;
if (!reportDataList.isEmpty()) { if (!reportDataList.isEmpty()) {
TestPlanReportExecuteCatch.setReportDataCheckResult(planReportId,true); TestPlanReportExecuteCatch.setReportDataCheckResult(planReportId, true);
TestPlanReportContentWithBLOBs reportData = reportDataList.get(0); TestPlanReportContentWithBLOBs reportData = reportDataList.get(0);
report = testPlanReportMapper.selectByPrimaryKey(planReportId); report = testPlanReportMapper.selectByPrimaryKey(planReportId);
report = this.updateReport(report,reportData,updateTime,executeInfo); report = this.updateReport(report, reportData, updateTime, executeInfo);
} else { } else {
TestPlanReportExecuteCatch.setReportDataCheckResult(planReportId,false); TestPlanReportExecuteCatch.setReportDataCheckResult(planReportId, false);
} }
return report; return report;
@ -1045,7 +1046,7 @@ public class TestPlanReportService {
example.createCriteria().andTestPlanIdEqualTo(planId); example.createCriteria().andTestPlanIdEqualTo(planId);
List<TestPlanReport> reportList = this.testPlanReportMapper.selectByExample(example); List<TestPlanReport> reportList = this.testPlanReportMapper.selectByExample(example);
List<String> testPlanReportIdList = new ArrayList<>(); List<String> testPlanReportIdList = new ArrayList<>();
for (TestPlanReport report:reportList) { for (TestPlanReport report : reportList) {
testPlanReportIdList.add(report.getId()); testPlanReportIdList.add(report.getId());
} }
this.delete(testPlanReportIdList); this.delete(testPlanReportIdList);
@ -1054,14 +1055,14 @@ public class TestPlanReportService {
public void countReport(String planReportId) { public void countReport(String planReportId) {
TestPlanExecuteInfo executeInfo = TestPlanReportExecuteCatch.getTestPlanExecuteInfo(planReportId); TestPlanExecuteInfo executeInfo = TestPlanReportExecuteCatch.getTestPlanExecuteInfo(planReportId);
int unFinishNum = executeInfo.countUnFinishedNum(); int unFinishNum = executeInfo.countUnFinishedNum();
if(unFinishNum > 0){ if (unFinishNum > 0) {
//如果间隔超过5分钟没有案例执行完成则把执行结果变成false //如果间隔超过5分钟没有案例执行完成则把执行结果变成false
long lastCountTime = executeInfo.getLastFinishedNumCountTime(); long lastCountTime = executeInfo.getLastFinishedNumCountTime();
long nowTime = System.currentTimeMillis(); long nowTime = System.currentTimeMillis();
if(nowTime - lastCountTime > 300000){ if (nowTime - lastCountTime > 300000) {
TestPlanReportExecuteCatch.finishAllTask(planReportId); TestPlanReportExecuteCatch.finishAllTask(planReportId);
}
} }
}
this.updateExecuteApis(planReportId); this.updateExecuteApis(planReportId);
} }

View File

@ -82,7 +82,7 @@ export default {
this.totalCount = response.data.itemCount; this.totalCount = response.data.itemCount;
this.systemNoticeData.forEach(n => { this.systemNoticeData.forEach(n => {
n.user = this.userMap[n.operator]; n.user = this.userMap[n.operator] || {name: "MS"};
}); });
}); });
}, },

View File

@ -81,7 +81,7 @@ export default {
this.totalPage = response.data.pageCount; this.totalPage = response.data.pageCount;
this.totalCount = response.data.itemCount; this.totalCount = response.data.itemCount;
this.systemNoticeData.forEach(n => { this.systemNoticeData.forEach(n => {
n.user = this.userMap[n.operator]; n.user = this.userMap[n.operator] || {name: "MS"};
}); });
}); });
}, },