RealTimeAlarmJob.java
30.7 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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
package com.skua.modules.alarm.quartz;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.DoubleSummaryStatistics;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
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 com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.skua.core.service.IFactoryInfoService;
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.system.entity.SysUser;
import com.skua.modules.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
/**
* @program: fmPlatform
* @description: 实时判断是否存在超时的警告
* @author: xie chao
* @create: 2020-11-02 13:48
*/
@Slf4j
public class RealTimeAlarmJob implements Job {
@Autowired
private AlarmRuleConfigService alarmRuleConfigService;
@Autowired
private AlarmRecordHistoryService alarmRecordHistoryService;
@Autowired
private AlarmMessageHistoryService alarmMessageHistoryService;
@Autowired
private IFactoryInfoService factoryInfoService;
@Autowired
private ISysUserService sysUserService;
//private static String excessMsgContent = "${date},${factoryName}采集数据,${alarmParamName}:${actualMinValue}-${actualMaxValue}${alarmParamUnit}${compare}标准${limitValue},${alarmRuleLevelName}级别。";
private static String excessMsgContent = "${date},${factoryName}采集数据,${alarmParamName}:${actualMinValue}-${actualMaxValue}${alarmParamUnit}${compare}标准${limitValue}。";
private static String breakdownMsgContent = "尊敬的用户,数矿特别提示:${date},${factoryName}采集数据,${alarmParamName}反馈故障,${alarmRuleLevelName}级别,请您及时采取处理措施。";
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("********报警监测定时服务开启********");
//查询需要实时数据报警厂的信息和报警规则字段的集合
QueryWrapper<AlarmRuleAndAlarmRuleLevelVO> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("arc.alarm_rule_type","1");
queryWrapper.eq("alarm_rule_status","1");
queryWrapper.eq("arc.del_flag","1");
List<AlarmRuleAndAlarmRuleLevelVO> alarmRuleAndAlarmRuleLevelVOList = alarmRuleConfigService.getAlarmRuleAndAlarmRuleLeveList(queryWrapper);
//存放故障报警,并且实时数据源是pg
List<AlarmRuleAndAlarmRuleLevelVO> pgBreakdownAlarmList = new ArrayList<>();
//存放超标报警,并且实时数据源是pg
List<AlarmRuleAndAlarmRuleLevelVO> pgExcessAlarmList = new ArrayList<>();
alarmRuleAndAlarmRuleLevelVOList.forEach(alarmRuleAndAlarmRuleLevelVO->{
if (alarmRuleAndAlarmRuleLevelVO.getAlarmParamType().equals("1")) {
pgExcessAlarmList.add(alarmRuleAndAlarmRuleLevelVO);
} else if (alarmRuleAndAlarmRuleLevelVO.getAlarmParamType().equals("2")) {
pgBreakdownAlarmList.add(alarmRuleAndAlarmRuleLevelVO);
}
});
//故障报警逻辑
//List<Map<String, Object>> breakdownAlarmList = checkMysqlBreakdownAlarm(pgBreakdownAlarmList);
//sendMessage(breakdownAlarmList,pgBreakdownAlarmList);
//查询mysql数据超标警报
List<Map<String, Object>> excessAlarmList = checkMysqlExcessAlarm(pgExcessAlarmList);
sendMessage(excessAlarmList,pgExcessAlarmList);
System.out.println("********报警监测定时服务结束********");
}
/**
* 判断mysql数据库中超标警报
* @param mysqlExcessAlarmList
*/
public List<Map<String,Object>> checkMysqlExcessAlarm(List<AlarmRuleAndAlarmRuleLevelVO> mysqlExcessAlarmList){
//存放厂ID和指标的List集合
Map<String,String> stringMap = new HashMap<>();
mysqlExcessAlarmList.forEach(alarmRuleAndAlarmRuleLevelVO -> {
String alarmProgramCode = stringMap.get(alarmRuleAndAlarmRuleLevelVO.getFactoryId());
if(alarmProgramCode == null){
stringMap.put(alarmRuleAndAlarmRuleLevelVO.getFactoryId(),alarmRuleAndAlarmRuleLevelVO.getAlarmProgramCode());
}else{
stringMap.put(alarmRuleAndAlarmRuleLevelVO.getFactoryId(),alarmProgramCode +","+alarmRuleAndAlarmRuleLevelVO.getAlarmProgramCode());
}
});
Map<String,List<Map<String,Object>>> resultListMap = new HashMap<>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date nowDate = new Date();
Date afterDate = new Date(nowDate.getTime() - 600000);
//循环将实时数据取出
stringMap.forEach((factoryId,alarmProgramCodeStr)->{
//根据factoryId查询数据库的数据,查询的时候注意将超标的字段设置为查询字段
List<Map<String, Object>> maps = factoryInfoService.queryFactoryInfos(factoryId, alarmProgramCodeStr, sdf.format(afterDate), sdf.format(nowDate), "0");
resultListMap.put(factoryId,maps);
});
//存放报警规则级别ID和超标数据
Map<String, List<Double>> excessMap = new HashMap<>();
//存放报警规则级别ID和未超标数据
Map<String, List<Double>> noExcessMap = new HashMap<>();
//TODO 后期需要优化
mysqlExcessAlarmList.forEach(alarmRuleAndAlarmRuleLevelVO->{
//存放超标数据
List<Double> excessList = new ArrayList<>();
List<Double> noExcessList = new ArrayList<>();
String alarmRuleLevelId = alarmRuleAndAlarmRuleLevelVO.getAlarmRuleLevelId();
//通过厂ID得到厂的实际所有报警指标的实际数值
List<Map<String, Object>> mapList = resultListMap.get(alarmRuleAndAlarmRuleLevelVO.getFactoryId());
if(mapList.size() > 0) {
mapList.forEach(listMap -> {
listMap.forEach((alarmProgramCode, value) -> {
//判断指标是否相等
if (alarmRuleAndAlarmRuleLevelVO.getAlarmProgramCode().equals(alarmProgramCode)) {
//判断是否超过数据上下限
String lowerLimitData = alarmRuleAndAlarmRuleLevelVO.getAlarmParamLowerLimit();
String upperLimitData = alarmRuleAndAlarmRuleLevelVO.getAlarmParamUpperLimit();
//暂时默认取第一个阈值
if(!StringUtils.isEmpty(upperLimitData)&&upperLimitData.contains(",")) {
upperLimitData = upperLimitData.split(",")[0];
}
if(value!=null){
String actualValue = value.toString();
if(alarmRuleAndAlarmRuleLevelVO.getEffectiveUpperLimit() != null && alarmRuleAndAlarmRuleLevelVO.getEffectiveLowerLimit() != null) {
if (Double.parseDouble(actualValue) < Double.parseDouble(alarmRuleAndAlarmRuleLevelVO.getEffectiveUpperLimit())
&& Double.parseDouble(actualValue) > Double.parseDouble(alarmRuleAndAlarmRuleLevelVO.getEffectiveLowerLimit())) {
if (Double.parseDouble(actualValue) >= Double.parseDouble(upperLimitData)) {
excessList.add(Double.parseDouble(actualValue));
} else if (Double.parseDouble(actualValue) <= Double.parseDouble(lowerLimitData)) {
excessList.add(Double.parseDouble(actualValue));
} else {
noExcessList.add(Double.parseDouble(actualValue));
}
}
}else{
System.out.println(alarmRuleAndAlarmRuleLevelVO.getFactoryId()+"==="+actualValue+"==="+upperLimitData);
if (Double.parseDouble(actualValue) >= Double.parseDouble(upperLimitData)) {
excessList.add(Double.parseDouble(actualValue));
} else if (Double.parseDouble(actualValue) < Double.parseDouble(lowerLimitData)) {
excessList.add(Double.parseDouble(actualValue));
} else {
noExcessList.add(Double.parseDouble(actualValue));
}
}
}
}
});
});
}
//excessList和noExcessList不为空才放入到map集合中
if(excessList.size() > 0){
excessMap.put(alarmRuleLevelId,excessList);
}
if(noExcessList.size() > 0) {
noExcessMap.put(alarmRuleLevelId, noExcessList);
}
});
List<Map<String,Object>> resultList = getRestulList(excessMap,noExcessMap);
return resultList;
}
/**
* 判断mysql数据库中故障警报
* @param mysqlBreakdownAlarmList
*/
public List<Map<String,Object>> checkMysqlBreakdownAlarm(List<AlarmRuleAndAlarmRuleLevelVO> mysqlBreakdownAlarmList){
//存放厂ID和指标的List集合
Map<String,String> stringMap = new HashMap<>();
mysqlBreakdownAlarmList.forEach(alarmRuleAndAlarmRuleLevelVO -> {
String alarmProgramCode = stringMap.get(alarmRuleAndAlarmRuleLevelVO.getFactoryId());
if(alarmProgramCode == null){
stringMap.put(alarmRuleAndAlarmRuleLevelVO.getFactoryId(),alarmRuleAndAlarmRuleLevelVO.getAlarmProgramCode());
}else{
stringMap.put(alarmRuleAndAlarmRuleLevelVO.getFactoryId(),alarmProgramCode +","+alarmRuleAndAlarmRuleLevelVO.getAlarmProgramCode());
}
});
Map<String,List<Map<String,Object>>> resultListMap = new HashMap<>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date nowDate = new Date();
Date afterDate = new Date(nowDate.getTime() - 600000);
//循环将实时数据取出
stringMap.forEach((factoryId,alarmProgramCodeStr)->{
//根据factoryId查询数据库的数据,查询的时候注意将超标的字段设置为查询字段
List<Map<String, Object>> maps = factoryInfoService.queryFactoryInfos(factoryId, alarmProgramCodeStr, sdf.format(afterDate), sdf.format(nowDate), "0");
resultListMap.put(factoryId,maps);
});
//存放报警规则级别ID和超标数据
Map<String, List<Double>> breakdownMap = new HashMap<>();
//存放报警规则级别ID和未超标数据
Map<String, List<Double>> noBreakdownMap = new HashMap<>();
//TODO 后期需要优化
mysqlBreakdownAlarmList.forEach(alarmRuleAndAlarmRuleLevelVO->{
//存放超标数据
List<Double> breakdownList = new ArrayList<>();
List<Double> noBreakdownMapList = new ArrayList<>();
String alarmRuleLevelId = alarmRuleAndAlarmRuleLevelVO.getAlarmRuleLevelId();
//通过厂ID得到厂的实际所有报警指标的实际数值
resultListMap.get(alarmRuleAndAlarmRuleLevelVO.getFactoryId()).forEach(listMap ->{
listMap.forEach((alarmProgramCode,value)->{
String actualValue = value.toString();
//判断指标是否相等
if(alarmRuleAndAlarmRuleLevelVO.getAlarmProgramCode().equals(alarmProgramCode)){
if (Double.parseDouble(actualValue) == 1) {
breakdownList.add(Double.parseDouble(actualValue));
} else if (Double.parseDouble(actualValue) == 0) {
noBreakdownMapList.add(Double.parseDouble(actualValue));
}
}
});
});
//excessList和noExcessList不为空才放入到map集合中
if(breakdownList.size() > 0){
breakdownMap.put(alarmRuleLevelId,breakdownList);
}
if(noBreakdownMapList.size() > 0) {
noBreakdownMap.put(alarmRuleLevelId,noBreakdownMapList);
}
});
List<Map<String,Object>> resultList = getRestulList(breakdownMap,noBreakdownMap);
return resultList;
}
/**
* 判断是否需要发送短信和通知
* @param resultList
* @param alarmRuleAndAlarmRuleLevelVOList
*/
public void sendMessage(List<Map<String,Object>> resultList,List<AlarmRuleAndAlarmRuleLevelVO> alarmRuleAndAlarmRuleLevelVOList){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
List<AlarmRecordHistory> alarmRecordHistoryList = new ArrayList<>();
List<AlarmMessageHistory> alarmMessageHistoryList = new ArrayList<>();
List<String> userList = new ArrayList<>();
//获取需要发送短信的手机号
alarmRuleAndAlarmRuleLevelVOList.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());
});
resultList.forEach((resultMap)->{
alarmRuleAndAlarmRuleLevelVOList.forEach( alarmRuleAndAlarmRuleLevelVO -> {
//判断报警规则级别ID是否相等
if(resultMap.get("alarmRuleLevelId").equals(alarmRuleAndAlarmRuleLevelVO.getAlarmRuleLevelId())){
//判断是否超过容错率,如果有,则表示需要生成警报记录
if(Double.parseDouble(resultMap.get("faultToleranceRate").toString()) > Double.parseDouble(alarmRuleAndAlarmRuleLevelVO.getAlarmDataOverStandardRate())){
//查询上一条报警是否处理,未处理就不新增报警记录
//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 = "";
if("1".equals(alarmRuleAndAlarmRuleLevelVO.getAlarmParamType())) {
//替换报警的内容
String compareCon = "";
String limitValue = "";
String unit = "";
if(alarmRuleAndAlarmRuleLevelVO.getAlarmParamUnit() != null){
unit = alarmRuleAndAlarmRuleLevelVO.getAlarmParamUnit();
}
if (Double.parseDouble(resultMap.get("maxValue").toString()) > Double.parseDouble(alarmRuleAndAlarmRuleLevelVO.getAlarmParamUpperLimit())) {
compareCon = "高于";
limitValue = "最大值"+alarmRuleAndAlarmRuleLevelVO.getAlarmParamUpperLimit()+unit;
} else if (Double.parseDouble(resultMap.get("maxValue").toString()) < Double.parseDouble(alarmRuleAndAlarmRuleLevelVO.getAlarmParamLowerLimit())) {
compareCon = "低于";
limitValue = "最小值"+alarmRuleAndAlarmRuleLevelVO.getAlarmParamLowerLimit()+unit;
}
msgContent = excessMsgContent.replace("${date}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())).replace("${factoryName}", alarmRuleAndAlarmRuleLevelVO.getFactoryName())
.replace("${alarmParamName}", alarmRuleAndAlarmRuleLevelVO.getAlarmParamName()).replace("${actualMinValue}", resultMap.get("minValue").toString())
.replace("${actualMaxValue}", resultMap.get("maxValue").toString()).replace("${alarmParamUnit}", unit)
.replace("${compare}", compareCon).replace("${alarmRuleLevelName}", alarmRuleAndAlarmRuleLevelVO.getAlarmRuleLevelName()).replace("${limitValue}", limitValue);
}else if("2".equals(alarmRuleAndAlarmRuleLevelVO.getAlarmParamType())){
//"【金控数矿】尊敬的用户,数矿特别提示:${date},${factoryName}采集数据,${alarmParamName}反馈故障,报警级别${alarmRuleLevelName},请您及时采取处理措施。";
msgContent = breakdownMsgContent.replace("${date}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())).replace("${factoryName}", alarmRuleAndAlarmRuleLevelVO.getFactoryName())
.replace("${alarmParamName}", alarmRuleAndAlarmRuleLevelVO.getAlarmParamName()).replace("${alarmRuleLevelName}", alarmRuleAndAlarmRuleLevelVO.getAlarmRuleLevelName());
}
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.setAlarmParamCode(alarmRuleAndAlarmRuleLevelVO.getAlarmParamCode());
alarmRecordHistory.setAlarmParamName(alarmRuleAndAlarmRuleLevelVO.getAlarmParamName());
alarmRecordHistory.setAlarmRuleLevelId(alarmRuleAndAlarmRuleLevelVO.getAlarmRuleLevelId());
alarmRecordHistory.setAlarmRuleType(alarmRuleAndAlarmRuleLevelVO.getAlarmRuleType());
alarmRecordHistory.setAlarmParamType(alarmRuleAndAlarmRuleLevelVO.getAlarmParamType());
alarmRecordHistory.setRecordTime(new Date());
alarmRecordHistory.setAlarmParamUpperLimit(alarmRuleAndAlarmRuleLevelVO.getAlarmParamUpperLimit());
alarmRecordHistory.setAlarmParamLowerLimit(alarmRuleAndAlarmRuleLevelVO.getAlarmParamLowerLimit());
alarmRecordHistory.setAlarmParamUnit(alarmRuleAndAlarmRuleLevelVO.getAlarmParamUnit());
alarmRecordHistory.setFactoryId(alarmRuleAndAlarmRuleLevelVO.getFactoryId());
alarmRecordHistory.setActualMinValue(resultMap.get("minValue").toString());
alarmRecordHistory.setActualMaxValue(resultMap.get("maxValue").toString());
alarmRecordHistory.setActualAvgValue(resultMap.get("avgValue").toString());
alarmRecordHistory.setHandleStatus(0);
alarmRecordHistory.setAlarmDataOverStandardRate(resultMap.get("faultToleranceRate").toString());
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.setAlarmParamCode(alarmRuleAndAlarmRuleLevelVO.getAlarmParamCode());
alarmMessageHistory.setAlarmParamName(alarmRuleAndAlarmRuleLevelVO.getAlarmParamName());
alarmMessageHistory.setAlarmRuleLevelId(alarmRuleAndAlarmRuleLevelVO.getAlarmRuleLevelId());
alarmMessageHistory.setAlarmSendType(1);
alarmMessageHistory.setAlarmRuleType(alarmRuleAndAlarmRuleLevelVO.getAlarmRuleType());
alarmMessageHistory.setAlarmParamType(alarmRuleAndAlarmRuleLevelVO.getAlarmParamType());
alarmMessageHistory.setAlarmParamUpperLimit(alarmRuleAndAlarmRuleLevelVO.getAlarmParamUpperLimit());
alarmMessageHistory.setAlarmParamLowerLimit(alarmRuleAndAlarmRuleLevelVO.getAlarmParamLowerLimit());
alarmMessageHistory.setAlarmParamUnit(alarmRuleAndAlarmRuleLevelVO.getAlarmParamUnit());
alarmMessageHistory.setFactoryId(alarmRuleAndAlarmRuleLevelVO.getFactoryId());
alarmMessageHistory.setActualMinValue(resultMap.get("minValue").toString());
alarmMessageHistory.setActualMaxValue(resultMap.get("maxValue").toString());
alarmMessageHistory.setActualAvgValue(resultMap.get("avgValue").toString());
alarmMessageHistoryList.add(alarmMessageHistory);
}
}
}
}
}
});
});
alarmMessageHistoryService.saveBatch(alarmMessageHistoryList);
alarmRecordHistoryService.saveBatch(alarmRecordHistoryList);
}
/**
* 通过指标超标集合和指标未超标集合,得到超标率和最大值,最小值,平均值
* @param excessMap
* @param noExcessMap
* @return
*/
public List<Map<String,Object>> getRestulList(Map<String, List<Double>> excessMap,Map<String, List<Double>> noExcessMap){
List<Map<String,Object>> resultList = new ArrayList<>();
excessMap.forEach((alarmRuleLevelId,excessList)->{
Map<String,Object> resultMap = new HashMap<>();
List<Double> noExcessList = noExcessMap.get(alarmRuleLevelId);
int allCount = 0;
if(noExcessList != null){
allCount = excessList.size()+noExcessList.size();
}else{
allCount = excessList.size();
}
//计算容错率
NumberFormat numberFormat = NumberFormat.getInstance();
// 设置精确到小数点后2位
numberFormat.setMaximumFractionDigits(2);
String faultToleranceRate = numberFormat.format((double)excessList.size()/allCount*100);
DoubleSummaryStatistics statistics = excessList.stream().mapToDouble(Number::doubleValue).summaryStatistics();
//取最大值
double maxValue = statistics.getMax();
//取最小值
double minValue = statistics.getMin();
//取平均值
double avgValue = statistics.getAverage();
resultMap.put("faultToleranceRate",faultToleranceRate);
resultMap.put("maxValue",maxValue);
resultMap.put("minValue",minValue);
resultMap.put("avgValue",numberFormat.format(avgValue));
resultMap.put("alarmRuleLevelId",alarmRuleLevelId);
resultList.add(resultMap);
});
return resultList;
}
/**
* 判断当前时间是否在[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;
}
}
}