From 38e988077ca50fb3043fb4778b5e3edf5dd5d3b7 Mon Sep 17 00:00:00 2001 From: van Date: Thu, 22 Oct 2026 01:27:54 +0800 Subject: [PATCH] 1 --- .../jarvis/OrderProfitAnalysisController.java | 14 ++ .../service/IOrderProfitAnalysisService.java | 7 +- .../impl/OrderProfitAnalysisServiceImpl.java | 238 ++++++++++++++++-- 3 files changed, 244 insertions(+), 15 deletions(-) diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/jarvis/OrderProfitAnalysisController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/jarvis/OrderProfitAnalysisController.java index c82e8af..d612fcb 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/jarvis/OrderProfitAnalysisController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/jarvis/OrderProfitAnalysisController.java @@ -82,6 +82,20 @@ public class OrderProfitAnalysisController extends BaseController { } } + /** + * 大模型调用记录(新的在前):开始时间、耗时、返回正文。 + */ + @GetMapping("/call-logs") + public AjaxResult callLogs(@RequestParam(value = "limit", required = false) Integer limit) { + try { + int n = limit == null ? 30 : limit; + return AjaxResult.success(orderProfitAnalysisService.listCallLogs(n)); + } catch (Exception e) { + logger.error("读取利润分析调用记录失败", e); + return AjaxResult.error("读取失败: " + e.getMessage()); + } + } + /** * 发送记录(新的在前)。 */ diff --git a/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/IOrderProfitAnalysisService.java b/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/IOrderProfitAnalysisService.java index 0a199a5..2e8c2bc 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/IOrderProfitAnalysisService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/IOrderProfitAnalysisService.java @@ -16,12 +16,17 @@ public interface IOrderProfitAnalysisService { Map buildStats(String periodType, String date); /** - * 统计 + AI 分析;push=true 时推送到企微。 + * 提交一次 AI 分析后立即返回。模型调用在后台进行,结果写入调用记录。 * * @param trigger scheduled / manual / test,写入发送记录 */ Map analyze(String periodType, String date, boolean push, String profileId, String trigger); + /** + * 大模型调用记录,新的在前。含开始时间、耗时和返回正文。 + */ + java.util.List> listCallLogs(int limit); + /** * 读取最近一次分析结果(Redis)。 */ diff --git a/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/impl/OrderProfitAnalysisServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/impl/OrderProfitAnalysisServiceImpl.java index 93b674f..138e574 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/impl/OrderProfitAnalysisServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/impl/OrderProfitAnalysisServiceImpl.java @@ -14,6 +14,8 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service; +import javax.annotation.PreDestroy; + import java.io.InputStream; import java.io.OutputStream; import java.math.BigDecimal; @@ -34,6 +36,10 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedDeque; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.regex.Pattern; @@ -50,7 +56,12 @@ public class OrderProfitAnalysisServiceImpl implements IOrderProfitAnalysisServi private static final String REDIS_PROMPT_PREFIX = "social_media:prompt:"; private static final String REDIS_LATEST_PREFIX = "order_profit_analysis:latest:"; private static final String REDIS_SEND_LOG = "order_profit_analysis:send_log"; + private static final String REDIS_CALL_PREFIX = "order_profit_analysis:call:"; + private static final String REDIS_CALL_IDS = "order_profit_analysis:call_ids"; private static final int SEND_LOG_MAX = 80; + private static final int CALL_LOG_MAX = 80; + /** 超过该时间仍是进行中,列表里记为失败,避免页面一直轮询 */ + private static final long CALL_STALE_MS = 8L * 60L * 1000L; private static final Pattern THINK_BLOCK = Pattern.compile( "(?is)]*>.*?"); private static final Pattern THINK_TAG = Pattern.compile("(?i)]*>"); @@ -133,6 +144,19 @@ public class OrderProfitAnalysisServiceImpl implements IOrderProfitAnalysisServi @Value("${jarvis.wecom.goofish-notify-touser:}") private String fallbackTouser; + private final ExecutorService analyzeExecutor = Executors.newFixedThreadPool(2, r -> { + Thread t = new Thread(r, "order-profit-analyze"); + t.setDaemon(true); + return t; + }); + private final ConcurrentHashMap localCallJson = new ConcurrentHashMap<>(); + private final ConcurrentLinkedDeque localCallIds = new ConcurrentLinkedDeque<>(); + + @PreDestroy + public void shutdownAnalyzeExecutor() { + analyzeExecutor.shutdownNow(); + } + @Override public Map buildStats(String periodType, String date) { PeriodRange current = resolvePeriod(periodType, date, false); @@ -144,7 +168,13 @@ public class OrderProfitAnalysisServiceImpl implements IOrderProfitAnalysisServi @Override public Map analyze(String periodType, String date, boolean push, String profileId, String trigger) { PeriodRange current = resolvePeriod(periodType, date, false); - return doAnalyze(periodType, current, push, profileId, trigger); + Map rec = newRunningRecord(current, push, trigger); + persistCall(rec, true); + Map snapshot = new LinkedHashMap<>(rec); + final String profile = profileId; + final String trig = normalizeTrigger(trigger); + analyzeExecutor.execute(() -> runRecorded(current, push, profile, trig, rec)); + return snapshot; } @Override @@ -191,14 +221,28 @@ public class OrderProfitAnalysisServiceImpl implements IOrderProfitAnalysisServi return; } PeriodRange current = resolvePeriod(periodType, null, true); - try { - Map result = doAnalyze(periodType, current, true, null, "scheduled"); - log.info("慢单利润分析定时完成 period={} pushed={} reportLen={}", - current.label, result.get("pushed"), - result.get("report") == null ? 0 : String.valueOf(result.get("report")).length()); - } catch (Exception e) { - log.error("慢单利润分析定时失败 periodType={} range={}", periodType, current.label, e); + Map rec = newRunningRecord(current, true, "scheduled"); + persistCall(rec, true); + runRecorded(current, true, null, "scheduled", rec); + } + + @Override + public List> listCallLogs(int limit) { + int n = limit <= 0 ? 30 : Math.min(limit, CALL_LOG_MAX); + List> out = new ArrayList<>(); + for (String id : readCallIds(n)) { + String json = readCallJson(id); + if (StringUtils.isEmpty(json)) { + continue; + } + JSONObject jo = JSON.parseObject(json); + if (jo == null) { + continue; + } + markStaleCall(jo); + out.add(new LinkedHashMap<>(jo)); } + return out; } private Map doAnalyze(String periodType, PeriodRange current, boolean push, String profileId, @@ -211,18 +255,26 @@ public class OrderProfitAnalysisServiceImpl implements IOrderProfitAnalysisServi String report; String aiError = null; + String rawReply = null; + long llmStart = System.currentTimeMillis(); + long llmDurationMs = 0; try { - report = callJarvisLlm(prompt, profileId); - if (StringUtils.isEmpty(report)) { + LlmReply llm = callJarvisLlm(prompt, profileId); + llmDurationMs = llm.elapsedMs > 0 ? llm.elapsedMs : (System.currentTimeMillis() - llmStart); + rawReply = llm.text; + if (StringUtils.isEmpty(rawReply)) { throw new RuntimeException("大模型返回为空"); } - report = stripThinkTags(report); + report = stripThinkTags(rawReply); if (StringUtils.isEmpty(report)) { throw new RuntimeException("大模型仅返回思考过程,无正文"); } } catch (Exception e) { + if (llmDurationMs <= 0) { + llmDurationMs = System.currentTimeMillis() - llmStart; + } aiError = e.getMessage(); - log.warn("慢单利润 AI 分析失败,改用数字摘要: {}", aiError); + log.warn("慢单利润 AI 分析失败,改用数字摘要,耗时 {} ms: {}", llmDurationMs, aiError); report = buildFallbackReport(stats, aiError); } @@ -256,6 +308,9 @@ public class OrderProfitAnalysisServiceImpl implements IOrderProfitAnalysisServi result.put("llmJsonChars", statsJson.length()); result.put("report", report); result.put("aiError", aiError); + result.put("llmDurationMs", llmDurationMs); + result.put("promptChars", prompt.length()); + result.put("replyChars", rawReply == null ? 0 : rawReply.length()); result.put("pushed", pushed); result.put("pushError", pushError); result.put("toUser", toUser); @@ -651,7 +706,146 @@ public class OrderProfitAnalysisServiceImpl implements IOrderProfitAnalysisServi return DEFAULT_PROMPT; } - private String callJarvisLlm(String message, String profileId) throws Exception { + private void runRecorded(PeriodRange current, boolean push, String profileId, String trigger, + Map rec) { + long started = longValue(rec.get("startedAtMs")); + try { + Map result = doAnalyze(current.type, current, push, profileId, trigger); + String aiError = result.get("aiError") == null ? "" : String.valueOf(result.get("aiError")); + rec.put("status", StringUtils.isEmpty(aiError) ? "success" : "failed"); + rec.put("report", result.get("report")); + rec.put("aiError", StringUtils.isEmpty(aiError) ? null : aiError); + rec.put("pushed", result.get("pushed")); + rec.put("pushError", result.get("pushError")); + rec.put("llmDurationMs", result.get("llmDurationMs")); + rec.put("promptChars", result.get("promptChars")); + rec.put("replyChars", result.get("replyChars")); + rec.put("toUser", result.get("toUser")); + if (result.get("periodLabel") != null) { + rec.put("periodLabel", result.get("periodLabel")); + } + } catch (Exception e) { + log.error("慢单利润调用失败 id={}", rec.get("id"), e); + rec.put("status", "failed"); + rec.put("aiError", e.getMessage() == null ? e.getClass().getSimpleName() : e.getMessage()); + } + rec.put("finishedAt", java.time.LocalDateTime.now(ZONE).format(TS)); + if (started > 0) { + rec.put("durationMs", System.currentTimeMillis() - started); + } + persistCall(rec, false); + log.info("慢单利润调用记录 id={} status={} durationMs={} llmMs={} replyChars={} error={}", + rec.get("id"), rec.get("status"), rec.get("durationMs"), rec.get("llmDurationMs"), + rec.get("replyChars"), rec.get("aiError")); + } + + private Map newRunningRecord(PeriodRange current, boolean push, String trigger) { + Map rec = new LinkedHashMap<>(); + rec.put("id", UUID.randomUUID().toString().replace("-", "")); + rec.put("status", "running"); + rec.put("startedAt", java.time.LocalDateTime.now(ZONE).format(TS)); + rec.put("startedAtMs", System.currentTimeMillis()); + rec.put("periodType", current.type); + rec.put("periodLabel", current.label); + rec.put("trigger", normalizeTrigger(trigger)); + rec.put("push", push); + return rec; + } + + private void persistCall(Map rec, boolean create) { + String id = String.valueOf(rec.get("id")); + String json = JSON.toJSONString(rec); + localCallJson.put(id, json); + if (create) { + localCallIds.addFirst(id); + while (localCallIds.size() > CALL_LOG_MAX) { + String old = localCallIds.pollLast(); + if (old != null) { + localCallJson.remove(old); + } + } + } + if (redisTemplate == null) { + return; + } + try { + redisTemplate.opsForValue().set(REDIS_CALL_PREFIX + id, json, 40, TimeUnit.DAYS); + if (create) { + redisTemplate.opsForList().leftPush(REDIS_CALL_IDS, id); + redisTemplate.opsForList().trim(REDIS_CALL_IDS, 0, CALL_LOG_MAX - 1); + } + } catch (Exception e) { + log.warn("写入利润分析调用记录失败: {}", e.getMessage()); + } + } + + private List readCallIds(int n) { + if (redisTemplate != null) { + try { + List ids = redisTemplate.opsForList().range(REDIS_CALL_IDS, 0, n - 1); + if (ids != null && !ids.isEmpty()) { + return ids; + } + } catch (Exception e) { + log.warn("读取调用记录索引失败: {}", e.getMessage()); + } + } + List ids = new ArrayList<>(); + for (String id : localCallIds) { + ids.add(id); + if (ids.size() >= n) { + break; + } + } + return ids; + } + + private String readCallJson(String id) { + if (redisTemplate != null) { + try { + String raw = redisTemplate.opsForValue().get(REDIS_CALL_PREFIX + id); + if (StringUtils.isNotEmpty(raw)) { + return raw; + } + } catch (Exception e) { + log.warn("读取调用记录失败: {}", e.getMessage()); + } + } + return localCallJson.get(id); + } + + private void markStaleCall(JSONObject jo) { + if (!"running".equals(jo.getString("status"))) { + return; + } + long started = jo.getLongValue("startedAtMs"); + if (started <= 0 || System.currentTimeMillis() - started < CALL_STALE_MS) { + return; + } + jo.put("status", "failed"); + jo.put("finishedAt", java.time.LocalDateTime.now(ZONE).format(TS)); + jo.put("durationMs", System.currentTimeMillis() - started); + if (StringUtils.isEmpty(jo.getString("aiError"))) { + jo.put("aiError", "调用未在时限内写回结果"); + } + persistCall(new LinkedHashMap<>(jo), false); + } + + private static long longValue(Object value) { + if (value instanceof Number) { + return ((Number) value).longValue(); + } + if (value == null) { + return 0; + } + try { + return Long.parseLong(String.valueOf(value)); + } catch (NumberFormatException e) { + return 0; + } + } + + private LlmReply callJarvisLlm(String message, String profileId) throws Exception { String base = jarvisBaseUrl == null ? "" : jarvisBaseUrl.trim(); if (base.endsWith("/")) { base = base.substring(0, base.length() - 1); @@ -663,7 +857,9 @@ public class OrderProfitAnalysisServiceImpl implements IOrderProfitAnalysisServi body.put("profileId", profileId.trim()); } // 须长于 Jarvis 调模型的读超时(360 秒),否则外层会先断开 + long httpStart = System.currentTimeMillis(); String resp = postJson(url, body.toJSONString(), 15000, 420000); + long httpMs = System.currentTimeMillis() - httpStart; if (StringUtils.isEmpty(resp)) { throw new RuntimeException("Jarvis 返回为空,请检查服务可达性"); } @@ -683,7 +879,21 @@ public class OrderProfitAnalysisServiceImpl implements IOrderProfitAnalysisServi if (!Boolean.TRUE.equals(data.getBoolean("success"))) { throw new RuntimeException(StringUtils.isEmpty(data.getString("error")) ? "大模型调用失败" : data.getString("error")); } - return data.getString("reply"); + long elapsed = data.getLongValue("elapsedMs"); + if (elapsed <= 0) { + elapsed = httpMs; + } + return new LlmReply(data.getString("reply"), elapsed); + } + + private static final class LlmReply { + final String text; + final long elapsedMs; + + LlmReply(String text, long elapsedMs) { + this.text = text; + this.elapsedMs = elapsedMs; + } } private List buildPushChunks(String title, String report) {