调整模板存储方式

This commit is contained in:
xy
2026-06-16 16:20:08 +08:00
parent acec35f6b0
commit 17e23fcb78
3 changed files with 42 additions and 61 deletions

View File

@@ -0,0 +1,15 @@
package com.njcn.stat.dto;
import lombok.Data;
import java.io.Serializable;
/**
* @author 徐扬
*/
@Data
public class DataArrayLiteDto implements Serializable {
private String name;
private String influxDbName;
private String phase;
}

View File

@@ -21,7 +21,7 @@ public enum StatResponseEnum {
DICT_NULL("A10002","字典数据为空"), DICT_NULL("A10002","字典数据为空"),
LINE_NULL("A10002","监测点为空"), LINE_NULL("A10002","监测点为空"),
ARRAY_DATA_NOT_MATCH("A10003","上送数据与模板匹配失败"), ARRAY_DATA_NOT_MATCH("A10003","上送数据个数与模板指标个数不一致"),
; ;

View File

@@ -25,6 +25,7 @@ import com.njcn.influx.utils.InfluxDbUtils;
import com.njcn.mq.message.AppAutoDataMessage; import com.njcn.mq.message.AppAutoDataMessage;
import com.njcn.redis.pojo.enums.AppRedisKey; import com.njcn.redis.pojo.enums.AppRedisKey;
import com.njcn.redis.utils.RedisUtil; import com.njcn.redis.utils.RedisUtil;
import com.njcn.stat.dto.DataArrayLiteDto;
import com.njcn.stat.enums.StatResponseEnum; import com.njcn.stat.enums.StatResponseEnum;
import com.njcn.stat.service.IStatService; import com.njcn.stat.service.IStatService;
import com.njcn.system.enums.DicDataEnum; import com.njcn.system.enums.DicDataEnum;
@@ -66,9 +67,9 @@ public class StatServiceImpl implements IStatService {
private final CsCommunicateFeignClient csCommunicateFeignClient; private final CsCommunicateFeignClient csCommunicateFeignClient;
// 本地缓存模板数据变更极少60分钟过期足够最大缓存50000条 // 本地缓存模板数据变更极少60分钟过期足够最大缓存50000条
private final Cache<String, List<CsDataArray>> modelCache = Caffeine.newBuilder() private final Cache<String, List<DataArrayLiteDto>> modelCache = Caffeine.newBuilder()
.maximumSize(50000)
.expireAfterWrite(60, TimeUnit.MINUTES) .expireAfterWrite(60, TimeUnit.MINUTES)
.softValues()
.build(); .build();
@Override @Override
@@ -123,21 +124,22 @@ public class StatServiceImpl implements IStatService {
Map<String,String> map = new Gson().fromJson(String.valueOf(redisUtil.getObjectByKey(AppRedisKey.ELE_EPD_PQD)), Map.class); Map<String,String> map = new Gson().fromJson(String.valueOf(redisUtil.getObjectByKey(AppRedisKey.ELE_EPD_PQD)), Map.class);
List<String> recordList = new ArrayList<>(); List<String> recordList = new ArrayList<>();
for (AppAutoDataMessage.DataArray item : list) { for (AppAutoDataMessage.DataArray item : list) {
log.info("{}-->处理分钟数据", po.getNdid());
switch (item.getDataAttr()) { switch (item.getDataAttr()) {
case 1: case 1:
log.info("{}-->处理最大值", po.getNdid() + appAutoDataMessage.getDid() + appAutoDataMessage.getMsg().getClDid()); //log.info("{}-->处理最大值", po.getNdid() + appAutoDataMessage.getDid() + appAutoDataMessage.getMsg().getClDid());
dataArrayParam.setStatMethod("max"); dataArrayParam.setStatMethod("max");
break; break;
case 2: case 2:
log.info("{}-->处理最小值", po.getNdid() + appAutoDataMessage.getDid() + appAutoDataMessage.getMsg().getClDid()); //log.info("{}-->处理最小值", po.getNdid() + appAutoDataMessage.getDid() + appAutoDataMessage.getMsg().getClDid());
dataArrayParam.setStatMethod("min"); dataArrayParam.setStatMethod("min");
break; break;
case 3: case 3:
log.info("{}-->处理avg", po.getNdid() + appAutoDataMessage.getDid() + appAutoDataMessage.getMsg().getClDid()); //log.info("{}-->处理avg", po.getNdid() + appAutoDataMessage.getDid() + appAutoDataMessage.getMsg().getClDid());
dataArrayParam.setStatMethod("avg"); dataArrayParam.setStatMethod("avg");
break; break;
case 4: case 4:
log.info("{}-->处理cp95", po.getNdid() + appAutoDataMessage.getDid() + appAutoDataMessage.getMsg().getClDid()); //log.info("{}-->处理cp95", po.getNdid() + appAutoDataMessage.getDid() + appAutoDataMessage.getMsg().getClDid());
dataArrayParam.setStatMethod("cp95"); dataArrayParam.setStatMethod("cp95");
break; break;
default: default:
@@ -146,14 +148,7 @@ public class StatServiceImpl implements IStatService {
boolean flag = Objects.equals(DicDataEnum.DEV_CLD.getCode(), code) && Objects.equals(po.getDevAccessMethod(), "CLD"); boolean flag = Objects.equals(DicDataEnum.DEV_CLD.getCode(), code) && Objects.equals(po.getDevAccessMethod(), "CLD");
int clDid = flag?1:appAutoDataMessage.getMsg().getClDid(); int clDid = flag?1:appAutoDataMessage.getMsg().getClDid();
String key = AppRedisKey.DEV_MODEL.concat(dataArrayParam.getId() + dataArrayParam.getDid() + clDid + dataArrayParam.getIdx()); String key = AppRedisKey.DEV_MODEL.concat(dataArrayParam.getId() + dataArrayParam.getDid() + clDid + dataArrayParam.getIdx());
// Object object = redisUtil.getObjectByKey(key); List<DataArrayLiteDto> dataArrayList = getModelData(dataArrayParam, key);
// List<CsDataArray> dataArrayList;
// if (Objects.isNull(object)){
// dataArrayList = saveModelData(dataArrayParam,key);
// } else {
// dataArrayList = objectToList(object);
// }
List<CsDataArray> dataArrayList = getModelData(dataArrayParam, key);
List<String> result = assembleData(lineId,dataArrayList,item,appAutoDataMessage.getMsg().getClDid(),dataArrayParam.getStatMethod(),po.getProcess(),code,po.getDevAccessMethod(),map); List<String> result = assembleData(lineId,dataArrayList,item,appAutoDataMessage.getMsg().getClDid(),dataArrayParam.getStatMethod(),po.getProcess(),code,po.getDevAccessMethod(),map);
recordList.addAll(result); recordList.addAll(result);
//获取时间 //获取时间
@@ -184,7 +179,6 @@ public class StatServiceImpl implements IStatService {
csCommunicateFeignClient.insertion(dto); csCommunicateFeignClient.insertion(dto);
} }
} }
System.gc();
} }
@Override @Override
@@ -237,14 +231,7 @@ public class StatServiceImpl implements IStatService {
boolean flag = Objects.equals(DicDataEnum.DEV_CLD.getCode(), code) && Objects.equals(po.getDevAccessMethod(), "CLD"); boolean flag = Objects.equals(DicDataEnum.DEV_CLD.getCode(), code) && Objects.equals(po.getDevAccessMethod(), "CLD");
int clDid = flag?1:appAutoDataMessage.getMsg().getClDid(); int clDid = flag?1:appAutoDataMessage.getMsg().getClDid();
String key = AppRedisKey.DEV_MODEL.concat(dataArrayParam.getId() + dataArrayParam.getDid() + clDid + dataArrayParam.getIdx()); String key = AppRedisKey.DEV_MODEL.concat(dataArrayParam.getId() + dataArrayParam.getDid() + clDid + dataArrayParam.getIdx());
// Object object = redisUtil.getObjectByKey(key); List<DataArrayLiteDto> dataArrayList = getModelData(dataArrayParam, key);
// List<CsDataArray> dataArrayList;
// if (Objects.isNull(object)){
// dataArrayList = saveModelData(dataArrayParam,key);
// } else {
// dataArrayList = objectToList(object);
// }
List<CsDataArray> dataArrayList = getModelData(dataArrayParam, key);
List<String> result = assembleDdData(lineId,dataArrayList,item,appAutoDataMessage.getMsg().getClDid(),code,po.getDevAccessMethod(),map); List<String> result = assembleDdData(lineId,dataArrayList,item,appAutoDataMessage.getMsg().getClDid(),code,po.getDevAccessMethod(),map);
recordList.addAll(result); recordList.addAll(result);
//获取时间 //获取时间
@@ -275,10 +262,8 @@ public class StatServiceImpl implements IStatService {
csCommunicateFeignClient.insertion(dto); csCommunicateFeignClient.insertion(dto);
} }
} }
System.gc();
} }
/** /**
* 清理本地模板缓存由Redis Pub/Sub通知触发 * 清理本地模板缓存由Redis Pub/Sub通知触发
*/ */
@@ -287,23 +272,10 @@ public class StatServiceImpl implements IStatService {
modelCache.invalidateAll(); modelCache.invalidateAll();
} }
/**
* 缓存设备模板信息
*/
public List<CsDataArray> saveModelData(DataArrayParam dataArrayParam,String key) {
List<CsDataArray> dataArrayList = dataArrayFeignClient.findListByParam(dataArrayParam).getData();
if (CollectionUtil.isEmpty(dataArrayList)){
throw new BusinessException(StatResponseEnum.DATA_ARRAY_NULL);
}
redisUtil.saveByKey(key,dataArrayList);
return dataArrayList;
}
/** /**
* influxDB数据组装 * influxDB数据组装
*/ */
public List<String> assembleData(String lineId,List<CsDataArray> dataArrayList,AppAutoDataMessage.DataArray item,Integer clDid,String statMethod,Integer process,String devType,String accessMethod, Map<String,String> map) { public List<String> assembleData(String lineId,List<DataArrayLiteDto> dataArrayList,AppAutoDataMessage.DataArray item,Integer clDid,String statMethod,Integer process,String devType,String accessMethod, Map<String,String> map) {
List<String> records = new ArrayList<String>(); List<String> records = new ArrayList<String>();
List<Float> floats = PubUtils.byteArrayToFloatList(Base64.getDecoder().decode(item.getData())); List<Float> floats = PubUtils.byteArrayToFloatList(Base64.getDecoder().decode(item.getData()));
if (CollectionUtil.isEmpty(floats)){ if (CollectionUtil.isEmpty(floats)){
@@ -363,7 +335,7 @@ public class StatServiceImpl implements IStatService {
* 电度influxDB数据组装 * 电度influxDB数据组装
* 电度数据15分钟入库一次 * 电度数据15分钟入库一次
*/ */
public List<String> assembleDdData(String lineId,List<CsDataArray> dataArrayList,AppAutoDataMessage.DataArray item,Integer clDid,String devType,String accessMethod, Map<String,String> map) { public List<String> assembleDdData(String lineId,List<DataArrayLiteDto> dataArrayList,AppAutoDataMessage.DataArray item,Integer clDid,String devType,String accessMethod, Map<String,String> map) {
List<String> records = new ArrayList<String>(); List<String> records = new ArrayList<String>();
List<Float> floats = PubUtils.byteArrayToFloatList(Base64.getDecoder().decode(item.getData())); List<Float> floats = PubUtils.byteArrayToFloatList(Base64.getDecoder().decode(item.getData()));
if (CollectionUtil.isEmpty(floats)){ if (CollectionUtil.isEmpty(floats)){
@@ -404,29 +376,23 @@ public class StatServiceImpl implements IStatService {
return records; return records;
} }
public List<CsDataArray> objectToList(Object object) { public List<DataArrayLiteDto> getModelData(DataArrayParam dataArrayParam, String key) {
List<CsDataArray> urlList = new ArrayList<>(); List<DataArrayLiteDto> result = new ArrayList<>();
if (object != null) { List<DataArrayLiteDto> dataArrayList = modelCache.getIfPresent(key);
if (object instanceof ArrayList<?>) {
for (Object o : (List<?>) object) {
urlList.add((CsDataArray) o);
}
}
}
return urlList;
}
/**
* 获取设备模板信息(本地缓存优先)
*/
public List<CsDataArray> getModelData(DataArrayParam dataArrayParam, String key) {
List<CsDataArray> dataArrayList = modelCache.getIfPresent(key);
if (CollectionUtil.isEmpty(dataArrayList)) { if (CollectionUtil.isEmpty(dataArrayList)) {
dataArrayList = dataArrayFeignClient.findListByParam(dataArrayParam).getData(); List<CsDataArray> data = dataArrayFeignClient.findListByParam(dataArrayParam).getData();
if (CollectionUtil.isEmpty(dataArrayList)){ if (CollectionUtil.isEmpty(data)){
throw new BusinessException(StatResponseEnum.DATA_ARRAY_NULL); throw new BusinessException(StatResponseEnum.DATA_ARRAY_NULL);
} else {
data.forEach(item->{
DataArrayLiteDto dataArrayLiteDto = new DataArrayLiteDto();
dataArrayLiteDto.setName(item.getName());
dataArrayLiteDto.setInfluxDbName(item.getInfluxDbName());
dataArrayLiteDto.setPhase(item.getPhase());
result.add(dataArrayLiteDto);
});
} }
modelCache.put(key, dataArrayList); modelCache.put(key, result);
} }
return dataArrayList; return dataArrayList;
} }