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 params, int currPage, int pageSize) throws Exception { PageHelper.startPage(currPage, pageSize); List list = heathMonitorMapper.selectByParams(params); PageInfo pageInfo = new PageInfo(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 recordList) throws Exception { if (recordList.size() < 1) { return; } for (HeathMonitor as : recordList) { as.setId(UUIDUtil.getUUID()); } heathMonitorMapper.insertList(recordList); } public int countByParams(Map 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 recordList) throws Exception { heathMonitorMapper.updateList(recordList); } public List selectAllByParams(Map 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 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 displayHeaderJson(HeathMonitor heathMonitor) { HashMap 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 statusMap = new HashMap<>(); if (StaticKeys.METHOD_POST.equals(h.getMethod())) { HashMap 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; }