package com.wgcloud.controller; import cn.hutool.json.JSONArray; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import com.wgcloud.config.CommonConfig; import com.wgcloud.entity.DbInfo; import com.wgcloud.entity.DbTable; import com.wgcloud.entity.DbTableCount; import com.wgcloud.service.DbInfoService; import com.wgcloud.service.DbTableService; import com.wgcloud.service.LogInfoService; import com.wgcloud.util.ResDataUtils; import com.wgcloud.util.ThreadPoolUtil; import com.wgcloud.util.TokenUtils; 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.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import java.util.*; /** * @version v3.3 * @ClassName:AgentDbTableGoController.java * @author: http://www.wgstart.com * @date: 2022年9月16日 * @Description: server-backup监控数据表提交处理,默认每60分钟提交一次 * @Copyright: 2019-2022 wgcloud. All rights reserved. */ @Controller @RequestMapping("/agentDbTableGo") public class AgentDbTableGoController { private static final Logger logger = LoggerFactory.getLogger(AgentDbTableGoController.class); @Resource private LogInfoService logInfoService; @Autowired private DbInfoService dbInfoService; @Autowired private DbTableService dbTableService; @Autowired private TokenUtils tokenUtils; @Autowired private CommonConfig commonConfig; @ResponseBody @RequestMapping("/minTask") public String minTask(@RequestBody String paramBean) { JSONObject agentJsonObject = (JSONObject) JSONUtil.parse(paramBean); logger.debug("server-backup监控数据表上报数据-------------" + agentJsonObject.toString()); if (!tokenUtils.checkAgentToken(agentJsonObject)) { logger.error(StaticKeys.TOKEN_ERROR); return ResDataUtils.resetErrorJson(StaticKeys.TOKEN_ERROR); } Date nowtime = new Date(); try { //数据源状态数据 JSONArray dbInfosJsonArr = agentJsonObject.getJSONArray("dbInfosUpdate"); //数据表状态数据 JSONArray dbTablesJsonArr = agentJsonObject.getJSONArray("dbTablesUpdate"); if (dbInfosJsonArr == null) { logger.error("dbInfosUpdate is null"); return ResDataUtils.resetErrorJson("dbInfosUpdate is null"); } //处理数据源状态数据 List dbInfoList = JSONUtil.toList(dbInfosJsonArr, DbInfo.class); for (DbInfo dbInfo : dbInfoList) { //之前存贮的数据源旧数据 DbInfo dbInfoSaved = dbInfoService.selectById(dbInfo.getId()); //只有数据源在线时候,才更新时间,连接失败就不更新时间 if (StaticKeys.ON_STATE.equals(dbInfo.getDbState())) { dbInfo.setCreateTime(nowtime); if (null != WarnPools.MEM_WARN_MAP && null != WarnPools.MEM_WARN_MAP.get(dbInfo.getId())) { Runnable runnable = () -> { WarnMailUtil.sendDbDown(dbInfoSaved, false); }; ThreadPoolUtil.executor.execute(runnable); } } else { Runnable runnable = () -> { WarnMailUtil.sendDbDown(dbInfoSaved, true); }; ThreadPoolUtil.executor.execute(runnable); } dbInfoService.updateById(dbInfo); } //处理数据表上报数据 if (null != dbTablesJsonArr) { List dbTableList = JSONUtil.toList(dbTablesJsonArr, DbTable.class); for (DbTable dbTable : dbTableList) { dbTable.setCreateTime(nowtime); DbTableCount dbTableCount = new DbTableCount(); dbTableCount.setCreateTime(nowtime); dbTableCount.setDbTableId(dbTable.getId()); dbTableCount.setTableCount(dbTable.getTableCount()); BatchData.DBTABLE_COUNT_LIST.add(dbTableCount); } Map params = new HashMap<>(); List dbInfos = new ArrayList<>(); if (StaticKeys.TRUE_VAL.equals(commonConfig.getUserInfoManage())) { dbInfos = dbInfoService.selectAllByParams(params); } dbTableService.warnCheckExp(dbTableList, dbInfos); dbTableService.updateRecord(dbTableList); } } catch (Exception e) { logger.error("解析server-backup监控数据表上报数据错误", e); return ResDataUtils.resetErrorJson(e.toString()); } return ResDataUtils.resetSuccessJson(null); } }