Commit ddc2c93d by 林永健

refactor(pe-analysis): 优化数据报告AI文本摘要服务

- 引入ObjectUtil工具类处理对象空值判断
- 添加最大重试次数配置支持,在查询处理中快照时过滤重试次数
- 提取重试逻辑为独立方法updateBrandSnapshotWithMaxTry统一处理
- 优化查询结果轮询逻辑,提前返回成功和处理中状态避免重复处理
- 重构响应数据转义处理逻辑到独立方法convertResponseDataEscape
- 修复任务查询异常时的错误日志输出
- 更新成功判断逻辑同时校验返回码和任务完成状态
1 parent 3abb0836
...@@ -3,6 +3,7 @@ package cn.com.glsx.dj.peanalysis.service; ...@@ -3,6 +3,7 @@ package cn.com.glsx.dj.peanalysis.service;
import cn.com.glsx.dj.peanalysis.config.DateReportAiTextSummaryConfig; import cn.com.glsx.dj.peanalysis.config.DateReportAiTextSummaryConfig;
import cn.com.glsx.dj.peanalysis.mapper.DataReportBrandSnapshotMapper; import cn.com.glsx.dj.peanalysis.mapper.DataReportBrandSnapshotMapper;
import cn.com.glsx.dj.peanalysis.model.DataReportBrandSnapshot; import cn.com.glsx.dj.peanalysis.model.DataReportBrandSnapshot;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import lombok.Data; import lombok.Data;
...@@ -79,11 +80,16 @@ public class DataReportAiTextSummaryService { ...@@ -79,11 +80,16 @@ public class DataReportAiTextSummaryService {
public List<DataReportBrandSnapshot> findProcessingSnapshots() { public List<DataReportBrandSnapshot> findProcessingSnapshots() {
try { try {
// 构建查询条件:summary_task_status = 1 (处理中) // 构建查询条件:summary_task_status = 1 (处理中)
Integer maxTryCnt = environment.getProperty("dj.pe.analysis.data.report.task.max.try", Integer.class, 5);
Example example = new Example(DataReportBrandSnapshot.class); Example example = new Example(DataReportBrandSnapshot.class);
example.createCriteria() example.createCriteria()
.andEqualTo("summaryTaskStatus", 1) .andEqualTo("summaryTaskStatus", 1)
.andIsNotNull("summaryTaskId") .andIsNotNull("summaryTaskId")
.andEqualTo("deletedFlag", "N"); .andEqualTo("deletedFlag", "N");
Example.Criteria subCriteria = example.createCriteria()
.orIsNull("maxTry")
.orLessThanOrEqualTo("maxTry", maxTryCnt);
example.and(subCriteria);
List<DataReportBrandSnapshot> processingList = brandSnapshotMapper.selectByExample(example); List<DataReportBrandSnapshot> processingList = brandSnapshotMapper.selectByExample(example);
log.info("查询到处理中的快照数量:{}", processingList != null ? processingList.size() : 0); log.info("查询到处理中的快照数量:{}", processingList != null ? processingList.size() : 0);
...@@ -120,21 +126,13 @@ public class DataReportAiTextSummaryService { ...@@ -120,21 +126,13 @@ public class DataReportAiTextSummaryService {
snapshot.setSummaryTaskId(taskId); snapshot.setSummaryTaskId(taskId);
snapshot.setSummaryTaskStatus(1); snapshot.setSummaryTaskStatus(1);
successCount++; successCount++;
snapshot.setUpdatedDate(new Date());
brandSnapshotMapper.updateByPrimaryKeySelective(snapshot);
log.info("快照提交成功,id: {}, taskId: {}", snapshot.getId(), taskId); log.info("快照提交成功,id: {}, taskId: {}", snapshot.getId(), taskId);
} else { } else {
Integer maxTryCnt = environment.getProperty("dj.pe.analysis.data.report.task.max.try", Integer.class, 5);
int currentMaxTry = snapshot.getMaxTry() + 1;
if (currentMaxTry > maxTryCnt) {
snapshot.setSummaryTaskStatus(3);
log.error("快照提交失败,达到最大尝试次数,id: {}", snapshot.getId());
} else {
snapshot.setMaxTry(currentMaxTry);
log.error("快照提交失败,重试中,id: {}, 重试次数: {}", snapshot.getId(), snapshot.getMaxTry());
}
log.error("获取 taskId 失败,snapshotId: {}", snapshot.getId()); log.error("获取 taskId 失败,snapshotId: {}", snapshot.getId());
updateBrandSnapshotWithMaxTry(snapshot);
} }
snapshot.setUpdatedDate(new Date());
brandSnapshotMapper.updateByPrimaryKeySelective(snapshot);
} catch (Exception e) { } catch (Exception e) {
log.error("提交快照失败,id: {}, error: {}", snapshot.getId(), e.getMessage(), e); log.error("提交快照失败,id: {}, error: {}", snapshot.getId(), e.getMessage(), e);
} }
...@@ -145,6 +143,23 @@ public class DataReportAiTextSummaryService { ...@@ -145,6 +143,23 @@ public class DataReportAiTextSummaryService {
} }
/** /**
* 更新品牌数据快照,并维护最大重试次数
*/
public void updateBrandSnapshotWithMaxTry(DataReportBrandSnapshot snapshot) {
Integer maxTryCnt = environment.getProperty("dj.pe.analysis.data.report.task.max.try", Integer.class, 5);
int currentMaxTry = ObjectUtil.defaultIfNull(snapshot.getMaxTry(), 0) + 1;
if (currentMaxTry > maxTryCnt) {
snapshot.setSummaryTaskStatus(3);
log.error("快照提交失败,达到最大尝试次数,id: {}, config:{}", snapshot.getId(), maxTryCnt);
} else {
snapshot.setMaxTry(currentMaxTry);
log.error("快照提交失败,稍后重试,id: {}, 重试次数: {}", snapshot.getId(), snapshot.getMaxTry());
}
snapshot.setUpdatedDate(new Date());
brandSnapshotMapper.updateByPrimaryKeySelective(snapshot);
}
/**
* 轮询处理中的快照数据,查询 AI 任务结果 * 轮询处理中的快照数据,查询 AI 任务结果
* *
* @param processingSnapshots 处理中的快照列表 * @param processingSnapshots 处理中的快照列表
...@@ -171,7 +186,8 @@ public class DataReportAiTextSummaryService { ...@@ -171,7 +186,8 @@ public class DataReportAiTextSummaryService {
// 调用二方接口查询结果 // 调用二方接口查询结果
QueryResult result = callQueryResult(queryParams); QueryResult result = callQueryResult(queryParams);
if (result != null && result.isSuccess()) { if (result != null) {
if (result.isSuccess()) {
// 更新快照:保存结论和建议,状态改为成功 // 更新快照:保存结论和建议,状态改为成功
snapshot.setOverallConclusion(result.getConclusion()); snapshot.setOverallConclusion(result.getConclusion());
snapshot.setActionSuggestion(result.getAction()); snapshot.setActionSuggestion(result.getAction());
...@@ -180,18 +196,20 @@ public class DataReportAiTextSummaryService { ...@@ -180,18 +196,20 @@ public class DataReportAiTextSummaryService {
successCount++; successCount++;
brandSnapshotMapper.updateByPrimaryKeySelective(snapshot); brandSnapshotMapper.updateByPrimaryKeySelective(snapshot);
log.info("快照结果查询成功,id: {}, taskId: {}", snapshot.getId(), snapshot.getSummaryTaskId()); log.info("快照结果查询成功,id: {}, taskId: {}", snapshot.getId(), snapshot.getSummaryTaskId());
} else if (result != null && result.isProcessing()) { return successCount;
}
if (result.isProcessing()) {
// 任务仍在处理中,不更新状态 // 任务仍在处理中,不更新状态
log.debug("任务仍在处理中,id: {}, taskId: {}", snapshot.getId(), snapshot.getSummaryTaskId()); log.debug("任务仍在处理中,id: {}, taskId: {}", snapshot.getId(), snapshot.getSummaryTaskId());
} else { return successCount;
// 任务失败,更新状态为失败 }
snapshot.setSummaryTaskStatus(3);
brandSnapshotMapper.updateByPrimaryKeySelective(snapshot);
log.error("任务查询失败,id: {}, taskId: {}", snapshot.getId(), snapshot.getSummaryTaskId());
} }
} catch (Exception e) { } catch (Exception e) {
log.error("轮询快照结果失败,id: {}, error: {}", snapshot.getId(), e.getMessage(), e); log.error("轮询快照结果发送异常,id: {}, error: {}", snapshot.getId(), e.getMessage(), e);
} }
// 任务失败,更新状态为失败
updateBrandSnapshotWithMaxTry(snapshot);
log.error("任务查询失败,id: {}, taskId: {}", snapshot.getId(), snapshot.getSummaryTaskId());
} }
log.info("批量轮询快照完成,总数:{}, 成功:{}", processingSnapshots.size(), successCount); log.info("批量轮询快照完成,总数:{}, 成功:{}", processingSnapshots.size(), successCount);
...@@ -272,16 +290,12 @@ public class DataReportAiTextSummaryService { ...@@ -272,16 +290,12 @@ public class DataReportAiTextSummaryService {
result.setCode(outer.getInteger("code")); result.setCode(outer.getInteger("code"));
result.setTaskFinished(outer.getBoolean("taskFinished")); result.setTaskFinished(outer.getBoolean("taskFinished"));
result.setSuccess(outer.getBoolean("success")); result.setSuccess(outer.getBoolean("success"));
// 处理双层转义 log.debug("响应结果, result:{}", result);
String dataStr = outer.getString("data"); if (!result.isSuccess()) {
if (dataStr.startsWith("\"") && dataStr.endsWith("\"")) { return result;
dataStr = dataStr.substring(1, dataStr.length() - 1);
// 处理转义字符
dataStr = dataStr.replace("\\\"", "\"")
.replace("\\\\", "\\")
.replace("\\n", "\n")
.replace("\\r", "\r");
} }
// 处理双层转义
String dataStr = convertResponseDataEscape(outer.getString("data"));
JSONObject innerData = JSON.parseObject(dataStr); JSONObject innerData = JSON.parseObject(dataStr);
String conclusion = innerData.getString("conclusion"); String conclusion = innerData.getString("conclusion");
String action = innerData.getString("action"); String action = innerData.getString("action");
...@@ -289,8 +303,7 @@ public class DataReportAiTextSummaryService { ...@@ -289,8 +303,7 @@ public class DataReportAiTextSummaryService {
result.setAction(action); result.setAction(action);
return result; return result;
} else { } else {
log.error("调用查询结果接口失败,status: {}, body: {}", log.error("调用查询结果接口失败,status: {}, body: {}", response.getStatusCode(), response.getBody());
response.getStatusCode(), response.getBody());
} }
} catch (Exception e) { } catch (Exception e) {
log.error("调用查询结果接口异常", e); log.error("调用查询结果接口异常", e);
...@@ -298,6 +311,27 @@ public class DataReportAiTextSummaryService { ...@@ -298,6 +311,27 @@ public class DataReportAiTextSummaryService {
return null; return null;
} }
private String convertResponseDataEscape(String dataStr) {
try {
if (StringUtils.isBlank(dataStr)) {
log.warn("data数据为空");
return null;
}
if (dataStr.startsWith("\"") && dataStr.endsWith("\"")) {
dataStr = dataStr.substring(1, dataStr.length() - 1);
// 处理转义字符
dataStr = dataStr.replace("\\\"", "\"")
.replace("\\\\", "\\")
.replace("\\n", "\n")
.replace("\\r", "\r");
}
return dataStr;
} catch (Exception e) {
log.error("处理响应data数据异常, data:{}", dataStr, e);
}
return null;
}
/** /**
* 内部类:查询结果封装 * 内部类:查询结果封装
*/ */
...@@ -310,7 +344,7 @@ public class DataReportAiTextSummaryService { ...@@ -310,7 +344,7 @@ public class DataReportAiTextSummaryService {
private Boolean success; private Boolean success;
public boolean isSuccess() { public boolean isSuccess() {
return taskFinished != null && taskFinished; return Objects.equals(code, 200) && taskFinished != null && taskFinished;
} }
public boolean isProcessing() { public boolean isProcessing() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!