定时任务优化

This commit is contained in:
zhuxinyu
2023-05-05 16:20:42 +08:00
parent 69ecd92564
commit 128eea3354
2 changed files with 68 additions and 45 deletions

View File

@@ -21,7 +21,7 @@ public class DeviceAbnormalStatisticsJob {
@XxlJob("deviceAbnormalStatisticsJob")
public void deviceAbnormalStatisticsJob () {
String time = LocalDateTime.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
String time = LocalDateTime.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
log.info("执行日期deviceAbnormalStatisticsJob===============>"+time);
DeviceAbnormaStatisticsParam param =new DeviceAbnormaStatisticsParam();
param.setTime(time);

View File

@@ -135,26 +135,19 @@ public class DeviceAbnormaStatisticsServiceImpl implements DeviceAbnormalStatist
topMsg.setComOutCount(abnormals.size());
List<String> abnormalDesc = abnormals.stream().map(Communicate::getDesc).collect(Collectors.toList());
topMsg.setComOutDescription(String.join(",",abnormalDesc));
} else {
try {
if (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(devEntry.getValue().get(0).getUpdateTime()).after(DateUtil.endOfDay(DateUtil.parse(param.getTime())))) {
topMsg.setOfftimeFlag(0);
topMsg.setComOutCount(1);
topMsg.setComOutDescription(DateUtil.beginOfDay(DateUtil.parse(param.getTime()))+""+devEntry.getValue().get(0).getUpdateTime());
} else {
topMsg.setOfftimeFlag(1);
topMsg.setComOutCount(0);
}
} catch (ParseException e) {
log.error("ParseException "+e);
}
}
if (topMsg.getFlowFlag() ==0 || topMsg.getIntegrityFlag() ==0 || topMsg.getOfftimeFlag() == 0
|| topMsg.getAlarmCountFlag() ==0 || topMsg.getOnlineRateFlag() ==0) {
topMsgPOS.add(topMsg);
}
}
for (TopMsgPO topMsgPO : topMsgPOS) {
System.out.println(topMsgPO);
}
return deviceAbnormaStatisticsMapper.insertTopMsg(topMsgPOS);
}
@@ -174,12 +167,11 @@ public class DeviceAbnormaStatisticsServiceImpl implements DeviceAbnormalStatist
QueryResult result = influxDbUtils.query(sql);
InfluxDBResultMapper influxDBResultMapper = new InfluxDBResultMapper();
List<Communicate> communicates = influxDBResultMapper.toPOJO(result, Communicate.class);
// 通讯状态统计结果集
List<TopMsgPO> comMsgs = new ArrayList<>();
if (CollectionUtil.isEmpty(communicates)) {
return comMsgs;
}
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Map<String, List<Communicate>> comMap = communicates.stream().collect(Collectors.groupingBy(Communicate::getDevId));
if (CollectionUtil.isNotEmpty(communicates)) {
for (Map.Entry<String, List<Communicate>> entry : comMap.entrySet()) {
String key = entry.getKey();
List<Communicate> value = entry.getValue();
@@ -215,6 +207,37 @@ public class DeviceAbnormaStatisticsServiceImpl implements DeviceAbnormalStatist
topMsgPO.setCommunicates(comOuts);
comMsgs.add(topMsgPO);
}
}
List<String> coms = new ArrayList<>(comMap.keySet());
// 计算influxdb中当天无记录装置id
List<String> extraComs = devs.stream().filter(t -> !coms.contains(t)).collect(Collectors.toList());
if (CollectionUtil.isNotEmpty(extraComs)) {
for (String devId : extraComs) {
TopMsgPO topMsgPO = new TopMsgPO();
List<Communicate> comOuts = new ArrayList<>();
topMsgPO.setDevId(devId);
// 组装sql语句
StringBuilder builder = new StringBuilder();
builder.append("dev_id ='").append(devId).append("'");
builder.append(" AND time <= '").append(DateUtil.beginOfDay(DateUtil.parse(time))).append("'").append(" order by time DESC ").append("limit 1 ");
String sql1 = "select * from " + InfluxDBPublicParam.PQS_COMMUNICATE + " where "+builder.toString() + InfluxDBPublicParam.TIME_ZONE;
// 获取暂降事件
QueryResult result1 = influxDbUtils.query(sql1);
InfluxDBResultMapper influxDBResultMapper1 = new InfluxDBResultMapper();
List<Communicate> earlyData = influxDBResultMapper1.toPOJO(result1, Communicate.class);
Communicate early = earlyData.get(0);
Communicate communicate = new Communicate();
if (early.getType() == 0) {
int durationTime = (int) (DateUtil.endOfDay(DateUtil.parse(time)).getTime() - Date.from(early.getUpdateTime()).getTime()) / 1000 / 60;
communicate.setDuration(durationTime);
communicate.setDesc(formatter.format(Date.from(early.getUpdateTime())) + "" + DateUtil.endOfDay(DateUtil.parse(time)));
comOuts.add(communicate);
}
topMsgPO.setCommunicates(comOuts);
comMsgs.add(topMsgPO);
}
}
return comMsgs;
}
}