#
Junjie
9 天以前 dc3f9cc91759823ce59486f19b138be4b296a0f1
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.zy.ai.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.core.annotations.ManagerAuth;
import com.core.common.R;
import com.zy.ai.domain.autotune.AutoTuneApplyResult;
import com.zy.ai.entity.AiAutoTuneChange;
import com.zy.ai.entity.AiAutoTuneJob;
import com.zy.ai.service.AiAutoTuneChangeService;
import com.zy.ai.service.AiAutoTuneJobService;
import com.zy.ai.service.AutoTuneApplyService;
import com.zy.ai.service.AutoTuneCoordinatorService;
import com.zy.ai.service.AutoTuneSnapshotService;
import com.zy.common.web.BaseController;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
@RestController
@RequestMapping("/ai/autoTune")
@RequiredArgsConstructor
public class AutoTuneConsoleController extends BaseController {
 
    private static final int DEFAULT_JOB_LIMIT = 10;
    private static final int MAX_JOB_LIMIT = 50;
 
    private final AutoTuneSnapshotService autoTuneSnapshotService;
    private final AutoTuneCoordinatorService autoTuneCoordinatorService;
    private final AutoTuneApplyService autoTuneApplyService;
    private final AiAutoTuneJobService aiAutoTuneJobService;
    private final AiAutoTuneChangeService aiAutoTuneChangeService;
 
    @GetMapping("/snapshot/auth")
    @ManagerAuth(memo = "查看AI自动调参快照")
    public R snapshot() {
        return R.ok(autoTuneSnapshotService.buildSnapshot());
    }
 
    @GetMapping("/jobs/auth")
    @ManagerAuth(memo = "查看AI自动调参记录")
    public R jobs(@RequestParam(value = "limit", required = false) Integer limit) {
        int safeLimit = normalizeLimit(limit);
        List<AiAutoTuneJob> jobs = aiAutoTuneJobService.list(new QueryWrapper<AiAutoTuneJob>()
                .orderByDesc("start_time")
                .orderByDesc("id")
                .last("limit " + safeLimit));
        return R.ok(toJobSummaries(jobs));
    }
 
    @PostMapping("/triggerManual/auth")
    @ManagerAuth(memo = "手动触发AI自动调参Agent")
    public R triggerManual() {
        return R.ok(autoTuneCoordinatorService.runManualAutoTune());
    }
 
    @PostMapping("/triggerScheduler/auth")
    @ManagerAuth(memo = "按后台规则触发AI自动调参")
    public R triggerScheduler() {
        return R.ok(autoTuneCoordinatorService.runAutoTuneIfEligible());
    }
 
    @PostMapping("/rollback/auth")
    @ManagerAuth(memo = "回滚最近一次AI自动调参")
    public R rollback(@RequestParam(value = "reason", required = false) String reason) {
        AutoTuneApplyResult result = autoTuneApplyService.rollbackLastSuccessfulJob(reason);
        return R.ok(result);
    }
 
    private int normalizeLimit(Integer limit) {
        if (limit == null || limit <= 0) {
            return DEFAULT_JOB_LIMIT;
        }
        return Math.min(limit, MAX_JOB_LIMIT);
    }
 
    private List<Map<String, Object>> toJobSummaries(List<AiAutoTuneJob> jobs) {
        List<Map<String, Object>> result = new ArrayList<>();
        if (jobs == null || jobs.isEmpty()) {
            return result;
        }
        for (AiAutoTuneJob job : jobs) {
            result.add(toJobSummary(job));
        }
        return result;
    }
 
    private Map<String, Object> toJobSummary(AiAutoTuneJob job) {
        LinkedHashMap<String, Object> item = new LinkedHashMap<>();
        item.put("id", job.getId());
        item.put("triggerType", job.getTriggerType());
        item.put("status", job.getStatus());
        item.put("startTime", job.getStartTime());
        item.put("finishTime", job.getFinishTime());
        item.put("hasActiveTasks", job.getHasActiveTasks());
        item.put("summary", job.getSummary());
        item.put("reasoningDigest", job.getReasoningDigest());
        item.put("snapshotDigest", job.getSnapshotDigest());
        item.put("intervalBefore", job.getIntervalBefore());
        item.put("intervalAfter", job.getIntervalAfter());
        item.put("successCount", job.getSuccessCount());
        item.put("rejectCount", job.getRejectCount());
        item.put("errorMessage", job.getErrorMessage());
        item.put("llmCallCount", job.getLlmCallCount());
        item.put("promptTokens", job.getPromptTokens());
        item.put("completionTokens", job.getCompletionTokens());
        item.put("totalTokens", job.getTotalTokens());
        item.put("changes", listChangeSummaries(job.getId()));
        return item;
    }
 
    private List<Map<String, Object>> listChangeSummaries(Long jobId) {
        List<Map<String, Object>> result = new ArrayList<>();
        if (jobId == null) {
            return result;
        }
        List<AiAutoTuneChange> changes = aiAutoTuneChangeService.list(new QueryWrapper<AiAutoTuneChange>()
                .eq("job_id", jobId)
                .orderByAsc("id"));
        if (changes == null || changes.isEmpty()) {
            return result;
        }
        for (AiAutoTuneChange change : changes) {
            result.add(toChangeSummary(change));
        }
        return result;
    }
 
    private Map<String, Object> toChangeSummary(AiAutoTuneChange change) {
        LinkedHashMap<String, Object> item = new LinkedHashMap<>();
        item.put("id", change.getId());
        item.put("targetType", change.getTargetType());
        item.put("targetId", change.getTargetId());
        item.put("targetKey", change.getTargetKey());
        item.put("oldValue", change.getOldValue());
        item.put("requestedValue", change.getRequestedValue());
        item.put("appliedValue", change.getAppliedValue());
        item.put("resultStatus", change.getResultStatus());
        item.put("rejectReason", change.getRejectReason());
        item.put("cooldownExpireTime", change.getCooldownExpireTime());
        item.put("createTime", change.getCreateTime());
        return item;
    }
}