|
@@ -15,6 +15,7 @@ import com.usky.demo.service.rocketmq.MyProducer;
|
|
|
import com.usky.demo.service.utils.TsdbUtils;
|
|
|
import com.usky.demo.service.vo.DeviceMapVO;
|
|
|
import com.usky.demo.service.vo.ProductMapVO;
|
|
|
+import org.apache.commons.collections.ResettableListIterator;
|
|
|
import org.influxdb.dto.QueryResult;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.cache.annotation.Cacheable;
|
|
@@ -43,41 +44,6 @@ public class QueryInfluxdbDataServiceImpl extends AbstractCrudService<QueryInflu
|
|
|
@Autowired
|
|
|
private DmpDeviceService dmpDeviceService;
|
|
|
|
|
|
- @Resource
|
|
|
- private MyProducer myProducer;
|
|
|
-
|
|
|
-// @Override
|
|
|
-// public Map<String,Object> sendDeviceData(DeviceDataWriteVO writeVO){
|
|
|
-// Map<String,Object> rec_map = new HashMap<>();
|
|
|
-// DeviceDataInfoVO dataInfo = new DeviceDataInfoVO();
|
|
|
-// Map<String,Object> metrics = writeVO.getMetrics();
|
|
|
-// Map<String,String> tags = writeVO.getTags();
|
|
|
-// if(metrics.size() > 0){
|
|
|
-// Map<String,Object> mp = new HashMap<>();
|
|
|
-// for(Map.Entry<String,Object> map:metrics.entrySet()){
|
|
|
-// mp.put(map.getKey(),map.getValue());
|
|
|
-// }
|
|
|
-// dataInfo.setMetrics(mp);
|
|
|
-// if(tags != null && tags.size() > 0){
|
|
|
-// mp.clear();
|
|
|
-// for(Map.Entry<String,String> map:tags.entrySet()){
|
|
|
-// mp.put(map.getKey(),map.getValue());
|
|
|
-// }
|
|
|
-// dataInfo.setTags(mp);
|
|
|
-// }else{
|
|
|
-// dataInfo.setTags(new HashMap<>());
|
|
|
-// }
|
|
|
-// dataInfo.setProduct_code(writeVO.getProductCode());
|
|
|
-// dataInfo.setDevice_id(writeVO.getDeviceId());
|
|
|
-// dataInfo.setTimestamp(writeVO.getTimestamp());
|
|
|
-//
|
|
|
-// myProducer.sendMessage("data-collector", JSONArray.toJSON(dataInfo).toString());
|
|
|
-// }
|
|
|
-//
|
|
|
-// rec_map.put("code",200);
|
|
|
-// rec_map.put("message","操作成功!");
|
|
|
-// return rec_map;
|
|
|
-// }
|
|
|
|
|
|
@Cacheable(cacheNames = "productList",sync = true)
|
|
|
public Map<String, ProductMapVO> getProductMap(){
|
|
@@ -128,12 +94,26 @@ public class QueryInfluxdbDataServiceImpl extends AbstractCrudService<QueryInflu
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public LastResultVO queryLastDeviceData(String productCode, String deviceId){
|
|
|
+ public LastResultVO queryLastDeviceData(String deviceUUId){
|
|
|
|
|
|
- String tableName = productCode + "_" + deviceId;
|
|
|
+ String tableName = deviceUUId;
|
|
|
String query = "SELECT *::field FROM \""+tableName+"\" order by desc limit 1 tz('Asia/Shanghai')";
|
|
|
List<Map<String, Object>> metrics = tsdbUtils.fetchRecords(query);
|
|
|
- LastResultVO resultVO = new LastResultVO(deviceId,metrics);
|
|
|
+ String tagQuery = "SELECT \"deviceid\",*::field FROM \""+tableName+"\" order by desc limit 1 tz('Asia/Shanghai')";
|
|
|
+ List<Map<String, String>> tag = tsdbUtils.fetchTagRecords(tagQuery);
|
|
|
+ LastResultVO resultVO = new LastResultVO();
|
|
|
+ if(tag.size() == 0){
|
|
|
+ Map<String,String> tagMap = new HashMap<>();
|
|
|
+ tagMap.put("deviceId","");
|
|
|
+ tag.add(tagMap);
|
|
|
+ resultVO.setDeviceUUId(deviceUUId);
|
|
|
+ resultVO.setTags(tag);
|
|
|
+ resultVO.setMetrics(metrics);
|
|
|
+ }else{
|
|
|
+ resultVO.setDeviceUUId(deviceUUId);
|
|
|
+ resultVO.setTags(tag);
|
|
|
+ resultVO.setMetrics(metrics);
|
|
|
+ }
|
|
|
|
|
|
return resultVO;
|
|
|
}
|
|
@@ -141,14 +121,18 @@ public class QueryInfluxdbDataServiceImpl extends AbstractCrudService<QueryInflu
|
|
|
@Override
|
|
|
public List<LastResultVO> queryLastDeviceData(LastRequestVO requestVO){
|
|
|
List<LastResultVO> result = new ArrayList<>();
|
|
|
- String productCode = requestVO.getProductCode();
|
|
|
- List<String> deviceIds = requestVO.getDeviceId();
|
|
|
- if(CollectionUtils.isNotEmpty(deviceIds)){
|
|
|
- for (int i = 0; i < deviceIds.size(); i++) {
|
|
|
- String tableName = productCode + "_" + deviceIds.get(i);
|
|
|
+ List<String> deviceUUIds = requestVO.getDeviceUUId();
|
|
|
+ if(CollectionUtils.isNotEmpty(deviceUUIds)){
|
|
|
+ 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>> metrics = tsdbUtils.fetchRecords(query);
|
|
|
- LastResultVO resultVO = new LastResultVO(deviceIds.get(i),metrics);
|
|
|
+ String tagQuery = "SELECT \"deviceid\",*::field FROM \""+tableName+"\" order by desc limit 1 tz('Asia/Shanghai')";
|
|
|
+ List<Map<String, String>> tag = tsdbUtils.fetchTagRecords(tagQuery);
|
|
|
+ LastResultVO resultVO = new LastResultVO();
|
|
|
+ resultVO.setDeviceUUId(deviceUUIds.get(i));
|
|
|
+ resultVO.setTags(tag);
|
|
|
+ resultVO.setMetrics(metrics);
|
|
|
result.add(resultVO);
|
|
|
}
|
|
|
}
|
|
@@ -158,11 +142,13 @@ public class QueryInfluxdbDataServiceImpl extends AbstractCrudService<QueryInflu
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public HistoryResultVO queryHistoryDeviceData(String productCode,String deviceId,String startTime,String endTime){
|
|
|
+ public HistoryResultVO queryHistoryDeviceData(String deviceUUId,String startTime,String endTime){
|
|
|
List<MetricVO> metricList = new ArrayList<>();
|
|
|
- String tableName = productCode + "_" + deviceId;
|
|
|
+ String tableName = deviceUUId;
|
|
|
String query = "SELECT *::field FROM \""+tableName+"\" order by desc limit 1 tz('Asia/Shanghai')";
|
|
|
QueryResult queryResult = tsdbUtils.query(query);
|
|
|
+ String tagQuery = "SELECT \"deviceid\",*::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)){
|
|
@@ -180,7 +166,9 @@ public class QueryInfluxdbDataServiceImpl extends AbstractCrudService<QueryInflu
|
|
|
map.put("value",metircs.get(k).get(field));
|
|
|
metircItems.add(map);
|
|
|
}
|
|
|
- MetricVO metricVO = new MetricVO(fields.get(j),metircItems);
|
|
|
+ MetricVO metricVO = new MetricVO();
|
|
|
+ metricVO.setMetric(fields.get(j));
|
|
|
+ metricVO.setMetricItems(metircItems);
|
|
|
metricList.add(metricVO);
|
|
|
}
|
|
|
|
|
@@ -190,22 +178,26 @@ public class QueryInfluxdbDataServiceImpl extends AbstractCrudService<QueryInflu
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- HistoryResultVO resultVO = new HistoryResultVO(deviceId,metricList);
|
|
|
+ HistoryResultVO resultVO = new HistoryResultVO();
|
|
|
+ resultVO.setDeviceUUId(deviceUUId);
|
|
|
+ resultVO.setTags(tag);
|
|
|
+ resultVO.setMetrics(metricList);
|
|
|
return resultVO;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<HistoryResultVO> queryHistoryDeviceData(HistoryRequestVO requestVO){
|
|
|
List<HistoryResultVO> list = new ArrayList<>();
|
|
|
- String productCode = requestVO.getProductCode();
|
|
|
- List<String> deviceIds = requestVO.getDeviceId();
|
|
|
+ List<String> deviceIds = requestVO.getDeviceUUId();
|
|
|
String startTime = requestVO.getStartTime();
|
|
|
String endTime = requestVO.getEndTime();
|
|
|
for (int i = 0; i < deviceIds.size(); i++) {
|
|
|
List<MetricVO> metricList = new ArrayList<>();
|
|
|
- String tableName = productCode + "_" + deviceIds.get(i);
|
|
|
+ String tableName = deviceIds.get(i);
|
|
|
String query = "SELECT *::field FROM \""+tableName+"\" order by desc limit 1 tz('Asia/Shanghai')";
|
|
|
QueryResult queryResult = tsdbUtils.query(query);
|
|
|
+ String tagQuery = "SELECT \"deviceid\",*::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)){
|
|
@@ -223,7 +215,9 @@ public class QueryInfluxdbDataServiceImpl extends AbstractCrudService<QueryInflu
|
|
|
map.put("value",metircs.get(k).get(field));
|
|
|
metircItems.add(map);
|
|
|
}
|
|
|
- MetricVO metricVO = new MetricVO(fields.get(j),metircItems);
|
|
|
+ MetricVO metricVO = new MetricVO();
|
|
|
+ metricVO.setMetric(fields.get(j));
|
|
|
+ metricVO.setMetricItems(metircItems);
|
|
|
metricList.add(metricVO);
|
|
|
}
|
|
|
|
|
@@ -233,7 +227,10 @@ public class QueryInfluxdbDataServiceImpl extends AbstractCrudService<QueryInflu
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- HistoryResultVO resultVO = new HistoryResultVO(deviceIds.get(i),metricList);
|
|
|
+ HistoryResultVO resultVO = new HistoryResultVO();
|
|
|
+ resultVO.setDeviceUUId(deviceIds.get(i));
|
|
|
+ resultVO.setTags(tag);
|
|
|
+ resultVO.setMetrics(metricList);
|
|
|
list.add(resultVO);
|
|
|
}
|
|
|
|
|
@@ -241,117 +238,4 @@ public class QueryInfluxdbDataServiceImpl extends AbstractCrudService<QueryInflu
|
|
|
}
|
|
|
|
|
|
|
|
|
- @Override
|
|
|
- public LastResultVO queryOuterLastDeviceData(String productCode, String deviceId){
|
|
|
-
|
|
|
- String tableName = productCode + "_" + deviceId;
|
|
|
- String query = "SELECT *::field FROM \""+tableName+"\" order by desc limit 1 tz('Asia/Shanghai')";
|
|
|
- List<Map<String, Object>> metrics = tsdbUtils.fetchRecords(query);
|
|
|
- LastResultVO resultVO = new LastResultVO(deviceId,metrics);
|
|
|
-
|
|
|
- return resultVO;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public List<LastResultVO> queryOuterLastDeviceData(LastRequestVO requestVO){
|
|
|
- List<LastResultVO> result = new ArrayList<>();
|
|
|
- String productCode = requestVO.getProductCode();
|
|
|
- List<String> deviceIds = requestVO.getDeviceId();
|
|
|
- if(CollectionUtils.isNotEmpty(deviceIds)){
|
|
|
- for (int i = 0; i < deviceIds.size(); i++) {
|
|
|
- String tableName = productCode + "_" + deviceIds.get(i);
|
|
|
- String query = "SELECT *::field FROM \""+tableName+"\" order by desc limit 1 tz('Asia/Shanghai')";
|
|
|
- List<Map<String, Object>> metrics = tsdbUtils.fetchRecords(query);
|
|
|
- LastResultVO resultVO = new LastResultVO(deviceIds.get(i),metrics);
|
|
|
- result.add(resultVO);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return result;
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public HistoryResultVO queryOuterHistoryDeviceData(String productCode,String deviceId,String startTime,String endTime){
|
|
|
- List<MetricVO> metricList = new ArrayList<>();
|
|
|
- String tableName = productCode + "_" + deviceId;
|
|
|
- String query = "SELECT *::field FROM \""+tableName+"\" order by desc limit 1 tz('Asia/Shanghai')";
|
|
|
- QueryResult queryResult = tsdbUtils.query(query);
|
|
|
- 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);
|
|
|
-
|
|
|
- if(CollectionUtils.isNotEmpty(metircs)){
|
|
|
- for (int j = 0; j < fields.size(); j++) {
|
|
|
- List<Map<String,Object>> metircItems = new ArrayList<>();
|
|
|
- if(!"time".equals(fields.get(j))){
|
|
|
- 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 metricVO = new MetricVO(fields.get(j),metircItems);
|
|
|
- metricList.add(metricVO);
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- HistoryResultVO resultVO = new HistoryResultVO(deviceId,metricList);
|
|
|
- return resultVO;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public List<HistoryResultVO> queryOuterHistoryDeviceData(HistoryRequestVO requestVO){
|
|
|
- List<HistoryResultVO> list = new ArrayList<>();
|
|
|
- String productCode = requestVO.getProductCode();
|
|
|
- List<String> deviceIds = requestVO.getDeviceId();
|
|
|
- String startTime = requestVO.getStartTime();
|
|
|
- String endTime = requestVO.getEndTime();
|
|
|
- for (int i = 0; i < deviceIds.size(); i++) {
|
|
|
- List<MetricVO> metricList = new ArrayList<>();
|
|
|
- String tableName = productCode + "_" + deviceIds.get(i);
|
|
|
- String query = "SELECT *::field FROM \""+tableName+"\" order by desc limit 1 tz('Asia/Shanghai')";
|
|
|
- QueryResult queryResult = tsdbUtils.query(query);
|
|
|
- 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);
|
|
|
-
|
|
|
- if(CollectionUtils.isNotEmpty(metircs)){
|
|
|
- for (int j = 0; j < fields.size(); j++) {
|
|
|
- List<Map<String,Object>> metircItems = new ArrayList<>();
|
|
|
- if(!"time".equals(fields.get(j))){
|
|
|
- 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 metricVO = new MetricVO(fields.get(j),metircItems);
|
|
|
- metricList.add(metricVO);
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- HistoryResultVO resultVO = new HistoryResultVO(deviceIds.get(i),metricList);
|
|
|
- list.add(resultVO);
|
|
|
- }
|
|
|
-
|
|
|
- return list;
|
|
|
- }
|
|
|
-
|
|
|
}
|