From 21d11af9dc9e0f4aa76da80fb77ab1fc94a83992 Mon Sep 17 00:00:00 2001 From: yahaozhang Date: Wed, 17 Sep 2025 15:51:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=9C=A8DeviceStats=E7=B1=BB=E4=B8=AD?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=87=AA=E5=8A=A8=E5=AE=8C=E6=88=90LOGGED=5F?= =?UTF-8?q?IN=E4=BB=BB=E5=8A=A1=E7=9A=84=E9=80=BB=E8=BE=91=EF=BC=8C?= =?UTF-8?q?=E8=8B=A5=E8=AE=BE=E5=A4=87=E7=A9=BA=E9=97=B2=E8=B6=85=E8=BF=87?= =?UTF-8?q?3=E5=88=86=E9=92=9F=E5=88=99=E6=A0=87=E8=AE=B0=E4=B8=BACOMPLETE?= =?UTF-8?q?D=EF=BC=8C=E5=B9=B6=E8=AE=B0=E5=BD=95=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E5=8E=86=E5=8F=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/device/DeviceStats.java | 63 ++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/gameplatform/server/device/DeviceStats.java b/src/main/java/com/gameplatform/server/device/DeviceStats.java index 045a752..08ef9f2 100644 --- a/src/main/java/com/gameplatform/server/device/DeviceStats.java +++ b/src/main/java/com/gameplatform/server/device/DeviceStats.java @@ -3,6 +3,8 @@ package com.gameplatform.server.device; import com.gameplatform.server.model.dto.device.DeviceStatusResponse; import com.gameplatform.server.model.entity.agent.LinkTask; import com.gameplatform.server.model.entity.history.DeviceStatusTransition; +import com.gameplatform.server.mapper.history.LinkTaskStatusHistoryMapper; +import com.gameplatform.server.model.entity.history.LinkTaskStatusHistory; import com.gameplatform.server.service.cooldown.MemoryMachineCooldownService; import com.gameplatform.server.service.link.DeviceAllocationService; import com.gameplatform.server.mapper.agent.LinkTaskMapper; @@ -15,6 +17,7 @@ import org.springframework.stereotype.Service; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.time.LocalDateTime; +import java.time.Duration; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; @@ -68,6 +71,7 @@ public class DeviceStats { private final MemoryMachineCooldownService cooldownService; private final DeviceStatusTransitionMapper transitionMapper; private final DeviceAllocationService deviceAllocationService; + private final LinkTaskStatusHistoryMapper statusHistoryMapper; private final SystemConfigService systemConfigService; // 记录上一次统计时每台设备的分类结果,用于检测状态变更 @@ -77,12 +81,14 @@ public class DeviceStats { MemoryMachineCooldownService cooldownService, DeviceStatusTransitionMapper transitionMapper, DeviceAllocationService deviceAllocationService, - SystemConfigService systemConfigService) { + SystemConfigService systemConfigService, + LinkTaskStatusHistoryMapper statusHistoryMapper) { this.linkTaskMapper = linkTaskMapper; this.cooldownService = cooldownService; this.transitionMapper = transitionMapper; this.deviceAllocationService = deviceAllocationService; this.systemConfigService = systemConfigService; + this.statusHistoryMapper = statusHistoryMapper; } /** @@ -121,6 +127,19 @@ public class DeviceStats { // log.debug("设备[{}] 原始脚本值='{}' | LOGGED_IN={} USING={} COOLDOWN={} NUMERIC={}", // deviceId, v, loggedIn, usingTask, cooldown, numeric); + // 当脚本值为配置的空闲标识时,若设备上存在 LOGGED_IN 且登录超过3分钟的任务,则自动完成这些任务 + if (v != null && configuredIdle != null && configuredIdle.equals(v)) { + try { + int completed = autoCompleteLoggedInTasksIfIdleOver3m(deviceId); + if (completed > 0) { + // 任务状态可能已变化,重新评估 loggedIn + loggedIn = hasLoggedInTask(deviceId); + } + } catch (Exception e) { + log.warn("autoCompleteLoggedInTasksIfIdleOver3m 执行异常 device={} err={}", deviceId, e.getMessage()); + } + } + // 分类与原因 Category newCategory; String reason; @@ -276,6 +295,48 @@ public class DeviceStats { return selectedDeviceId; } + /** + * 当脚本回报为配置的空闲标识时,如果设备上仍有 LOGGED_IN 任务且运行超过3分钟,则将其自动标记为 COMPLETED。 + * 返回成功完成的任务数量。 + */ + private int autoCompleteLoggedInTasksIfIdleOver3m(String deviceId) { + List loggedInTasks = linkTaskMapper.findByMachineIdAndStatus(deviceId, "LOGGED_IN"); + if (loggedInTasks == null || loggedInTasks.isEmpty()) { + return 0; + } + LocalDateTime now = LocalDateTime.now(); + int completed = 0; + for (LinkTask task : loggedInTasks) { + LocalDateTime loginAt = task.getLoginAt(); + boolean over3m = loginAt != null && loginAt.isBefore(now.minusMinutes(3)); + if (!over3m) { + continue; + } + String prev = task.getStatus(); + task.setStatus("COMPLETED"); + task.setReason("AUTO_COMPLETE_IDLE_3M"); + task.setUpdatedAt(now); + try { + int updated = linkTaskMapper.update(task); + if (updated > 0) { + Integer sinceLoginSeconds = loginAt != null ? (int) Duration.between(loginAt, now).getSeconds() : null; + try { + statusHistoryMapper.insert(new LinkTaskStatusHistory( + task.getId(), task.getCodeNo(), deviceId, + prev, "COMPLETED", "DEVICE_STATS", "idleConfiguredAndRunningOver3m", + sinceLoginSeconds, null + )); + } catch (Exception ignore) {} + completed++; + log.info("自动完成任务:codeNo={} device={} loginAt={} 超过3分钟", task.getCodeNo(), deviceId, loginAt); + } + } catch (Exception e) { + log.warn("自动完成任务失败:codeNo={} device={} err={}", task.getCodeNo(), deviceId, e.getMessage()); + } + } + return completed; + } + private boolean hasLoggedInTask(String deviceId) { List loggedInTasks = linkTaskMapper.findByMachineIdAndStatus(deviceId, "LOGGED_IN"); return loggedInTasks != null && !loggedInTasks.isEmpty();