NoWarnDataJob.java
12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package com.skua.modules.alarm.quartz;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.skua.core.util.chuanglan.HttpSenderMsg;
import com.skua.core.util.push.IPushService;
import com.skua.core.util.push.MessageEntity;
import com.skua.core.util.push.PushMessageFactory;
import com.skua.modules.alarm.entity.AlarmMessageHistory;
import com.skua.modules.alarm.entity.AlarmRecordHistory;
import com.skua.modules.alarm.service.AlarmMessageHistoryService;
import com.skua.modules.alarm.service.AlarmRecordHistoryService;
import com.skua.modules.alarm.service.AlarmRuleConfigService;
import com.skua.modules.alarm.vo.AlarmRuleAndAlarmRuleLevelVO;
import com.skua.modules.quartz.util.TimeUtils;
import com.skua.modules.system.entity.SysUser;
import com.skua.modules.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* 采集数据报警
*/
@Slf4j
public class NoWarnDataJob implements Job {
@Autowired
private AlarmRuleConfigService alarmRuleConfigService;
@Autowired
private AlarmRecordHistoryService alarmRecordHistoryService;
@Autowired
private AlarmMessageHistoryService alarmMessageHistoryService;
@Autowired
private ISysUserService sysUserService;
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("********30天异常数据监测服务开启********");
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateNowStr = sdf.format(d);
String nowDateTime = dateNowStr + " 23:59:59";
Calendar now = Calendar.getInstance();
String month = "01";
if((now.get(Calendar.MONTH) + 1)>9){
month = "" + (now.get(Calendar.MONTH) + 1);
}else{
month = "0" + (now.get(Calendar.MONTH) + 1);
}
String lastDateTime = now.get(Calendar.YEAR) +"-"+ month +"-"+ now.get(Calendar.DAY_OF_MONTH) + " 00:00:00";
//30天无报警预警指标告警
//查询需要实时数据报警厂的信息和报警规则字段的集合
List<AlarmRuleAndAlarmRuleLevelVO> list = alarmRuleConfigService.getAlarmList(nowDateTime,lastDateTime);
//查询mysql数据超标警报
sendMessage(list);
System.out.println("********30天异常数据监测服务结束********");
}
private void sendMessage(List<AlarmRuleAndAlarmRuleLevelVO> list) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
List<AlarmRecordHistory> alarmRecordHistoryList = new ArrayList<>();
List<AlarmMessageHistory> alarmMessageHistoryList = new ArrayList<>();
List<String> userList = new ArrayList<>();
//获取需要发送短信的手机号
list.forEach(alarmRuleAndAlarmRuleLevelVO -> {
if(alarmRuleAndAlarmRuleLevelVO.getIsSendSms() == 1) {
String[] userArr = alarmRuleAndAlarmRuleLevelVO.getAlarmSmsRecipient().split(",");
for (int i=0; i < userArr.length;i++){
userList.add(userArr[i]);
}
}
});
QueryWrapper<SysUser> sysUserQueryWrapper = new QueryWrapper<>();
if(userList.size() >0){
sysUserQueryWrapper.in("id",userList);
}
List<SysUser> sysUserList = sysUserService.list(sysUserQueryWrapper);
//用map存放用户信息和手机号信息
Map<String,String> userPhoneMap = new HashMap<>();
Map<String,String> userCidMap = new HashMap<>();
sysUserList.forEach(sysUser -> {
userPhoneMap.put(sysUser.getId(),sysUser.getPhone());
userCidMap.put(sysUser.getId(), sysUser.getCid());
});
list.forEach((alarmRuleAndAlarmRuleLevelVO)->{
//查询上一条报警是否处理,未处理就不新增报警记录
//TODO 先从数据库中获取,后期优化放redis中
QueryWrapper<AlarmRecordHistory> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("alarm_rule_level_id",alarmRuleAndAlarmRuleLevelVO.getAlarmRuleLevelId());
queryWrapper.orderByDesc("record_time").last("limit 1");
AlarmRecordHistory alarmRecordHistoryOne = alarmRecordHistoryService.getOne(queryWrapper);
//获取上次信息发送的时间,如果满足则发送
//TODO 先从数据库中获取,后期优化方redis中
QueryWrapper<AlarmMessageHistory> alarmMessageHistoryQueryWrapper = new QueryWrapper<>();
alarmMessageHistoryQueryWrapper.eq("alarm_rule_level_id",alarmRuleAndAlarmRuleLevelVO.getAlarmRuleLevelId());
alarmMessageHistoryQueryWrapper.eq("alarm_send_type","1");
alarmMessageHistoryQueryWrapper.orderByDesc("alarm_send_time").last("limit 1");
AlarmMessageHistory messageHistoryOne = alarmMessageHistoryService.getOne(alarmMessageHistoryQueryWrapper);
String msgContent = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+","+alarmRuleAndAlarmRuleLevelVO.getFactoryName()+
"采集数据:"+alarmRuleAndAlarmRuleLevelVO.getAlarmParamName()+" 距当前时间30天无预警记录,请提高警惕!";
Date lastTime = null;
//如果数据库没有上次时间,则设置一个默认时间
if(messageHistoryOne != null){
lastTime = messageHistoryOne.getAlarmSendTime();
}else{
try {
lastTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2020-01-01 00:00:00");
} catch (ParseException e) {
e.printStackTrace();
}
}
//如果没有上条记录或者处理状态(0:未处理,1:已处理)为已处理
if(alarmRecordHistoryOne == null || alarmRecordHistoryOne.getHandleStatus() == 1){
AlarmRecordHistory alarmRecordHistory = new AlarmRecordHistory();
//手动创建UUID,用于报警自动送审
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
alarmRecordHistory.setId(uuid);
alarmRecordHistory.setAlarmParamName(alarmRuleAndAlarmRuleLevelVO.getAlarmParamName());
alarmRecordHistory.setAlarmRuleLevelId(alarmRuleAndAlarmRuleLevelVO.getAlarmRuleLevelId());
alarmRecordHistory.setAlarmRuleType(alarmRuleAndAlarmRuleLevelVO.getAlarmRuleType());
alarmRecordHistory.setAlarmParamType(alarmRuleAndAlarmRuleLevelVO.getAlarmParamType());
alarmRecordHistory.setRecordTime(new Date());
alarmRecordHistory.setAlarmParamUnit(alarmRuleAndAlarmRuleLevelVO.getAlarmParamUnit());
alarmRecordHistory.setFactoryId(alarmRuleAndAlarmRuleLevelVO.getFactoryId());
alarmRecordHistory.setHandleStatus(0);
alarmRecordHistory.setAlarmContent(msgContent);
alarmRecordHistoryList.add(alarmRecordHistory);
}
//判断是否需要发送短信,1表示发送
if(alarmRuleAndAlarmRuleLevelVO.getIsSendSms() == 1){
//判读是否在发送时间内
Date allowStartDate = null;
Date allowEndDate = null;
Date nowDate = null;
try {
allowEndDate = simpleDateFormat.parse(alarmRuleAndAlarmRuleLevelVO.getSmsAllowSendTime().split("-")[1]);
allowStartDate = simpleDateFormat.parse(alarmRuleAndAlarmRuleLevelVO.getSmsAllowSendTime().split("-")[0]);
nowDate = simpleDateFormat.parse(simpleDateFormat.format(new Date()));
} catch (ParseException e) {
e.printStackTrace();
}
//如果在发送时间内,则发送
if(isEffectiveDate(nowDate,allowStartDate,allowEndDate )){
//判断是否满足短信发送频率,将两个时间转为毫秒
Long lastTimeLong = lastTime.getTime();
Long nowDateLong = new Date().getTime();
if(nowDateLong - lastTimeLong > Double.parseDouble(alarmRuleAndAlarmRuleLevelVO.getSmsSendFrequency())*60*60*1000){
//获取报警接收人的手机号
StringBuilder phoneStringBuilder = new StringBuilder();
String[] userArr = alarmRuleAndAlarmRuleLevelVO.getAlarmSmsRecipient().split(",");
for (int i=0; i < userArr.length;i++){
// phoneStringBuilder.append(",");
// phoneStringBuilder.append(userPhoneMap.get(userArr[i]));
//发送短信
// HttpSenderMsg.sendMsg(msgContent, userPhoneMap.get(userArr[i]));
//移动端消息推送
MessageEntity messageEntity = new MessageEntity();
messageEntity.setMessageTitle("实时数据异常提示");
messageEntity.setMessageBody(msgContent);
List<String> cidList = new ArrayList<String>();
String cid = userCidMap.get(userArr[i]);
if(!StringUtils.isEmpty(cid)) {
cidList.add(cid);
messageEntity.setReceiveUserCid(cidList);
IPushService appPushService = PushMessageFactory.getPushService("MOB");
appPushService.pushMessage(messageEntity);
}
}
// String phone = phoneStringBuilder.toString().replaceFirst(",","");
//将发送的短信保存到数据库
AlarmMessageHistory alarmMessageHistory = new AlarmMessageHistory();
alarmMessageHistory.setAlarmRuleLevelId(alarmRuleAndAlarmRuleLevelVO.getAlarmRuleLevelId());
alarmMessageHistory.setAlarmSendUser(alarmRuleAndAlarmRuleLevelVO.getAlarmSmsRecipient());
// alarmMessageHistory.setAlarmSendPhone(phone);
alarmMessageHistory.setAlarmSendTime(new Date());
alarmMessageHistory.setAlarmSendContent(msgContent);
alarmMessageHistory.setAlarmParamName(alarmRuleAndAlarmRuleLevelVO.getAlarmParamName());
alarmMessageHistory.setAlarmRuleLevelId(alarmRuleAndAlarmRuleLevelVO.getAlarmRuleLevelId());
alarmMessageHistory.setAlarmSendType(1);
alarmMessageHistory.setAlarmRuleType(alarmRuleAndAlarmRuleLevelVO.getAlarmRuleType());
alarmMessageHistory.setAlarmParamType(alarmRuleAndAlarmRuleLevelVO.getAlarmParamType());
alarmMessageHistory.setAlarmParamUnit(alarmRuleAndAlarmRuleLevelVO.getAlarmParamUnit());
alarmMessageHistory.setFactoryId(alarmRuleAndAlarmRuleLevelVO.getFactoryId());
alarmMessageHistoryList.add(alarmMessageHistory);
}
}
}
});
alarmMessageHistoryService.saveBatch(alarmMessageHistoryList);
alarmRecordHistoryService.saveBatch(alarmRecordHistoryList);
}
/**
* 判断当前时间是否在[startTime, endTime]区间,注意时间格式要一致
*
* @param nowTime 当前时间
* @param startTime 开始时间
* @param endTime 结束时间
* @return
* @author xiechao
*/
public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
if (nowTime.getTime() == startTime.getTime()
|| nowTime.getTime() == endTime.getTime()) {
return true;
}
Calendar date = Calendar.getInstance();
date.setTime(nowTime);
Calendar begin = Calendar.getInstance();
begin.setTime(startTime);
Calendar end = Calendar.getInstance();
end.setTime(endTime);
if (date.after(begin) && date.before(end)) {
return true;
} else {
return false;
}
}
}