refactor(接口定义): 重构单接口执行/调试获取报告方式
This commit is contained in:
parent
0c613af43c
commit
a03431e6f2
|
@ -0,0 +1,112 @@
|
|||
package io.metersphere.websocket;
|
||||
|
||||
import com.alibaba.nacos.client.utils.StringUtils;
|
||||
import io.metersphere.api.dto.APIReportResult;
|
||||
import io.metersphere.api.service.ApiDefinitionService;
|
||||
import io.metersphere.commons.utils.LogUtil;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.websocket.*;
|
||||
import javax.websocket.server.PathParam;
|
||||
import javax.websocket.server.ServerEndpoint;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@ServerEndpoint("/api/definition/run/report/{reportId}/{runMode}")
|
||||
@Component
|
||||
public class ApiReportWebSocket {
|
||||
|
||||
private static ApiDefinitionService apiDefinitionService;
|
||||
private ConcurrentHashMap<Session, Timer> refreshTasks = new ConcurrentHashMap<>();
|
||||
|
||||
@Resource
|
||||
public void setReportService(ApiDefinitionService apiDefinitionService) {
|
||||
ApiReportWebSocket.apiDefinitionService = apiDefinitionService;
|
||||
}
|
||||
|
||||
/**
|
||||
* 开启连接的操作
|
||||
*/
|
||||
@OnOpen
|
||||
public void onOpen(@PathParam("reportId") String reportId, @PathParam("runMode") String runMode, Session session) {
|
||||
Timer timer = new Timer(true);
|
||||
ApiReportTask task = new ApiReportTask(session, reportId, runMode);
|
||||
timer.schedule(task, 0, 1000);
|
||||
refreshTasks.putIfAbsent(session, timer);
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接关闭的操作
|
||||
*/
|
||||
@OnClose
|
||||
public void onClose(Session session) {
|
||||
Timer timer = refreshTasks.get(session);
|
||||
if (timer != null) {
|
||||
timer.cancel();
|
||||
refreshTasks.remove(session);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 给服务器发送消息告知数据库发生变化
|
||||
*/
|
||||
@OnMessage
|
||||
public void onMessage(@PathParam("reportId") String reportId, @PathParam("runMode") String runMode, Session session, String message) {
|
||||
try {
|
||||
LogUtil.info(message);
|
||||
Timer timer = refreshTasks.get(session);
|
||||
if (timer != null) {
|
||||
timer.cancel();
|
||||
}
|
||||
Timer newTimer = new Timer(true);
|
||||
newTimer.schedule(new ApiReportTask(session, reportId, runMode), 0, 1000L);
|
||||
refreshTasks.put(session, newTimer);
|
||||
} catch (Exception e) {
|
||||
LogUtil.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 出错的操作
|
||||
*/
|
||||
@OnError
|
||||
public void onError(Throwable error) {
|
||||
System.out.println(error);
|
||||
error.printStackTrace();
|
||||
}
|
||||
|
||||
public static class ApiReportTask extends TimerTask {
|
||||
private Session session;
|
||||
private String reportId;
|
||||
private String runMode;
|
||||
|
||||
ApiReportTask(Session session, String reportId, String runMode) {
|
||||
this.session = session;
|
||||
this.reportId = reportId;
|
||||
this.runMode = runMode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
APIReportResult report = null;
|
||||
if (StringUtils.equals(runMode, "debug")) {
|
||||
report = apiDefinitionService.getResult(reportId, runMode);
|
||||
} else {
|
||||
report = apiDefinitionService.getReportById(reportId);
|
||||
}
|
||||
if (!session.isOpen()) {
|
||||
return;
|
||||
}
|
||||
if (report != null) {
|
||||
session.getBasicRemote().sendText(report.getContent());
|
||||
session.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LogUtil.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,9 +4,9 @@
|
|||
<script>
|
||||
import {getBodyUploadFiles, getCurrentProjectID, strMapToObj} from "@/common/js/utils";
|
||||
import ThreadGroup from "./jmeter/components/thread-group";
|
||||
import TestPlan from "./jmeter/components/test-plan";
|
||||
import TestPlan from "./jmeter/components/test-plan";
|
||||
|
||||
export default {
|
||||
export default {
|
||||
name: 'MsRun',
|
||||
components: {},
|
||||
props: {
|
||||
|
@ -33,28 +33,33 @@ import ThreadGroup from "./jmeter/components/thread-group";
|
|||
}
|
||||
},
|
||||
methods: {
|
||||
getResult() {
|
||||
if (this.runId) {
|
||||
let url = "";
|
||||
if (this.debug) {
|
||||
url = "/api/definition/report/get/" + this.runId + "/" + "debug";
|
||||
} else {
|
||||
url = "/api/definition/report/get/" + this.runId;
|
||||
initWebSocket() {
|
||||
let protocol = "ws://";
|
||||
if (window.location.protocol === 'https:') {
|
||||
protocol = "wss://";
|
||||
}
|
||||
this.$get(url, response => {
|
||||
if (response.data) {
|
||||
let data = JSON.parse(response.data.content);
|
||||
this.$emit('runRefresh', data);
|
||||
} else {
|
||||
if (this.reqNumber < 60) {
|
||||
this.reqNumber++;
|
||||
setTimeout(this.getResult, 2000);
|
||||
} else {
|
||||
this.$error("获取报告超时");
|
||||
let runMode = this.debug ? "debug" : "run";
|
||||
const uri = protocol + window.location.host + "/api/definition/run/report/" + this.runId + "/" + runMode;
|
||||
this.websocket = new WebSocket(uri);
|
||||
this.websocket.onmessage = this.onMessage;
|
||||
this.websocket.onopen = this.onOpen;
|
||||
this.websocket.onerror = this.onError;
|
||||
this.websocket.onclose = this.onClose;
|
||||
},
|
||||
onOpen() {
|
||||
},
|
||||
onError(e) {
|
||||
this.$emit('runRefresh', {});
|
||||
},
|
||||
onMessage(e) {
|
||||
if (e.data) {
|
||||
let data = JSON.parse(e.data);
|
||||
this.$emit('runRefresh', data);
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
onClose(e) {
|
||||
if (e.code === 1005) {
|
||||
return;
|
||||
}
|
||||
},
|
||||
run() {
|
||||
|
@ -93,12 +98,12 @@ import ThreadGroup from "./jmeter/components/thread-group";
|
|||
}
|
||||
this.$fileUpload(url, null, bodyFiles, reqObj, response => {
|
||||
this.runId = response.data;
|
||||
this.getResult();
|
||||
this.initWebSocket();
|
||||
this.$emit('autoCheckStatus'); // 执行结束后,自动更新计划状态
|
||||
}, error => {
|
||||
this.$emit('errorRefresh', {});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
|
Loading…
Reference in New Issue