Commit 8048b88f by zhouhaibao

[+]指标性能问题优化,指标拆开多线程查询

1 parent 152a1288
...@@ -5,6 +5,7 @@ import cn.com.glsx.dj.supervisor.model.DataResp; ...@@ -5,6 +5,7 @@ import cn.com.glsx.dj.supervisor.model.DataResp;
import cn.com.glsx.dj.supervisor.util.HttpRequestUtils; import cn.com.glsx.dj.supervisor.util.HttpRequestUtils;
import cn.com.glsx.framework.core.exception.RpcServiceException; import cn.com.glsx.framework.core.exception.RpcServiceException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -111,7 +112,6 @@ public class GetDataStatsService { ...@@ -111,7 +112,6 @@ public class GetDataStatsService {
BeanUtil.copyProperties(queryDataBiz, queryCopy); BeanUtil.copyProperties(queryDataBiz, queryCopy);
queryCopy.setItemCodes(itemCode); queryCopy.setItemCodes(itemCode);
String jsonBody = JSONUtil.toJsonStr(queryCopy); String jsonBody = JSONUtil.toJsonStr(queryCopy);
log.info("jsonBody: {}", jsonBody);
futures.add(CompletableFuture.runAsync(() -> { futures.add(CompletableFuture.runAsync(() -> {
try { try {
DataResp<List<DataBusinessDTO>> dataResp = this.getListDataResp(jsonBody); DataResp<List<DataBusinessDTO>> dataResp = this.getListDataResp(jsonBody);
...@@ -285,4 +285,133 @@ public class GetDataStatsService { ...@@ -285,4 +285,133 @@ public class GetDataStatsService {
return getListDataResp(jsonBody); return getListDataResp(jsonBody);
} }
/**
* 方法1:单个itemcode多线程查询,返回Map<String, DataBusinessDTO>
* @param itemCodes 指标编码列表
* @param bizs 场景
* @param merchantIds 门店ID列表,多个用逗号分隔
* @param staffIds 员工ID列表,多个用逗号分隔
* @param startDate 开始时间
* @param endDate 结束时间
* @return 指标代码到数据的映射
*/
public Map<String, DataBusinessDTO> getSingleItemCodeMap(List<String> itemCodes, Integer bizs, String merchantIds, String staffIds, String startDate, String endDate) {
DataBizQueryParamsDTO queryParams = new DataBizQueryParamsDTO();
queryParams.setBizs(bizs);
queryParams.setMerchantIds(merchantIds);
queryParams.setStaffIds(staffIds);
queryParams.setStartDate(startDate);
queryParams.setEndDate(endDate);
return parallelCallMap(itemCodes, queryParams);
}
/**
* 方法2:单个itemcode多线程查询,返回Map<String, List<DataBusinessDTO>>
* @param itemCodes 指标编码列表
* @param bizs 场景
* @param merchantIds 门店ID列表,多个用逗号分隔
* @param staffIds 员工ID列表,多个用逗号分隔
* @param startDate 开始时间
* @param endDate 结束时间
* @return 指标代码到数据列表的映射
*/
public Map<String, List<DataBusinessDTO>> getSingleItemCodeListMap(List<String> itemCodes, Integer bizs, String merchantIds, String staffIds, String startDate, String endDate) {
DataBizQueryParamsDTO queryParams = new DataBizQueryParamsDTO();
queryParams.setBizs(bizs);
queryParams.setMerchantIds(merchantIds);
queryParams.setStaffIds(staffIds);
queryParams.setStartDate(startDate);
queryParams.setEndDate(endDate);
return parallelCallListMap(itemCodes, queryParams);
}
/**
* 方法3:单个itemcode多线程查询,返回合并后的List<DataBusinessDTO>
* @param itemCodes 指标编码列表
* @param bizs 场景
* @param merchantIds 门店ID列表,多个用逗号分隔
* @param staffIds 员工ID列表,多个用逗号分隔
* @param startDate 开始时间
* @param endDate 结束时间
* @return 合并后的数据列表
*/
public List<DataBusinessDTO> getSingleItemCodeMergedList(List<String> itemCodes, Integer bizs, String merchantIds, String staffIds, String startDate, String endDate) {
Map<String, List<DataBusinessDTO>> resultMap = getSingleItemCodeListMap(itemCodes, bizs, merchantIds, staffIds, startDate, endDate);
List<DataBusinessDTO> mergedList = new ArrayList<>();
for (List<DataBusinessDTO> dataList : resultMap.values()) {
if (CollUtil.isNotEmpty(dataList)) {
mergedList.addAll(dataList);
}
}
return mergedList;
}
/**
* 方法4:单个itemcode多线程查询,使用自定义key返回Map<String, List<DataBusinessDTO>>
* @param itemCodeMap 自定义key到指标编码的映射
* @param bizs 场景
* @param merchantIds 门店ID列表,多个用逗号分隔
* @param staffIds 员工ID列表,多个用逗号分隔
* @param startDate 开始时间
* @param endDate 结束时间
* @return 自定义key到数据列表的映射
*/
public Map<String, List<DataBusinessDTO>> getSingleItemCodeWithCustomKey(Map<String, String> itemCodeMap, Integer bizs, String merchantIds, String staffIds, String startDate, String endDate) {
Map<String, DataBizQueryParamsDTO> queryMap = new HashMap<>();
for (Map.Entry<String, String> entry : itemCodeMap.entrySet()) {
DataBizQueryParamsDTO queryParams = new DataBizQueryParamsDTO();
queryParams.setBizs(bizs);
queryParams.setMerchantIds(merchantIds);
queryParams.setStaffIds(staffIds);
queryParams.setStartDate(startDate);
queryParams.setEndDate(endDate);
queryParams.setItemCodes(entry.getValue());
queryMap.put(entry.getKey(), queryParams);
}
return parallelCallListWithKey(queryMap);
}
/**
* 新方法:替代 getListDataResp,使用单个 itemcode 多线程查询并合并结果
* @param params 查询参数
* @return 数据响应
*/
public DataResp<List<DataBusinessDTO>> getMultiItemCodeData(DataBizQueryParamsDTO params) {
// 解析 itemCodes,支持逗号分隔的多个指标
String itemCodes = params.getItemCodes();
if (itemCodes == null || itemCodes.trim().isEmpty()) {
return new DataResp<>(200, "success", new ArrayList<>());
}
// 拆分指标编码
List<String> itemCodeList = Arrays.asList(itemCodes.split(","));
if (itemCodeList.isEmpty()) {
return new DataResp<>(200, "success", new ArrayList<>());
}
// 创建查询参数副本
DataBizQueryParamsDTO queryCopy = new DataBizQueryParamsDTO();
BeanUtil.copyProperties(params, queryCopy);
// 多线程查询每个指标
Map<String, List<DataBusinessDTO>> resultMap = parallelCallListMap(itemCodeList, queryCopy);
// 合并结果
List<DataBusinessDTO> mergedList = new ArrayList<>();
for (List<DataBusinessDTO> dataList : resultMap.values()) {
if (CollUtil.isNotEmpty(dataList)) {
mergedList.addAll(dataList);
}
}
// 返回合并后的结果
return new DataResp<>(200, "success", mergedList);
}
} }
...@@ -397,8 +397,8 @@ public class SupervisorDashboardService { ...@@ -397,8 +397,8 @@ public class SupervisorDashboardService {
} }
params.setItemCodes("A036,A075,A084"); params.setItemCodes("A036,A075,A084");
params.setExts("merchantId,staffId"); params.setExts("merchantId,staffId");
DataResp<List<DataBusinessDTO>> listDataResp = getDataStatsService.getListDataResp( DataResp<List<DataBusinessDTO>> listDataResp = getDataStatsService.getMultiItemCodeData(
JSONObject.toJSONString(params)); params);
Map<String, EmployeeMetricTemp> phoneMetricMap = getStringEmployeeMetricTempMap( Map<String, EmployeeMetricTemp> phoneMetricMap = getStringEmployeeMetricTempMap(
listDataResp); listDataResp);
...@@ -664,9 +664,7 @@ public class SupervisorDashboardService { ...@@ -664,9 +664,7 @@ public class SupervisorDashboardService {
Map<String, EmployeeRanking> phoneMap = new HashMap<>(); Map<String, EmployeeRanking> phoneMap = new HashMap<>();
DataResp<List<DataBusinessDTO>> biResp = getDataStatsService.getListDataResp( DataResp<List<DataBusinessDTO>> biResp = getDataStatsService.getMultiItemCodeData(biParams);
JSONObject.toJSONString(biParams)
);
if (biResp != null && CollectionUtils.isNotEmpty(biResp.getContent())) { if (biResp != null && CollectionUtils.isNotEmpty(biResp.getContent())) {
for (DataBusinessDTO dto : biResp.getContent()) { for (DataBusinessDTO dto : biResp.getContent()) {
...@@ -1320,7 +1318,7 @@ public class SupervisorDashboardService { ...@@ -1320,7 +1318,7 @@ public class SupervisorDashboardService {
storeQueryParams.setDateType(req.getDateType()); storeQueryParams.setDateType(req.getDateType());
String storeJsonBody = JSONUtil.toJsonStr(storeQueryParams); String storeJsonBody = JSONUtil.toJsonStr(storeQueryParams);
DataResp<List<DataBusinessDTO>> storeDataResp = getDataStatsService.getListDataResp(storeJsonBody); DataResp<List<DataBusinessDTO>> storeDataResp = getDataStatsService.getMultiItemCodeData(storeQueryParams);
if (storeDataResp != null && storeDataResp.getContent() != null && !storeDataResp.getContent().isEmpty()) { if (storeDataResp != null && storeDataResp.getContent() != null && !storeDataResp.getContent().isEmpty()) {
for (DataBusinessDTO storeData : storeDataResp.getContent()) { for (DataBusinessDTO storeData : storeDataResp.getContent()) {
...@@ -1369,7 +1367,7 @@ public class SupervisorDashboardService { ...@@ -1369,7 +1367,7 @@ public class SupervisorDashboardService {
String employeeJsonBody = JSONUtil.toJsonStr(employeeQueryParams); String employeeJsonBody = JSONUtil.toJsonStr(employeeQueryParams);
DataResp<List<DataBusinessDTO>> employeeDataResp = getDataStatsService.getListDataResp(employeeJsonBody); DataResp<List<DataBusinessDTO>> employeeDataResp = getDataStatsService.getMultiItemCodeData(employeeQueryParams);
if (employeeDataResp != null && employeeDataResp.getContent() != null && !employeeDataResp.getContent().isEmpty()) { if (employeeDataResp != null && employeeDataResp.getContent() != null && !employeeDataResp.getContent().isEmpty()) {
for (DataBusinessDTO employeeData : employeeDataResp.getContent()) { for (DataBusinessDTO employeeData : employeeDataResp.getContent()) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!