Merge remote-tracking branch 'origin/master'

This commit is contained in:
Captain.B 2020-12-24 10:24:57 +08:00
commit 74abe9ece5
7 changed files with 74 additions and 29 deletions

View File

@ -302,7 +302,7 @@ public class APITestController {
if(allCount!=0){ if(allCount!=0){
float coverageRageNumber =(float)apiCountResult.getSuccessCount()*100/allCount; float coverageRageNumber =(float)apiCountResult.getSuccessCount()*100/allCount;
DecimalFormat df = new DecimalFormat("0.0"); DecimalFormat df = new DecimalFormat("0.0");
apiCountResult.setCoverageRage(df.format(coverageRageNumber)+"%"); apiCountResult.setSuccessRage(df.format(coverageRageNumber)+"%");
} }
return apiCountResult; return apiCountResult;
@ -354,6 +354,6 @@ public class APITestController {
public void updateScheduleEnableByPrimyKey(@RequestBody ScheduleInfoRequest request) { public void updateScheduleEnableByPrimyKey(@RequestBody ScheduleInfoRequest request) {
Schedule schedule = scheduleService.getSchedule(request.getTaskID()); Schedule schedule = scheduleService.getSchedule(request.getTaskID());
schedule.setEnable(request.isEnable()); schedule.setEnable(request.isEnable());
apiTestService.updateSchedule(schedule); apiAutomationService.updateSchedule(schedule);
} }
} }

View File

@ -1,6 +1,7 @@
package io.metersphere.api.jmeter; package io.metersphere.api.jmeter;
import io.metersphere.api.service.*; import io.metersphere.api.service.*;
import io.metersphere.base.domain.ApiScenarioReport;
import io.metersphere.base.domain.ApiTestReport; import io.metersphere.base.domain.ApiTestReport;
import io.metersphere.commons.constants.APITestStatus; import io.metersphere.commons.constants.APITestStatus;
import io.metersphere.commons.constants.ApiRunMode; import io.metersphere.commons.constants.ApiRunMode;
@ -20,6 +21,7 @@ import org.apache.jmeter.protocol.http.sampler.HTTPSampleResult;
import org.apache.jmeter.samplers.SampleResult; import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.visualizers.backend.AbstractBackendListenerClient; import org.apache.jmeter.visualizers.backend.AbstractBackendListenerClient;
import org.apache.jmeter.visualizers.backend.BackendListenerContext; import org.apache.jmeter.visualizers.backend.BackendListenerContext;
import org.python.antlr.ast.Str;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -50,6 +52,7 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
private ApiScenarioReportService apiScenarioReportService; private ApiScenarioReportService apiScenarioReportService;
public String runMode = ApiRunMode.RUN.name(); public String runMode = ApiRunMode.RUN.name();
// 测试ID // 测试ID
@ -155,6 +158,7 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
testResult.getScenarios().addAll(scenarios.values()); testResult.getScenarios().addAll(scenarios.values());
testResult.getScenarios().sort(Comparator.comparing(ScenarioResult::getId)); testResult.getScenarios().sort(Comparator.comparing(ScenarioResult::getId));
ApiTestReport report = null; ApiTestReport report = null;
String reportUrl = null;
// 这部分后续优化只留 DELIMIT SCENARIO 两部分 // 这部分后续优化只留 DELIMIT SCENARIO 两部分
if (StringUtils.equals(this.runMode, ApiRunMode.DEBUG.name())) { if (StringUtils.equals(this.runMode, ApiRunMode.DEBUG.name())) {
report = apiReportService.get(debugReportId); report = apiReportService.get(debugReportId);
@ -173,7 +177,31 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
} else if (StringUtils.equalsAny(this.runMode, ApiRunMode.SCENARIO.name(), ApiRunMode.SCENARIO_PLAN.name())) { } else if (StringUtils.equalsAny(this.runMode, ApiRunMode.SCENARIO.name(), ApiRunMode.SCENARIO_PLAN.name())) {
// 执行报告不需要存储由用户确认后在存储 // 执行报告不需要存储由用户确认后在存储
testResult.setTestId(testId); testResult.setTestId(testId);
apiScenarioReportService.complete(testResult, this.runMode); ApiScenarioReport scenarioReport = apiScenarioReportService.complete(testResult, this.runMode);
report = new ApiTestReport();
report.setStatus(scenarioReport.getStatus());
report.setId(scenarioReport.getId());
report.setTriggerMode(scenarioReport.getTriggerMode());
report.setName(scenarioReport.getName());
SystemParameterService systemParameterService = CommonBeanFactory.getBean(SystemParameterService.class);
assert systemParameterService != null;
BaseSystemConfigDTO baseSystemConfigDTO = systemParameterService.getBaseInfo();
reportUrl = baseSystemConfigDTO.getUrl() + "/#/api/automation/report";
String scenaName = scenarioReport.getName();
if(scenaName==null){
scenaName = "";
}else {
scenaName = scenaName.split("-")[0];
}
String scenarioID = apiScenarioReportService.getApiScenarioId(scenaName,scenarioReport.getProjectId());
testResult.setTestId(scenarioID);
} else { } else {
apiTestService.changeStatus(testId, APITestStatus.Completed); apiTestService.changeStatus(testId, APITestStatus.Completed);
report = apiReportService.getRunningReport(testResult.getTestId()); report = apiReportService.getRunningReport(testResult.getTestId());
@ -196,22 +224,24 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
} }
} }
try { try {
sendTask(report, testResult); sendTask(report,reportUrl, testResult);
} catch (Exception e) { } catch (Exception e) {
LogUtil.error(e.getMessage(), e); LogUtil.error(e.getMessage(), e);
} }
} }
private static void sendTask(ApiTestReport report, TestResult testResult) { private static void sendTask(ApiTestReport report,String reportUrl, TestResult testResult) {
SystemParameterService systemParameterService = CommonBeanFactory.getBean(SystemParameterService.class); SystemParameterService systemParameterService = CommonBeanFactory.getBean(SystemParameterService.class);
NoticeSendService noticeSendService = CommonBeanFactory.getBean(NoticeSendService.class); NoticeSendService noticeSendService = CommonBeanFactory.getBean(NoticeSendService.class);
assert systemParameterService != null; assert systemParameterService != null;
assert noticeSendService != null; assert noticeSendService != null;
BaseSystemConfigDTO baseSystemConfigDTO = systemParameterService.getBaseInfo(); BaseSystemConfigDTO baseSystemConfigDTO = systemParameterService.getBaseInfo();
String url = baseSystemConfigDTO.getUrl() + "/#/api/report/view/" + report.getId(); String url = reportUrl;
if(StringUtils.isEmpty(url)){
url = baseSystemConfigDTO.getUrl() + "/#/api/report/view/" + report.getId();
}
String successContext = ""; String successContext = "";
String failedContext = ""; String failedContext = "";
String subject = ""; String subject = "";

View File

@ -40,6 +40,7 @@ import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.collections.ListedHashTree; import org.apache.jorphan.collections.ListedHashTree;
import org.python.antlr.ast.Str;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@ -180,6 +181,7 @@ public class ApiAutomationService {
} }
} }
public void preDelete(List<String> scenarioIDList) { public void preDelete(List<String> scenarioIDList) {
List<String> testPlanApiScenarioIdList = new ArrayList<>(); List<String> testPlanApiScenarioIdList = new ArrayList<>();
List<String> scheduleIdList = new ArrayList<>(); List<String> scheduleIdList = new ArrayList<>();
@ -202,9 +204,11 @@ public class ApiAutomationService {
} }
} }
public void deleteBatch(List<String> ids) { public void deleteBatch(List<String> ids) {
//及连删除外键表 //及连删除外键表
preDelete(ids);; preDelete(ids);
;
ApiScenarioExample example = new ApiScenarioExample(); ApiScenarioExample example = new ApiScenarioExample();
example.createCriteria().andIdIn(ids); example.createCriteria().andIdIn(ids);
apiScenarioMapper.deleteByExample(example); apiScenarioMapper.deleteByExample(example);

View File

@ -50,7 +50,18 @@ public class ApiScenarioReportService {
@Resource @Resource
private TestPlanApiScenarioMapper testPlanApiScenarioMapper; private TestPlanApiScenarioMapper testPlanApiScenarioMapper;
public void complete(TestResult result, String runMode) { public String getApiScenarioId(String name, String projectID) {
ApiScenarioExample example = new ApiScenarioExample();
example.createCriteria().andNameEqualTo(name).andProjectIdEqualTo(projectID).andStatusNotEqualTo("Trash");
List<ApiScenario> list = apiScenarioMapper.selectByExample(example);
if (list.isEmpty()) {
return null;
} else {
return list.get(0).getId();
}
}
public ApiScenarioReport complete(TestResult result, String runMode) {
Object obj = cache.get(result.getTestId()); Object obj = cache.get(result.getTestId());
if (obj == null) { if (obj == null) {
MSException.throwException(Translator.get("api_report_is_null")); MSException.throwException(Translator.get("api_report_is_null"));
@ -83,6 +94,7 @@ public class ApiScenarioReportService {
if (!report.getTriggerMode().equals(ReportTriggerMode.SCHEDULE.name())) { if (!report.getTriggerMode().equals(ReportTriggerMode.SCHEDULE.name())) {
cache.put(report.getId(), report); cache.put(report.getId(), report);
} }
return report;
} }
/** /**

View File

@ -179,8 +179,8 @@
SELECT count(acr.report_id) AS countNumber FROM api_scenario_report_detail acr SELECT count(acr.report_id) AS countNumber FROM api_scenario_report_detail acr
INNER JOIN api_scenario_report ar ON ar.id = acr.report_id INNER JOIN api_scenario_report ar ON ar.id = acr.report_id
INNER JOIN ( INNER JOIN (
SELECT acitem.id FROM api_scenario acitem INNER JOIN `schedule` sc ON acitem.id = sc.resource_id SELECT acitem.`name` FROM api_scenario acitem INNER JOIN `schedule` sc ON acitem.id = sc.resource_id
) ac on acr.content like CONCAT('%', ac.id,'%') ) ac on acr.content like CONCAT('%"', ac.`name`,'"%')
WHERE acr.project_id = #{projectId} AND ar.create_time BETWEEN #{firstDayTimestamp} AND #{lastDayTimestamp} WHERE acr.project_id = #{projectId} AND ar.create_time BETWEEN #{firstDayTimestamp} AND #{lastDayTimestamp}
</select> </select>
@ -189,8 +189,8 @@
FROM api_scenario_report_detail acr FROM api_scenario_report_detail acr
INNER JOIN api_scenario_report ar ON ar.id = acr.report_id INNER JOIN api_scenario_report ar ON ar.id = acr.report_id
INNER JOIN ( INNER JOIN (
SELECT acitem.id FROM api_scenario acitem INNER JOIN `schedule` sc ON acitem.id = sc.resource_id SELECT acitem.`name` FROM api_scenario acitem INNER JOIN `schedule` sc ON acitem.id = sc.resource_id
) ac on acr.content like CONCAT('%', ac.id,'%') ) ac on acr.content like CONCAT('%"', ac.`name`,'"%')
WHERE acr.project_id = #{projectId} WHERE acr.project_id = #{projectId}
</select> </select>
</mapper> </mapper>

View File

@ -160,7 +160,6 @@
WHERE sch.resource_id IN ( WHERE sch.resource_id IN (
SELECT id FROM api_test WHERE project_id = #{projectId,jdbcType=VARCHAR} SELECT id FROM api_test WHERE project_id = #{projectId,jdbcType=VARCHAR}
) )
AND `group` = #{group}
</select> </select>
<select id="countByProjectIDAndCreateInThisWeek" resultType="java.lang.Long"> <select id="countByProjectIDAndCreateInThisWeek" resultType="java.lang.Long">