refactor(接口测试): 任务中心加载当前工作空间下所有任务

--bug=1011600 --user=赵勇 【测试跟踪】测试计划引用了其他项目的case,执行后任务中心不显示执行记录 https://www.tapd.cn/55049933/s/1124098
This commit is contained in:
fit2-zhao 2022-03-23 10:54:37 +08:00 committed by 刘瑞斌
parent 1ef593d3c1
commit 4fdba1074b
7 changed files with 80 additions and 21 deletions

View File

@ -6,7 +6,10 @@
SELECT tt.* FROM (
(select t.id,if(t.scenario_id like "[\"%\"]", t.name,t.scenario_name) as name ,'SCENARIO' as executionModule,t.report_type, ifnull(t2.name,'LOCAL') as actuator, t1.`name` as executor,t.create_time as executionTime, t.trigger_mode as triggerMode ,t.status as executionStatus
from api_scenario_report t left join `user` t1 ON t.user_id = t1.id left join test_resource_pool t2 on t.actuator = t2.id
where to_days(FROM_UNIXTIME(t.create_time/1000))= to_days(now()) and t.execute_type !='Debug' and t.execute_type !='Marge' and t.project_id= #{request.projectId}
where to_days(FROM_UNIXTIME(t.create_time/1000))= to_days(now()) and t.execute_type !='Debug' and t.execute_type !='Marge' and t.project_id in
<foreach collection="request.projects" item="id" separator="," open="(" close=")">
#{id}
</foreach>
<if test="request.triggerMode != null and request.triggerMode != ''">
and t.trigger_mode = #{request.triggerMode}
</if>
@ -20,8 +23,12 @@
UNION ALL
(select t.id,t.name,'API' as executionModule, t.report_type, ifnull(t2.name,'LOCAL') as actuator, t1.`name` as executor,t.create_time as executionTime, ifnull(t.trigger_mode,'MANUAL') as triggerMode ,ifnull(t.status,'Saved') as executionStatus
from api_definition_exec_result t left join `user` t1 ON t.user_id = t1.id left join test_resource_pool t2 on t.actuator = t2.id
where to_days(FROM_UNIXTIME(t.create_time/1000))= to_days(now()) and t.project_id =#{request.projectId}
<if test="request.triggerMode != null and request.triggerMode != ''">
where to_days(FROM_UNIXTIME(t.create_time/1000))= to_days(now()) and t.project_id in
<foreach collection="request.projects" item="id" separator="," open="(" close=")">
#{id}
</foreach>
<if test="request.triggerMode != null and request.triggerMode != ''">
and t.trigger_mode = #{request.triggerMode}
</if>
<if test="request.executionStatus != null and request.executionStatus != ''">
@ -35,7 +42,10 @@
UNION ALL
(select t.id,t.name,'PERFORMANCE' as executionModule,'PERFORMANCE' as report_type, ifnull(t2.name,'LOCAL') as actuator, t1.`name` as executor,t.create_time as executionTime, t.trigger_mode as triggerMode ,t.`status` as executionStatus
from load_test_report t left join `user` t1 ON t.user_id = t1.id left join test_resource_pool t2 on t.test_resource_pool_id = t2.id
where to_days(FROM_UNIXTIME(t.create_time/1000))= to_days(now()) and t.project_id= #{request.projectId}
where to_days(FROM_UNIXTIME(t.create_time/1000))= to_days(now()) and t.project_id in
<foreach collection="request.projects" item="id" separator="," open="(" close=")">
#{id}
</foreach>
<if test="request.triggerMode != null and request.triggerMode != ''">
and t.trigger_mode = #{request.triggerMode}
</if>
@ -70,20 +80,32 @@
SELECT count(tt.id) FROM (
(select t.id,t.create_time as executionTime
from api_scenario_report t left join `user` t1 ON t.user_id = t1.id left join test_resource_pool t2 on t.actuator = t2.id
where to_days(FROM_UNIXTIME(t.create_time/1000))= to_days(now()) and t.execute_type !='Debug' and t.execute_type !='Marge' and t.project_id= #{request.projectId} and t.status not in ("saved","completed","success","error","STOP")
where to_days(FROM_UNIXTIME(t.create_time/1000))= to_days(now()) and t.execute_type !='Debug' and t.execute_type !='Marge' and t.project_id in
<foreach collection="request.projects" item="id" separator="," open="(" close=")">
#{id}
</foreach>
and t.status not in ("saved","completed","success","error","STOP")
)
UNION ALL
(select t.id,t.create_time as executionTime
from api_definition_exec_result t left join `user` t1 ON t.user_id = t1.id left join test_resource_pool t2 on t.actuator = t2.id
where to_days(FROM_UNIXTIME(t.create_time/1000))= to_days(now())
and (t.integrated_report_id is null or t.integrated_report_id='null')
and t.project_id =#{request.projectId}
and t.project_id in
<foreach collection="request.projects" item="id" separator="," open="(" close=")">
#{id}
</foreach>
and t.status not in ("saved","completed","success","error","STOP")
)
UNION ALL
(select t.id,t.create_time as executionTime
from load_test_report t left join `user` t1 ON t.user_id = t1.id left join test_resource_pool t2 on t.test_resource_pool_id = t2.id
where to_days(FROM_UNIXTIME(t.create_time/1000))= to_days(now()) and t.project_id= #{request.projectId} and t.status not in ("saved","completed","success","error")
where to_days(FROM_UNIXTIME(t.create_time/1000))= to_days(now()) and
t.project_id in
<foreach collection="request.projects" item="id" separator="," open="(" close=")">
#{id}
</foreach>
and t.status not in ("saved","completed","success","error")
)
)tt ORDER BY tt.executionTime DESC;
</select>

View File

@ -128,7 +128,8 @@ public class CheckPermissionService {
MSException.throwException(Translator.get("check_owner_review"));
}
}
public List<ProjectDTO> getOwnerProjects() {
public List<ProjectDTO> getOwnerProjects() {
Set<String> userRelatedProjectIds = getUserRelatedProjectIds();
if (CollectionUtils.isEmpty(userRelatedProjectIds)) {
return new ArrayList<>(0);
@ -136,4 +137,17 @@ public class CheckPermissionService {
List<String> projectIds = new ArrayList<>(userRelatedProjectIds);
return extProjectMapper.queryListByIds(projectIds);
}
public Set<String> getOwnerByUserId(String userId) {
UserDTO userDTO = userService.getUserDTO(userId);
List<String> groupIds = userDTO.getGroups()
.stream()
.filter(g -> StringUtils.equals(g.getType(), UserGroupType.PROJECT))
.map(Group::getId)
.collect(Collectors.toList());
return userDTO.getUserGroups().stream()
.filter(ur -> groupIds.contains(ur.getGroupId()))
.map(UserGroup::getSourceId)
.collect(Collectors.toSet());
}
}

View File

@ -20,6 +20,7 @@ public class TaskController {
@PostMapping("/list/{goPage}/{pageSize}")
public Pager<List<TaskCenterDTO>> getTasks(@PathVariable int goPage, @PathVariable int pageSize, @RequestBody TaskCenterRequest request) {
request.setProjects(taskService.getOwnerProjectIds(null));
request.setGoPage(goPage);
request.setPageSize(pageSize);
Page<Object> page = PageHelper.startPage(goPage, pageSize, true);

View File

@ -2,10 +2,15 @@ package io.metersphere.task.dto;
import lombok.Data;
import java.util.List;
@Data
public class TaskCenterRequest {
private String projectId;
private String userId;
/**
* 触发方式
*/
@ -22,4 +27,6 @@ public class TaskCenterRequest {
private int goPage;
private int pageSize;
List<String> projects;
}

View File

@ -19,6 +19,7 @@ import io.metersphere.commons.utils.LogUtil;
import io.metersphere.dto.NodeDTO;
import io.metersphere.jmeter.LocalRunner;
import io.metersphere.performance.service.PerformanceTestService;
import io.metersphere.service.CheckPermissionService;
import io.metersphere.task.dto.TaskCenterDTO;
import io.metersphere.task.dto.TaskCenterRequest;
import org.apache.commons.collections.CollectionUtils;
@ -28,10 +29,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
@Service
@ -61,16 +59,32 @@ public class TaskService {
private ExecThreadPoolExecutor execThreadPoolExecutor;
@Resource
private ApiExecutionQueueService apiExecutionQueueService;
@Resource
private CheckPermissionService checkPermissionService;
public List<String> getOwnerProjectIds(String userId) {
Set<String> userRelatedProjectIds = null;
if (StringUtils.isEmpty(userId)) {
userRelatedProjectIds = checkPermissionService.getUserRelatedProjectIds();
} else {
userRelatedProjectIds = checkPermissionService.getOwnerByUserId(userId);
}
if (CollectionUtils.isEmpty(userRelatedProjectIds)) {
return new ArrayList<>(0);
}
return new ArrayList<>(userRelatedProjectIds);
}
public List<TaskCenterDTO> getTasks(TaskCenterRequest request) {
if (StringUtils.isEmpty(request.getProjectId())) {
if (CollectionUtils.isEmpty(request.getProjects())) {
return new ArrayList<>();
}
return extTaskMapper.getTasks(request);
}
public int getRunningTasks(TaskCenterRequest request) {
if (StringUtils.isEmpty(request.getProjectId())) {
request.setProjects(this.getOwnerProjectIds(request.getUserId()));
if (CollectionUtils.isEmpty(request.getProjects())) {
return 0;
}
return extTaskMapper.getRunningTasks(request);

View File

@ -13,7 +13,7 @@ import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
@ServerEndpoint("/task/center/count/running/{projectId}")
@ServerEndpoint("/task/center/count/running/{projectId}/{userId}")
@Component
public class TaskCenterWebSocket {
private static TaskService taskService;
@ -28,9 +28,9 @@ public class TaskCenterWebSocket {
* 开启连接的操作
*/
@OnOpen
public void onOpen(@PathParam("projectId") String projectId, Session session) {
public void onOpen(@PathParam("projectId") String projectId, @PathParam("userId") String userId, Session session) {
Timer timer = new Timer(true);
TaskCenterWebSocket.TaskCenter task = new TaskCenterWebSocket.TaskCenter(session, projectId);
TaskCenterWebSocket.TaskCenter task = new TaskCenterWebSocket.TaskCenter(session, projectId, userId);
timer.schedule(task, 0, 10 * 1000);
refreshTasks.putIfAbsent(session, timer);
}
@ -51,7 +51,7 @@ public class TaskCenterWebSocket {
* 推送消息
*/
@OnMessage
public void onMessage(@PathParam("projectId") String projectId, Session session, String message) {
public void onMessage(@PathParam("projectId") String projectId, @PathParam("userId") String userId, Session session, String message) {
int refreshTime = 10;
try {
refreshTime = Integer.parseInt(message);
@ -62,7 +62,7 @@ public class TaskCenterWebSocket {
timer.cancel();
Timer newTimer = new Timer(true);
newTimer.schedule(new TaskCenterWebSocket.TaskCenter(session, projectId), 0, refreshTime * 1000L);
newTimer.schedule(new TaskCenterWebSocket.TaskCenter(session, projectId, userId), 0, refreshTime * 1000L);
refreshTasks.put(session, newTimer);
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
@ -82,10 +82,11 @@ public class TaskCenterWebSocket {
private Session session;
private TaskCenterRequest request;
TaskCenter(Session session, String projectId) {
TaskCenter(Session session, String projectId, String userId) {
this.session = session;
TaskCenterRequest request = new TaskCenterRequest();
request.setProjectId(projectId);
request.setUserId(userId);
this.request = request;
}

View File

@ -262,7 +262,7 @@ export default {
if (window.location.protocol === 'https:') {
protocol = "wss://";
}
const uri = protocol + window.location.host + "/task/center/count/running/" + getCurrentProjectID();
const uri = protocol + window.location.host + "/task/center/count/running/" + getCurrentProjectID() + "/" + getCurrentUser().id;
this.websocket = new WebSocket(uri);
this.websocket.onmessage = this.onMessage;
this.websocket.onopen = this.onOpen;