refactor(接口定义): 接口执行历史保留

This commit is contained in:
fit2-zhao 2020-12-16 16:37:42 +08:00
parent daaf8eb0d2
commit ee82f669c3
6 changed files with 60 additions and 25 deletions

View File

@ -4,7 +4,6 @@ import com.alibaba.fastjson.JSON;
import io.metersphere.api.jmeter.TestResult; import io.metersphere.api.jmeter.TestResult;
import io.metersphere.base.domain.ApiDefinitionExecResult; import io.metersphere.base.domain.ApiDefinitionExecResult;
import io.metersphere.base.mapper.ApiDefinitionExecResultMapper; import io.metersphere.base.mapper.ApiDefinitionExecResultMapper;
import io.metersphere.base.mapper.ext.ExtApiDefinitionExecResultMapper;
import io.metersphere.commons.utils.SessionUtils; import io.metersphere.commons.utils.SessionUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -18,14 +17,9 @@ import java.util.UUID;
public class ApiDefinitionExecResultService { public class ApiDefinitionExecResultService {
@Resource @Resource
private ApiDefinitionExecResultMapper apiDefinitionExecResultMapper; private ApiDefinitionExecResultMapper apiDefinitionExecResultMapper;
@Resource
private ExtApiDefinitionExecResultMapper extApiDefinitionExecResultMapper;
public void saveApiResult(TestResult result) { public void saveApiResult(TestResult result) {
result.getScenarios().get(0).getRequestResults().forEach(item -> { result.getScenarios().get(0).getRequestResults().forEach(item -> {
// 清理原始资源每个执行 保留一条结果
extApiDefinitionExecResultMapper.deleteByResourceId(item.getName());
ApiDefinitionExecResult saveResult = new ApiDefinitionExecResult(); ApiDefinitionExecResult saveResult = new ApiDefinitionExecResult();
saveResult.setId(UUID.randomUUID().toString()); saveResult.setId(UUID.randomUUID().toString());
saveResult.setUserId(Objects.requireNonNull(SessionUtils.getUser()).getId()); saveResult.setUserId(Objects.requireNonNull(SessionUtils.getUser()).getId());

View File

@ -326,7 +326,7 @@ public class ApiDefinitionService {
* @return * @return
*/ */
public APIReportResult getDbResult(String testId) { public APIReportResult getDbResult(String testId) {
ApiDefinitionExecResult result = extApiDefinitionExecResultMapper.selectByResourceId(testId); ApiDefinitionExecResult result = extApiDefinitionExecResultMapper.selectMaxResultByResourceId(testId);
if (result == null) { if (result == null) {
return null; return null;
} }

View File

@ -6,7 +6,6 @@ public interface ExtApiDefinitionExecResultMapper {
void deleteByResourceId(String id); void deleteByResourceId(String id);
ApiDefinitionExecResult selectByResourceId(String resourceId); ApiDefinitionExecResult selectMaxResultByResourceId(String resourceId);
} }

View File

@ -5,8 +5,8 @@
delete from api_definition_exec_result where resource_id = #{id,jdbcType=VARCHAR} delete from api_definition_exec_result where resource_id = #{id,jdbcType=VARCHAR}
</delete> </delete>
<select id="selectByResourceId" parameterType="java.lang.String" resultType="io.metersphere.base.domain.ApiDefinitionExecResult"> <select id="selectMaxResultByResourceId" parameterType="java.lang.String" resultType="io.metersphere.base.domain.ApiDefinitionExecResult">
select * from api_definition_exec_result select * from api_definition_exec_result
where resource_id = #{resourceId,jdbcType=VARCHAR} where resource_id = #{resourceId,jdbcType=VARCHAR} ORDER BY update_time DESC LIMIT 1
</select> </select>
</mapper> </mapper>

View File

@ -89,17 +89,29 @@
<select id="selectByIds" resultType="io.metersphere.api.dto.definition.ApiComputeResult"> <select id="selectByIds" resultType="io.metersphere.api.dto.definition.ApiComputeResult">
select t1.api_definition_id apiDefinitionId,count(t1.id) caseTotal, select t1.api_definition_id apiDefinitionId,count(t1.id) caseTotal,
case t2.status case t2.status
when 'success' then '通过' when 'success' then '通过'
when 'error' then '未通过' when 'error' then '未通过'
ELSE '未执行' end as status , ELSE '未执行' end as status ,
CONCAT(FORMAT(SUM(IF(t2.`status` = 'success', 1, 0))/ COUNT(t1.id)*100, 2), '%') passRate CONCAT(FORMAT(SUM(IF(t2.`status` = 'success', 1, 0))/ COUNT(t1.id)*100, 2), '%') passRate
from api_test_case t1 left join api_definition_exec_result t2 on t1.id = t2.resource_id from api_test_case t1 left join (
select
a.status, a.id, a.resource_id
from
api_definition_exec_result a
left join (
select
max(start_time) start_time , id, resource_id
from
api_definition_exec_result
group by
resource_id ) as b on a.id = b.id
where
a.start_time = b.start_time)as t2 on t1.id = t2.resource_id
group by t1.api_definition_id having t1.api_definition_id in group by t1.api_definition_id having t1.api_definition_id in
<foreach collection="ids" item="v" separator="," open="(" close=")"> <foreach collection="ids" item="v" separator="," open="(" close=")">
#{v} #{v}
</foreach> </foreach>
order by t2.end_time desc;
</select> </select>
<sql id="combine"> <sql id="combine">

View File

@ -146,15 +146,45 @@
</sql> </sql>
<select id="list" resultType="io.metersphere.api.dto.definition.ApiTestCaseResult"> <select id="list" resultType="io.metersphere.api.dto.definition.ApiTestCaseResult">
select atc.id, atc.project_id, select
atc.name,atc.priority,atc.api_definition_id,T1.name as createUser ,T2.name as updateUser, atc.id,
atc.description,atc.request,atc.response,atc.create_user_id, atc.project_id,
atc.create_time,atc.update_user_id, atc.update_time,ader.status execResult atc.name,
from api_test_case atc left join user T1 on atc.create_user_id = T1.id left join user T2 on atc.priority,
atc.update_user_id = T2.id left join api_definition_exec_result ader on atc.id = ader.resource_id atc.api_definition_id,
u1.name as createUser ,
u2.name as updateUser,
atc.description,
atc.request,
atc.response,
atc.create_user_id,
atc.create_time,
atc.update_user_id,
atc.update_time,
ader.status execResult
from
api_test_case atc
left join user u1 on
atc.create_user_id = u1.id
left join user u2 on
atc.update_user_id = u2.id
left join (
select
a.status, a.id, a.resource_id
from
api_definition_exec_result a
left join (
select
max(start_time) start_time , id, resource_id
from
api_definition_exec_result
group by
resource_id ) as b on a.id = b.id
where
a.start_time = b.start_time) as ader
on atc.id = ader.resource_id
<where> <where>
<if test="request.name != null and request.name!=''"> <if test="request.name != null and request.name!=''">
and atc.name like CONCAT('%', #{request.name},'%') and atc.name like CONCAT('%', #{request.name},'%')
</if> </if>
<if test="request.id != null and request.id!=''"> <if test="request.id != null and request.id!=''">