design-workflowlog.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <template>
  2. <el-dialog
  3. :title="'工作流日志'"
  4. :close-on-click-modal="false"
  5. :visible.sync="visible"
  6. >
  7. <!-- <el-form :model="dataForm" ref="dataForm" label-width="160px">
  8. <el-form-item class="boxformitem"> -->
  9. <div style="width:'10%',height:'100%'">
  10. <el-radio-group
  11. v-model="containertype"
  12. default-value="main"
  13. @change="workflowcontainerchange"
  14. >
  15. <el-radio label="main" value="main">main</el-radio>
  16. <el-radio label="init" value="init">init</el-radio>
  17. <el-radio label="wait" value="wait">wait</el-radio>
  18. </el-radio-group>
  19. <!-- <div class="log-box"> -->
  20. <div class="log-box" style="position: relative; overflow: scroll">
  21. <div
  22. v-if="
  23. this.workflowContentlogsplit && this.workflowContentlogsplit.length
  24. "
  25. >
  26. <p
  27. class="logList-item"
  28. v-for="(item, index) in this.workflowContentlogsplit"
  29. :key="index"
  30. >
  31. {{ item }}
  32. </p>
  33. </div>
  34. <p v-else>暂无数据</p>
  35. </div>
  36. <!-- </div> -->
  37. <!-- <a-descriptions
  38. title=""
  39. layout="vertical"
  40. bordered
  41. style="white-space: pre-wrap"
  42. >
  43. <a-descriptions-item>
  44. {{ this.workflowContentlog }}
  45. </a-descriptions-item>
  46. </a-descriptions> -->
  47. <!-- </a-layout-footer> -->
  48. </div>
  49. <!-- </el-form-item>
  50. </el-form> -->
  51. <!-- <span slot="footer" class="dialog-footer">
  52. <el-button @click="visible = false">取消</el-button>
  53. <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
  54. </span> -->
  55. </el-dialog>
  56. </template>
  57. <script>
  58. export default {
  59. data() {
  60. return {
  61. visible: false,
  62. dataForm: {
  63. id: 0,
  64. modelName: "",
  65. },
  66. workflowName: "", //提交后的工作流名称
  67. workflowContent: { main: "暂无数据", wait: "暂无数据", init: "暂无数据" },
  68. workflowContentlog: "",
  69. workflowContentlogsplit: [],
  70. workflowStatus: "", //记录工作流状态
  71. containertype: "main",
  72. websocket: null,
  73. log: "", // 日志内容
  74. logCache: "",
  75. websocket: null,
  76. socketTimer: null,
  77. };
  78. },
  79. methods: {
  80. // init1(cell, celldata, workflowName) {
  81. // this.visible = true;
  82. // this.workflowName = workflowName;
  83. // // this.wftemplateName = celldata.wftemplatename;
  84. // //此时celldata.sourcealgocell == null
  85. // },
  86. init(workflowName) {
  87. this.visible = true;
  88. this.workflowName = workflowName;
  89. // this.createWebsocket();
  90. var intervalID = window.setInterval(() => {
  91. // this.getWorkflowStatus();
  92. this.$http({
  93. url: this.$http.adornUrl("/visi/visiworkflow/getworkflowlog2"),
  94. method: "get",
  95. params: this.$http.adornParams({
  96. workflowName: this.workflowName,
  97. }),
  98. })
  99. .then(({ data }) => {
  100. if (data && data.code === 0) {
  101. this.$nextTick(() => {
  102. this.workflowContent = data.workflowContent;
  103. if (this.containertype == "main") {
  104. this.workflowContentlog = this.workflowContent.main;
  105. this.workflowContentlogsplit =
  106. this.workflowContent.main.split(/[(\r\n)\r\n]+/);
  107. }
  108. if (this.containertype == "init") {
  109. this.workflowContentlog = this.workflowContent.init;
  110. this.workflowContentlogsplit =
  111. this.workflowContent.init.split(/[(\r\n)\r\n]+/);
  112. }
  113. if (this.containertype == "wait") {
  114. this.workflowContentlog = this.workflowContent.wait;
  115. this.workflowContentlogsplit =
  116. this.workflowContent.wait.split(/[(\r\n)\r\n]+/);
  117. }
  118. this.workflowStatus = data.workflowStatus;
  119. // this.handleLog();
  120. // this.podlogs = data.podlogs;
  121. // console.log("workflowcontent");
  122. // console.log(this.workflowContent);
  123. // console.log(this.workflowContent.main);
  124. // console.log(this.workflowContent.init);
  125. // console.log(this.workflowContent.wait);
  126. // console.log(this.workflowStatus);
  127. // console.log("podlogs")
  128. // console.log(this.podlogs);
  129. });
  130. }
  131. })
  132. .then(() => {
  133. // this.visible = true
  134. // this.$nextTick(() => {
  135. // this.$refs['dataForm'].resetFields()
  136. // })
  137. // 取消该定时设置
  138. if (
  139. this.workflowStatus == "Succeeded" ||
  140. this.workflowStatus == "Error" ||
  141. this.workflowStatus == "Failed"
  142. ) {
  143. // if (this.workflowStatus == "Succeeded") {
  144. // this.workflowstatustag.name = "Succeeded";
  145. // this.workflowstatustag.type = "success";
  146. // } else if (
  147. // this.workflowStatus == "Error" ||
  148. // this.workflowStatus == "Failed"
  149. // ) {
  150. // this.workflowstatustag.name = "Failed";
  151. // this.workflowstatustag.type = "danger";
  152. // }
  153. console.log("关闭定时器-workflowlog");
  154. window.clearInterval(intervalID);
  155. }
  156. });
  157. }, 5000);
  158. },
  159. init1() {
  160. this.visible = true;
  161. this.workflowContentlog = this.workflowContent.main;
  162. this.workflowContentlogsplit =
  163. this.workflowContent.main.split(/[(\r\n)\r\n]+/);
  164. //暂未生成workflowname
  165. },
  166. //单选控制 日志类型 main init wait
  167. workflowcontainerchange(e) {
  168. if (this.containertype == "main") {
  169. this.workflowContentlog = this.workflowContent.main;
  170. this.workflowContentlogsplit =
  171. this.workflowContent.main.split(/[(\r\n)\r\n]+/);
  172. }
  173. if (this.containertype == "init") {
  174. this.workflowContentlog = this.workflowContent.init;
  175. this.workflowContentlogsplit =
  176. this.workflowContent.init.split(/[(\r\n)\r\n]+/);
  177. }
  178. if (this.containertype == "wait") {
  179. this.workflowContentlog = this.workflowContent.wait;
  180. this.workflowContentlogsplit =
  181. this.workflowContent.wait.split(/[(\r\n)\r\n]+/);
  182. }
  183. },
  184. //获取webSocket消息
  185. handleLog() {
  186. var websocket = null;
  187. var host = "ws://127.0.0.1:8082/renren-fast/webSocket/log";
  188. // if (window.location.protocol == 'http:') {
  189. // host = 'ws://' + window.location.host + '/websocket';
  190. // } else {
  191. // host = 'wss://' + window.location.host + '/websocket';
  192. // }
  193. //判断当前浏览器是否支持WebSocket
  194. if ("WebSocket" in window) {
  195. this.websocket = new WebSocket(host);
  196. } else if ("MozWebSocket" in window) {
  197. this.websocket = new MozWebSocket(host);
  198. } else {
  199. alert("该浏览器不支持WebSocket!");
  200. }
  201. console.log(host);
  202. this.initWebSocket();
  203. // if ("WebSocket" in window) {
  204. // let url = `ws://localhost/websocket`;
  205. // console.log(url);
  206. // this.websocket = new WebSocket(url);
  207. // this.initWebSocket();
  208. // }
  209. },
  210. //初始化webSocket
  211. initWebSocket() {
  212. // 连接错误
  213. this.websocket.onerror = () => {
  214. console.log(
  215. "WebSocket连接发生错误 状态码:" + this.websocket.readyState
  216. );
  217. };
  218. // 连接成功
  219. this.websocket.onopen = () => {
  220. console.log(
  221. "WebSocket连接成功 状态码:" + this.websocket.readyState
  222. );
  223. };
  224. this.websocket.onclose = () => {
  225. console.log("WebSocket断开 状态码:" + this.websocket.readyState);
  226. };
  227. // 收到消息的回调
  228. this.websocket.onmessage = (event) => {
  229. // this.consumeCount = this.consumeCount + 1;
  230. // if (this.filter && event.data.indexOf(this.keyword) == -1) {
  231. // return
  232. // }
  233. // var curIndex=id;
  234. if (event.data.match("日志获取结束")) {
  235. console.log(event.data);
  236. this.websocket.close();
  237. } else {
  238. this.workflowContent = JSON.parse(event.data);
  239. this.containertype = "main";
  240. this.workflowContentlog = this.workflowContent.main;
  241. this.workflowContentlogsplit =
  242. this.workflowContent.main.split(/[(\r\n)\r\n]+/);
  243. console.log("event.data" + event.data);
  244. }
  245. /* else{
  246. this.websocket.close();
  247. } */
  248. };
  249. },
  250. createWebsocket() {
  251. const url = "ws://127.0.0.1:8082/renren-fast/webSocket/workflowlog";
  252. this.websocket = new WebSocket(url);
  253. this.websocket.onopen = () => {
  254. console.log("websocket.onopen...");
  255. this.websocket.send(
  256. JSON.stringify({ workflowName: this.workflowName })
  257. );
  258. this.openSocketTimer();
  259. };
  260. this.websocket.onmessage = (res) => {
  261. console.log("websocket.onmessage...");
  262. const data = res.data;
  263. console.log(res.data);
  264. // let lineRes = "";
  265. // let arrN = [];
  266. // if (data.indexOf("\n") !== -1) {
  267. // arrN = data.split("\n");
  268. // }
  269. // if (arrN.length > 0) {
  270. // for (let i = 0; i < arrN.length - 1; i++) {
  271. // let lineStr = arrN[i];
  272. // if (lineStr.indexOf("\r") !== -1) {
  273. // lineStr = lineStr[lineStr.length - 1];
  274. // }
  275. // lineRes += lineStr + "\n\n";
  276. // }
  277. // } else {
  278. // lineRes = data;
  279. // }
  280. // 放到缓存logCache中
  281. if (res.data) {
  282. if (!this.log || this.log.length == 0) {
  283. this.$nextTick(() => {
  284. // this.log = `${this.log}${lineRes}`;
  285. this.log = res.data;
  286. this.workflowContent = JSON.parse(res.data);
  287. this.containertype = "main";
  288. this.workflowContentlog = this.workflowContent.main;
  289. this.workflowContentlogsplit =
  290. this.workflowContent.main.split(/[(\r\n)\r\n]+/);
  291. });
  292. } else {
  293. // this.logCache = `${this.logCache}${lineRes}`;
  294. this.logCache = res.data;
  295. }
  296. }
  297. };
  298. this.websocket.onclose = function (e) {
  299. console.log("websocket.onclose...");
  300. console.log(`连接已断开...>>>${e.code}`);
  301. };
  302. },
  303. // 开启定时器,每隔3秒检验下是否需要刷新日志
  304. openSocketTimer() {
  305. this.clearSocketTimer();
  306. this.socketTimer = setInterval(this.checkRenderEnable, 3000);
  307. },
  308. // 关闭定时器
  309. clearSocketTimer() {
  310. if (this.socketTimer != null) {
  311. clearInterval(this.socketTimer);
  312. this.socketTimer = null;
  313. }
  314. },
  315. // 判断是否需要刷新页面,也就是log缓存是否为空
  316. checkRenderEnable() {
  317. if (this.logCache && this.logCache.length > 0) {
  318. this.renderPage();
  319. }
  320. },
  321. // 刷新页面
  322. renderPage() {
  323. this.$nextTick(() => {
  324. // this.log = `${this.log}${this.logCache}`;
  325. this.log = this.logCache;
  326. this.workflowContent = JSON.parse(this.log);
  327. this.containertype = "main";
  328. this.workflowContentlog = this.workflowContent.main;
  329. this.workflowContentlogsplit =
  330. this.workflowContent.main.split(/[(\r\n)\r\n]+/);
  331. this.logCache = ""; // 刷新完成之后,清空log缓存
  332. });
  333. },
  334. },
  335. };
  336. </script>
  337. <style>
  338. .el-form-item .el-select {
  339. width: 100%;
  340. }
  341. .el-form-item .el-input {
  342. width: 100%;
  343. }
  344. .logList-item {
  345. padding: 0px 0;
  346. line-height: 20px;
  347. margin: 0;
  348. font: normal 13px/1.2 "Courier", sans-serif;
  349. }
  350. .log-box {
  351. min-height: 500px;
  352. max-height: 800px;
  353. padding: 10px;
  354. margin: 10px;
  355. background-color: #fff;
  356. }
  357. </style>