Junjie
昨天 411ff551ae7641dfc5c9331e99bf8b6e5770e2fa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.zy.ai.service;
 
import org.springframework.stereotype.Service;
 
import java.text.SimpleDateFormat;
import java.util.*;
 
@Service
public class WcsQueryService {
 
    // 这里注入你现有的各类 Service/Mapper:deviceService/taskService/logService/configService...
 
    public Map<String, Object> buildDiagnosisSnapshot(Map<String, Object> args) {
        Map<String, Object> focus = (Map<String, Object>) args.get("focus");
        List<String> deviceIds = focus == null ? Collections.emptyList() : (List<String>) focus.get("deviceIds");
        List<String> taskIds   = focus == null ? Collections.emptyList() : (List<String>) focus.get("taskIds");
 
        int timeWindowSec = getInt(args, "timeWindowSec", 600);
        int logLimit      = getInt(args, "logLimit", 400);
        boolean includeConfigs = getBool(args, "includeConfigs", true);
        boolean includeDerived = getBool(args, "includeDerivedSignals", true);
 
        Date now = new Date();
        Date from = new Date(now.getTime() - timeWindowSec * 1000L);
 
        // 1) devices
        List<Object> devices = new ArrayList<>();
        for (String deviceId : safeList(deviceIds)) {
            Map<String, Object> a = new HashMap<>();
            a.put("deviceId", deviceId);
            devices.add(getDeviceState(a));
        }
 
        // 2) tasks
        Map<String, Object> taskArgs = new HashMap<>();
        if (!safeList(taskIds).isEmpty()) {
            // 你可以让 listTasks 支持 taskIds 精确过滤
            taskArgs.put("taskIds", taskIds);
        } else if (!safeList(deviceIds).isEmpty()) {
            taskArgs.put("deviceId", deviceIds.get(0)); // 或改成支持 deviceIds 数组
            taskArgs.put("statuses", Arrays.asList("WAITING","RUNNING","SUSPENDED"));
            taskArgs.put("limit", 200);
        }
        Map<String, Object> taskResult = (Map<String, Object>) listTasks(taskArgs);
        List<Object> tasks = taskResult == null ? new ArrayList<>() : (List<Object>) taskResult.get("tasks");
 
        // 3) logs
        Map<String, Object> logArgs = new HashMap<>();
        logArgs.put("timeFrom", iso(from));
        logArgs.put("timeTo", iso(now));
        logArgs.put("limit", logLimit);
        if (!safeList(deviceIds).isEmpty()) logArgs.put("deviceId", deviceIds.get(0));
        if (!safeList(taskIds).isEmpty())   logArgs.put("taskId", taskIds.get(0));
        Map<String, Object> logResult = (Map<String, Object>) queryLogs(logArgs);
        List<Object> logs = logResult == null ? new ArrayList<>() : (List<Object>) logResult.get("logs");
 
        // 4) configs
        Object configs = null;
        if (includeConfigs) {
            configs = getSystemConfig(new HashMap<String, Object>());
        }
 
        // 5) derived
        Object derived = null;
        if (includeDerived) {
            derived = deriveSignals(devices, tasks, logs);
        }
 
        // output
        Map<String, Object> out = new LinkedHashMap<>();
        Map<String, Object> meta = new LinkedHashMap<>();
        meta.put("snapshotId", UUID.randomUUID().toString());
        meta.put("generatedAt", iso(now));
        meta.put("timeFrom", iso(from));
        meta.put("timeTo", iso(now));
 
        out.put("meta", meta);
        out.put("devices", devices);
        out.put("tasks", tasks);
        out.put("logs", logs);
        out.put("configs", configs);
        out.put("derivedSignals", derived);
        return out;
    }
 
    private Map<String, Object> deriveSignals(List<Object> devices, List<Object> tasks, List<Object> logs) {
        Map<String, Object> d = new LinkedHashMap<>();
        List<String> flags = new ArrayList<>();
        List<String> causes = new ArrayList<>();
        List<String> bottlenecks = new ArrayList<>();
 
        // TODO:把你 WCS 领域规则塞这里(心跳超时、等待确认、命令无ACK、站台满、下游阻塞等)
        // 先给个示例:
        if (tasks != null && !tasks.isEmpty() && (devices == null || devices.isEmpty())) {
            flags.add("HAS_TASKS_BUT_NO_DEVICE_SNAPSHOT");
        }
 
        d.put("anomalyFlags", flags);
        d.put("suspectedRootCauses", causes);
        d.put("suspectedBottlenecks", bottlenecks);
        return d;
    }
 
    // ======= 下面这些函数,你用现有 service 实现即可 =======
    public Map<String, Object> getDeviceState(Map<String, Object> args) { /* ... */ return new HashMap<>(); }
    public Map<String, Object> listTasks(Map<String, Object> args) { /* ... */ return new HashMap<>(); }
    public Map<String, Object> queryLogs(Map<String, Object> args) { /* ... */ return new HashMap<>(); }
    public Object getSystemConfig(Map<String, Object> args) { /* ... */ return new HashMap<>(); }
 
    // helpers
    private int getInt(Map<String, Object> m, String k, int def) {
        Object v = m.get(k);
        if (v == null) return def;
        if (v instanceof Number) return ((Number) v).intValue();
        return Integer.parseInt(String.valueOf(v));
    }
    private boolean getBool(Map<String, Object> m, String k, boolean def) {
        Object v = m.get(k);
        if (v == null) return def;
        if (v instanceof Boolean) return (Boolean) v;
        return Boolean.parseBoolean(String.valueOf(v));
    }
    private List<String> safeList(List<String> l) { return l == null ? Collections.emptyList() : l; }
    private String iso(Date d) {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
        f.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        return f.format(d);
    }
}