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; const currentModuleName = packageInfo.name;
export function getTaskSocket(hasLicense) { export function getTaskSocket(userId,triggerMode,hasLicense) {
return socket("/websocket/task/running/" + getCurrentProjectID() + "/" + getCurrentUser().id + "/" + hasLicense) triggerMode = triggerMode || 'ALL';
return socket(`/websocket/task/running/${userId}/${triggerMode}/${hasLicense}`)
} }
export function getCaseData(id) { export function getCaseData(id) {

View File

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

View File

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