|
@@ -27,6 +27,7 @@ import javax.annotation.Resource;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
+import java.util.concurrent.RejectedExecutionException;
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -78,39 +79,47 @@ public class ChartServiceImpl extends ServiceImpl<ChartMapper, Chart>
|
|
save(chart);
|
|
save(chart);
|
|
//使用线程池优化生成图表的逻辑
|
|
//使用线程池优化生成图表的逻辑
|
|
String finalCsvData = csvData;
|
|
String finalCsvData = csvData;
|
|
- threadPoolExecutor.execute(()->{
|
|
|
|
- chart.setState("生成中");
|
|
|
|
- updateById(chart);
|
|
|
|
|
|
+ try {
|
|
|
|
+ threadPoolExecutor.execute(()->{
|
|
|
|
+ chart.setState("生成中");
|
|
|
|
+ updateById(chart);
|
|
|
|
|
|
- StringBuilder message = new StringBuilder("原始数据:\n");
|
|
|
|
- message.append(finalCsvData);
|
|
|
|
- message.append("分析目标:\n");
|
|
|
|
- message.append(chartDTO.getAnalysisTarget());
|
|
|
|
- message.append("\n.使用").append(chartDTO.getChartType()).append("进行可视化分析.\n");
|
|
|
|
- //配置prompt向AI发送请求
|
|
|
|
- HttpRequestData requestData = AiUtil.createDefaultRequestData(message.toString());
|
|
|
|
- HttpResponseData responseData = AiUtil.doChat(requestData);
|
|
|
|
- //解析AI返回的数据
|
|
|
|
- String content = responseData.getChoices().get(0).getMessage().getContent();
|
|
|
|
- log.info("AI返回的数据为:{}", content);
|
|
|
|
- int index = content.indexOf("```");
|
|
|
|
- int endIndex = content.lastIndexOf("```");
|
|
|
|
- if (index == -1 || endIndex == -1){
|
|
|
|
- chart.setState("失败");
|
|
|
|
- chart.setExecuteMessage("AI生成图表失败");
|
|
|
|
|
|
+ StringBuilder message = new StringBuilder("原始数据:\n");
|
|
|
|
+ message.append(finalCsvData);
|
|
|
|
+ message.append("分析目标:\n");
|
|
|
|
+ message.append(chartDTO.getAnalysisTarget());
|
|
|
|
+ message.append("\n.使用").append(chartDTO.getChartType()).append("进行可视化分析.\n");
|
|
|
|
+ //配置prompt向AI发送请求
|
|
|
|
+ HttpRequestData requestData = AiUtil.createDefaultRequestData(message.toString());
|
|
|
|
+ HttpResponseData responseData = AiUtil.doChat(requestData);
|
|
|
|
+ //解析AI返回的数据
|
|
|
|
+ String content = responseData.getChoices().get(0).getMessage().getContent();
|
|
|
|
+ log.info("AI返回的数据为:{}", content);
|
|
|
|
+ int index = content.indexOf("```");
|
|
|
|
+ int endIndex = content.lastIndexOf("```");
|
|
|
|
+ if (index == -1 || endIndex == -1){
|
|
|
|
+ chart.setState("失败");
|
|
|
|
+ chart.setExecuteMessage("AI生成图表失败");
|
|
|
|
+ updateById(chart);
|
|
|
|
+ throw new SystemException(500, "AI生成图表失败");
|
|
|
|
+ }
|
|
|
|
+ //数据可视化,Echarts的option代码
|
|
|
|
+ chart.setGeneratedChartData(content.substring(index+7, endIndex).trim());
|
|
|
|
+ index = endIndex;
|
|
|
|
+ //分析结论
|
|
|
|
+ chart.setAnalysisConclusion(content.substring(index+3).trim());
|
|
|
|
+ //保存到数据库
|
|
|
|
+ chart.setState("成功");
|
|
|
|
+ chart.setExecuteMessage("AI生成图表成功");
|
|
updateById(chart);
|
|
updateById(chart);
|
|
- throw new SystemException(500, "AI生成图表失败");
|
|
|
|
- }
|
|
|
|
- //数据可视化,Echarts的option代码
|
|
|
|
- chart.setGeneratedChartData(content.substring(index+7, endIndex).trim());
|
|
|
|
- index = endIndex;
|
|
|
|
- //分析结论
|
|
|
|
- chart.setAnalysisConclusion(content.substring(index+3).trim());
|
|
|
|
- //保存到数据库
|
|
|
|
- chart.setState("成功");
|
|
|
|
- chart.setExecuteMessage("AI生成图表成功");
|
|
|
|
|
|
+ });
|
|
|
|
+ } catch (RejectedExecutionException e) {
|
|
|
|
+ //e.printStackTrace();
|
|
|
|
+ log.error("线程池已满,请稍后再试",e);
|
|
|
|
+ chart.setState("失败");
|
|
|
|
+ chart.setExecuteMessage("系统异常");
|
|
updateById(chart);
|
|
updateById(chart);
|
|
- });
|
|
|
|
|
|
+ }
|
|
|
|
|
|
return chartId;
|
|
return chartId;
|
|
}
|
|
}
|