|
|
|
package org.jeecg.modules.airag.app.service.impl;
|
|
|
|
|
|
|
|
import cn.hutool.core.lang.generator.SnowflakeGenerator;
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
...
|
...
|
@@ -9,11 +10,13 @@ import com.fasterxml.jackson.core.JsonProcessingException; |
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import com.fasterxml.jackson.databind.util.ObjectBuffer;
|
|
|
|
import io.minio.messages.Metadata;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.apache.poi.ss.formula.functions.T;
|
|
|
|
import org.jeecg.modules.airag.app.entity.AiragButton;
|
|
|
|
import org.jeecg.modules.airag.app.entity.AiragLog;
|
|
|
|
import org.jeecg.modules.airag.app.entity.Embeddings;
|
|
|
|
import org.jeecg.modules.airag.app.entity.QuestionEmbedding;
|
|
|
|
import org.jeecg.modules.airag.app.mapper.AiragLogMapper;
|
|
|
|
import org.jeecg.modules.airag.app.mapper.QuestionEmbeddingMapper;
|
|
|
|
import org.jeecg.modules.airag.app.mapper.*;
|
|
|
|
import org.jeecg.modules.airag.app.service.IAiragLogService;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
|
|
...
|
...
|
@@ -36,6 +39,12 @@ public class AiragLogServiceImpl extends ServiceImpl<AiragLogMapper, AiragLog> i |
|
|
|
private QuestionEmbeddingMapper questionEmbeddingMapper;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private AiragButtonMapper airagButtonMapper;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private PgVectorMapper pgVectorMapper;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private AiragLogMapper airagLogMapper;
|
|
|
|
|
|
|
|
|
|
...
|
...
|
@@ -60,7 +69,24 @@ public class AiragLogServiceImpl extends ServiceImpl<AiragLogMapper, AiragLog> i |
|
|
|
questionEmbedding.setMetadata(metadataJson);
|
|
|
|
questionEmbeddingMapper.insert(questionEmbedding);
|
|
|
|
airagLogMapper.updataIsStorage(log.getIsStorage(),log.getId());
|
|
|
|
System.out.println("1");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void saveToEmbeddingLibrary(AiragLog log) throws JsonProcessingException {
|
|
|
|
// 这里实现将问题和回答存入问题库数据表的逻辑
|
|
|
|
// 假设问题库数据表的实体类为 QuestionLibrary,Mapper 接口为 QuestionLibraryMapper
|
|
|
|
Embeddings embeddings = new Embeddings();
|
|
|
|
embeddings.setText(log.getAnswer());
|
|
|
|
embeddings.setKnowledgeId(log.getKnowledgeId());
|
|
|
|
Map<String, Object> metadata = new LinkedHashMap<>();
|
|
|
|
metadata.put("title", "");
|
|
|
|
metadata.put("docName", "");
|
|
|
|
metadata.put("knowledgeId", embeddings.getKnowledgeId());
|
|
|
|
metadata.put("storedFileName", ""); // 自动生成唯一文档ID
|
|
|
|
embeddings.setMetadata(metadata);
|
|
|
|
pgVectorMapper.insert(embeddings);
|
|
|
|
airagLogMapper.updataIfSaveKnowledge(log.getIfSaveKnowledge(),log.getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
...
|
...
|
@@ -73,6 +99,69 @@ public class AiragLogServiceImpl extends ServiceImpl<AiragLogMapper, AiragLog> i |
|
|
|
return airagLogMapper.pageList(airagLog,page);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Map<String, Object> getStatistics() {
|
|
|
|
Map<String, Object> result = new HashMap<>();
|
|
|
|
|
|
|
|
// 1. 获取今日问答次数
|
|
|
|
QueryWrapper<AiragLog> todayWrapper = new QueryWrapper<>();
|
|
|
|
todayWrapper.apply("DATE(create_time) = CURDATE()");
|
|
|
|
long todayCount = this.count(todayWrapper);
|
|
|
|
|
|
|
|
// 2. 获取昨日问答次数
|
|
|
|
QueryWrapper<AiragLog> yesterdayWrapper = new QueryWrapper<>();
|
|
|
|
yesterdayWrapper.apply("DATE(create_time) = DATE_SUB(CURDATE(), INTERVAL 1 DAY)");
|
|
|
|
long yesterdayCount = this.count(yesterdayWrapper);
|
|
|
|
|
|
|
|
// 3. 计算日增长率
|
|
|
|
double growthRate = 0.0;
|
|
|
|
if (yesterdayCount > 0) {
|
|
|
|
growthRate = ((double)(todayCount - yesterdayCount) / yesterdayCount) * 100;
|
|
|
|
} else if (todayCount > 0) {
|
|
|
|
// 昨天为0,今天有数据,则增长率为100%
|
|
|
|
growthRate = 100.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 4. 获取拒绝回答次数
|
|
|
|
QueryWrapper<AiragLog> rejectedWrapper = new QueryWrapper<>();
|
|
|
|
rejectedWrapper.eq("answer_type", 3); // 假设3表示拒绝回答
|
|
|
|
long rejectedCount = this.count(rejectedWrapper);
|
|
|
|
|
|
|
|
// 5. 获取累计问答次数
|
|
|
|
long totalCount = this.count();
|
|
|
|
|
|
|
|
// 6. 计算平均问答次数(基于有记录的天数)
|
|
|
|
QueryWrapper<AiragLog> distinctDaysWrapper = new QueryWrapper<>();
|
|
|
|
distinctDaysWrapper.select("DISTINCT DATE(create_time)");
|
|
|
|
long distinctDays = this.count(distinctDaysWrapper);
|
|
|
|
|
|
|
|
double averageCount = 0.0;
|
|
|
|
if (distinctDays > 0) {
|
|
|
|
averageCount = (double) totalCount / distinctDays;
|
|
|
|
// 保留两位小数
|
|
|
|
averageCount = Math.round(averageCount * 100.0) / 100.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 7.获取按钮问题和按钮code次数,只获取有按钮code的问题(log.question)
|
|
|
|
List<Map<String, Object>> buttonStats = airagLogMapper.getButtonRankList();
|
|
|
|
|
|
|
|
// 8. 获取最近12个月的月度数据
|
|
|
|
List<Map<String, Object>> monthlyData = airagLogMapper.getMonthlyCount();
|
|
|
|
|
|
|
|
// 9. 添加前一天数据
|
|
|
|
result.put("yesterdayCount", yesterdayCount);
|
|
|
|
result.put("growthRate", Math.round(growthRate * 100.0) / 100.0); // 保留两位小数
|
|
|
|
result.put("todayCount", todayCount);
|
|
|
|
result.put("rejectedCount", rejectedCount);
|
|
|
|
result.put("totalCount", totalCount);
|
|
|
|
result.put("averageCount", averageCount);
|
|
|
|
result.put("buttonStats", buttonStats);
|
|
|
|
result.put("monthlyData", monthlyData);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* @Override
|
|
|
|
public List<T> list(Page<AiragLog> page, QueryWrapper<AiragLog> queryWrapper) {
|
|
|
|
|
...
|
...
|
|