refactor(接口定义): 重构单接口执行/调试获取报告方式

This commit is contained in:
fit2-zhao 2021-07-07 17:23:14 +08:00 committed by fit2-zhao
parent 0c613af43c
commit a03431e6f2
2 changed files with 204 additions and 87 deletions

View File

@ -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);
}
}
}
}

View File

@ -4,101 +4,106 @@
<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 {
name: 'MsRun',
components: {},
props: {
environment: Object,
debug: Boolean,
reportId: String,
runData: Array,
type: String,
envMap: Map
export default {
name: 'MsRun',
components: {},
props: {
environment: Object,
debug: Boolean,
reportId: String,
runData: Array,
type: String,
envMap: Map
},
data() {
return {
result: {},
loading: false,
runId: "",
reqNumber: 0,
}
},
watch: {
//
reportId() {
this.run()
}
},
methods: {
initWebSocket() {
let protocol = "ws://";
if (window.location.protocol === 'https:') {
protocol = "wss://";
}
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;
},
data() {
return {
result: {},
loading: false,
runId: "",
reqNumber: 0,
onOpen() {
},
onError(e) {
this.$emit('runRefresh', {});
},
onMessage(e) {
if (e.data) {
let data = JSON.parse(e.data);
this.$emit('runRefresh', data);
}
},
watch: {
//
reportId() {
this.run()
onClose(e) {
if (e.code === 1005) {
return;
}
},
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;
}
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("获取报告超时");
this.$emit('runRefresh', {});
}
}
});
run() {
let projectId = getCurrentProjectID();
// envMap
if (!this.envMap || this.envMap.size === 0) {
projectId = getCurrentProjectID();
} else {
//
if (this.runData.projectId) {
projectId = this.runData.projectId;
}
},
run() {
let projectId = getCurrentProjectID();
// envMap
if (!this.envMap || this.envMap.size === 0) {
projectId = getCurrentProjectID();
} else {
//
if (this.runData.projectId) {
projectId = this.runData.projectId;
}
}
let testPlan = new TestPlan();
let threadGroup = new ThreadGroup();
threadGroup.hashTree = [];
testPlan.hashTree = [threadGroup];
this.runData.forEach(item => {
item.projectId = projectId;
threadGroup.hashTree.push(item);
})
let reqObj = {id: this.reportId, testElement: testPlan, type: this.type, projectId: projectId, environmentMap: strMapToObj(this.envMap)};
let bodyFiles = getBodyUploadFiles(reqObj, this.runData);
if (this.runData[0].url) {
reqObj.name = this.runData[0].url;
} else {
reqObj.name = this.runData[0].path;
}
let url = "";
if (this.debug) {
reqObj.reportId = this.reportId;
url = "/api/definition/run/debug";
} else {
url = "/api/definition/run";
}
this.$fileUpload(url, null, bodyFiles, reqObj, response => {
this.runId = response.data;
this.getResult();
this.$emit('autoCheckStatus'); //
}, error => {
this.$emit('errorRefresh', {});
});
}
let testPlan = new TestPlan();
let threadGroup = new ThreadGroup();
threadGroup.hashTree = [];
testPlan.hashTree = [threadGroup];
this.runData.forEach(item => {
item.projectId = projectId;
threadGroup.hashTree.push(item);
})
let reqObj = {id: this.reportId, testElement: testPlan, type: this.type, projectId: projectId, environmentMap: strMapToObj(this.envMap)};
let bodyFiles = getBodyUploadFiles(reqObj, this.runData);
if (this.runData[0].url) {
reqObj.name = this.runData[0].url;
} else {
reqObj.name = this.runData[0].path;
}
let url = "";
if (this.debug) {
reqObj.reportId = this.reportId;
url = "/api/definition/run/debug";
} else {
url = "/api/definition/run";
}
this.$fileUpload(url, null, bodyFiles, reqObj, response => {
this.runId = response.data;
this.initWebSocket();
this.$emit('autoCheckStatus'); //
}, error => {
this.$emit('errorRefresh', {});
});
}
}
}
</script>