refactor(图表): 优化预览图表处理速度

This commit is contained in:
wangjiahao 2024-11-24 16:26:48 +08:00
parent f8c7c22b31
commit 84a1dd0ccb
2 changed files with 59 additions and 22 deletions

View File

@ -30,6 +30,7 @@ import io.dataease.license.config.XpackInteract;
import io.dataease.utils.BeanUtils;
import io.dataease.utils.IDUtils;
import io.dataease.utils.JsonUtil;
import io.dataease.utils.LogUtil;
import io.dataease.visualization.dao.auto.entity.DataVisualizationInfo;
import io.dataease.visualization.dao.auto.mapper.DataVisualizationInfoMapper;
import jakarta.annotation.Resource;
@ -41,6 +42,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.*;
import java.util.concurrent.*;
import java.util.stream.Collectors;
/**
@ -127,23 +129,53 @@ public class ChartViewManege {
QueryWrapper<CoreChartView> wrapper = new QueryWrapper<>();
wrapper.eq("scene_id", sceneId);
List<ChartViewDTO> chartViewDTOS = transChart(coreChartViewMapper.selectList(wrapper));
if(!CollectionUtils.isEmpty(chartViewDTOS)){
List<Long> tableIds = chartViewDTOS.stream()
.map(ChartViewDTO::getTableId) // 提取 id
if (!CollectionUtils.isEmpty(chartViewDTOS)) {
List<Long> tableIds = chartViewDTOS.stream()
.map(ChartViewDTO::getTableId).distinct()
.toList();
if(!CollectionUtils.isEmpty(tableIds)){
if (!CollectionUtils.isEmpty(tableIds)) {
QueryWrapper<CoreDatasetTableField> wp = new QueryWrapper<>();
wp.in("dataset_group_id", tableIds);
List<CoreDatasetTableField> coreDatasetTableFields = coreDatasetTableFieldMapper.selectList(wp);
Map<Long, List<CoreDatasetTableField>> groupedByTableId = coreDatasetTableFields.stream()
.collect(Collectors.groupingBy(CoreDatasetTableField::getDatasetGroupId));
chartViewDTOS.forEach(dto ->{
if(dto.getTableId() !=null){
dto.setCalParams(Utils.getParams(datasetTableFieldManage.transDTO(groupedByTableId.get(dto.getTableId()))));
}
});
}
if(chartViewDTOS.size()<10){
chartViewDTOS.forEach(dto -> {
if (dto.getTableId() != null) {
dto.setCalParams(Utils.getParams(datasetTableFieldManage.transDTO(groupedByTableId.get(dto.getTableId()))));
}
});
}else{
ExecutorService executor = Executors.newFixedThreadPool(10);
try {
// 超过10个图表要处理启用多线程处理
CountDownLatch latch = new CountDownLatch(chartViewDTOS.size());
chartViewDTOS.forEach(dto -> {
executor.submit(() -> {
try {
if (dto.getTableId() != null) {
dto.setCalParams(Utils.getParams(datasetTableFieldManage.transDTO(groupedByTableId.get(dto.getTableId()))));
}
} finally {
latch.countDown(); // 减少计数器
}
});
});
// 等待所有线程完成
boolean completedInTime = latch.await(200, TimeUnit.SECONDS);
if (!completedInTime) {
throw new InterruptedException("Tasks did not complete within 200 seconds");
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
LogUtil.error(e);
} finally {
executor.shutdown(); // 确保线程池关闭
}
}
}
}
return chartViewDTOS;
}

View File

@ -279,18 +279,23 @@ public class DatasetTableFieldManage {
}
public List<DatasetTableFieldDTO> transDTO(List<CoreDatasetTableField> list) {
return list.stream().map(ele -> {
DatasetTableFieldDTO dto = new DatasetTableFieldDTO();
if (ele == null) return null;
BeanUtils.copyBean(dto, ele);
if (StringUtils.isNotEmpty(ele.getParams())) {
TypeReference<List<CalParam>> tokenType = new TypeReference<>() {
};
List<CalParam> calParams = JsonUtil.parseList(ele.getParams(), tokenType);
dto.setParams(calParams);
}
return dto;
}).collect(Collectors.toList());
if(!CollectionUtils.isEmpty(list)){
return list.stream().map(ele -> {
DatasetTableFieldDTO dto = new DatasetTableFieldDTO();
if (ele == null) return null;
BeanUtils.copyBean(dto, ele);
if (StringUtils.isNotEmpty(ele.getParams())) {
TypeReference<List<CalParam>> tokenType = new TypeReference<>() {
};
List<CalParam> calParams = JsonUtil.parseList(ele.getParams(), tokenType);
dto.setParams(calParams);
}
return dto;
}).collect(Collectors.toList());
}else{
return new ArrayList<>();
}
}
private CoreDatasetTableField transDTO2Record(DatasetTableFieldDTO dto) {