calculate.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. <template>
  2. <div>
  3. <div class="right-panel">
  4. <div style="display: flex; align-items: center">
  5. <h3 style="margin: 0; flex-grow: 1">处理算法计算</h3>
  6. <div class="custom-icon-plans">
  7. <el-button type="primary" @click="startCalculate">开始</el-button>
  8. <el-button type="warning" @click="pauseCalculate">暂停</el-button>
  9. <el-button type="danger" @click="stopCalculate">停止</el-button>
  10. </div>
  11. </div>
  12. <div class="flow-container" ref="flowContainer">
  13. <!-- 连接线画布 -->
  14. <svg class="connector-canvas" :width="flowContainer?.offsetWidth" :height="flowContainer?.offsetHeight">
  15. <path v-for="(conn, index) in connections" :key="'conn-' + index" :d="conn.path" class="connection-line"
  16. marker-end="url(#arrowhead)" />
  17. <defs>
  18. <marker id="arrowhead" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" orient="auto">
  19. <path d="M 0 0 L 10 5 L 0 10 z" class="arrow-head" />
  20. </marker>
  21. </defs>
  22. </svg>
  23. <div class="flow-plans" style="padding-top: 20px">
  24. <div v-for="(plan, index) in plans" :key="'plan-' + plan.id" ref="buttonRefs" class="flow-button-wrapper"
  25. :style="{
  26. transform: `translate(${plan.position.x}px, ${plan.position.y}px)`
  27. }">
  28. <el-button class="plan-button" :style="progressStyle(index)" @click="viewResult(index)">
  29. {{ plan.algorithm.label }}
  30. </el-button>
  31. </div>
  32. </div>
  33. </div>
  34. </div>
  35. <!-- 用于展示结果视图的modal框 -->
  36. <el-dialog v-model="showModal" title="选择展示方式" :close-on-click-modal="false" :show-close="false" width="80%">
  37. <!-- 操作按钮区域 -->
  38. <div class="mode-buttons">
  39. <el-button type="primary" @click="switchView('chart')">
  40. <el-icon>
  41. <PieChart />
  42. </el-icon>
  43. 以图表视图查看
  44. </el-button>
  45. <el-button type="success" @click="switchView('3d')">
  46. <el-icon>
  47. <Promotion />
  48. </el-icon>
  49. 以3D视图查看
  50. </el-button>
  51. <el-button type="warning" @click="switchView('vr')">
  52. <el-icon>
  53. <VideoCamera />
  54. </el-icon>
  55. 以VR视图查看
  56. </el-button>
  57. </div>
  58. <!-- 动态内容区域 -->
  59. <div class="content-area">
  60. <template v-if="currentView">
  61. <component :is="currentViewComponent" :plan="currentResult" />
  62. </template>
  63. <el-empty v-else description="请选择视图展示方式" class="empty-tip">
  64. <template #image>
  65. <el-icon :size="80" color="var(--el-color-info)">
  66. <Select />
  67. </el-icon>
  68. </template>
  69. <div class="tip-text">
  70. 点击上方按钮选择查看方式
  71. </div>
  72. </el-empty>
  73. </div>
  74. <template #footer>
  75. <el-button @click="closeModal">关闭</el-button>
  76. </template>
  77. </el-dialog>
  78. </div>
  79. </template>
  80. <script setup>
  81. import { inject, onMounted, onUnmounted, ref, computed, defineAsyncComponent, shallowRef, watch, nextTick } from 'vue'
  82. import { ElMessage, ElMessageBox } from 'element-plus'
  83. import { postData, getData } from '@/api/axios.js'
  84. import { useRoute } from 'vue-router'
  85. import {
  86. PieChart,
  87. Promotion,
  88. VideoCamera,
  89. Select,
  90. } from '@element-plus/icons-vue'
  91. const useAnalyzeInfo = inject('analyzeInfo')
  92. const plans = inject('plans')
  93. const progress = ref(new Array(plans.length).fill(0))
  94. const mission = useAnalyzeInfo.analyzeInfo.value.mission
  95. const connections = ref([])
  96. const flowContainer = ref(null)
  97. const buttonRefs = ref(null)
  98. const progressInterval = ref(null)
  99. // 布局参数
  100. const COLUMN_GAP = 300
  101. const ROW_GAP = 100
  102. const MAX_COLUMNS = 3
  103. const MAX_ROWS = 5
  104. // 视图展示控制变量
  105. // 加载组件
  106. const ChartView = defineAsyncComponent(() => import('./chartView.vue'))
  107. const ThreeDView = defineAsyncComponent(() => import('./threeDView.vue'))
  108. const VRView = defineAsyncComponent(() => import('./VRView.vue'))
  109. // 状态管理
  110. const showModal = ref(false)
  111. const currentView = ref(null)
  112. const currentResult = ref(null)
  113. // 动态组件处理
  114. const currentViewComponent = shallowRef(null)
  115. const viewComponents = {
  116. 'chart': ChartView,
  117. '3d': ThreeDView,
  118. 'vr': VRView,
  119. }
  120. // 控制按钮内部进度条
  121. // 动态样式计算
  122. const progressStyle = (index) => {
  123. return computed(() => {
  124. const baseColor = getBaseColor(progress.value[index])
  125. const gradient = generateGradient(progress.value[index], baseColor)
  126. return {
  127. backgroundImage: gradient,
  128. backgroundColor: progress.value[index] === 100 ? baseColor : 'transparent'
  129. }
  130. }).value
  131. }
  132. // 获取基础颜色
  133. const getBaseColor = (value) => {
  134. if (value >= 100) return 'rgba(100, 255, 100, 0.3)' // 绿色,表示任务完成
  135. if (value < 0) return 'rgba(255, 20, 20, 0.3)' // 红色,表示任务失败
  136. return 'rgba(66, 144, 255, 0.3)' // 蓝色,任务执行中,按比例显示
  137. }
  138. // 生成渐变背景
  139. const generateGradient = (value, color) => {
  140. if (value <= 0 || value >= 100) return 'none' // 任务失败或任务完成都是完整显示,当存在不满进度时逐渐填满
  141. const percentage = `${Math.min(value, 100)}%`
  142. return `linear-gradient(
  143. to right,
  144. ${color} 0%,
  145. ${color} ${percentage},
  146. transparent ${percentage},
  147. transparent 100%
  148. )`
  149. }
  150. // 查看结果
  151. const viewResult = (index) => {
  152. if (progress.value[index] == 100) {
  153. // 初始化视图显示框
  154. currentView.value = null
  155. currentViewComponent.value = null
  156. currentResult.value = plans.value[index].id
  157. showModal.value = true
  158. } else {
  159. ElMessage.warning("请先开始计算处理,或等待该任务完成")
  160. }
  161. }
  162. // 选择视图显示模式
  163. const switchView = (type) => {
  164. currentView.value = type
  165. currentViewComponent.value = viewComponents[type]
  166. }
  167. // 关闭视图显示框
  168. const closeModal = () => {
  169. showModal.value = false
  170. // 清空当前视图
  171. currentView.value = null
  172. currentResult.value = null
  173. currentViewComponent.value = null
  174. }
  175. // 再次绘制所有连接线
  176. // 获取按钮中心坐标
  177. const getButtonCenter = (plan) => {
  178. const index = plans.value.findIndex(b => b.id === plan.id)
  179. if (index === -1 || !buttonRefs.value[index]) return { x: 0, y: 0 }
  180. const el = buttonRefs.value[index]
  181. const containerRect = flowContainer.value.getBoundingClientRect()
  182. const planRect = el.getBoundingClientRect()
  183. return {
  184. x: planRect.left - containerRect.left + el.offsetWidth / 2,
  185. y: planRect.top - containerRect.top + el.offsetHeight / 2,
  186. top: planRect.top - containerRect.top,
  187. bottom: planRect.top - containerRect.top + el.offsetHeight,
  188. left: planRect.left - containerRect.left,
  189. right: planRect.left - containerRect.left + el.offsetWidth
  190. }
  191. }
  192. // 自动布局计算
  193. const calculateLayout = () => {
  194. nextTick(() => {
  195. const container = flowContainer.value
  196. if (!container) return
  197. const containerWidth = container.offsetWidth
  198. const layoutMap = new Map() // 使用Map记录行列位置
  199. // 第一代按钮布局
  200. let currentRow = 0
  201. let currentCol = 0
  202. plans.value.forEach((btn) => {
  203. // 仅处理根节点按钮
  204. if (!btn.parentId) {
  205. btn.row = currentRow
  206. btn.col = currentCol
  207. currentCol++
  208. if (currentCol >= MAX_COLUMNS) {
  209. currentCol = 0
  210. currentRow++
  211. }
  212. }
  213. })
  214. // 子节点布局
  215. plans.value.forEach((btn) => {
  216. if (btn.parentId) {
  217. const parent = plans.value.find(b => b.id === btn.parentId)
  218. if (!parent) return
  219. // 处理换行
  220. if (btn.col >= MAX_COLUMNS) {
  221. btn.row += Math.floor(btn.col / MAX_COLUMNS)
  222. btn.col = btn.col % MAX_COLUMNS
  223. }
  224. }
  225. // 计算绝对位置(无响应式更新)
  226. const pos = {
  227. x: 20 + btn.col * COLUMN_GAP,
  228. y: btn.row * ROW_GAP
  229. }
  230. // 直接赋值避免触发响应式更新
  231. if (btn.position.x !== pos.x || btn.position.y !== pos.y) {
  232. btn.position.x = pos.x
  233. btn.position.y = pos.y
  234. }
  235. })
  236. setTimeout(() => {
  237. updateConnections()
  238. }, 200)
  239. })
  240. }
  241. // 更新连接线
  242. const updateConnections = () => {
  243. connections.value = []
  244. // 遍历所有按钮建立连接关系
  245. plans.value.forEach(plan => {
  246. if (plan.parentId) {
  247. const parent = plans.value.find(b => b.id === plan.parentId)
  248. if (!parent) return
  249. // 获取实际渲染位置
  250. const start = getButtonCenter(parent)
  251. const end = getButtonCenter(plan)
  252. // 除初始节点的并行外,均为父下到子上
  253. if (plan.row !== 0) {
  254. if (start.x > 0 && start.y > 0 && end.x > 0 && end.y > 0) {
  255. connections.value.push({
  256. source: parent.id,
  257. target: plan.id,
  258. path: createSmartPath({ x: start.x, y: start.bottom }, { x: end.x, y: end.top })
  259. })
  260. }
  261. }
  262. // 如果第一行并行,则不做连线
  263. }
  264. })
  265. }
  266. // 创建曲线路径
  267. const createSmartPath = (start, end) => {
  268. const deltaX = end.x - start.x
  269. const deltaY = end.y - start.y
  270. // // 水平连接优先
  271. // if (Math.abs(deltaX) > 50) {
  272. // const cp1 = { x: start.x + deltaX * 0.8, y: start.y }
  273. // const cp2 = { x: end.x - deltaX * 0.2, y: end.y }
  274. // return `M ${start.x} ${start.y} C ${cp1.x} ${cp1.y}, ${cp2.x} ${cp2.y}, ${end.x} ${end.y}`
  275. // }
  276. // 垂直连接
  277. const cp1 = { x: start.x + deltaX * 0.3, y: start.y + deltaY * 0.2 }
  278. const cp2 = { x: end.x - deltaX * 0.3, y: end.y - deltaY * 0.8 }
  279. return `M ${start.x} ${start.y} C ${cp1.x} ${cp1.y}, ${cp2.x} ${cp2.y}, ${end.x} ${end.y}`
  280. }
  281. const startCalculate = async () => {
  282. // // 测试用模拟进度100%
  283. // progress.value.forEach((item, index) => progress.value[index] = 100)
  284. // return
  285. // 真实调用后台数据处理
  286. const response = await postData('/calculate/', {
  287. mission: mission.id,
  288. command: 'start',
  289. })
  290. progressInterval.value = setInterval(async () => {
  291. try {
  292. const response = await getData('/results', { 'mission': JSON.stringify(mission) })
  293. // 成功获取进度更新信息,将进度赋值给progress
  294. console.log(response)
  295. if (response.status === "success") {
  296. // 任务是否全部完成
  297. let missionComplete = true
  298. // 任务是否发生失败
  299. let missionFailed = false
  300. response.data.forEach(result => {
  301. // 查找序号,根据序号修改progress
  302. const index = plans.value.findIndex(p => p.id === result.planId)
  303. progress.value[index] = result.progress
  304. // 只要有一个没有全部完成的任务,就不会停止获取进度
  305. if (result.progress !== 100) {
  306. missionComplete = false
  307. }
  308. if (result.progress == -1 || result.progress == -2) {
  309. // -1表示单个任务中止
  310. // -2表示整个任务中止
  311. }
  312. })
  313. if (missionComplete) {
  314. ElMessageBox.alert("任务已全部完成", "", {
  315. confirmButtonText: '确定',
  316. })
  317. clearInterval(progressInterval.value)
  318. }
  319. } else {
  320. ElMessage.error("更新任务信息失败,", response.message)
  321. }
  322. } catch (error) {
  323. console.log("catch error when start interval", error)
  324. clearInterval(progressInterval.value)
  325. }
  326. }, 2000) // 2s更新一次
  327. }
  328. const pauseCalculate = async () => {
  329. console.log('pause')
  330. try {
  331. //暂停、停止时都清除定时器
  332. clearInterval(progressInterval.value)
  333. // 发送暂停请求
  334. const response = await postData('/calculate/', {
  335. mission: mission.id,
  336. command: 'pause',
  337. })
  338. } catch (error) {
  339. console.log(error)
  340. }
  341. //暂停时需要发送暂停请求
  342. // todos
  343. }
  344. const stopCalculate = async () => {
  345. console.log('stop')
  346. //暂停、停止时都清除定时器
  347. try {
  348. clearInterval(progressInterval.value)
  349. // 发送暂停请求
  350. const response = await postData('/calculate/', {
  351. mission: mission.id,
  352. command: 'stop',
  353. })
  354. } catch (error) {
  355. console.log(error)
  356. }
  357. //停止时需要发送停止请求
  358. // todos
  359. }
  360. onMounted(() => {
  361. setTimeout(() => {
  362. calculateLayout()
  363. updateConnections()
  364. }, 500)
  365. })
  366. onUnmounted(() => {
  367. try {
  368. clearInterval(progressInterval.value)
  369. } catch (error) {
  370. console.log(error)
  371. }
  372. })
  373. const route = useRoute();
  374. // 监听路由变化
  375. watch(
  376. () => route.path,
  377. (newPath) => {
  378. // 路由变为plan时,修改显示内容
  379. if (newPath === '/dashboard/analyze/plan/calculate') {
  380. setTimeout(() => {
  381. calculateLayout()
  382. updateConnections()
  383. }, 500)
  384. }
  385. },
  386. { immediate: true }
  387. );
  388. </script>
  389. <style lang="scss" scoped>
  390. .container {
  391. width: 100%;
  392. height: 800px;
  393. display: flex;
  394. margin: 0 auto;
  395. }
  396. .connector-canvas {
  397. position: absolute;
  398. top: 0;
  399. left: 0;
  400. z-index: 1;
  401. }
  402. .flow-plans {
  403. position: relative;
  404. z-index: 2;
  405. display: inline-grid;
  406. grid-template-areas: "main";
  407. margin: 20px;
  408. }
  409. .left-panel {
  410. flex: 1;
  411. padding: 20px;
  412. margin-right: 20px;
  413. }
  414. .right-panel {
  415. flex: 3;
  416. padding: 20px;
  417. position: relative;
  418. }
  419. .stats-item {
  420. display: flex;
  421. flex-direction: column;
  422. gap: 8px;
  423. }
  424. .flow-container {
  425. position: relative;
  426. height: 600px;
  427. border: 1px dashed #ccc;
  428. margin: 20px 0;
  429. }
  430. .flow-button-wrapper {
  431. width: 180px;
  432. position: absolute;
  433. transition: all 0.3s;
  434. }
  435. .button-actions {
  436. position: absolute;
  437. right: -30px;
  438. bottom: -30px;
  439. display: flex;
  440. flex-direction: column;
  441. column-gap: 1px;
  442. }
  443. .flow-plans {
  444. position: relative;
  445. min-height: 400px;
  446. }
  447. :deep(.plan-button) {
  448. --el-button-text-color: #000;
  449. --el-button-hover-text-color: #000;
  450. }
  451. .plan-button {
  452. grid-area: main;
  453. position: relative;
  454. width: 100%;
  455. padding: 12px 24px;
  456. }
  457. .floating-controls {
  458. position: absolute;
  459. grid-area: main;
  460. z-index: 2000;
  461. }
  462. .delete-plan {
  463. position: absolute;
  464. top: -32px;
  465. left: 50%;
  466. transform: translateX(-50%);
  467. border: solid rgba(256, 100, 140, 0.2);
  468. background-color: rgba(256, 100, 140, 0.2);
  469. ;
  470. }
  471. .right-arrow {
  472. position: absolute;
  473. right: -138px;
  474. top: 50%;
  475. border: solid rgba(13, 161, 13, 0.2);
  476. background-color: rgba(13, 161, 13, 0.2);
  477. }
  478. .down-arrow {
  479. position: absolute;
  480. bottom: -64px;
  481. left: 50%;
  482. transform: translateX(-50%);
  483. border: solid rgba(13, 161, 13, 0.2);
  484. background-color: rgba(13, 161, 13, 0.2);
  485. }
  486. .flow-button-wrapper {
  487. position: absolute;
  488. transition: all 0.3s ease;
  489. display: flex;
  490. flex-direction: column;
  491. align-items: center;
  492. gap: 8px;
  493. }
  494. .button-actions {
  495. display: flex;
  496. gap: 8px;
  497. position: absolute;
  498. right: -40px;
  499. bottom: -40px;
  500. }
  501. .connection-line {
  502. stroke: #409EFF;
  503. stroke-width: 2;
  504. fill: none;
  505. transition: d 0.3s ease;
  506. }
  507. .arrow-head {
  508. /* stroke: #409EFF; */
  509. stroke-width: 2;
  510. fill: #409EFF;
  511. transition: d 0.3s ease;
  512. }
  513. .custom-icon-plans {
  514. display: flex;
  515. gap: 12px;
  516. }
  517. .mode-buttons {
  518. margin-bottom: 20px;
  519. display: flex;
  520. gap: 15px;
  521. justify-content: center;
  522. }
  523. .content-area {
  524. min-height: 600px;
  525. border: 1px solid var(--el-border-color);
  526. border-radius: var(--el-border-radius-base);
  527. position: relative;
  528. }
  529. .empty-tip {
  530. position: absolute;
  531. top: 50%;
  532. left: 50%;
  533. transform: translate(-50%, -50%);
  534. width: 100%;
  535. }
  536. .tip-text {
  537. color: var(--el-text-color-secondary);
  538. margin-top: 10px;
  539. font-size: 14px;
  540. }
  541. /* 按钮图标样式 */
  542. .el-button [class*=el-icon]+span {
  543. margin-left: 6px;
  544. }
  545. </style>