fix(接口测试): 修复任务中心条件切换不刷新统计状态问题

--bug=1019596 --user=赵勇 【任务中心】开源版的任务中场景测试任务统计数量不更新 https://www.tapd.cn/55049933/s/1305073
This commit is contained in:
fit2-zhao 2022-11-25 12:05:03 +08:00 committed by fit2-zhao
parent 9be0e14ecb
commit dd5dd16184
3 changed files with 31 additions and 18 deletions

View File

@ -5,8 +5,9 @@ import packageInfo from '@/../package.json'
const currentModuleName = packageInfo.name;
export function getTaskSocket(hasLicense) {
return socket("/websocket/task/running/" + getCurrentProjectID() + "/" + getCurrentUser().id + "/" + hasLicense)
export function getTaskSocket(userId,triggerMode,hasLicense) {
triggerMode = triggerMode || 'ALL';
return socket(`/websocket/task/running/${userId}/${triggerMode}/${hasLicense}`)
}
export function getCaseData(id) {

View File

@ -44,7 +44,7 @@
<el-row>
<el-col :span="12">
<el-form-item :label="$t('test_track.report.list.trigger_mode')" prop="runMode">
<el-select size="mini" style="margin-right: 10px" v-model="condition.triggerMode" @change="init(true)"
<el-select size="mini" style="margin-right: 10px" v-model="condition.triggerMode" @change="changeInit"
:disabled="isDebugHistory">
<el-option v-for="item in runMode" :key="item.id" :value="item.id" :label="item.label"/>
</el-select>
@ -64,7 +64,7 @@
<el-col :span="12">
<el-form-item :label="$t('commons.executor')" prop="status">
<el-select v-model="condition.executor" :placeholder="$t('commons.executor')" filterable size="mini"
style="margin-right: 10px" @change="init(true)" :disabled="isDebugHistory">
style="margin-right: 10px" @change="changeInit" :disabled="isDebugHistory">
<el-option
v-for="item in maintainerOptions"
:key="item.id"
@ -255,7 +255,7 @@ export default {
},
initWebSocket() {
let isLicense = hasLicense();
this.websocket = getTaskSocket(isLicense ? isLicense : false);
this.websocket = getTaskSocket(this.condition.executor,this.condition.triggerMode,isLicense || false);
this.websocket.onmessage = this.onMessage;
this.websocket.onopen = this.onOpen;
this.websocket.onerror = this.onError;
@ -389,6 +389,13 @@ export default {
this.pageSize = pageSize;
this.init(true);
},
changeInit(){
if (this.websocket && this.websocket.close instanceof Function) {
this.websocket.close();
}
this.getTaskRunning();
this.init(true);
},
init(loading) {
if (this.showType === "CASE" || this.showType === "SCENARIO") {
return;

View File

@ -1,10 +1,12 @@
package io.metersphere.websocket;
import com.mchange.lang.IntegerUtils;
import io.metersphere.commons.utils.JSON;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.task.dto.TaskCenterRequest;
import io.metersphere.task.dto.TaskStatisticsDTO;
import io.metersphere.task.service.TaskService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@ -15,11 +17,12 @@ import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
@ServerEndpoint("/websocket/task/running/{projectId}/{userId}/{hasLicense}")
@ServerEndpoint("/websocket/task/running/{userId}/{triggerMode}/{hasLicense}")
@Component
public class TaskCenterWebSocket {
private static TaskService taskService;
private static ConcurrentHashMap<Session, Timer> refreshTasks = new ConcurrentHashMap<>();
private final static String ALL = "ALL";
@Resource
public void setTaskService(TaskService taskService) {
@ -30,9 +33,11 @@ public class TaskCenterWebSocket {
* 开启连接的操作
*/
@OnOpen
public void onOpen(@PathParam("projectId") String projectId, @PathParam("userId") String userId, @PathParam("hasLicense") boolean hasLicense, Session session) {
public void onOpen(@PathParam("userId") String userId,
@PathParam("triggerMode") String triggerMode,
@PathParam("hasLicense") boolean hasLicense, Session session) {
Timer timer = new Timer(true);
TaskCenter task = new TaskCenter(session, projectId, userId, hasLicense);
TaskCenter task = new TaskCenter(session, userId, triggerMode, hasLicense);
timer.schedule(task, 0, 10 * 1000);
refreshTasks.putIfAbsent(session, timer);
}
@ -53,18 +58,15 @@ public class TaskCenterWebSocket {
* 推送消息
*/
@OnMessage
public void onMessage(@PathParam("projectId") String projectId, @PathParam("userId") String userId, @PathParam("hasLicense") boolean hasLicense, Session session, String message) {
int refreshTime = 10;
try {
refreshTime = Integer.parseInt(message);
} catch (Exception e) {
}
public void onMessage(@PathParam("userId") String userId,
@PathParam("triggerMode") String triggerMode,
@PathParam("hasLicense") boolean hasLicense, Session session, String message) {
try {
Timer timer = refreshTasks.get(session);
timer.cancel();
int refreshTime = IntegerUtils.parseInt(message, 10);
Timer newTimer = new Timer(true);
newTimer.schedule(new TaskCenter(session, projectId, userId, hasLicense), 0, refreshTime * 1000L);
newTimer.schedule(new TaskCenter(session, userId, triggerMode, hasLicense), 0, refreshTime * 1000L);
refreshTasks.put(session, newTimer);
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
@ -84,11 +86,14 @@ public class TaskCenterWebSocket {
private Session session;
private TaskCenterRequest request;
TaskCenter(Session session, String projectId, String userId, boolean hasLicense) {
TaskCenter(Session session, String userId, String triggerMode, boolean hasLicense) {
this.session = session;
TaskCenterRequest request = new TaskCenterRequest();
request.setProjectId(projectId);
if (!StringUtils.equals(triggerMode, ALL)) {
request.setTriggerMode(triggerMode);
}
request.setUserId(userId);
request.setExecutor(userId);
request.setHasLicense(hasLicense);
this.request = request;
}