实时数据查询,历史区间数据查询接口新增

This commit is contained in:
huguanghan 2024-10-21 17:11:57 +08:00
parent e3c6d293b6
commit ca0195cb38

View File

@ -36,88 +36,97 @@ public class DataService {
private DataServiceImpl dataService; private DataServiceImpl dataService;
// 读取实时数据快照 // 读取实时数据快照
public Map<String,Map<String,Object>> querySnapshotValues(List<SnapshotValueQueryParam> paramList){ public Map<String, Map<String, Object>> querySnapshotValues(List<SnapshotValueQueryParam> paramList) {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
Map<String,Map<String,Object>> result = new HashMap<>(paramList.size()); Map<String, Map<String, Object>> result = new HashMap<>(paramList.size());
Map<String,Map<String,Object>> finalResult = result; Map<String, Map<String, Object>> finalResult = result;
ListUtil.page(paramList, COMMIT_COUNT, list->{ ListUtil.page(paramList, COMMIT_COUNT, list -> {
List<String> keyList = new ArrayList<>(); List<String> keyList = new ArrayList<>();
for (int i = 0; i < list.size(); i++) { for (int i = 0; i < list.size(); i++) {
SnapshotValueQueryParam snapshotValueQueryParam = list.get(i); SnapshotValueQueryParam snapshotValueQueryParam = list.get(i);
List<String> attributes = snapshotValueQueryParam.getAttributes(); List<String> attributes = snapshotValueQueryParam.getAttributes();
if (CollectionUtils.isEmpty(attributes)){ if (CollectionUtils.isEmpty(attributes)) {
//为空查全部 //为空查全部
List<String> sysIotModelFields = sysIotModelFieldMapper.queryAllFiledNames(Long.valueOf(snapshotValueQueryParam.getDeviceId())); List<String> sysIotModelFields = sysIotModelFieldMapper.queryAllFiledNames(Long.valueOf(snapshotValueQueryParam.getDeviceId()));
for (String item : sysIotModelFields){ for (String item : sysIotModelFields) {
String key = String.format("RT:[%s]:[%s]", snapshotValueQueryParam.getDeviceId(), item); String key = String.format("RT:[%s]:[%s]", snapshotValueQueryParam.getDeviceId(), item);
keyList.add(key); keyList.add(key);
} }
}else { } else {
for (String item :attributes){ for (String item : attributes) {
String key = String.format("RT:[%s]:[%s]", snapshotValueQueryParam.getDeviceId(), item); String key = String.format("RT:[%s]:[%s]", snapshotValueQueryParam.getDeviceId(), item);
keyList.add(key); keyList.add(key);
} }
} }
} }
List<Object> dataList = adminRedisTemplate.mGet(keyList); List<Object> dataList = adminRedisTemplate.mGet(keyList);
for (int i = 0; i < keyList.size(); i++){ for (int i = 0; i < keyList.size(); i++) {
String key = keyList.get(i); String key = keyList.get(i);
int firstColonIndex = key.indexOf('['); int firstColonIndex = key.indexOf('[');
int firstIndex = key.indexOf(']'); int firstIndex = key.indexOf(']');
int secondIndex = key.indexOf(']',firstIndex + 1); int secondIndex = key.indexOf(']', firstIndex + 1);
int secondColonIndex = key.indexOf('[', firstColonIndex + 1); int secondColonIndex = key.indexOf('[', firstColonIndex + 1);
String deviceId = key.substring(firstColonIndex + 1, firstIndex); String deviceId = key.substring(firstColonIndex + 1, firstIndex);
String fieldName = key.substring(secondColonIndex + 1,secondIndex); String fieldName = key.substring(secondColonIndex + 1, secondIndex);
if (finalResult.get(deviceId) == null){ if (finalResult.get(deviceId) == null) {
Map<String,Object> valueMap = new HashMap<>(); Map<String, Object> valueMap = new HashMap<>();
valueMap.put(fieldName,dataList.get(i)); valueMap.put(fieldName, dataList.get(i));
finalResult.put(deviceId,valueMap); finalResult.put(deviceId, valueMap);
}else { } else {
finalResult.get(deviceId).put(fieldName,dataList.get(i)); finalResult.get(deviceId).put(fieldName, dataList.get(i));
} }
} }
}); });
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();
log.debug("读取快照{}个,耗时: {}秒", paramList.size(), (end-start)/ 1000.0); log.debug("读取快照{}个,耗时: {}秒", paramList.size(), (end - start) / 1000.0);
return finalResult; return finalResult;
} }
public Map<String, Map<String, Map<String, Object>>> queryTimeSeriesValues(TSValueQueryParam param) { public Map<String, Map<String, Map<String, Object>>> queryTimeSeriesValues(TSValueQueryParam param) {
if(CollectionUtil.isEmpty(param.getDevices()) || (param.getStartTime() == null && param.getEndTime() == null)){ if (CollectionUtil.isEmpty(param.getDevices()) || (param.getStartTime() == null && param.getEndTime() == null)) {
throw new ServiceException("必要参数缺失"); throw new ServiceException("必要参数缺失");
} }
Date startTime = new Date(Long.parseLong(param.getStartTime())); Date startTime = new Date(Long.parseLong(param.getStartTime()));
Date endTime = new Date(Long.parseLong(param.getEndTime())); Date endTime = new Date(Long.parseLong(param.getEndTime()));
Map<String, Map<String, Map<String, Object>>> result = new HashMap<>(param.getDevices().size()); Map<String, Map<String, Map<String, Object>>> result = new HashMap<>(param.getDevices().size());
List<SnapshotValueQueryParam> deviceFieldList = param.getDevices(); List<SnapshotValueQueryParam> deviceFieldList = param.getDevices();
for (SnapshotValueQueryParam item : deviceFieldList){ for (SnapshotValueQueryParam item : deviceFieldList) {
//field分为高频和低频查询 //field分为高频和低频查询
Map<String, Map<String, Map<String, Object>>> values = queryHistoryCurveValues(Long.valueOf(item.getDeviceId()), startTime, endTime, param.getInterval(), param.getFill(),item.getAttributes()); Map<String, Map<String, Map<String, Object>>> values = queryHistoryCurveValues(Long.valueOf(item.getDeviceId()), startTime, endTime, param.getInterval(), param.getFill(), item.getAttributes());
result.putAll(values); result.putAll(values);
} }
return result; return result;
} }
private Map<String, Map<String, Map<String, Object>>> queryHistoryCurveValues(Long irn, Date startTime, Date endTime, String interval, String fill,List<String> attributes ){ private Map<String, Map<String, Map<String, Object>>> queryHistoryCurveValues(Long irn, Date startTime, Date endTime, String interval, String fill, List<String> attributes) {
String iotModelCode = sysIotModelFieldMapper.queryModelCodeByDeviceId(irn); String iotModelCode = sysIotModelFieldMapper.queryModelCodeByDeviceId(irn);
Map<String, Object> highSpeedFieldMap = dataService.highIotFieldMap.get(iotModelCode); Map<String, Object> highSpeedFieldMap = dataService.highIotFieldMap.get(iotModelCode);
Map<String, Object> lowSpeedFieldMap = dataService.lowIotFieldMap.get(iotModelCode); Map<String, Object> lowSpeedFieldMap = dataService.lowIotFieldMap.get(iotModelCode);
List<String> highSpeedField = new ArrayList<>(); List<String> highSpeedField = new ArrayList<>();
List<String> lowSpeedField = new ArrayList<>(); List<String> lowSpeedField = new ArrayList<>();
for (String field : attributes){ for (String field : attributes) {
if (highSpeedFieldMap.containsKey(field)){ if (highSpeedFieldMap.containsKey(field)) {
highSpeedField.add(field); highSpeedField.add(field);
} }
if (lowSpeedFieldMap.containsKey(field)){ if (lowSpeedFieldMap.containsKey(field)) {
lowSpeedField.add(field); lowSpeedField.add(field);
} }
} }
Map<String, Map<String, Map<String, Object>>> lowHistoryCurve = tdEngineService.fetchLowHistoryCurve(irn, startTime, endTime, interval, lowSpeedField); Map<String, Map<String, Map<String, Object>>> result = new HashMap<>();
Map<String, Map<String, Map<String, Object>>> highHistoryCurve = tdEngineService.fetchHighHistoryCurve(irn, startTime, endTime, interval, highSpeedField); if (!CollectionUtils.isEmpty(highSpeedField)) {
Map<String, Map<String, Map<String, Object>>> result = new HashMap<>(lowHistoryCurve); Map<String, Map<String, Map<String, Object>>> highHistoryCurve = tdEngineService.fetchHighHistoryCurve(irn, startTime, endTime, interval, highSpeedField);
result.get(irn.toString()).putAll(highHistoryCurve.get(irn.toString())); result.putAll(highHistoryCurve);
}
if (!CollectionUtils.isEmpty(lowSpeedField)) {
Map<String, Map<String, Map<String, Object>>> lowHistoryCurve = tdEngineService.fetchLowHistoryCurve(irn, startTime, endTime, interval, lowSpeedField);
if (result.get(irn.toString()) == null) {
result.putAll(lowHistoryCurve);
} else {
result.get(irn.toString()).putAll(lowHistoryCurve.get(irn.toString()));
}
}
return result; return result;
} }