付延余
2022-12-16 f0f8ee8c4a945adbc742d9bab69382b28ad311fb
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
package com.wgcloud.service;
 
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wgcloud.dto.HeaderDto;
import com.wgcloud.entity.HeathMonitor;
import com.wgcloud.entity.HeathState;
import com.wgcloud.mapper.HeathMonitorMapper;
import com.wgcloud.util.DateUtil;
import com.wgcloud.util.HostUtil;
import com.wgcloud.util.RestUtil;
import com.wgcloud.util.UUIDUtil;
import com.wgcloud.util.msg.WarnMailUtil;
import com.wgcloud.util.msg.WarnPools;
import com.wgcloud.util.staticvar.BatchData;
import com.wgcloud.util.staticvar.StaticKeys;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.servlet.http.HttpServletRequest;
import java.util.*;
 
/**
 * @version v3.3
 * @ClassName:HeathMonitorService.java
 * @author: http://www.wgstart.com
 * @date: 2021年1月16日
 * @Description: HeathMonitorService.java
 * @Copyright: 2019-2021 wgcloud. All rights reserved.
 */
@Service
public class HeathMonitorService {
 
    private static final Logger logger = LoggerFactory.getLogger(HeathMonitorService.class);
 
 
    public PageInfo selectByParams(Map<String, Object> params, int currPage, int pageSize) throws Exception {
        PageHelper.startPage(currPage, pageSize);
        List<HeathMonitor> list = heathMonitorMapper.selectByParams(params);
        PageInfo<HeathMonitor> pageInfo = new PageInfo<HeathMonitor>(list);
        return pageInfo;
    }
 
    public void save(HeathMonitor heathMonitor) throws Exception {
        heathMonitor.setId(UUIDUtil.getUUID());
        heathMonitor.setCreateTime(DateUtil.getNowTime());
        if (StringUtils.isEmpty(heathMonitor.getResNoKeyword())) {
            heathMonitor.setResNoKeyword("");
        }
        if (StringUtils.isEmpty(heathMonitor.getResKeyword())) {
            heathMonitor.setResKeyword("");
        }
        if (StringUtils.isEmpty(heathMonitor.getHeathUrl())) {
            heathMonitor.setHeathUrl(heathMonitor.getHeathUrl().trim());
        }
 
        //如果是post方式,处理header键值对转为json
        setHeaderJson(heathMonitor);
 
        heathMonitorMapper.save(heathMonitor);
    }
 
 
    @Transactional
    public void saveRecord(List<HeathMonitor> recordList) throws Exception {
        if (recordList.size() < 1) {
            return;
        }
        for (HeathMonitor as : recordList) {
            as.setId(UUIDUtil.getUUID());
        }
        heathMonitorMapper.insertList(recordList);
    }
 
    public int countByParams(Map<String, Object> params) throws Exception {
        return heathMonitorMapper.countByParams(params);
    }
 
    @Transactional
    public int deleteById(String[] id) throws Exception {
        return heathMonitorMapper.deleteById(id);
    }
 
    public void updateById(HeathMonitor heathMonitor)
            throws Exception {
        if (StringUtils.isEmpty(heathMonitor.getHeathUrl())) {
            heathMonitor.setHeathUrl(heathMonitor.getHeathUrl().trim());
        }
 
        //如果是post方式,处理header键值对转为json
        setHeaderJson(heathMonitor);
 
        heathMonitorMapper.updateById(heathMonitor);
    }
 
    public void updateForServerBackup(HeathMonitor heathMonitor)
            throws Exception {
        heathMonitorMapper.updateById(heathMonitor);
    }
 
    public HeathMonitor selectById(String id) throws Exception {
        return heathMonitorMapper.selectById(id);
    }
 
    @Transactional
    public void updateRecord(List<HeathMonitor> recordList) throws Exception {
        heathMonitorMapper.updateList(recordList);
    }
 
    public List<HeathMonitor> selectAllByParams(Map<String, Object> params) throws Exception {
        return heathMonitorMapper.selectAllByParams(params);
    }
 
    /**
     * 如果是post方式,处理header键值对转为json
     *
     * @param heathMonitor
     */
    private void setHeaderJson(HeathMonitor heathMonitor) {
        if (StaticKeys.METHOD_POST.equals(heathMonitor.getMethod())) {
            List<HeaderDto> dtoList = new ArrayList<>();
            if (!StringUtils.isEmpty(heathMonitor.getHeaderKey())) {
                HeaderDto dto1 = new HeaderDto();
                dto1.setSort("1");
                dto1.setKey(heathMonitor.getHeaderKey());
                dto1.setValue(heathMonitor.getHeaderValue());
                dtoList.add(dto1);
 
            }
            if (!StringUtils.isEmpty(heathMonitor.getHeaderKey2())) {
                HeaderDto dto2 = new HeaderDto();
                dto2.setSort("2");
                dto2.setKey(heathMonitor.getHeaderKey2());
                dto2.setValue(heathMonitor.getHeaderValue2());
                dtoList.add(dto2);
 
            }
            if (!StringUtils.isEmpty(heathMonitor.getHeaderKey3())) {
                HeaderDto dto3 = new HeaderDto();
                dto3.setSort("3");
                dto3.setKey(heathMonitor.getHeaderKey3());
                dto3.setValue(heathMonitor.getHeaderValue3());
                dtoList.add(dto3);
 
            }
            if (!StringUtils.isEmpty(heathMonitor.getHeaderKey4())) {
                HeaderDto dto4 = new HeaderDto();
                dto4.setSort("4");
                dto4.setKey(heathMonitor.getHeaderKey4());
                dto4.setValue(heathMonitor.getHeaderValue4());
                dtoList.add(dto4);
 
            }
            if (!StringUtils.isEmpty(heathMonitor.getHeaderKey5())) {
                HeaderDto dto5 = new HeaderDto();
                dto5.setSort("5");
                dto5.setKey(heathMonitor.getHeaderKey5());
                dto5.setValue(heathMonitor.getHeaderValue5());
                dtoList.add(dto5);
 
            }
            heathMonitor.setHeaderJson(JSONUtil.toJsonStr(dtoList));
        }
    }
 
    /**
     * 如果是post方式,处理header键值对页面回显
     *
     * @param heathMonitor
     */
    public HashMap<String, String> displayHeaderJson(HeathMonitor heathMonitor) {
        HashMap<String, String> headerHashMap = new HashMap<>();
        try {
            if (StaticKeys.METHOD_POST.equals(heathMonitor.getMethod())) {
                if (!StringUtils.isEmpty(heathMonitor.getHeaderJson())) {
                    if (JSONUtil.isJsonObj(heathMonitor.getHeaderJson())) {
                        //此处是为了兼容v3.4.0之前版本,因为之前存贮的是json对象
                        HeaderDto dto = JSONUtil.toBean(heathMonitor.getHeaderJson(), HeaderDto.class);
                        heathMonitor.setHeaderKey(dto.getKey());
                        heathMonitor.setHeaderValue(dto.getValue());
                        headerHashMap.put(dto.getKey(), dto.getValue());
                    } else {
                        JSONArray jsonArray = JSONUtil.parseArray(heathMonitor.getHeaderJson());
                        for (Object jsonObject : jsonArray) {
                            HeaderDto dto = JSONUtil.toBean((JSONObject) jsonObject, HeaderDto.class);
                            headerHashMap.put(dto.getKey(), dto.getValue());
                            if (dto.getSort().equals("1")) {
                                heathMonitor.setHeaderKey(dto.getKey());
                                heathMonitor.setHeaderValue(dto.getValue());
                            }
                            if (dto.getSort().equals("2")) {
                                heathMonitor.setHeaderKey2(dto.getKey());
                                heathMonitor.setHeaderValue2(dto.getValue());
                            }
                            if (dto.getSort().equals("3")) {
                                heathMonitor.setHeaderKey3(dto.getKey());
                                heathMonitor.setHeaderValue3(dto.getValue());
                            }
                            if (dto.getSort().equals("4")) {
                                heathMonitor.setHeaderKey4(dto.getKey());
                                heathMonitor.setHeaderValue4(dto.getValue());
                            }
                            if (dto.getSort().equals("5")) {
                                heathMonitor.setHeaderKey5(dto.getKey());
                                heathMonitor.setHeaderValue5(dto.getValue());
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            logger.error("displayHeaderJson错误", e);
        }
        return headerHashMap;
    }
 
    /**
     * 保存操作日志
     *
     * @param request 获取当前登录用户
     * @param action  操作标识
     */
    public void saveLog(HttpServletRequest request, String action, HeathMonitor heathMonitor) {
        if (null == heathMonitor) {
            return;
        }
        logInfoService.save(HostUtil.getAccountByRequest(request).getAccount() + action + "服务接口监测信息:" + heathMonitor.getAppName(),
                "服务接口URL:" + heathMonitor.getHeathUrl(), StaticKeys.LOG_XTCZ);
    }
 
    /**
     * 检测服务接口任务具体处理线程
     *
     * @param h
     * @param date
     */
    public void taskThreadHandler(HeathMonitor h, Date date) {
        int status = 500;
        Map<String, Integer> statusMap = new HashMap<>();
        if (StaticKeys.METHOD_POST.equals(h.getMethod())) {
            HashMap<String, String> headerMap = displayHeaderJson(h);
            statusMap = restUtil.post(h.getHeathUrl(), h.getPostStr(), h.getResKeyword(), h.getResNoKeyword(), headerMap);
        } else {
            statusMap = restUtil.get(h.getHeathUrl(), h.getResKeyword(), h.getResNoKeyword());
        }
        status = statusMap.get("heathStatus");
        h.setCreateTime(date);
        //接口下线后,不再更新时间 begin
        if (status != 200) {
            h.setCreateTime(null);
        }
        //接口下线后,不再更新时间 end
        h.setHeathStatus(status + "");
        h.setResTimes(statusMap.get("resTimes"));
        try {
            updateById(h);
        } catch (Exception e) {
            e.printStackTrace();
        }
        HeathState heathState = new HeathState();
        heathState.setResTimes(h.getResTimes());
        heathState.setHeathId(h.getId());
        heathState.setCreateTime(date);
        BatchData.HEATH_STATE_LIST.add(heathState);
        if (!StaticKeys.HEATH_STATUS_200.equals(h.getHeathStatus())) {
            //返回错误处理
            WarnMailUtil.sendHeathInfo(h, true);
        } else {
            //返回成功处理
            if (null != WarnPools.MEM_WARN_MAP.get(h.getId())) {
                WarnMailUtil.sendHeathInfo(h, false);
            }
        }
    }
 
    @Autowired
    private HeathMonitorMapper heathMonitorMapper;
    @Autowired
    private LogInfoService logInfoService;
    @Autowired
    private RestUtil restUtil;
 
 
}