From 088d6d943e2669426a1b410e438e9da3a29f1203 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期二, 14 四月 2026 08:57:35 +0800
Subject: [PATCH] #算法耗时优化loadLoopMergeGuardContext增加缓存

---
 src/main/java/com/zy/core/trace/StationTaskTraceRegistry.java |   86 ++++++++++++++++++++++++++++++-------------
 1 files changed, 60 insertions(+), 26 deletions(-)

diff --git a/src/main/java/com/zy/core/trace/StationTaskTraceRegistry.java b/src/main/java/com/zy/core/trace/StationTaskTraceRegistry.java
index d76b8f3..d56af70 100644
--- a/src/main/java/com/zy/core/trace/StationTaskTraceRegistry.java
+++ b/src/main/java/com/zy/core/trace/StationTaskTraceRegistry.java
@@ -11,6 +11,7 @@
 import com.zy.core.model.command.StationCommand;
 import com.zy.core.enums.RedisKeyType;
 import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
@@ -22,6 +23,7 @@
 import java.util.Objects;
 import java.util.concurrent.ConcurrentHashMap;
 
+@Slf4j
 @Component
 public class StationTaskTraceRegistry {
 
@@ -205,6 +207,31 @@
         return result;
     }
 
+    public List<StationTaskTraceVo> listPlanningActiveTraceSnapshots() {
+        ensureCacheLoaded();
+        cleanupExpired();
+        List<StationTaskTraceVo> result = new ArrayList<>();
+        for (TraceTaskState state : taskStateMap.values()) {
+            if (state == null) {
+                continue;
+            }
+            StationTaskTraceVo traceVo = state.toPlanningVo();
+            if (traceVo == null) {
+                continue;
+            }
+            result.add(traceVo);
+        }
+        result.sort(new Comparator<StationTaskTraceVo>() {
+            @Override
+            public int compare(StationTaskTraceVo a, StationTaskTraceVo b) {
+                long av = a.getUpdatedAt() == null ? 0L : a.getUpdatedAt();
+                long bv = b.getUpdatedAt() == null ? 0L : b.getUpdatedAt();
+                return Long.compare(bv, av);
+            }
+        });
+        return result;
+    }
+
     private void cleanupExpired() {
         long now = System.currentTimeMillis();
         for (Map.Entry<Integer, TraceTaskState> entry : taskStateMap.entrySet()) {
@@ -259,7 +286,7 @@
     }
 
     private boolean isStationTraceActiveWrkStatus(Long wrkSts) {
-        return Objects.equals(wrkSts, WrkStsType.INBOUND_DEVICE_RUN.sts)
+        return Objects.equals(wrkSts, WrkStsType.INBOUND_STATION_RUN.sts)
                 || Objects.equals(wrkSts, WrkStsType.STATION_RUN.sts);
     }
 
@@ -277,7 +304,13 @@
             if (loadedFromRedis) {
                 return;
             }
+            long redisStartNs = System.nanoTime();
             Map<Object, Object> cacheMap = redisUtil == null ? null : redisUtil.hmget(TRACE_CACHE_KEY);
+            long redisCostMs = (System.nanoTime() - redisStartNs) / 1_000_000L;
+            if (redisCostMs > 100) {
+                log.info("StationTaskTraceRegistry Redis鍔犺浇鑰楁椂={}ms, entries={}", redisCostMs,
+                        cacheMap == null ? 0 : cacheMap.size());
+            }
             if (cacheMap != null && !cacheMap.isEmpty()) {
                 long now = System.currentTimeMillis();
                 for (Map.Entry<Object, Object> entry : cacheMap.entrySet()) {
@@ -619,9 +652,6 @@
             }
             this.status = terminalStatus;
             this.blockedStationId = blockedStationId;
-            if (shouldClearPathOnTerminal(terminalStatus)) {
-                clearPathState();
-            }
             this.updatedAt = System.currentTimeMillis();
             this.terminalExpireAt = this.updatedAt + TERMINAL_KEEP_MS;
 
@@ -640,12 +670,12 @@
                                                  Map<String, Object> details) {
             boolean active = Boolean.TRUE.equals(loopAlertActive)
                     && loopAlertCount != null
-                    && loopAlertCount > 1
+                    && loopAlertCount > 2
                     && loopAlertText != null
                     && !loopAlertText.trim().isEmpty();
             String nextType = active ? loopAlertType : null;
             String nextText = active ? loopAlertText.trim() : null;
-            Integer nextCount = active ? loopAlertCount : null;
+            Integer nextCount = loopAlertCount != null && loopAlertCount > 0 ? loopAlertCount : null;
             boolean changed = !Objects.equals(this.loopAlertActive, active)
                     || !Objects.equals(this.loopAlertType, nextType)
                     || !Objects.equals(this.loopAlertText, nextText)
@@ -669,22 +699,6 @@
             nextDetails.put("loopAlertText", this.loopAlertText);
             nextDetails.put("loopAlertCount", this.loopAlertCount);
             appendEvent("LOOP_REPEAT_ALERT", this.loopAlertText, nextDetails);
-        }
-
-        private void clearPathState() {
-            this.fullPathStationIds = new ArrayList<>();
-            this.issuedStationIds = new ArrayList<>();
-            this.passedStationIds = new ArrayList<>();
-            this.pendingStationIds = new ArrayList<>();
-            this.latestIssuedSegmentPath = new ArrayList<>();
-            this.segmentList = new ArrayList<>();
-            this.issuedSegmentCount = 0;
-            this.totalSegmentCount = 0;
-        }
-
-        private boolean shouldClearPathOnTerminal(String terminalStatus) {
-            return STATUS_BLOCKED.equals(terminalStatus)
-                    || STATUS_CANCELLED.equals(terminalStatus);
         }
 
         private synchronized boolean shouldRemove(long now) {
@@ -719,6 +733,24 @@
             vo.setLoopAlertCount(loopAlertCount);
             vo.setUpdatedAt(updatedAt);
             vo.setEvents(copyEventList(events));
+            return vo;
+        }
+
+        private synchronized StationTaskTraceVo toPlanningVo() {
+            if (isTerminalStatus(this.status)) {
+                return null;
+            }
+            StationTaskTraceVo vo = new StationTaskTraceVo();
+            vo.setTaskNo(taskNo);
+            vo.setThreadImpl(threadImpl);
+            vo.setStatus(status);
+            vo.setTraceVersion(traceVersion);
+            vo.setStartStationId(startStationId);
+            vo.setCurrentStationId(currentStationId);
+            vo.setFinalTargetStationId(finalTargetStationId);
+            vo.setPendingStationIds(copyIntegerList(pendingStationIds));
+            vo.setLatestIssuedSegmentPath(copyIntegerList(latestIssuedSegmentPath));
+            vo.setUpdatedAt(updatedAt);
             return vo;
         }
 
@@ -829,13 +861,15 @@
         }
 
         private void appendLoopHintDetails(Map<String, Object> details) {
-            if (details == null || !Boolean.TRUE.equals(this.loopAlertActive) || this.loopAlertCount == null || this.loopAlertCount <= 1) {
+            if (details == null || this.loopAlertCount == null || this.loopAlertCount <= 0) {
                 return;
             }
-            details.put("loopAlertActive", Boolean.TRUE);
-            details.put("loopAlertType", this.loopAlertType);
-            details.put("loopAlertText", this.loopAlertText);
             details.put("loopAlertCount", this.loopAlertCount);
+            if (Boolean.TRUE.equals(this.loopAlertActive)) {
+                details.put("loopAlertActive", Boolean.TRUE);
+                details.put("loopAlertType", this.loopAlertType);
+                details.put("loopAlertText", this.loopAlertText);
+            }
         }
 
         private boolean acceptTraceVersion(Integer incomingTraceVersion) {

--
Gitblit v1.9.1