Commit 0cf773bd authored by caotao's avatar caotao

接口查询慢问题优化

parent 1301c96e
...@@ -7,11 +7,14 @@ import com.yeejoin.amos.boot.module.jxiop.biz.dto.IndicatorsDto; ...@@ -7,11 +7,14 @@ import com.yeejoin.amos.boot.module.jxiop.biz.dto.IndicatorsDto;
import com.yeejoin.amos.boot.module.jxiop.biz.dto.SocialContributionDto; import com.yeejoin.amos.boot.module.jxiop.biz.dto.SocialContributionDto;
import com.yeejoin.amos.boot.module.jxiop.biz.mapper2.SjglZsjZsbtzMapper; import com.yeejoin.amos.boot.module.jxiop.biz.mapper2.SjglZsjZsbtzMapper;
import com.yeejoin.amos.boot.module.jxiop.biz.utils.InfluxDButils; import com.yeejoin.amos.boot.module.jxiop.biz.utils.InfluxDButils;
import com.yeejoin.amos.component.influxdb.InfluxdbUtil;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -20,7 +23,8 @@ public class CommonServiceImpl { ...@@ -20,7 +23,8 @@ public class CommonServiceImpl {
@Autowired @Autowired
InfluxDButils influxDButils; InfluxDButils influxDButils;
@Autowired
InfluxdbUtil influxdbUtil;
@Autowired @Autowired
SjglZsjZsbtzMapper sjglZsjZsbtzMapper; SjglZsjZsbtzMapper sjglZsjZsbtzMapper;
...@@ -33,9 +37,8 @@ public class CommonServiceImpl { ...@@ -33,9 +37,8 @@ public class CommonServiceImpl {
public Double getTotalByIndicatior(String gatewayId,String indicator){ public Double getTotalByIndicatior(String gatewayId,String indicator){
String sql = "SELECT * FROM indicators_"+gatewayId+" where equipmentIndexName='"+indicator+"'"; String sql = "SELECT * FROM indicators_"+gatewayId+" where equipmentIndexName='"+indicator+"'";
Double totalvalue = 0.0; Double totalvalue = 0.0;
List<IndicatorsDto> indicatorsDtoList = influxDButils.getListData(sql,IndicatorsDto.class); List<Map<String,Object>> mapList = influxdbUtil.query(sql);
List<Double> doubleList = indicatorsDtoList.stream().map(indicatorsDto -> Double.parseDouble(indicatorsDto.getValue())).collect(Collectors.toList()); totalvalue =mapList.stream().filter(stringObjectMap -> !ObjectUtils.isEmpty(stringObjectMap.get("value"))).mapToDouble(l->Double.parseDouble((String) l.get("value"))).sum();
totalvalue = doubleList.stream().mapToDouble(Double::doubleValue).sum();
return Double.valueOf(String.format("%.2f",totalvalue)); return Double.valueOf(String.format("%.2f",totalvalue));
} }
/** /**
...@@ -47,9 +50,8 @@ public class CommonServiceImpl { ...@@ -47,9 +50,8 @@ public class CommonServiceImpl {
public Double getAvgvalueByIndicatior(String gatewayId,String indicator){ public Double getAvgvalueByIndicatior(String gatewayId,String indicator){
String sql = "SELECT * FROM indicators_"+gatewayId+" where equipmentIndexName='"+indicator+"'"; String sql = "SELECT * FROM indicators_"+gatewayId+" where equipmentIndexName='"+indicator+"'";
Double avageValue = 0.0; Double avageValue = 0.0;
List<IndicatorsDto> indicatorsDtoList = influxDButils.getListData(sql,IndicatorsDto.class); List<Map<String,Object>> mapList = influxdbUtil.query(sql);
List<Double> doubleList = indicatorsDtoList.stream().map(indicatorsDto -> Double.parseDouble(indicatorsDto.getValue())).collect(Collectors.toList()); avageValue =mapList.stream().filter(stringObjectMap -> !ObjectUtils.isEmpty(stringObjectMap.get("value"))).mapToDouble(l->Double.parseDouble((String) l.get("value"))).average().getAsDouble();
avageValue = doubleList.stream().mapToDouble(Double::doubleValue).average().getAsDouble();
return Double.valueOf(String.format("%.2f",avageValue)); return Double.valueOf(String.format("%.2f",avageValue));
} }
......
package com.yeejoin.amos.boot.module.jxiop.biz.utils; package com.yeejoin.amos.boot.module.jxiop.biz.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.squareup.moshi.Json;
import com.yeejoin.amos.boot.module.jxiop.biz.dto.IndicatorsDto;
import com.yeejoin.amos.component.influxdb.InfluxDbConnection; import com.yeejoin.amos.component.influxdb.InfluxDbConnection;
import com.yeejoin.amos.component.influxdb.InfluxdbUtil;
import org.influxdb.dto.QueryResult; import org.influxdb.dto.QueryResult;
import org.springframework.beans.BeanWrapperImpl; import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -10,11 +15,15 @@ import org.springframework.util.ObjectUtils; ...@@ -10,11 +15,15 @@ import org.springframework.util.ObjectUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Component @Component
public class InfluxDButils { public class InfluxDButils {
@Autowired @Autowired
InfluxDbConnection influxDbConnection; InfluxDbConnection influxDbConnection;
@Autowired
InfluxdbUtil influxdbUtil;
public <T> List<T> getListData(String sql, Class<T> clazz) { public <T> List<T> getListData(String sql, Class<T> clazz) {
List<T> list = new ArrayList<>(); List<T> list = new ArrayList<>();
try { try {
...@@ -53,39 +62,8 @@ public class InfluxDButils { ...@@ -53,39 +62,8 @@ public class InfluxDButils {
public <T> List<T> getListData1(String sql, Class<T> clazz) { public <T> List<T> getListData1(String sql, Class<T> clazz) {
List<T> list = new ArrayList<>(); List<T> list = new ArrayList<>();
try { try {
QueryResult query = influxDbConnection.query(sql); List<Map<String,Object>> mapList = influxdbUtil.query(sql);
List<QueryResult.Result> queryResults = query.getResults(); list=mapList.stream().map(stringObjectMap -> JSONObject.parseObject(JSON.toJSONString(stringObjectMap),clazz)).collect(Collectors.toList());
for (QueryResult.Result result : queryResults) {
List<QueryResult.Series> series = result.getSeries();
if (series == null) {
continue;
}
for (QueryResult.Series serie : series) {
List<List<Object>> values = serie.getValues();
List<String> columns = serie.getColumns();
for (int i = 0; i < values.size(); ++i) {
T object = clazz.newInstance();
BeanWrapperImpl bean = new BeanWrapperImpl(object);
for (int j = 0; j < columns.size(); ++j) {
String k = columns.get(j).replace("last_","");
Object v = values.get(i).get(j);
System.out.println(k);
System.out.println(v);
if ("time".equals(k)) {
continue;
} else {
// if(ObjectUtils.isEmpty(v)){
// bean.setPropertyValue(k, "");
// }else{
bean.setPropertyValue(k, v);
// }
}
}
list.add(object);
}
}
}
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment