fix:添加保存

This commit is contained in:
wenyann 2020-09-17 16:14:38 +08:00
parent c0a9dc003d
commit 167dd8d0e8
6 changed files with 107 additions and 71 deletions

View File

@ -3,16 +3,20 @@ package io.metersphere.api.jmeter;
import io.metersphere.api.service.APIReportService;
import io.metersphere.api.service.APITestService;
import io.metersphere.base.domain.ApiTestReport;
import io.metersphere.base.domain.Notice;
import io.metersphere.commons.constants.APITestStatus;
import io.metersphere.commons.constants.ApiRunMode;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.notice.service.MailService;
import io.metersphere.notice.service.NoticeService;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.assertions.AssertionResult;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.visualizers.backend.AbstractBackendListenerClient;
import org.apache.jmeter.visualizers.backend.BackendListenerContext;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.*;
@ -113,9 +117,12 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
report = apiReportService.getRunningReport(testResult.getTestId());
}
apiReportService.complete(testResult, report);
queue.clear();
super.teardownTest(context);
NoticeService noticeService = CommonBeanFactory.getBean(NoticeService.class);
List<Notice> notice = noticeService.queryNotice(testResult.getTestId());
MailService mailService = CommonBeanFactory.getBean(MailService.class);
mailService.sendHtml(report.getId(), notice, report.getStatus(), "api");
}
private RequestResult getRequestResult(SampleResult result) {
@ -170,7 +177,7 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
this.runMode = context.getParameter("runMode");
this.debugReportId = context.getParameter("debugReportId");
if (StringUtils.isBlank(this.runMode)) {
this.runMode = ApiRunMode.RUN.name();
this.runMode = ApiRunMode.RUN.name();
}
}

View File

@ -223,10 +223,10 @@ public class APITestService {
apiTest.setUserId(request.getUserId());
}
String reportId = apiReportService.create(apiTest, request.getTriggerMode());
if (request.getTriggerMode().equals("SCHEDULE")) {
/*if (request.getTriggerMode().equals("SCHEDULE")) {
List<Notice> notice = noticeService.queryNotice(request.getId());
mailService.sendHtml(reportId,notice,"api");
}
}*/
changeStatus(request.getId(), APITestStatus.Running);
jMeterService.run(request.getId(), null, is);
return reportId;

View File

@ -9,6 +9,7 @@ import io.metersphere.commons.utils.LogUtil;
import io.metersphere.dto.LoadTestDTO;
import io.metersphere.service.SystemParameterService;
import io.metersphere.service.UserService;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
@ -30,25 +31,25 @@ public class MailService {
@Resource
private SystemParameterService systemParameterService;
public void sendHtml(String id, List<Notice> notice,String type) {
public void sendHtml(String id, List<Notice> notice, String status, String type) {
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
List<SystemParameter> paramList=systemParameterService.getParamList(ParamConstants.Classify.MAIL.getValue());
List<SystemParameter> paramList = systemParameterService.getParamList(ParamConstants.Classify.MAIL.getValue());
javaMailSender.setDefaultEncoding("UTF-8");
javaMailSender.setProtocol("smtps");
for(SystemParameter p:paramList){
if(p.getParamKey().equals("smtp.host")){
javaMailSender.setHost(p.getParamValue());
}
if(p.getParamKey().equals("smtp.port")){
javaMailSender.setPort(Integer.parseInt(p.getParamValue()));
}
if(p.getParamKey().equals("smtp.account")){
javaMailSender.setUsername(p.getParamValue());
}
if(p.getParamKey().equals("smtp.password")){
javaMailSender.setPassword(EncryptUtils.aesDecrypt(p.getParamValue()).toString());
}
for (SystemParameter p : paramList) {
if (p.getParamKey().equals("smtp.host")) {
javaMailSender.setHost(p.getParamValue());
}
if (p.getParamKey().equals("smtp.port")) {
javaMailSender.setPort(Integer.parseInt(p.getParamValue()));
}
if (p.getParamKey().equals("smtp.account")) {
javaMailSender.setUsername(p.getParamValue());
}
if (p.getParamKey().equals("smtp.password")) {
javaMailSender.setPassword(EncryptUtils.aesDecrypt(p.getParamValue()).toString());
}
}
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
@ -58,15 +59,12 @@ public class MailService {
javaMailSender.setJavaMailProperties(props);
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
String testName = "";
String state = "";
if (type.equals("api")) {
APIReportResult reportResult = apiAndPerformanceHelper.getApi(id);
testName = reportResult.getTestName();
state = reportResult.getStatus();
} else if (type.equals("performance")) {
LoadTestDTO performanceResult = apiAndPerformanceHelper.getPerformance(id);
testName = performanceResult.getName();
state = performanceResult.getStatus();
}
String html1 = "<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
@ -76,12 +74,12 @@ public class MailService {
"</head>\n" +
"<body style=\"text-align: left\">\n" +
" <div>\n" +
" <h3>"+type+"定时任务结果通知</h3>\n" +
" <p> 尊敬的用户:您好,您所执行的"+testName+"运行失败,请点击报告链接查看</p>\n" +
" <h3>" + type + "定时任务结果通知</h3>\n" +
" <p> 尊敬的用户:您好,您所执行的" + testName + "运行失败,请点击报告链接查看</p>\n" +
" </div>\n" +
"</body>\n" +
"</html>";
String html2="<!DOCTYPE html>\n" +
String html2 = "<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
"<head>\n" +
" <meta charset=\"UTF-8\">\n" +
@ -89,8 +87,8 @@ public class MailService {
"</head>\n" +
"<body style=\"text-align: left\">\n" +
" <div>\n" +
" <h3>"+type+"定时任务结果通知</h3>\n" +
" <p> 尊敬的用户:您好,"+testName+"运行成功,请点击报告链接查看</p>\n" +
" <h3>" + type + "定时任务结果通知</h3>\n" +
" <p> 尊敬的用户:您好," + testName + "运行成功,请点击报告链接查看</p>\n" +
" </div>\n" +
"</body>\n" +
"</html>";
@ -99,36 +97,40 @@ public class MailService {
helper.setFrom(javaMailSender.getUsername());
helper.setSubject("MeterSphere定时任务结果通知");
String users[] = {};
List<String> successEmailList=new ArrayList<>();
List<String> failEmailList=new ArrayList<>();
if(notice.size()>0){
for(Notice n:notice){
if (n.getEnable().equals("true")&&n.getEvent().equals("执行成功")) {
successEmailList=userService.queryEmail(n.getNames());
List<String> successEmailList = new ArrayList<>();
List<String> failEmailList = new ArrayList<>();
if (notice.size() > 0) {
for (Notice n : notice) {
if (n.getEnable().equals("true") && n.getEvent().equals("执行成功")) {
successEmailList = userService.queryEmail(n.getNames());
}
if (n.getEnable().equals("true")&&n.getEvent().equals("执行失败")) {
failEmailList=userService.queryEmail(n.getNames());
if (n.getEnable().equals("true") && n.getEvent().equals("执行失败")) {
failEmailList = userService.queryEmail(n.getNames());
}
}
}else{
} else {
LogUtil.error("Recipient information is empty");
}
if(state.equals("success")){
users=successEmailList.toArray(new String[successEmailList.size()]);
helper.setText(html2,true);
}else{
users=failEmailList.toArray(new String[failEmailList.size()]);
helper.setText(html1,true);
if (status.equals("Success")) {
users = successEmailList.toArray(new String[successEmailList.size()]);
helper.setText(html2, true);
} else {
users = failEmailList.toArray(new String[failEmailList.size()]);
helper.setText(html1, true);
}
helper.setTo(users);
javaMailSender.send(mimeMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
try {
javaMailSender.send(mimeMessage);
} catch (MailException e) {
e.printStackTrace();
}
}

View File

@ -16,53 +16,77 @@ import java.util.List;
public class NoticeService {
@Resource
private NoticeMapper noticeMapper;
public void saveNotice(NoticeRequest noticeRequest) {
Notice notice = new Notice();
NoticeExample example = new NoticeExample();
example.createCriteria().andTestIdEqualTo(noticeRequest.getTestId());
List<Notice> notices = noticeMapper.selectByExample(example);
if (notices != null) {
if (notices.size() > 0) {
noticeMapper.deleteByExample(example);
noticeRequest.getNotices().forEach(notice -> {
for(String x:notice.getNames()){
notice.setEvent(notice.getEvent());
notice.setEmail(notice.getEmail());
notice.setEnable(notice.getEnable());
noticeRequest.getNotices().forEach(n -> {
if (n.getNames().length > 0) {
for (String x : n.getNames()) {
notice.setEvent(n.getEvent());
notice.setEmail(n.getEmail());
notice.setEnable(n.getEnable());
notice.setTestId(noticeRequest.getTestId());
notice.setName(x);
noticeMapper.insert(notice);
}
} else {
notice.setEvent(n.getEvent());
notice.setEmail(n.getEmail());
notice.setEnable(n.getEnable());
notice.setTestId(noticeRequest.getTestId());
notice.setName(x);
notice.setName("");
noticeMapper.insert(notice);
}
});
} else {
noticeRequest.getNotices().forEach(notice -> {
notice.setEvent(noticeRequest.getEvent());
notice.setTestId(noticeRequest.getTestId());
noticeMapper.insert(notice);
noticeRequest.getNotices().forEach(n -> {
if (n.getNames().length > 0) {
for (String x : n.getNames()) {
notice.setEvent(n.getEvent());
notice.setEmail(n.getEmail());
notice.setEnable(n.getEnable());
notice.setTestId(noticeRequest.getTestId());
notice.setName(x);
noticeMapper.insert(notice);
}
} else {
notice.setEvent(n.getEvent());
notice.setEmail(n.getEmail());
notice.setEnable(n.getEnable());
notice.setTestId(noticeRequest.getTestId());
notice.setName("");
noticeMapper.insert(notice);
}
});
}
}
public List<Notice> queryNotice(String id) {
NoticeExample example = new NoticeExample();
example.createCriteria().andTestIdEqualTo(id);
List<Notice> notices=noticeMapper.selectByExample(example);
List<Notice> notice=new ArrayList<>();
List<String> success=new ArrayList<>();
List<String> fail=new ArrayList<>();
String[] successArray=new String[success.size()];
String[] failArray=new String[fail.size()];
Notice notice1=new Notice();
Notice notice2=new Notice();
if(notices.size()>0){
for(Notice n:notices){
if(n.getEvent().equals("执行成功")){
List<Notice> notices = noticeMapper.selectByExample(example);
List<Notice> notice = new ArrayList<>();
List<String> success = new ArrayList<>();
List<String> fail = new ArrayList<>();
String[] successArray = new String[success.size()];
String[] failArray = new String[fail.size()];
Notice notice1 = new Notice();
Notice notice2 = new Notice();
if (notices.size() > 0) {
for (Notice n : notices) {
if (n.getEvent().equals("执行成功")) {
success.add(n.getName());
notice1.setEnable(n.getEnable());
notice1.setTestId(id);
notice1.setEvent(n.getEvent());
notice1.setEmail(n.getEmail());
}
if(n.getEvent().equals("执行失败")){
if (n.getEvent().equals("执行失败")) {
fail.add(n.getName());
notice2.setEnable(n.getEnable());
notice2.setTestId(id);
@ -70,8 +94,8 @@ public class NoticeService {
notice2.setEmail(n.getEmail());
}
}
successArray=success.toArray(new String[success.size()]);
failArray=fail.toArray(new String[fail.size()]);
successArray = success.toArray(new String[success.size()]);
failArray = fail.toArray(new String[fail.size()]);
notice1.setNames(successArray);
notice2.setNames(failArray);
notice.add(notice1);

View File

@ -239,7 +239,7 @@ public class PerformanceTestService {
startEngine(loadTest, engine, request.getTriggerMode());
if (request.getTriggerMode().equals("SCHEDULE")) {
List<Notice> notice = noticeService.queryNotice(request.getId());
mailService.sendHtml(engine.getReportId(),notice,"performance");
mailService.sendHtml(engine.getReportId(), notice, "success", "performance");
}
return engine.getReportId();
}

View File

@ -71,6 +71,7 @@
</template>
</el-table-column>
</el-table>
<el-button type="primary" @click="saveNotice">{{$t('commons.save')}}</el-button>
</template>
</el-tab-pane>
</el-tabs>
@ -206,9 +207,11 @@
return false;
}
});
},
saveNotice(){
let param = this.buildParam();
this.result = this.$post("notice/save", param, () => {
this.$success(this.$t('commons.save_success'));
})
},
close() {