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,101 +4,106 @@
|
||||||
<script>
|
<script>
|
||||||
import {getBodyUploadFiles, getCurrentProjectID, strMapToObj} from "@/common/js/utils";
|
import {getBodyUploadFiles, getCurrentProjectID, strMapToObj} from "@/common/js/utils";
|
||||||
import ThreadGroup from "./jmeter/components/thread-group";
|
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',
|
name: 'MsRun',
|
||||||
components: {},
|
components: {},
|
||||||
props: {
|
props: {
|
||||||
environment: Object,
|
environment: Object,
|
||||||
debug: Boolean,
|
debug: Boolean,
|
||||||
reportId: String,
|
reportId: String,
|
||||||
runData: Array,
|
runData: Array,
|
||||||
type: String,
|
type: String,
|
||||||
envMap: Map
|
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() {
|
onOpen() {
|
||||||
return {
|
},
|
||||||
result: {},
|
onError(e) {
|
||||||
loading: false,
|
this.$emit('runRefresh', {});
|
||||||
runId: "",
|
},
|
||||||
reqNumber: 0,
|
onMessage(e) {
|
||||||
|
if (e.data) {
|
||||||
|
let data = JSON.parse(e.data);
|
||||||
|
this.$emit('runRefresh', data);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
onClose(e) {
|
||||||
watch: {
|
if (e.code === 1005) {
|
||||||
// 初始化
|
return;
|
||||||
reportId() {
|
|
||||||
this.run()
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
run() {
|
||||||
getResult() {
|
let projectId = getCurrentProjectID();
|
||||||
if (this.runId) {
|
// 如果envMap不存在,是单接口调用
|
||||||
let url = "";
|
if (!this.envMap || this.envMap.size === 0) {
|
||||||
if (this.debug) {
|
projectId = getCurrentProjectID();
|
||||||
url = "/api/definition/report/get/" + this.runId + "/" + "debug";
|
} else {
|
||||||
} else {
|
// 场景步骤下接口调用
|
||||||
url = "/api/definition/report/get/" + this.runId;
|
if (this.runData.projectId) {
|
||||||
}
|
projectId = this.runData.projectId;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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>
|
</script>
|
||||||
|
|
Loading…
Reference in New Issue