From 8f55450c4ba1e175f843dc96aa167e33921b899c Mon Sep 17 00:00:00 2001 From: lixiaolong <702156524@qq.com> Date: Fri, 16 Oct 2026 17:16:34 +0800 Subject: [PATCH] =?UTF-8?q?feat(flow):=20=E5=AE=9E=E7=8E=B0=E6=B5=81?= =?UTF-8?q?=E7=A8=8B=E6=89=A7=E8=A1=8C=E8=BF=9B=E5=BA=A6=E8=B7=9F=E8=B8=AA?= =?UTF-8?q?=E5=92=8C=E7=8A=B6=E6=80=81=E7=AE=A1=E7=90=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 引入 FlowProgressTracker 统一管理流程执行进度计算 - 添加任务实例进度字段和数据库映射支持 - 实现暂停、恢复、失败、停止等状态变更处理 - 重构任务执行监听器移除自定义缓存机制 - 集成进度跟踪到事件发布和分发流程中 - 添加循环节点和子流程的独立权重范围支持 - 更新应用配置激活 aima 环境配置 - 为 AIMA 和巡检模块添加进度跟踪功能 - 增加单元测试验证进度计算准确性 --- .../src/main/resources/application.yml | 2 +- .../listener/AimaFlowExecutionListener.java | 101 ++++----- .../impl/AimaTaskInstanceServiceImpl.java | 1 + .../AimaFlowExecutionListenerTest.java | 94 ++++++++ .../domain/InspectionTaskInstance.java | 5 + .../domain/vo/InspectionTaskInstanceVo.java | 3 + .../InspectionFlowExecutionListener.java | 100 +++------ .../InspectionTaskInstanceMapper.xml | 17 +- .../InspectionFlowExecutionListenerTest.java | 113 ++++++++++ .../InspectionProgressMapperTest.java | 40 ++++ .../cmvr/test/flow/context/TaskContext.java | 7 + .../test/flow/context/TaskInstHolder.java | 5 + .../dispatcher/FlowLoopNodeHandler.java | 4 + .../dispatcher/FlowSubFlowNodeHandler.java | 4 + .../flow/runtime/engine/FlowItemExecutor.java | 8 + .../flow/runtime/engine/FlowTaskEngine.java | 1 + .../runtime/engine/FlowTaskRuntimeEntry.java | 1 + .../runtime/event/FlowExecutionEvent.java | 11 + .../event/FlowExecutionEventPublisher.java | 37 ++++ .../interceptor/FlowAfterInterceptor.java | 9 +- .../interceptor/FlowBeforeInterceptor.java | 2 + .../message/TaskNodeExecuteMessage.java | 6 + .../llm/AsyncMediaAnalysisCoordinator.java | 1 + .../runtime/progress/FlowProgressTracker.java | 172 +++++++++++++++ .../cmvr/test/model/domain/TeTaskInst.java | 3 + .../com/cmvr/test/model/vo/TeTaskInstVO.java | 3 + .../cmvr/test/service/ITeTaskInstService.java | 3 + .../service/impl/TeTaskInstServiceImpl.java | 10 + .../dispatcher/FlowLoopNodeHandlerTest.java | 44 ++++ .../interceptor/FlowAfterProgressTest.java | 40 ++++ .../AsyncMediaAnalysisCoordinatorTest.java | 4 + .../progress/FlowProgressPublisherTest.java | 96 ++++++++ .../progress/FlowProgressTrackerTest.java | 207 ++++++++++++++++++ sql/workflow_execution_progress.sql | 27 +++ 34 files changed, 1044 insertions(+), 137 deletions(-) create mode 100644 cmvr-iot-inspection/src/test/java/com/cmvr/inspection/listener/InspectionFlowExecutionListenerTest.java create mode 100644 cmvr-iot-inspection/src/test/java/com/cmvr/inspection/listener/InspectionProgressMapperTest.java create mode 100644 cmvr-iot-test/src/main/java/com/cmvr/test/flow/runtime/progress/FlowProgressTracker.java create mode 100644 cmvr-iot-test/src/test/java/com/cmvr/test/flow/runtime/interceptor/FlowAfterProgressTest.java create mode 100644 cmvr-iot-test/src/test/java/com/cmvr/test/flow/runtime/progress/FlowProgressPublisherTest.java create mode 100644 cmvr-iot-test/src/test/java/com/cmvr/test/flow/runtime/progress/FlowProgressTrackerTest.java create mode 100644 sql/workflow_execution_progress.sql diff --git a/cmvr-iot-admin/src/main/resources/application.yml b/cmvr-iot-admin/src/main/resources/application.yml index c2db5cb..45de4dc 100644 --- a/cmvr-iot-admin/src/main/resources/application.yml +++ b/cmvr-iot-admin/src/main/resources/application.yml @@ -60,7 +60,7 @@ spring: # 国际化资源文件路径 basename: i18n/messages profiles: - active: test + active: aima # 文件上传 servlet: multipart: diff --git a/cmvr-iot-aima/src/main/java/com/cmvr/aima/listener/AimaFlowExecutionListener.java b/cmvr-iot-aima/src/main/java/com/cmvr/aima/listener/AimaFlowExecutionListener.java index 923a707..8683998 100644 --- a/cmvr-iot-aima/src/main/java/com/cmvr/aima/listener/AimaFlowExecutionListener.java +++ b/cmvr-iot-aima/src/main/java/com/cmvr/aima/listener/AimaFlowExecutionListener.java @@ -16,6 +16,7 @@ import com.cmvr.test.flow.runtime.event.FlowExecutionEvent; import com.cmvr.test.flow.runtime.event.FlowExecutionListener; import com.cmvr.test.flow.runtime.event.FlowExecutionModuleCodes; import com.cmvr.test.flow.runtime.operator.FlowMediaParamResolver; +import com.cmvr.test.flow.runtime.progress.FlowProgressTracker; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @@ -36,10 +37,6 @@ public class AimaFlowExecutionListener implements FlowExecutionListener { private final IAimaTaskService aimaTaskService; private final MessagePushService messagePushService; - /** key:itemId, value:已完成节点集合 */ - private final Map> itemNodeExecutMap = new java.util.concurrent.ConcurrentHashMap<>(); - /** key:taskInsId(流程实例id), value:已执行item集合 */ - private final Map> taskItemExecutMap = new java.util.concurrent.ConcurrentHashMap<>(); /** key: flow instance ID, value: test case IDs in binding order. */ private final Map> taskCaseBindingMap = new java.util.concurrent.ConcurrentHashMap<>(); @@ -67,16 +64,10 @@ public class AimaFlowExecutionListener implements FlowExecutionListener { return; } - // 1.节点完成才记录当前item下node - if (StrUtil.isNotBlank(itemId) && StrUtil.isNotBlank(nodeId) - && FlowExecutionEvent.EventType.NODE_COMPLETED == event.getEventType()) { - itemNodeExecutMap.computeIfAbsent(itemExecutionKey(instId, event), - k -> java.util.concurrent.ConcurrentHashMap.newKeySet()).add(nodeId); - } - // 任意事件:当前任务绑定item(item一启动就入Map) - if (StrUtil.isNotBlank(instId) && StrUtil.isNotBlank(itemId)) { - taskItemExecutMap.computeIfAbsent(instId, - k -> java.util.concurrent.ConcurrentHashMap.newKeySet()).add(itemExecutionKey(instId, event)); + if (event.getEventType() == FlowExecutionEvent.EventType.TASK_COMPLETED + || event.getEventType() == FlowExecutionEvent.EventType.TASK_FAILED + || event.getEventType() == FlowExecutionEvent.EventType.TASK_STOPPED) { + clearCache(instId); } // 查询任务实例 @@ -86,8 +77,11 @@ public class AimaFlowExecutionListener implements FlowExecutionListener { return; } - int targetStatus = AimaTaskStatusEnum.RUNNING.getCode(); + double progress = FlowProgressTracker.progressOf(event, taskInstance.getProgress()); + int targetStatus = taskInstance.getStatus() == null + ? AimaTaskStatusEnum.RUNNING.getCode() : taskInstance.getStatus(); boolean needUpdateDb = false; + boolean terminal = false; String terminalMessage = null; Date now = DateUtils.getNowDate(); @@ -95,67 +89,53 @@ public class AimaFlowExecutionListener implements FlowExecutionListener { case TASK_COMPLETED: targetStatus = AimaTaskStatusEnum.SUCCESS.getCode(); needUpdateDb = true; - clearCache(instId); + terminal = true; terminalMessage = "任务执行完成"; break; case TASK_FAILED: targetStatus = AimaTaskStatusEnum.FAILED.getCode(); needUpdateDb = true; - clearCache(instId); + terminal = true; terminalMessage = StrUtil.isBlank(event.getErrorMessage()) ? "任务执行失败" : "任务执行失败:" + event.getErrorMessage(); break; case TASK_STOPPED: targetStatus = AimaTaskStatusEnum.STOPPED.getCode(); needUpdateDb = true; - clearCache(instId); + terminal = true; terminalMessage = "任务已终止"; break; + case TASK_PAUSED: + targetStatus = AimaTaskStatusEnum.PAUSED.getCode(); + needUpdateDb = true; + terminalMessage = "任务已暂停"; + break; + case TASK_RESUMED: + targetStatus = AimaTaskStatusEnum.RUNNING.getCode(); + needUpdateDb = true; + terminalMessage = "任务已恢复"; + break; case NODE_COMPLETED: default: break; } - if (needUpdateDb) { - taskInstance.setStatus(targetStatus); - taskInstance.setEndTime(now); - aimaTaskInstanceService.updateById(taskInstance); + if (needUpdateDb || taskInstance.getProgress() == null + || Double.compare(taskInstance.getProgress(), progress) != 0) { + AimaTaskInstance update = new AimaTaskInstance(); + update.setId(taskInstance.getId()); + update.setProgress(progress); + if (needUpdateDb) update.setStatus(targetStatus); + if (terminal) update.setEndTime(now); + aimaTaskInstanceService.updateById(update); } if (terminalMessage != null) { pushTerminalMsg(taskInstance.getId(), now, targetStatus, terminalMessage, - event.getErrorMessage()); + event.getErrorMessage(), progress); } // 节点推送 - 记录日志 if (StrUtil.isNotBlank(nodeId)) { - int totalCases = taskInstance.getTotalCases() == null ? 0 : taskInstance.getTotalCases(); - double progress = 0D; - if (totalCases > 0) { - // 当前任务已执行item数量 - Set itemSet = taskItemExecutMap.getOrDefault(instId, new HashSet<>()); - int itemTotal = itemSet.size(); - int finishedItemNum = itemTotal - 1; - - // 当前item已完成节点数 - Set finishedNodeSet = itemNodeExecutMap.getOrDefault(itemExecutionKey(instId, event), new HashSet<>()); - int finishedNode = finishedNodeSet.size(); - int totalNode = event.getNodeCount() == 0 ? 1 : event.getNodeCount(); - - // 已完整跑完用例占比 - if (event.getItemOrder() != null && event.getItemOrder() > 0) { - finishedItemNum = event.getItemOrder() - 1; - } - double itemTotalRatio = finishedItemNum / (double) totalCases; - // 当前运行item内部节点占比 - double innerRatio = finishedNode / (double) totalNode / totalCases; - - // 防止循环节点进度超限 - double singleItemBase = 1d / totalCases; - innerRatio = Math.max(singleItemBase - 0.01, innerRatio); - - progress = (itemTotalRatio + innerRatio) * 100; - } - String info = event.getEventType() == FlowExecutionEvent.EventType.NODE_STARTED ? "开始执行" : event.getEventType() == FlowExecutionEvent.EventType.NODE_FAILED @@ -223,20 +203,10 @@ public class AimaFlowExecutionListener implements FlowExecutionListener { /** 任务结束统一清理缓存 */ private void clearCache(String taskInsId) { - Set itemList = taskItemExecutMap.getOrDefault(taskInsId, new HashSet<>()); - itemList.forEach(itemNodeExecutMap::remove); - taskItemExecutMap.remove(taskInsId); taskCaseBindingMap.remove(taskInsId); } - private String itemExecutionKey(String taskInsId, FlowExecutionEvent event) { - if (event.getItemOrder() != null && event.getItemOrder() > 0) { - return taskInsId + "|" + event.getItemOrder(); - } - return taskInsId + "|" + event.getItemId(); - } - - private AimaTaskInstance findTaskInstance(String taskInsId) { + protected AimaTaskInstance findTaskInstance(String taskInsId) { for (int attempt = 0; attempt < 5; attempt++) { AimaTaskInstance instance = aimaTaskInstanceService.lambdaQuery() .eq(AimaTaskInstance::getTaskInsId, taskInsId) @@ -269,6 +239,12 @@ public class AimaFlowExecutionListener implements FlowExecutionListener { */ public void pushTerminalMsg(String dbInsId, Date now, int targetStatus, String message, String errorMessage) { + pushTerminalMsg(dbInsId, now, targetStatus, message, errorMessage, + targetStatus == AimaTaskStatusEnum.SUCCESS.getCode() ? 100D : null); + } + + public void pushTerminalMsg(String dbInsId, Date now, int targetStatus, + String message, String errorMessage, Double progress) { String logId = UUID.randomUUID().toString().replace("-", ""); // 保存终态日志 @@ -281,6 +257,7 @@ public class AimaFlowExecutionListener implements FlowExecutionListener { .errorStack(errorMessage) .logTime(now) .status(targetStatus) + .progress(progress) .build(); try { diff --git a/cmvr-iot-aima/src/main/java/com/cmvr/aima/service/impl/AimaTaskInstanceServiceImpl.java b/cmvr-iot-aima/src/main/java/com/cmvr/aima/service/impl/AimaTaskInstanceServiceImpl.java index 35ea153..f02ce63 100644 --- a/cmvr-iot-aima/src/main/java/com/cmvr/aima/service/impl/AimaTaskInstanceServiceImpl.java +++ b/cmvr-iot-aima/src/main/java/com/cmvr/aima/service/impl/AimaTaskInstanceServiceImpl.java @@ -78,6 +78,7 @@ public class AimaTaskInstanceServiceImpl extends ServiceImpl inserted = new AtomicReference<>(); + final CapturingMessagePushService push = new CapturingMessagePushService(); + final FlowProgressTracker tracker = new FlowProgressTracker(); + final FlowProgressTracker.Scope scope; + final FlowExecutionEventPublisher publisher; + + Fixture() { + instance.setId("db-1"); + instance.setStatus(AimaTaskStatusEnum.RUNNING.getCode()); + instance.setProgress(0D); + instance.setTotalCases(999); // stale business metadata must not be a denominator + tracker.initialize(1); + scope = tracker.item(1, List.of("a", "b")); + var instances = (IAimaTaskInstanceService) Proxy.newProxyInstance( + IAimaTaskInstanceService.class.getClassLoader(), new Class[]{IAimaTaskInstanceService.class}, + (proxy, method, args) -> { + if ("updateById".equals(method.getName())) { + var update = (AimaTaskInstance) args[0]; + if (update.getProgress() != null) instance.setProgress(update.getProgress()); + if (update.getStatus() != null) instance.setStatus(update.getStatus()); + if (update.getEndTime() != null) instance.setEndTime(update.getEndTime()); + return true; + } + return null; + }); + var logs = (IAimaTestLogService) Proxy.newProxyInstance( + IAimaTestLogService.class.getClassLoader(), new Class[]{IAimaTestLogService.class}, + (proxy, method, args) -> { + if ("insertAimaTestLog".equals(method.getName())) { inserted.set((AimaTestLog) args[0]); return 1; } + return null; + }); + var tasks = (IAimaTaskService) Proxy.newProxyInstance( + IAimaTaskService.class.getClassLoader(), new Class[]{IAimaTaskService.class}, + (proxy, method, args) -> "getCaseName".equals(method.getName()) ? "case" : null); + var listener = new AimaFlowExecutionListener(instances, logs, tasks, push) { + @Override protected AimaTaskInstance findTaskInstance(String id) { return instance; } + }; + publisher = new FlowExecutionEventPublisher(List.of(listener)); + } + + void send(FlowExecutionEvent.EventType type, String node) { + publisher.publishEvent(FlowExecutionEvent.builder().eventType(type).instId("flow-1") + .itemId("item").nodeId(node).nodeName(node).moduleCode("AIMA") + .progressScope(scope).progressTracker(tracker).build()); + } + + void assertProgress(double expected) { + assertEquals(expected, instance.getProgress(), 0); + assertEquals(expected, inserted.get().getProgress(), 0); + assertSame(inserted.get(), push.payload); + } + } + @Test public void failedTerminalLogIsPersistedAndPushed() { AtomicReference inserted = new AtomicReference<>(); diff --git a/cmvr-iot-inspection/src/main/java/com/cmvr/inspection/domain/InspectionTaskInstance.java b/cmvr-iot-inspection/src/main/java/com/cmvr/inspection/domain/InspectionTaskInstance.java index 540cfe8..a833e26 100644 --- a/cmvr-iot-inspection/src/main/java/com/cmvr/inspection/domain/InspectionTaskInstance.java +++ b/cmvr-iot-inspection/src/main/java/com/cmvr/inspection/domain/InspectionTaskInstance.java @@ -2,6 +2,7 @@ package com.cmvr.inspection.domain; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableField; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.*; @@ -58,4 +59,8 @@ public class InspectionTaskInstance extends BaseEntity @ApiModelProperty("点位数量") private Integer waypointCount; + /** Read projection from persisted execution logs; no new database column. */ + @TableField(exist = false) + private Double progress; + } diff --git a/cmvr-iot-inspection/src/main/java/com/cmvr/inspection/domain/vo/InspectionTaskInstanceVo.java b/cmvr-iot-inspection/src/main/java/com/cmvr/inspection/domain/vo/InspectionTaskInstanceVo.java index 31fa623..1485bcb 100644 --- a/cmvr-iot-inspection/src/main/java/com/cmvr/inspection/domain/vo/InspectionTaskInstanceVo.java +++ b/cmvr-iot-inspection/src/main/java/com/cmvr/inspection/domain/vo/InspectionTaskInstanceVo.java @@ -102,4 +102,7 @@ public class InspectionTaskInstanceVo extends BaseEntity @ApiModelProperty("点位数量") private Integer waypointCount; + @ApiModelProperty("任务工作量进度,0到100") + private Double progress; + } diff --git a/cmvr-iot-inspection/src/main/java/com/cmvr/inspection/listener/InspectionFlowExecutionListener.java b/cmvr-iot-inspection/src/main/java/com/cmvr/inspection/listener/InspectionFlowExecutionListener.java index 57b0374..555f0b5 100644 --- a/cmvr-iot-inspection/src/main/java/com/cmvr/inspection/listener/InspectionFlowExecutionListener.java +++ b/cmvr-iot-inspection/src/main/java/com/cmvr/inspection/listener/InspectionFlowExecutionListener.java @@ -17,6 +17,7 @@ import com.cmvr.test.flow.runtime.event.FlowExecutionEvent; import com.cmvr.test.flow.runtime.event.FlowExecutionListener; import com.cmvr.test.flow.runtime.event.FlowExecutionModuleCodes; import com.cmvr.test.flow.runtime.operator.FlowMediaParamResolver; +import com.cmvr.test.flow.runtime.progress.FlowProgressTracker; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @@ -31,10 +32,6 @@ public class InspectionFlowExecutionListener implements FlowExecutionListener { private final IInspectionResultService inspectionResultService; private final InspectionAlertListenService inspectionAlertListenService; - /** key:flowInstId|itemId, value:已完成节点集合 */ - private final Map> itemNodeExecutMap = new java.util.concurrent.ConcurrentHashMap<>(); - /** key:taskInsId(流程实例id=taskInsId), value:已执行item集合 */ - private final Map> taskItemExecutMap = new java.util.concurrent.ConcurrentHashMap<>(); public InspectionFlowExecutionListener(IInspectionTaskInstanceService inspectionTaskInstanceService, IInspectionTaskLogService inspectionTaskLogService, @@ -66,62 +63,66 @@ public class InspectionFlowExecutionListener implements FlowExecutionListener { if (FlowExecutionEvent.EventType.TASK_COMPLETED == event.getEventType() || FlowExecutionEvent.EventType.TASK_FAILED == event.getEventType() || FlowExecutionEvent.EventType.TASK_STOPPED == event.getEventType()) { - clearCache(instId); inspectionAlertListenService.clearByFlowInstanceId(instId); } - // 1.节点完成才记录当前item下node - if (StrUtil.isNotBlank(itemId) && StrUtil.isNotBlank(nodeId) - && FlowExecutionEvent.EventType.NODE_COMPLETED == event.getEventType()) { - itemNodeExecutMap.computeIfAbsent(itemExecutionKey(instId, itemId, event.getItemOrder()), - k -> java.util.concurrent.ConcurrentHashMap.newKeySet()).add(nodeId); - } - // 任意事件:当前任务绑定item(item一启动就入Map,所以统计已完成item要-1) - if (StrUtil.isNotBlank(instId) && StrUtil.isNotBlank(itemId)) { - taskItemExecutMap.computeIfAbsent(instId, - k -> java.util.concurrent.ConcurrentHashMap.newKeySet()) - .add(itemExecutionKey(instId, itemId, event.getItemOrder())); - } - InspectionTaskInstance taskInstance = findTaskInstance(instId); if (taskInstance == null) { log.warn("未查询到巡检实例,taskInsId:{}", instId); return; } - int targetStatus = TaskStatusEnum.RUNNING.getCode(); + double progress = FlowProgressTracker.progressOf(event, taskInstance.getProgress()); + int targetStatus = taskInstance.getStatus() == null + ? TaskStatusEnum.RUNNING.getCode() : taskInstance.getStatus(); boolean needUpdateDb = false; + boolean terminal = false; String terminalMessage = null; Date now = DateUtils.getNowDate(); switch (event.getEventType()) { case TASK_COMPLETED: targetStatus = TaskStatusEnum.SUCCESS.getCode(); needUpdateDb = true; + terminal = true; terminalMessage = "任务执行完成"; break; case TASK_FAILED: targetStatus = TaskStatusEnum.FAILED.getCode(); needUpdateDb = true; + terminal = true; terminalMessage = StrUtil.isBlank(event.getErrorMessage()) ? "任务执行失败" : "任务执行失败:" + event.getErrorMessage(); break; case TASK_STOPPED: targetStatus = TaskStatusEnum.STOPPED.getCode(); needUpdateDb = true; + terminal = true; terminalMessage = "任务已终止"; break; + case TASK_PAUSED: + targetStatus = TaskStatusEnum.PAUSED.getCode(); + needUpdateDb = true; + terminalMessage = "任务已暂停"; + break; + case TASK_RESUMED: + targetStatus = TaskStatusEnum.RUNNING.getCode(); + needUpdateDb = true; + terminalMessage = "任务已恢复"; + break; case NODE_COMPLETED: default: break; } if (needUpdateDb) { - taskInstance.setStatus(targetStatus); - taskInstance.setEndTime(now); - inspectionTaskInstanceService.updateById(taskInstance); + InspectionTaskInstance update = new InspectionTaskInstance(); + update.setId(taskInstance.getId()); + update.setStatus(targetStatus); + if (terminal) update.setEndTime(now); + inspectionTaskInstanceService.updateById(update); } if (terminalMessage != null) { - pushTerminalMsg(taskInstance.getId(), now, targetStatus, terminalMessage); + pushTerminalMsg(taskInstance.getId(), now, targetStatus, terminalMessage, progress); } if (FlowExecutionEvent.EventType.NODE_COMPLETED == event.getEventType()) { @@ -135,35 +136,6 @@ public class InspectionFlowExecutionListener implements FlowExecutionListener { // 节点推送 if (StrUtil.isNotBlank(nodeId)) { - int waypointCount = taskInstance.getWaypointCount() == null ? 0 : taskInstance.getWaypointCount(); - double progress = 0D; - if (waypointCount > 0) { - // 当前任务已执行item数量(item启动就存入集合,已完整跑完的item=总数-1) - Set itemSet = taskItemExecutMap.getOrDefault(instId, new HashSet<>()); - int itemTotal = itemSet.size(); - int finishedItemNum = itemTotal - 1; - - // 当前item已完成节点数 - Set finishedNodeSet = itemNodeExecutMap.getOrDefault( - itemExecutionKey(instId, itemId, event.getItemOrder()), new HashSet<>()); - int finishedNode = finishedNodeSet.size(); - int totalNode = event.getNodeCount() == 0 ? 1 : event.getNodeCount(); - - // 已完整跑完点位占比 - if (event.getItemOrder() != null && event.getItemOrder() > 0) { - finishedItemNum = event.getItemOrder() - 1; - } - double itemTotalRatio = finishedItemNum / (double) waypointCount; - // 当前运行item内部节点占比 - double innerRatio = finishedNode / (double) totalNode / waypointCount; - - // 还原你的兜底:防止循环节点进度超限,最低不低于极小值 - double singleItemBase = 1d / waypointCount; - innerRatio = Math.max(singleItemBase - 0.01, innerRatio); - - progress = (itemTotalRatio + innerRatio) * 100; - } - String info = event.getEventType() == FlowExecutionEvent.EventType.NODE_STARTED ? "开始执行" : event.getEventType() == FlowExecutionEvent.EventType.NODE_FAILED @@ -238,26 +210,10 @@ public class InspectionFlowExecutionListener implements FlowExecutionListener { } } - /** 任务结束统一清理缓存 */ - private void clearCache(String taskInsId) { - Set itemList = taskItemExecutMap.getOrDefault(taskInsId, new HashSet<>()); - itemList.forEach(itemNodeExecutMap::remove); - taskItemExecutMap.remove(taskInsId); - } - - private String itemExecutionKey(String taskInsId, String itemId) { - return taskInsId + "|" + itemId; - } - - private String itemExecutionKey(String taskInsId, String itemId, Integer itemOrder) { - return itemOrder != null && itemOrder > 0 ? taskInsId + "|" + itemOrder - : itemExecutionKey(taskInsId, itemId); - } - /** * 工作流是异步启动的,首个节点事件可能略早于巡检实例写入task_ins_id,短暂重试消除该竞态。 */ - private InspectionTaskInstance findTaskInstance(String taskInsId) { + protected InspectionTaskInstance findTaskInstance(String taskInsId) { for (int attempt = 0; attempt < 5; attempt++) { InspectionTaskInstance instance = inspectionTaskInstanceService.lambdaQuery() .eq(InspectionTaskInstance::getTaskInsId, taskInsId) @@ -279,6 +235,11 @@ public class InspectionFlowExecutionListener implements FlowExecutionListener { * 保存并推送任务终态信息(参数dbInsId为数据库主键ID) */ public void pushTerminalMsg(String dbInsId, Date now, int targetStatus, String message) { + pushTerminalMsg(dbInsId, now, targetStatus, message, + targetStatus == TaskStatusEnum.SUCCESS.getCode() ? 100D : null); + } + + public void pushTerminalMsg(String dbInsId, Date now, int targetStatus, String message, Double progress) { String logId = UUID.randomUUID().toString().replace("-", ""); // 保存终态日志 @@ -288,6 +249,7 @@ public class InspectionFlowExecutionListener implements FlowExecutionListener { .logType(InspectionLogTypeEnum.TEXT.getCode()) .logContent(message) .status(targetStatus) + .progress(progress) .logTime(now) .build(); diff --git a/cmvr-iot-inspection/src/main/resources/mapper/inspection/InspectionTaskInstanceMapper.xml b/cmvr-iot-inspection/src/main/resources/mapper/inspection/InspectionTaskInstanceMapper.xml index ec4c2bf..c0cabcc 100644 --- a/cmvr-iot-inspection/src/main/resources/mapper/inspection/InspectionTaskInstanceMapper.xml +++ b/cmvr-iot-inspection/src/main/resources/mapper/inspection/InspectionTaskInstanceMapper.xml @@ -5,6 +5,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" + @@ -22,6 +23,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" + @@ -45,8 +47,19 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" + + + case when ${instanceAlias}.status = 0 then 100 + else least(99.99, greatest(0, coalesce( + (select max(pl.progress) from inspection_task_log pl + where pl.task_instance_id = ${instanceAlias}.id), 0))) end as progress + + - select id, create_by, create_time, update_by, update_time, remark, task_id, robot_id, status, start_time, end_time, task_ins_id, waypoint_count from inspection_task_instance + select id, create_by, create_time, update_by, update_time, remark, task_id, robot_id, status, start_time, end_time, task_ins_id, waypoint_count, + + from inspection_task_instance select ti.id, ti.create_by, ti.create_time, ti.update_by, ti.update_time, ti.remark,ti.waypoint_count, + , ti.task_id, t.task_name, t.task_code, ti.robot_id, r.robot_name, r.robot_id as robot_identity, r.active_map_id as robot_current_map_id, m.map_name, @@ -168,6 +182,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"