From ca0195cb38fe664eb0377bf75454cd5cd7bac509 Mon Sep 17 00:00:00 2001 From: huguanghan Date: Mon, 21 Oct 2024 17:11:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E6=97=B6=E6=95=B0=E6=8D=AE=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2,=E5=8E=86=E5=8F=B2=E5=8C=BA=E9=97=B4=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=9F=A5=E8=AF=A2=E6=8E=A5=E5=8F=A3=E6=96=B0=E5=A2=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../das/modules/data/service/DataService.java | 67 +++++++++++-------- 1 file changed, 38 insertions(+), 29 deletions(-) diff --git a/das/src/main/java/com/das/modules/data/service/DataService.java b/das/src/main/java/com/das/modules/data/service/DataService.java index 95d89a0a..7d1e75e6 100644 --- a/das/src/main/java/com/das/modules/data/service/DataService.java +++ b/das/src/main/java/com/das/modules/data/service/DataService.java @@ -36,88 +36,97 @@ public class DataService { private DataServiceImpl dataService; // 读取实时数据快照 - public Map> querySnapshotValues(List paramList){ + public Map> querySnapshotValues(List paramList) { long start = System.currentTimeMillis(); - Map> result = new HashMap<>(paramList.size()); - Map> finalResult = result; - ListUtil.page(paramList, COMMIT_COUNT, list->{ + Map> result = new HashMap<>(paramList.size()); + Map> finalResult = result; + ListUtil.page(paramList, COMMIT_COUNT, list -> { List keyList = new ArrayList<>(); for (int i = 0; i < list.size(); i++) { SnapshotValueQueryParam snapshotValueQueryParam = list.get(i); List attributes = snapshotValueQueryParam.getAttributes(); - if (CollectionUtils.isEmpty(attributes)){ + if (CollectionUtils.isEmpty(attributes)) { //为空查全部 List 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); keyList.add(key); } - }else { - for (String item :attributes){ + } else { + for (String item : attributes) { String key = String.format("RT:[%s]:[%s]", snapshotValueQueryParam.getDeviceId(), item); keyList.add(key); } } } List 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); int firstColonIndex = key.indexOf('['); int firstIndex = key.indexOf(']'); - int secondIndex = key.indexOf(']',firstIndex + 1); + int secondIndex = key.indexOf(']', firstIndex + 1); int secondColonIndex = key.indexOf('[', firstColonIndex + 1); String deviceId = key.substring(firstColonIndex + 1, firstIndex); - String fieldName = key.substring(secondColonIndex + 1,secondIndex); - if (finalResult.get(deviceId) == null){ - Map valueMap = new HashMap<>(); - valueMap.put(fieldName,dataList.get(i)); - finalResult.put(deviceId,valueMap); - }else { - finalResult.get(deviceId).put(fieldName,dataList.get(i)); + String fieldName = key.substring(secondColonIndex + 1, secondIndex); + if (finalResult.get(deviceId) == null) { + Map valueMap = new HashMap<>(); + valueMap.put(fieldName, dataList.get(i)); + finalResult.put(deviceId, valueMap); + } else { + finalResult.get(deviceId).put(fieldName, dataList.get(i)); } } }); long end = System.currentTimeMillis(); - log.debug("读取快照{}个,耗时: {}秒", paramList.size(), (end-start)/ 1000.0); + log.debug("读取快照{}个,耗时: {}秒", paramList.size(), (end - start) / 1000.0); return finalResult; } public Map>> 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("必要参数缺失"); } Date startTime = new Date(Long.parseLong(param.getStartTime())); Date endTime = new Date(Long.parseLong(param.getEndTime())); Map>> result = new HashMap<>(param.getDevices().size()); List deviceFieldList = param.getDevices(); - for (SnapshotValueQueryParam item : deviceFieldList){ + for (SnapshotValueQueryParam item : deviceFieldList) { //field分为高频和低频查询 - Map>> values = queryHistoryCurveValues(Long.valueOf(item.getDeviceId()), startTime, endTime, param.getInterval(), param.getFill(),item.getAttributes()); + Map>> values = queryHistoryCurveValues(Long.valueOf(item.getDeviceId()), startTime, endTime, param.getInterval(), param.getFill(), item.getAttributes()); result.putAll(values); } return result; } - private Map>> queryHistoryCurveValues(Long irn, Date startTime, Date endTime, String interval, String fill,List attributes ){ + private Map>> queryHistoryCurveValues(Long irn, Date startTime, Date endTime, String interval, String fill, List attributes) { String iotModelCode = sysIotModelFieldMapper.queryModelCodeByDeviceId(irn); Map highSpeedFieldMap = dataService.highIotFieldMap.get(iotModelCode); Map lowSpeedFieldMap = dataService.lowIotFieldMap.get(iotModelCode); List highSpeedField = new ArrayList<>(); List lowSpeedField = new ArrayList<>(); - for (String field : attributes){ - if (highSpeedFieldMap.containsKey(field)){ + for (String field : attributes) { + if (highSpeedFieldMap.containsKey(field)) { highSpeedField.add(field); } - if (lowSpeedFieldMap.containsKey(field)){ + if (lowSpeedFieldMap.containsKey(field)) { lowSpeedField.add(field); } } - Map>> lowHistoryCurve = tdEngineService.fetchLowHistoryCurve(irn, startTime, endTime, interval, lowSpeedField); - Map>> highHistoryCurve = tdEngineService.fetchHighHistoryCurve(irn, startTime, endTime, interval, highSpeedField); - Map>> result = new HashMap<>(lowHistoryCurve); - result.get(irn.toString()).putAll(highHistoryCurve.get(irn.toString())); + Map>> result = new HashMap<>(); + if (!CollectionUtils.isEmpty(highSpeedField)) { + Map>> highHistoryCurve = tdEngineService.fetchHighHistoryCurve(irn, startTime, endTime, interval, highSpeedField); + result.putAll(highHistoryCurve); + } + if (!CollectionUtils.isEmpty(lowSpeedField)) { + Map>> 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; }