XtDayAlarmJob.java
9.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
package com.skua.modules.quartz;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
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.api.vo.DictModel;
import com.skua.core.util.DateUtils;
import com.skua.modules.system.datestandard.service.ISysMonitorMetricInfoService;
import com.skua.modules.system.service.ISysDictService;
import com.skua.modules.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.skua.modules.util.SystemNoticeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@Slf4j
@Component
public class XtDayAlarmJob implements Job {
@Autowired
private ISysMonitorMetricInfoService sysMonitorMetricInfoService;
@Autowired
private SystemNoticeUtils systemNoticeUtils;
@Autowired
private ISysDictService sysDictService;
@Autowired
private AlarmRecordHistoryService alarmRecordHistoryService;
@Autowired
private AlarmMessageHistoryService alarmMessageHistoryService;
@Autowired
private IAlarmRuleLevelConfigTemplateService alarmRuleLevelConfigTemplateService;
@Autowired
private ISysUserService sysUserService;
//在${date}时${departName}的日处理量为:${num}负荷率${conent},请注意!
private static String prefix = "【金控数矿】";
private static String departNameTemplete = "${departName}";
private static String contentTemplete = "${alarmParamName}";
private static String suffix = ",请注意!";
//添加到报警记录表,用于移动端审批的使用
private static String tableName = "alarm_record_history";
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
try {
log.info("日均值报警任务开始");
//查询pg日均值警报
List<Map<String, Object>> excessAlarmPgList = checkPgExcessAlarm();
sendMessage(excessAlarmPgList);
log.info("日均值报警任务结束");
} catch (Exception e) {
log.info("日均值报警任务异常" + e);
}
}
public List<Map<String, Object>> checkPgExcessAlarm() {
List<Map<String, Object>> result = Lists.newArrayList();
List<Map<String, Object>> mets = sysMonitorMetricInfoService.queryWaterListAll1("8", DateUtils.getLastDay());
//获取负荷比例
List<DictModel> fhData = sysDictService.queryDictItemsByCode("fh_data");
if (fhData == null) {
return result;
}
Double low = 0.0;
Double up = 0.0;
for (DictModel fhDatum : fhData) {
if (fhDatum.getText().contains("低负荷")) {
low = Double.parseDouble(fhDatum.getValue());
}
if (fhDatum.getText().contains("高负荷")) {
up = Double.parseDouble(fhDatum.getValue());
}
}
for (Map<String, Object> v : mets) {
Double fh = (Double) v.get("fh");
if (fh != null) {
if (fh <= low) {
v.put("tread", "low");
v.put("limit", low);
result.add(v);
} else if (fh >= up) {
v.put("tread", "up");
v.put("limit", up);
result.add(v);
}
}
}
return result;
}
public void sendMessage(List<Map<String, Object>> alarmList) {
DecimalFormat df = new DecimalFormat("#.##");
String date = DateUtils.getDate("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
alarmList.forEach(v -> {
String tread = (String) v.get("tread");
Double limit = (Double) v.get("limit");
Double fh = (Double) v.get("fh");
String departName = (String) v.get("departName");
String metricdCode = (String) v.get("metricdCode");
Double metricdValue = (Double) v.get("metricdValue");
String departId = (String) v.get("departId");
String time = (String) v.get("time");
String content = "";
QueryWrapper<AlarmRuleLevelConfigTemplate> alarmRuleLevelConfigTemplateQueryWrapper = new QueryWrapper<>();
alarmRuleLevelConfigTemplateQueryWrapper.eq("depart_id", departId);
alarmRuleLevelConfigTemplateQueryWrapper.eq("alarm_template_name", "日均值流量报警配置");
alarmRuleLevelConfigTemplateQueryWrapper.eq("alarm_template_type", 3);
AlarmRuleLevelConfigTemplate alarmRuleLevelConfigTemplate = alarmRuleLevelConfigTemplateService.getOne(alarmRuleLevelConfigTemplateQueryWrapper);
if (alarmRuleLevelConfigTemplate != null) {
//在${date}时${departName}的日处理量为:${num}负荷率${conent},请注意!
content = contentTemplete.replace("${alarmParamName}", df.format(fh));
//是否需要报警
departName = departNameTemplete.replace("${departName}", departName);
Map<String, Object> alarmRecordHistoryMap = Maps.newHashMap();
//如果没有上条记录或者处理状态(0:未处理,1:已处理)为已处理
if (!alarmRecordHistoryMap.containsKey(departId + "_" + metricdCode)) {
alarmRecordHistoryMap.put(departId + "_" + metricdCode, "1");
AlarmRecordHistory alarmRecordHistory = new AlarmRecordHistory();
//手动创建UUID,用于报警自动送审
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
alarmRecordHistory.setId(uuid);
alarmRecordHistory.setAlarmParamCode(metricdCode);
alarmRecordHistory.setAlarmParamName("日处理量");
alarmRecordHistory.setAlarmParamUnit("m³");
alarmRecordHistory.setActualAvgValue(df.format(fh));
alarmRecordHistory.setActualMinValue(df.format(metricdValue));
alarmRecordHistory.setAlarmRuleLevelName(alarmRuleLevelConfigTemplate.getAlarmTemplateLevelName());
alarmRecordHistory.setAlarmRuleType("7");
alarmRecordHistory.setAlarmParamType(alarmRuleLevelConfigTemplate.getAlarmTemplateName());
try {
alarmRecordHistory.setRecordTime(simpleDateFormat1.parse(date));
} catch (ParseException e) {
e.printStackTrace();
}
alarmRecordHistory.setDepartId(departId);
alarmRecordHistory.setHandleStatus(0);
alarmRecordHistory.setDelFlag(1);
alarmRecordHistory.setAlarmParamUpperLimit(limit + "%");
//在${date}时${departName}的日处理量为:${num}负荷率${conent},请注意!
String contentTmp = "";
if ("up".equals(tread)) {
alarmRecordHistory.setAlarmParamLowerLimit("高负荷运行");
alarmRecordHistory.setAlarmContent(prefix + "在" + time + "时" + departName + "的日处理量为:" + metricdValue + "(m³),负荷率达到" + content + "%,处于高负荷运行状态" + suffix);
contentTmp = "在" + time + "时" + departName + "的日处理量为:" + metricdValue + "(m³),负荷率达到" + content + "%,处于高负荷运行状态" + suffix;
} else if ("low".equals(tread)) {
alarmRecordHistory.setAlarmParamLowerLimit("低负荷运行");
alarmRecordHistory.setAlarmContent(prefix + "在" + time + "时" + departName + "的日处理量为:" + metricdValue + "(m³),负荷率达到" + content + "%,处于低负荷运行状态" + suffix);
contentTmp = "在" + time + "时" + departName + "的日处理量为:" + metricdValue + "(m³),负荷率达到" + content + "%,处于低负荷运行状态" + suffix;
}
// alarmRecordHistory.setTableName(tableName);
alarmRecordHistory.setCreateBy("admin");
alarmRecordHistoryService.save(alarmRecordHistory);
//判断是否需要发送系统通知 (内容跟报警记录信息相同)
if ("1".equals(alarmRuleLevelConfigTemplate.getIsSendNotice())) {
if (StringUtils.isNotBlank(alarmRuleLevelConfigTemplate.getAlarmNoticeRecipient())) {
SystemNoticeVO systemNoticeVO = new SystemNoticeVO(alarmRuleLevelConfigTemplate.getAlarmNoticeRecipient(),
alarmRuleLevelConfigTemplate.getAlarmTemplateLevelName(), contentTmp, departId, "7");
systemNoticeUtils.dealNoticeMessage(systemNoticeVO);
}
}
}
}
});
}
}