|
|
@@ -2,10 +2,10 @@ package com.storlead.knowledge.api;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
-import com.fasterxml.jackson.databind.JsonMappingException;
|
|
|
import com.storlead.framework.common.result.Result;
|
|
|
import com.storlead.knowledge.config.DifyProperties;
|
|
|
import com.storlead.knowledge.entity.AiDatasetsFileEntity;
|
|
|
@@ -18,6 +18,7 @@ import com.storlead.knowledge.pojo.dto.AiTrainingDTO;
|
|
|
import com.storlead.knowledge.pojo.dto.AiDatasetsFileDTO;
|
|
|
import com.storlead.knowledge.pojo.vo.AiTrainingVO;
|
|
|
import com.storlead.knowledge.pojo.vo.AiDatasetsFileVO;
|
|
|
+import com.storlead.knowledge.pojo.vo.KnowledgeStatisticsVO;
|
|
|
import com.storlead.knowledge.service.AiTrainingService;
|
|
|
import com.storlead.knowledge.service.AiDatasetsFileService;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
@@ -31,7 +32,10 @@ import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import javax.validation.Valid;
|
|
|
+import java.time.*;
|
|
|
+import java.time.temporal.ChronoUnit;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@@ -61,6 +65,106 @@ public class KnowledgeController {
|
|
|
);
|
|
|
}
|
|
|
|
|
|
+ @GetMapping("statistics")
|
|
|
+ @ApiOperation("获取知识库统计信息(知识库总数、文档总数、文本块总数、本周新增文本块数)")
|
|
|
+ public Result<KnowledgeStatisticsVO> getStatistics() {
|
|
|
+ KnowledgeStatisticsVO statistics = new KnowledgeStatisticsVO();
|
|
|
+
|
|
|
+ // 1. 调用Dify API获取所有知识库列表(使用较大分页以获取全部数据)
|
|
|
+ String url = difyProperties.getBaseUrl() + "datasets";
|
|
|
+ Result<Object> datasetsResult = httpService.get(
|
|
|
+ url,
|
|
|
+ Map.of("page", 1, "size", 1000),
|
|
|
+ "Bearer " + difyProperties.getDatasetApiKey(),
|
|
|
+ new TypeReference<>() {}
|
|
|
+ );
|
|
|
+ //获取一周前的时间
|
|
|
+ Long oneWeekAgo = Instant.now()
|
|
|
+ .minus(7, ChronoUnit.DAYS)
|
|
|
+ .getEpochSecond();
|
|
|
+
|
|
|
+ int totalDatasets = 0;
|
|
|
+ int totalDocuments = 0;
|
|
|
+ int totalChunks = 0;
|
|
|
+ int thisWeekCount = 0 ;
|
|
|
+ if (datasetsResult.isSuccess()) {
|
|
|
+ Object responseData = datasetsResult.getResult();
|
|
|
+ if (responseData instanceof Map) {
|
|
|
+ Map<?, ?> responseMap = (Map<?, ?>) responseData;
|
|
|
+ // 获取总数
|
|
|
+ Object totalObj = responseMap.get("total");
|
|
|
+ if (totalObj instanceof Number) {
|
|
|
+ totalDatasets = ((Number) totalObj).intValue();
|
|
|
+ }
|
|
|
+ // 遍历数据集,累加文档数和文本块数
|
|
|
+ Object dataObj = responseMap.get("data");
|
|
|
+ if (dataObj instanceof List) {
|
|
|
+ List<?> dataList = (List<?>) dataObj;
|
|
|
+ for (Object item : dataList) {
|
|
|
+ if (item instanceof Map) {
|
|
|
+ Map<?, ?> datasetMap = (Map<?, ?>) item;
|
|
|
+ Object docCount = datasetMap.get("document_count");
|
|
|
+ if (docCount instanceof Number) {
|
|
|
+ totalDocuments += ((Number) docCount).intValue();
|
|
|
+ }
|
|
|
+ String datasets_id = datasetMap.get("id").toString();
|
|
|
+ //获取知识库中文档的列表
|
|
|
+ String url1 = difyProperties.getBaseUrl() + "datasets/"+datasets_id+"/documents";
|
|
|
+ Result<Object> documents_list = httpService.get(
|
|
|
+ url1,
|
|
|
+ Map.of("page", 1, "size", 1000),
|
|
|
+ "Bearer " + difyProperties.getDatasetApiKey(),
|
|
|
+ new TypeReference<>() {}
|
|
|
+ );
|
|
|
+
|
|
|
+
|
|
|
+ if (documents_list.isSuccess()) {
|
|
|
+ Object responseData1 = documents_list.getResult();
|
|
|
+ Map<?, ?> responseMap2 = (Map<?, ?>) responseData1;
|
|
|
+ Object dataObj1 = responseMap2.get("data");
|
|
|
+ if (dataObj1 instanceof List) {
|
|
|
+ List<?> dataList1 = (List<?>) dataObj1;
|
|
|
+ for (Object item1 : dataList1) {
|
|
|
+ if (item1 instanceof Map) {
|
|
|
+ Map<?, ?> datasetMap1 = (Map<?, ?>) item1;
|
|
|
+ String document_id = datasetMap1.get("id").toString();
|
|
|
+ Long create_at = Long.parseLong(datasetMap1.get("created_at") + "");
|
|
|
+ String url2 = difyProperties.getBaseUrl() + "datasets/" + datasets_id + "/documents/" + document_id;
|
|
|
+ Result<Object> document_detail_result = httpService.get(
|
|
|
+ url2,
|
|
|
+ null,
|
|
|
+ "Bearer " + difyProperties.getDatasetApiKey(),
|
|
|
+ new TypeReference<>() {
|
|
|
+ });
|
|
|
+ if (document_detail_result.isSuccess()) {
|
|
|
+ Object responseData2 = document_detail_result.getResult();
|
|
|
+ Map<?, ?> responseMap3 = (Map<?, ?>) responseData2;
|
|
|
+ Object segment_count = responseMap3.get("segment_count");
|
|
|
+ if (segment_count instanceof Number) {
|
|
|
+ totalChunks += Integer.parseInt(segment_count.toString());
|
|
|
+ if (create_at > oneWeekAgo) {
|
|
|
+ thisWeekCount += Integer.parseInt(segment_count.toString() );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ statistics.setTotalDatasets(totalDatasets);
|
|
|
+ statistics.setTotalDocuments(totalDocuments);
|
|
|
+ statistics.setTotalChunks(totalChunks);
|
|
|
+ statistics.setThisWeekNewChunks( thisWeekCount);
|
|
|
+
|
|
|
+ return Result.ok(statistics);
|
|
|
+ }
|
|
|
+
|
|
|
@GetMapping("datasets/{dataset_id}")
|
|
|
@ApiOperation("获取指定ID的知识库")
|
|
|
public Result<Object> getDataset(@PathVariable String dataset_id ) {
|
|
|
@@ -273,31 +377,4 @@ public class KnowledgeController {
|
|
|
return Result.ok(result);
|
|
|
}
|
|
|
|
|
|
-// @GetMapping("training/file/{id}")
|
|
|
-// @ApiOperation("根据ID获取AI训练文件")
|
|
|
-// public Result<AiDatasetsFileEntity> getTrainingFileById(@PathVariable Long id) {
|
|
|
-// AiDatasetsFileEntity vo = aiDatasetsFileService.getById(id);
|
|
|
-// return Result.ok(vo);
|
|
|
-// }
|
|
|
-
|
|
|
-// @PostMapping("training/file")
|
|
|
-// @ApiOperation("创建AI训练文件")
|
|
|
-// public Result<Boolean> createTrainingFile(@Valid @RequestBody AiDatasetsFileEntity dto) {
|
|
|
-// boolean result = aiDatasetsFileService.save(dto);
|
|
|
-// return Result.ok(result);
|
|
|
-// }
|
|
|
-
|
|
|
-// @PutMapping("training/file")
|
|
|
-// @ApiOperation("更新AI训练文件")
|
|
|
-// public Result<Boolean> updateTrainingFile(@Valid @RequestBody AiDatasetsFileEntity dto) {
|
|
|
-// boolean result = aiDatasetsFileService.updateById(dto);
|
|
|
-// return Result.ok(result);
|
|
|
-// }
|
|
|
-
|
|
|
-// @DeleteMapping("training/file/{id}")
|
|
|
-// @ApiOperation("删除AI训练文件")
|
|
|
-// public Result<Boolean> deleteTrainingFile(@PathVariable Long id) {
|
|
|
-// boolean result = aiDatasetsFileService.removeById(id);
|
|
|
-// return Result.ok(result);
|
|
|
-// }
|
|
|
}
|