|
@@ -120,7 +120,7 @@ public class MonitorDataServiceImpl implements MonitorDataService , Initializing
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public List<DayReportEntity> getMonitorDataGroupByHour(LocalDateTime dateTime ){
|
|
|
+ public List<DayReportEntity> getMonitorDataGroupByHour(LocalDateTime dateTime ,List<Integer> deviceIds ){
|
|
|
|
|
|
if(dateTime == null ){
|
|
|
return null;
|
|
@@ -130,6 +130,9 @@ public class MonitorDataServiceImpl implements MonitorDataService , Initializing
|
|
|
criteria.and("month").is(dateTime.getMonthValue());
|
|
|
criteria.and("day").is(dateTime.getDayOfMonth());
|
|
|
criteria.and("hour").is(dateTime.getHour());
|
|
|
+ if(deviceIds!=null && deviceIds.size()>0){
|
|
|
+ criteria.and("deviceId").in(deviceIds);
|
|
|
+ }
|
|
|
AggregationOptions aggregationOptions = AggregationOptions.builder().allowDiskUse(true).build();
|
|
|
Aggregation agg = Aggregation.newAggregation(
|
|
|
Aggregation.match(criteria), //查询条件
|
|
@@ -153,60 +156,59 @@ public class MonitorDataServiceImpl implements MonitorDataService , Initializing
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public List<DayReportEntity> getMonitorDataReportByHour(){
|
|
|
+ public void getMonitorDataReportByHour(){
|
|
|
//取前一个小时的时间
|
|
|
LocalDateTime dateTime = LocalDateTime.now().plusHours(-1);
|
|
|
|
|
|
- List<DayReportEntity> hourDatas = getMonitorDataGroupByHour(dateTime);
|
|
|
- List<DayReportEntity> lastHourDatas = getMonitorDataGroupByHour(dateTime.plusHours(-1));
|
|
|
+ List<DayReportEntity> hourDatas = getMonitorDataGroupByHour(dateTime,null);
|
|
|
+ List<DayReportEntity> lastHourDatas = getMonitorDataGroupByHour(dateTime.plusHours(-1),null);
|
|
|
|
|
|
Map<String,DayReportEntity> lastHourDataMap = lastHourDatas.stream().collect(Collectors.toMap(DayReportEntity::getMapkey, a -> a,(k1, k2)->k1));
|
|
|
//保存日报表数据
|
|
|
- if(hourDatas.size()>0){
|
|
|
+ saveReportDataByHour(hourDatas, lastHourDataMap);
|
|
|
+
|
|
|
+ //设备id
|
|
|
+ List<Integer> deviceIds = lastHourDataMap.values().stream().map(d -> d.getDeviceId().intValue()).distinct().collect(Collectors.toList());
|
|
|
+ //补数据,前6小时没有统计数据的
|
|
|
+ if(deviceIds.size()>0){
|
|
|
+ for(int i=1;i<7;i++){
|
|
|
+ dateTime = LocalDateTime.now().plusHours(-i);
|
|
|
+ //前几小时有统计数据的设备id
|
|
|
+ List<Integer> deviceIdsIsExit = monitorDataMapper.checkReportDataExit(dateTime.getYear(),dateTime.getMonthValue(),dateTime.getDayOfMonth(),dateTime.getHour(),deviceIds);
|
|
|
+ //需要补充数据的设备
|
|
|
+ deviceIds = deviceIds.stream().filter(deviceId -> !deviceIdsIsExit.contains(deviceId)).collect(Collectors.toList());
|
|
|
+
|
|
|
+ //查询需要补充数据的设备数据信息
|
|
|
+ hourDatas = new ArrayList<>(lastHourDataMap.values());
|
|
|
+
|
|
|
+ lastHourDatas = getMonitorDataGroupByHour(dateTime.plusHours(-i),deviceIds);
|
|
|
+ lastHourDataMap = lastHourDatas.stream().collect(Collectors.toMap(DayReportEntity::getMapkey, a -> a,(k1, k2)->k1));
|
|
|
+ //保存日报表数据
|
|
|
+ saveReportDataByHour(hourDatas, lastHourDataMap);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ //保存日报表数据
|
|
|
+ private void saveReportDataByHour(List<DayReportEntity> hourDatas, Map<String, DayReportEntity> lastHourDataMap) {
|
|
|
+ if (hourDatas.size() > 0) {
|
|
|
//计算累计值
|
|
|
- for(DayReportEntity dayReportEntity : hourDatas){
|
|
|
- if(dayReportEntity.getLatestValue()!=null && lastHourDataMap.containsKey(dayReportEntity.getMapkey())){
|
|
|
+ for (DayReportEntity dayReportEntity : hourDatas) {
|
|
|
+ if (dayReportEntity.getLatestValue() != null && lastHourDataMap.containsKey(dayReportEntity.getMapkey())) {
|
|
|
//上一个小时有值
|
|
|
- if( lastHourDataMap.get(dayReportEntity.getMapkey()).getLatestValue() != null){
|
|
|
+ if (lastHourDataMap.get(dayReportEntity.getMapkey()).getLatestValue() != null) {
|
|
|
dayReportEntity.setSumValue(new BigDecimal(dayReportEntity.getLatestValue().toString()).subtract(new BigDecimal(lastHourDataMap.get(dayReportEntity.getMapkey()).getLatestValue().toString())).doubleValue());
|
|
|
- }else if(dayReportEntity.getFirstValue() != null){//上一个小时没有值,取本小时的初始值
|
|
|
- dayReportEntity.setSumValue(new BigDecimal(dayReportEntity.getLatestValue().toString()).subtract(new BigDecimal(dayReportEntity.getFirstValue().toString())).doubleValue());
|
|
|
+ } else {
|
|
|
+ if (dayReportEntity.getFirstValue() != null) {//上一个小时没有值,取本小时的初始值
|
|
|
+ dayReportEntity.setSumValue(new BigDecimal(dayReportEntity.getLatestValue().toString()).subtract(new BigDecimal(dayReportEntity.getFirstValue().toString())).doubleValue());
|
|
|
+ }
|
|
|
+ lastHourDataMap.remove(dayReportEntity.getMapkey());
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|
|
|
monitorDataMapper.batchInsertDayReport(hourDatas);
|
|
|
}
|
|
|
- return hourDatas;
|
|
|
-
|
|
|
-// Map<String,DayReportEntity> hourDataMap = hourDatas.stream().collect(Collectors.toMap(DayReportEntity::getMapkey, a -> a,(k1, k2)->k1));
|
|
|
-// //需要统计的属性值(租户——场景——设备——属性)
|
|
|
-// List<DayReportEntity> dayReportEntities = monitorDataMapper.getReportBaseInfo();
|
|
|
-// //保存日报表数据
|
|
|
-// if(hourDatas.size()>0 && dayReportEntities.size()>0){
|
|
|
-// //填充数据
|
|
|
-//
|
|
|
-// dayReportEntities = dayReportEntities.stream()
|
|
|
-// .filter(d ->StringUtils.isNotBlank(d.getMapkey()) && hourDataMap.containsKey(d.getMapkey()))
|
|
|
-// .map(dayReportEntity ->{
|
|
|
-// DayReportEntity hourData = hourDataMap.get(dayReportEntity.getMapkey());
|
|
|
-// dayReportEntity.setYear(hourData.getYear());
|
|
|
-// dayReportEntity.setMonth(hourData.getMonth());
|
|
|
-// dayReportEntity.setDay(hourData.getDay());
|
|
|
-// dayReportEntity.setHour(hourData.getHour());
|
|
|
-// dayReportEntity.setMinValue(hourData.getMinValue());
|
|
|
-// dayReportEntity.setMaxValue(hourData.getMaxValue());
|
|
|
-// dayReportEntity.setAvgValue(hourData.getAvgValue());
|
|
|
-// dayReportEntity.setCollectDate(hourData.getCollectDate());
|
|
|
-// dayReportEntity.setLatestValue(hourData.getLatestValue());
|
|
|
-// if(lastHourDataMap.containsKey(dayReportEntity.getMapkey()) && lastHourDataMap.get(dayReportEntity.getMapkey()).getLatestValue() != null){
|
|
|
-// dayReportEntity.setSumValue(new BigDecimal(hourDataMap.get(dayReportEntity.getMapkey()).getLatestValue().toString()).subtract(new BigDecimal(lastHourDataMap.get(dayReportEntity.getMapkey()).getLatestValue().toString())).doubleValue());
|
|
|
-// }
|
|
|
-// return dayReportEntity;
|
|
|
-// }).collect(Collectors.toList());
|
|
|
-// monitorDataMapper.batchInsertDayReport(dayReportEntities);
|
|
|
-// }
|
|
|
-// return dayReportEntities;
|
|
|
}
|
|
|
|
|
|
@Override
|