Ver código fonte

优化设备实时数据和历史数据返回体,当设备在数据库没有记录时返回体结构中tags和metrics传空

james 1 mês atrás
pai
commit
ec23edeb39

+ 80 - 68
data-tsdb-proxy/data-tsdb-proxy-biz/src/main/java/com/usky/demo/service/impl/QueryInfluxdbDataServiceImpl.java

@@ -251,32 +251,37 @@ public class QueryInfluxdbDataServiceImpl extends AbstractCrudService<QueryInflu
             }
             for (int i = 0; i < deviceUUIds.size(); i++) {
                 String tableName = deviceUUIds.get(i);
-                String query = "SELECT time,"+metricStr+" FROM \""+tableName+"\" order by desc limit 1 tz('Asia/Shanghai')";
-                List<Map<String, Object>> metricList = tsdbUtils.fetchRecords(query);
-                if(CollectionUtils.isEmpty(metricList)){
-                    throw new BusinessException(tableName+"设备没有对应属性,请添加");
-                }
-
-                String tagQuery = "SELECT \"device_id\","+metricStr+" FROM \""+tableName+"\" order by desc limit 1 tz('Asia/Shanghai')";
-                List<Map<String, String>> tag = tsdbUtils.fetchTagRecords(tagQuery);
                 LastInnerResultVO resultVO = new LastInnerResultVO();
                 resultVO.setDeviceuuid(deviceUUIds.get(i));
-                resultVO.setTags(tag.get(0));
-                resultVO.setMetrics(metricList.get(0));
+
+                String tagQuery = "SELECT \"device_id\",*::field FROM \""+tableName+"\" order by desc limit 1 tz('Asia/Shanghai')";
+                List<Map<String, String>> tag = tsdbUtils.fetchTagRecords(tagQuery);
+                if(CollectionUtils.isNotEmpty(tag)){
+                    resultVO.setTags(tag.get(0));
+                    String query = "SELECT time,"+metricStr+" FROM \""+tableName+"\" order by desc limit 1 tz('Asia/Shanghai')";
+                    List<Map<String, Object>> metricList = tsdbUtils.fetchRecords(query);
+                    if(CollectionUtils.isEmpty(metricList)){
+                        throw new BusinessException(tableName+"设备没有对应属性,请添加");
+                    }
+                    resultVO.setMetrics(metricList.get(0));
+                }
+
                 list.add(resultVO);
             }
 
         }else{
             for (int i = 0; i < deviceUUIds.size(); i++) {
                 String tableName = deviceUUIds.get(i);
-                String query = "SELECT *::field FROM \""+tableName+"\" order by desc limit 1 tz('Asia/Shanghai')";
-                List<Map<String, Object>> metricList = tsdbUtils.fetchRecords(query);
-                String tagQuery = "SELECT \"device_id\",*::field FROM \""+tableName+"\" order by desc limit 1 tz('Asia/Shanghai')";
-                List<Map<String, String>> tag = tsdbUtils.fetchTagRecords(tagQuery);
                 LastInnerResultVO resultVO = new LastInnerResultVO();
                 resultVO.setDeviceuuid(deviceUUIds.get(i));
-                resultVO.setTags(tag.get(0));
-                resultVO.setMetrics(metricList.get(0));
+                String tagQuery = "SELECT \"device_id\",*::field FROM \""+tableName+"\" order by desc limit 1 tz('Asia/Shanghai')";
+                List<Map<String, String>> tag = tsdbUtils.fetchTagRecords(tagQuery);
+                if(CollectionUtils.isNotEmpty(tag)){
+                    resultVO.setTags(tag.get(0));
+                    String query = "SELECT *::field FROM \""+tableName+"\" order by desc limit 1 tz('Asia/Shanghai')";
+                    List<Map<String, Object>> metricList = tsdbUtils.fetchRecords(query);
+                    resultVO.setMetrics(metricList.get(0));
+                }
                 list.add(resultVO);
             }
         }
@@ -299,38 +304,43 @@ public class QueryInfluxdbDataServiceImpl extends AbstractCrudService<QueryInflu
             for (int i = 0; i < deviceUUIds.size(); i++) {
                 List<MetricVO> metricList = new ArrayList<>();
                 String tableName = deviceUUIds.get(i);
+                HistorysInnerResultVO resultVO = new HistorysInnerResultVO();
+                resultVO.setDeviceuuid(deviceUUIds.get(i));
+                String tagQuery = "SELECT \"device_id\",*::field FROM \""+tableName+"\" where time >= '"+startTime+"' and time <= '"+endTime+"' tz('Asia/Shanghai')";
+                List<Map<String, String>> tag = tsdbUtils.fetchTagRecords(tagQuery);
+                if(CollectionUtils.isNotEmpty(tag)){
+                    resultVO.setTags(tag.get(0));
 
-                String fieldQuery = "SELECT time,"+metricStr+" FROM \""+tableName+"\" where time >= '"+startTime+"' and time <= '"+endTime+"' tz('Asia/Shanghai')";
-                List<Map<String,Object>> metircList = tsdbUtils.fetchRecords(fieldQuery);
+                    String fieldQuery = "SELECT time,"+metricStr+" FROM \""+tableName+"\" where time >= '"+startTime+"' and time <= '"+endTime+"' tz('Asia/Shanghai')";
+                    List<Map<String,Object>> metircList = tsdbUtils.fetchRecords(fieldQuery);
+                    if(CollectionUtils.isEmpty(metircList)){
+                        throw new BusinessException(tableName+"设备没有对应属性,请添加");
+                    }
 
-                for (int j = 0; j < metrics.size(); j++) {
-                    if(!"time".equals(metrics.get(j))){
-                        List<Map<String,Object>> metircItems = new ArrayList<>();
-                        MetricVO metricVO = new MetricVO();
-                        metricVO.setMetric(metrics.get(j));
-                        if(CollectionUtils.isNotEmpty(metircList)){
-                            for (int k = 0; k < metircList.size(); k++) {
-                                String field = metrics.get(j);
-                                Map<String,Object> map = new HashMap<>();
-                                map.put("timestamp",metircList.get(k).get("time"));
-                                if(Objects.isNull(metircList.get(k).get(field)))
-                                    break;
-                                map.put("value",metircList.get(k).get(field));
-                                metircItems.add(map);
+                    for (int j = 0; j < metrics.size(); j++) {
+                        if(!"time".equals(metrics.get(j))){
+                            List<Map<String,Object>> metircItems = new ArrayList<>();
+                            MetricVO metricVO = new MetricVO();
+                            metricVO.setMetric(metrics.get(j));
+                            if(CollectionUtils.isNotEmpty(metircList)){
+                                for (int k = 0; k < metircList.size(); k++) {
+                                    String field = metrics.get(j);
+                                    Map<String,Object> map = new HashMap<>();
+                                    map.put("timestamp",metircList.get(k).get("time"));
+                                    if(Objects.isNull(metircList.get(k).get(field)))
+                                        break;
+                                    map.put("value",metircList.get(k).get(field));
+                                    metircItems.add(map);
+                                }
+                                metricVO.setMetricItems(metircItems);
                             }
-                            metricVO.setMetricItems(metircItems);
+                            metricList.add(metricVO);
                         }
-                        metricList.add(metricVO);
-                    }
 
+                    }
+                    resultVO.setMetrics(metricList);
                 }
 
-                String tagQuery = "SELECT \"device_id\",*::field FROM \""+tableName+"\" order by desc limit 1 tz('Asia/Shanghai')";
-                List<Map<String, String>> tag = tsdbUtils.fetchTagRecords(tagQuery);
-                HistorysInnerResultVO resultVO = new HistorysInnerResultVO();
-                resultVO.setDeviceuuid(deviceUUIds.get(i));
-                resultVO.setTags(tag.get(0));
-                resultVO.setMetrics(metricList);
                 list.add(resultVO);
             }
 
@@ -338,43 +348,45 @@ public class QueryInfluxdbDataServiceImpl extends AbstractCrudService<QueryInflu
             for (int i = 0; i < deviceUUIds.size(); i++) {
                 List<MetricVO> metricList = new ArrayList<>();
                 String tableName = deviceUUIds.get(i);
+                HistorysInnerResultVO resultVO = new HistorysInnerResultVO();
+                resultVO.setDeviceuuid(deviceUUIds.get(i));
                 String query = "SELECT *::field FROM \""+tableName+"\" order by desc limit 1 tz('Asia/Shanghai')";
                 QueryResult queryResult = tsdbUtils.query(query);
-                String tagQuery = "SELECT \"device_id\",*::field FROM \""+tableName+"\" order by desc limit 1 tz('Asia/Shanghai')";
-                List<Map<String, String>> tag = tsdbUtils.fetchTagRecords(tagQuery);
                 if(queryResult.getResults().get(0).getSeries() != null){
-                    List<String> fields = queryResult.getResults().get(0).getSeries().get(0).getColumns();
-                    if(CollectionUtils.isNotEmpty(fields)){
-                        String fieldQuery = "SELECT *::field FROM \""+tableName+"\" where time >= '"+startTime+"' and time <= '"+endTime+"' tz('Asia/Shanghai')";
-                        List<Map<String,Object>> metircs = tsdbUtils.fetchRecords(fieldQuery);
-
-                        for (int j = 0; j < fields.size(); j++) {
-                            if(!"time".equals(fields.get(j))){
-                                List<Map<String,Object>> metircItems = new ArrayList<>();
-                                MetricVO metricVO = new MetricVO();
-                                metricVO.setMetric(fields.get(j));
-                                if(CollectionUtils.isNotEmpty(metircs)){
-                                    for (int k = 0; k < metircs.size(); k++) {
-                                        String field = fields.get(j);
-                                        Map<String,Object> map = new HashMap<>();
-                                        map.put("timestamp",metircs.get(k).get("time"));
-                                        map.put("value",metircs.get(k).get(field));
-                                        metircItems.add(map);
+                    String tagQuery = "SELECT \"device_id\",*::field FROM \""+tableName+"\" where time >= '"+startTime+"' and time <= '"+endTime+"' tz('Asia/Shanghai')";
+                    List<Map<String, String>> tag = tsdbUtils.fetchTagRecords(tagQuery);
+                    if(CollectionUtils.isNotEmpty(tag)){
+                        resultVO.setTags(tag.get(0));
+
+                        List<String> fields = queryResult.getResults().get(0).getSeries().get(0).getColumns();
+                        if(CollectionUtils.isNotEmpty(fields)){
+                            String fieldQuery = "SELECT *::field FROM \""+tableName+"\" where time >= '"+startTime+"' and time <= '"+endTime+"' tz('Asia/Shanghai')";
+                            List<Map<String,Object>> metircs = tsdbUtils.fetchRecords(fieldQuery);
+
+                            for (int j = 0; j < fields.size(); j++) {
+                                if(!"time".equals(fields.get(j))){
+                                    List<Map<String,Object>> metircItems = new ArrayList<>();
+                                    MetricVO metricVO = new MetricVO();
+                                    metricVO.setMetric(fields.get(j));
+                                    if(CollectionUtils.isNotEmpty(metircs)){
+                                        for (int k = 0; k < metircs.size(); k++) {
+                                            String field = fields.get(j);
+                                            Map<String,Object> map = new HashMap<>();
+                                            map.put("timestamp",metircs.get(k).get("time"));
+                                            map.put("value",metircs.get(k).get(field));
+                                            metircItems.add(map);
+                                        }
+                                        metricVO.setMetricItems(metircItems);
                                     }
-
+                                    metricList.add(metricVO);
                                 }
-                                metricVO.setMetricItems(metircItems);
-                                metricList.add(metricVO);
+
                             }
 
                         }
-
+                        resultVO.setMetrics(metricList);
                     }
                 }
-                HistorysInnerResultVO resultVO = new HistorysInnerResultVO();
-                resultVO.setDeviceuuid(deviceUUIds.get(i));
-                resultVO.setTags(tag.get(0));
-                resultVO.setMetrics(metricList);
                 list.add(resultVO);
             }
         }