XjRelevanceAlarmJob.java
21.8 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package com.skua.modules.quartz;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.Expression;
import com.skua.modules.alarmtmp.entity.AlarmRecordHistory;
import com.skua.modules.alarmtmp.entity.AlarmRuleLevelConfigTemplate;
import com.skua.modules.alarmtmp.service.AlarmMessageHistoryService;
import com.skua.modules.alarmtmp.service.AlarmRecordHistoryService;
import com.skua.modules.alarmtmp.service.IAlarmRuleLevelConfigTemplateService;
import com.skua.modules.alarmtmp.vo.SystemNoticeVO;
import com.skua.core.util.DateUtils;
import com.skua.modules.system.service.IPgService;
import com.skua.modules.system.service.ISysDepartService;
import com.skua.modules.system.service.ISysUserService;
import com.skua.modules.util.SystemNoticeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* @program: skboot
* @description: 关联报警
* @author: xie chao
* @create: 2021-09-03 16:03
*/
@Slf4j
@Component
public class XjRelevanceAlarmJob implements Job {
@Autowired
private IAlarmRuleLevelConfigTemplateService alarmRuleLevelConfigTemplateService;
@Autowired
private ISysDepartService sysDepartService;
@Autowired
private AlarmRecordHistoryService alarmRecordHistoryService;
@Autowired
private AlarmMessageHistoryService alarmMessageHistoryService;
@Autowired
private IPgService pgService;
@Autowired
private ISysUserService sysUserService;
@Autowired
private SystemNoticeUtils systemNoticeUtils;
//${start}采集数据${conent}触发关联报警,请注意!
private static String prefix = "【金控数矿】";
private static String departNameTemplete = "${departName}";
private static String contentTemplete = "${conent}";
private static String suffix = "触发关联报警,请注意!";
//添加到报警记录表,用于移动端审批的使用
private static String tableName = "alarm_record_history";
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
try {
log.info("关联报警任务开始");
//仪表
Map<String, Object> excessAlarmPgList = checkPgRelevanceAlarm();
//设备
Map<String, Object> excessEquipAlarmPgList = checkPgRelevanceAlarmEquip();
//自动恢复
dealStatus(excessAlarmPgList, excessEquipAlarmPgList);
if (!excessAlarmPgList.isEmpty()) {
sendMessage(excessAlarmPgList);
}
if (!excessEquipAlarmPgList.isEmpty()) {
sendMessageEquip(excessEquipAlarmPgList);
}
log.info("关联报警任务开始结束");
} catch (Exception e) {
log.info("关联报警任务异常" + e);
}
}
private void sendMessageEquip(Map<String, Object> excessEquipAlarmPgList) {
List<AlarmRuleLevelConfigTemplate> alarmsList = (List<AlarmRuleLevelConfigTemplate>) excessEquipAlarmPgList.get("alarmsList");
if (alarmsList != null && alarmsList.size() > 0) {
String date = DateUtils.getDate("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
alarmsList.forEach(v -> {
//查询质量报警配置
String departId = v.getDepartId();
String content = "";
String departName = departNameTemplete.replace("${departName}", sysDepartService.queryDepartNameById(departId));
content = contentTemplete.replace("${conent}", v.getYexpression());
//查询短信上一条报警是否处理,未处理就不新增报警记录
//TODO 先从数据库中获取,后期优化放redis中
QueryWrapper<AlarmRecordHistory> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("depart_id", departId);
queryWrapper.eq("alarm_rule_type", "7");
queryWrapper.eq("alarm_rule_level_id", v.getId());
queryWrapper.eq("depart_id", departId);
queryWrapper.orderByDesc("record_time").last("limit 1");
AlarmRecordHistory alarmRecordHistoryOne = alarmRecordHistoryService.getOne(queryWrapper);
//如果没有上条记录或者处理状态(0:未处理,1:已处理)为已处理
if ((alarmRecordHistoryOne == null || alarmRecordHistoryOne.getHandleStatus() == 1)) {
AlarmRecordHistory alarmRecordHistory = new AlarmRecordHistory();
//手动创建UUID,用于报警自动送审
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
alarmRecordHistory.setId(uuid);
alarmRecordHistory.setAlarmRuleLevelName(v.getAlarmTemplateLevelName());
alarmRecordHistory.setAlarmRuleType("7");
alarmRecordHistory.setAlarmParamType(v.getAlarmTemplateName());
alarmRecordHistory.setAlarmRuleLevelId(v.getId());
try {
alarmRecordHistory.setRecordTime(simpleDateFormat1.parse(date));
} catch (ParseException e) {
e.printStackTrace();
}
alarmRecordHistory.setDepartId(departId);
alarmRecordHistory.setActualAvgValue(v.getYexpression());
alarmRecordHistory.setHandleStatus(0);
alarmRecordHistory.setDelFlag(1);
alarmRecordHistory.setAlarmContent(prefix + departName + "采集数据设备" + content + suffix);
// alarmRecordHistory.setTableName(tableName);
alarmRecordHistory.setCreateBy("admin");
alarmRecordHistoryService.save(alarmRecordHistory);
//判断是否需要发送系统通知 (内容跟报警记录信息相同)
if ("1".equals(v.getIsSendNotice())) {
if (StringUtils.isNotBlank(v.getAlarmNoticeRecipient())) {
SystemNoticeVO systemNoticeVO = new SystemNoticeVO(v.getAlarmNoticeRecipient(),
v.getAlarmTemplateLevelName(), departName + "采集数据设备" + content + suffix, departId, "7a'p");
systemNoticeUtils.dealNoticeMessage(systemNoticeVO);
}
}
}
});
}
}
private Map<String, Object> checkPgRelevanceAlarmEquip() {
Map<String, Object> resultList = Maps.newHashMap();
//查询模版中需要关联报警的数据
QueryWrapper<AlarmRuleLevelConfigTemplate> alarmRuleLevelConfigTemplateQueryWrapper = new QueryWrapper<>();
alarmRuleLevelConfigTemplateQueryWrapper.eq("alarm_template_type", "3");
alarmRuleLevelConfigTemplateQueryWrapper.eq("alarm_type", "1");
alarmRuleLevelConfigTemplateQueryWrapper.ne("alarm_template_name", "日均值流量报警配置");
List<AlarmRuleLevelConfigTemplate> alarmRuleLevelConfigTemplateList = alarmRuleLevelConfigTemplateService.list(alarmRuleLevelConfigTemplateQueryWrapper);
if (alarmRuleLevelConfigTemplateList != null && alarmRuleLevelConfigTemplateList.size() > 0) {
//保存厂和关联公式数据
StringBuilder stringBuilder = new StringBuilder();
alarmRuleLevelConfigTemplateList.forEach(v -> {
String expression = v.getExpression().replaceAll(";", ",").replaceAll("-", ",");
stringBuilder.append(expression + ",");
});
Map<String, Object> dataMap = pgService.queryMonitorByXTFeildsEquipTime(stringBuilder.toString());
if (!dataMap.isEmpty()) {
List<AlarmRuleLevelConfigTemplate> alars = Lists.newArrayList();
for (AlarmRuleLevelConfigTemplate alarmRuleLevelConfigTemplate : alarmRuleLevelConfigTemplateList) {
String expressions = alarmRuleLevelConfigTemplate.getExpression();
String zexpressions = alarmRuleLevelConfigTemplate.getZexpression();
String[] expressionsArr = expressions.split(";");
String[] zexpressionsArr = zexpressions.split(";");
String conditions = alarmRuleLevelConfigTemplate.getConditions();
if ("or".equals(conditions)) {
label:
for (int i = 0; i < expressionsArr.length; i++) {
String[] split = expressionsArr[i].split("-");
String stat = split[1];
if (StringUtils.isNotBlank(stat)) {
Integer orDefault = (Integer) dataMap.get(stat);
if (orDefault == 1) {
alarmRuleLevelConfigTemplate.setYexpression(zexpressionsArr[i]);
alars.add(alarmRuleLevelConfigTemplate);
}
} else {
//获取运行点
Integer orDefault = (Integer) dataMap.get(split[0]);
if (orDefault == 0) {
alarmRuleLevelConfigTemplate.setYexpression(zexpressionsArr[i]);
alars.add(alarmRuleLevelConfigTemplate);
}
}
}
} else {
boolean flag = true;
label:
for (int i = 0; i < expressionsArr.length; i++) {
String[] split = expressionsArr[i].split(",");
String stat = split[1];
if (StringUtils.isNotBlank(stat) && !stat.equals("null")) {
Integer orDefault = (Integer) dataMap.get(stat);
if (orDefault == 0) {
flag = false;
break label;
}
} else {
//获取运行点
Integer orDefault = (Integer) dataMap.get(split[0]);
if (orDefault == 1) {
flag = false;
break label;
}
}
}
if (flag) {
//报警
alarmRuleLevelConfigTemplate.setYexpression(zexpressions);
alars.add(alarmRuleLevelConfigTemplate);
}
}
}
if (alars.size() > 0) {
resultList.put("alarmsList", alars);
}
}
}
return resultList;
}
//自动恢复
private void dealStatus(Map<String, Object> excessAlarmPgList, Map<String, Object> excessAlarmPgListEquip) {
List<AlarmRuleLevelConfigTemplate> alarmsList = (List<AlarmRuleLevelConfigTemplate>) excessAlarmPgList.get("alarmsList");
List<AlarmRuleLevelConfigTemplate> alarmsListEquip = (List<AlarmRuleLevelConfigTemplate>) excessAlarmPgListEquip.get("alarmsList");
List<AlarmRuleLevelConfigTemplate> result = Lists.newArrayList();
if (alarmsListEquip != null && alarmsListEquip.size() > 0) {
result.addAll(alarmsListEquip);
}
if (alarmsList != null && alarmsList.size() > 0) {
result.addAll(alarmsList);
}
UpdateWrapper<AlarmRecordHistory> updateWrapper = new UpdateWrapper<>();
if (result != null && result.size() > 0) {
//日均值流量报警配置
List<String> names = Lists.newArrayList();
result.forEach(v -> {
names.add(v.getId());
});
updateWrapper.notIn("alarm_rule_level_id", names);
}
updateWrapper.eq("alarm_rule_type", "7");
updateWrapper.ne("alarm_param_type", "日均值流量报警配置");
updateWrapper.eq("handle_status", 0);
AlarmRecordHistory alarmRecordHistory = new AlarmRecordHistory();
alarmRecordHistory.setHandleUser("admin");
alarmRecordHistory.setHandleTime(DateUtils.getDate());
alarmRecordHistory.setHandleStatus(1);
alarmRecordHistoryService.update(alarmRecordHistory, updateWrapper);
}
private Map<String, Object> checkPgRelevanceAlarm() {
Map<String, Object> resultList = Maps.newHashMap();
//查询模版中需要关联报警的数据
QueryWrapper<AlarmRuleLevelConfigTemplate> alarmRuleLevelConfigTemplateQueryWrapper = new QueryWrapper<>();
alarmRuleLevelConfigTemplateQueryWrapper.eq("alarm_template_type", "3");
alarmRuleLevelConfigTemplateQueryWrapper.eq("alarm_type", "0");
alarmRuleLevelConfigTemplateQueryWrapper.ne("alarm_template_name", "日均值流量报警配置");
List<AlarmRuleLevelConfigTemplate> alarmRuleLevelConfigTemplateList = alarmRuleLevelConfigTemplateService.list(alarmRuleLevelConfigTemplateQueryWrapper);
if (alarmRuleLevelConfigTemplateList != null && alarmRuleLevelConfigTemplateList.size() > 0) {
//保存厂和关联公式数据
StringBuilder stringBuilder = new StringBuilder();
alarmRuleLevelConfigTemplateList.forEach(v -> {
String expression = v.getExpression().replaceAll(";", ",");
stringBuilder.append(expression + ",");
});
Map<String, Object> dataMap = pgService.queryMonitorByXTFeildsTime(stringBuilder.toString());
if (!dataMap.isEmpty()) {
List<AlarmRuleLevelConfigTemplate> alars = Lists.newArrayList();
for (AlarmRuleLevelConfigTemplate alarmRuleLevelConfigTemplate : alarmRuleLevelConfigTemplateList) {
String expressions = alarmRuleLevelConfigTemplate.getExpression();
String zexpressions = alarmRuleLevelConfigTemplate.getZexpression();
String[] expressionsArr = expressions.split(";");
String[] zexpressionsArr = zexpressions.split(";");
String conditions = alarmRuleLevelConfigTemplate.getConditions();
if ("or".equals(conditions)) {
label:
for (int i = 0; i < expressionsArr.length; i++) {
String expression = expressionsArr[i].replaceAll(",", "");
Expression compiledExp = AviatorEvaluator.compile(expression);
Boolean res = (Boolean) compiledExp.execute(dataMap);
if (res == true) {
StringBuilder stringBuilder1 = new StringBuilder();
String[] ids = expressionsArr[i].split(",");
for (String id : ids) {
Double orDefault = (Double) dataMap.get(id);
if (orDefault == null) {
stringBuilder1.append(id);
} else {
stringBuilder1.append(orDefault.doubleValue());
}
}
alarmRuleLevelConfigTemplate.setYexpression(zexpressionsArr[i] + "(" + stringBuilder1.toString() + ")");
alars.add(alarmRuleLevelConfigTemplate);
break label;
}
}
} else {
boolean flag = true;
StringBuilder stringBuilder1 = new StringBuilder();
label:
for (int i = 0; i < expressionsArr.length; i++) {
String expression = expressionsArr[i].replaceAll(",", "");
Expression compiledExp = AviatorEvaluator.compile(expression);
Boolean res = (Boolean) compiledExp.execute(dataMap);
if (res == false) {
flag = false;
break label;
} else {
String[] ids = expressionsArr[i].split(",");
for (String id : ids) {
Double orDefault = (Double) dataMap.get(id);
if (orDefault == null) {
stringBuilder1.append(id);
} else {
stringBuilder1.append(orDefault.doubleValue());
}
}
stringBuilder1.append(";");
}
}
if (flag) {
alarmRuleLevelConfigTemplate.setYexpression(zexpressions + "(" + stringBuilder1.toString() + ")");
alars.add(alarmRuleLevelConfigTemplate);
}
}
}
if (alars.size() > 0) {
resultList.put("alarmsList", alars);
}
}
}
return resultList;
}
private void sendMessage(Map<String, Object> excessAlarmPgList) {
List<AlarmRuleLevelConfigTemplate> alarmsList = (List<AlarmRuleLevelConfigTemplate>) excessAlarmPgList.get("alarmsList");
if (alarmsList != null && alarmsList.size() > 0) {
String date = DateUtils.getDate("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
alarmsList.forEach(v -> {
//查询质量报警配置
String departId = v.getDepartId();
String content = "";
String departName = departNameTemplete.replace("${departName}", sysDepartService.queryDepartNameById(departId));
content = contentTemplete.replace("${conent}", v.getYexpression());
//查询短信上一条报警是否处理,未处理就不新增报警记录
//TODO 先从数据库中获取,后期优化放redis中
QueryWrapper<AlarmRecordHistory> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("depart_id", departId);
queryWrapper.eq("alarm_rule_type", "7");
queryWrapper.eq("alarm_rule_level_id", v.getId());
queryWrapper.eq("depart_id", departId);
queryWrapper.orderByDesc("record_time").last("limit 1");
AlarmRecordHistory alarmRecordHistoryOne = alarmRecordHistoryService.getOne(queryWrapper);
//如果没有上条记录或者处理状态(0:未处理,1:已处理)为已处理
if ((alarmRecordHistoryOne == null || alarmRecordHistoryOne.getHandleStatus() == 1)) {
AlarmRecordHistory alarmRecordHistory = new AlarmRecordHistory();
//手动创建UUID,用于报警自动送审
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
alarmRecordHistory.setId(uuid);
alarmRecordHistory.setAlarmRuleLevelName(v.getAlarmTemplateLevelName());
alarmRecordHistory.setAlarmRuleType("7");
alarmRecordHistory.setAlarmParamType(v.getAlarmTemplateName());
alarmRecordHistory.setAlarmRuleLevelId(v.getId());
try {
alarmRecordHistory.setRecordTime(simpleDateFormat1.parse(date));
} catch (ParseException e) {
e.printStackTrace();
}
alarmRecordHistory.setDepartId(departId);
alarmRecordHistory.setActualAvgValue(v.getYexpression());
alarmRecordHistory.setHandleStatus(0);
alarmRecordHistory.setDelFlag(1);
alarmRecordHistory.setAlarmContent(prefix + departName + "采集数据" + content + suffix);
// alarmRecordHistory.setTableName(tableName);
alarmRecordHistory.setCreateBy("admin");
alarmRecordHistoryService.save(alarmRecordHistory);
}
});
}
}
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;
}
}
}