package com.wgcloud.controller; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import com.alibaba.excel.EasyExcel; import com.wgcloud.config.CommonConfig; import com.wgcloud.dto.ChartReportInfo; import com.wgcloud.dto.TjbbExcelChartDto; import com.wgcloud.entity.NetIoState; import com.wgcloud.entity.SysLoadState; import com.wgcloud.service.*; import com.wgcloud.util.DateUtil; import com.wgcloud.util.FormatUtil; import com.wgcloud.util.HostUtil; import com.wgcloud.util.ThreadPoolUtil; 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.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import javax.annotation.Resource; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @version v3.3 * @ClassName:ReportController.java * @author: http://www.wgstart.com * @date: 2021年9月16日 * @Description: 统计分析报表controller * @Copyright: 2019-2021 wgcloud. All rights reserved. */ @Controller @RequestMapping(value = "/report") public class ReportController { private void testThread() { Runnable runnable = () -> { logger.info("ReportController----------testThread"); }; ThreadPoolUtil.executor.execute(runnable); } private static final Logger logger = LoggerFactory.getLogger(ReportController.class); @Autowired private TaskUtilService taskUtilService; @Resource private LogInfoService logInfoService; @Resource private SystemInfoService systemInfoService; @Resource private MemStateService memStateService; @Resource private CpuStateService cpuStateService; @Resource private NetIoStateService netIoStateService; @Resource private SysLoadStateService sysLoadStateService; @Resource private AppInfoService appInfoService; @Resource private PortInfoService portInfoService; @Autowired private CommonConfig commonConfig; /** * 统计分析报表 * * @param model * @param request * @return */ @RequestMapping(value = "index") public String index(Model model, HttpServletRequest request) { Map params = new HashMap(); String days = request.getParameter("days"); if (StringUtils.isEmpty(days)) { days = "1"; } model.addAttribute("days", days); try { //顶部柱状图 setMainBarChart(model, days, request); //主机内存使用率最高柱状图,按时间段 setMemBarChart(model, days, request); //主机cpu使用率最高柱状图,按时间段 setCpuBarChart(model, days, request); //主机上下行速率最高柱状图,转为MB显示 setNetIoBarChart(model, days, request); //主机系统负载值最高柱状图,按时间段 setLoadBarChart(model, days, request); } catch (Exception e) { logger.error("统计报表报表告警次数错误", e); } return "report/index"; } /** * 顶部柱状图告警次数 * * @param model * @param days */ private List setMainBarChart(Model model, String days, HttpServletRequest request) throws Exception { Map params = new HashMap(); List chartInfoList = new ArrayList(); params.put("hostname", "告警"); //校验是否需要添加过滤用户查询条件 HostUtil.addAccountquery(request, params); try { List dateList = getDateList(Integer.valueOf(days)); for (String dateStr : dateList) { if ("1".equals(days)) { for (int i = 0; i < 24; i++) { String startHour = "" + i; if (i < 10) { startHour = "0" + i; } params.put(StaticKeys.SEARCH_START_TIME, dateStr + " " + startHour + ":00:00"); params.put(StaticKeys.SEARCH_END_TIME, dateStr + " " + startHour + ":59:59"); int size = logInfoService.countByParams(params); ChartReportInfo chartInfo = new ChartReportInfo(); chartInfo.setName(startHour + "时"); chartInfo.setVote(size); chartInfoList.add(chartInfo); } } else { params.put(StaticKeys.SEARCH_START_TIME, dateStr + " 00:00:00"); params.put(StaticKeys.SEARCH_END_TIME, dateStr + " 23:59:59"); int size = logInfoService.countByParams(params); ChartReportInfo chartInfo = new ChartReportInfo(); if (dateStr.length() > 5) { chartInfo.setName(dateStr.substring(5)); } chartInfo.setVote(size); chartInfoList.add(chartInfo); } } model.addAttribute("mainBarChart", JSONUtil.parseArray(chartInfoList)); } catch (Exception e) { logger.error("统计报表顶部柱状图告警次数错误", e); } return chartInfoList; } /** * 主机内存使用率最高柱状图 * * @param model * @param days */ private List setMemBarChart(Model model, String days, HttpServletRequest request) throws Exception { Map params = new HashMap(); //校验是否需要添加过滤用户查询条件 HostUtil.addAccountquery(request, params); List chartInfoList = new ArrayList(); try { List dateList = getDateList(Integer.valueOf(days)); for (String dateStr : dateList) { if ("1".equals(days)) { for (int i = 0; i < 24; i++) { String startHour = "" + i; if (i < 10) { startHour = "0" + i; } params.put(StaticKeys.SEARCH_START_TIME, dateStr + " " + startHour + ":00:00"); params.put(StaticKeys.SEARCH_END_TIME, dateStr + " " + startHour + ":59:59"); Double size = memStateService.selectMaxByDate(params); if (null == size) { size = 0d; } ChartReportInfo chartInfo = new ChartReportInfo(); chartInfo.setName(startHour + "时"); chartInfo.setValue(size); chartInfoList.add(chartInfo); } } else { params.put(StaticKeys.SEARCH_START_TIME, dateStr + " 00:00:00"); params.put(StaticKeys.SEARCH_END_TIME, dateStr + " 23:59:59"); Double size = memStateService.selectMaxByDate(params); if (null == size) { size = 0d; } ChartReportInfo chartInfo = new ChartReportInfo(); if (dateStr.length() > 5) { chartInfo.setName(dateStr.substring(5)); } chartInfo.setValue(size); chartInfoList.add(chartInfo); } } model.addAttribute("memBarChart", JSONUtil.parseArray(chartInfoList)); } catch (Exception e) { logger.error("统计报表内存使用率柱状图错误", e); } return chartInfoList; } /** * 主机cpu使用率最高柱状图 * * @param model * @param days */ private List setCpuBarChart(Model model, String days, HttpServletRequest request) throws Exception { Map params = new HashMap(); //校验是否需要添加过滤用户查询条件 HostUtil.addAccountquery(request, params); List chartInfoList = new ArrayList(); try { List dateList = getDateList(Integer.valueOf(days)); for (String dateStr : dateList) { if ("1".equals(days)) { for (int i = 0; i < 24; i++) { String startHour = "" + i; if (i < 10) { startHour = "0" + i; } params.put(StaticKeys.SEARCH_START_TIME, dateStr + " " + startHour + ":00:00"); params.put(StaticKeys.SEARCH_END_TIME, dateStr + " " + startHour + ":59:59"); Double size = cpuStateService.selectMaxByDate(params); if (null == size) { size = 0d; } ChartReportInfo chartInfo = new ChartReportInfo(); chartInfo.setName(startHour + "时"); chartInfo.setValue(size); chartInfoList.add(chartInfo); } } else { params.put(StaticKeys.SEARCH_START_TIME, dateStr + " 00:00:00"); params.put(StaticKeys.SEARCH_END_TIME, dateStr + " 23:59:59"); Double size = cpuStateService.selectMaxByDate(params); if (null == size) { size = 0d; } ChartReportInfo chartInfo = new ChartReportInfo(); if (dateStr.length() > 5) { chartInfo.setName(dateStr.substring(5)); } chartInfo.setValue(size); chartInfoList.add(chartInfo); } } model.addAttribute("cpuBarChart", JSONUtil.parseArray(chartInfoList)); } catch (Exception e) { logger.error("统计报表内存使用率柱状图错误", e); } return chartInfoList; } /** * 主机上下行速率最高柱状图,转为M显示 * 主机连接数最高柱状图 * * @param model * @param days */ private Map setNetIoBarChart(Model model, String days, HttpServletRequest request) throws Exception { Map resultMap = new HashMap(); Map params = new HashMap(); //校验是否需要添加过滤用户查询条件 HostUtil.addAccountquery(request, params); //主机连接数量集合 List chartInfoList = new ArrayList(); //上下行速率集合 List jsonObjectList = new ArrayList(); //时间段集合 List filedList = new ArrayList(); try { List dateList = getDateList(Integer.valueOf(days)); JSONObject rxbytMaxJson = new JSONObject(); rxbytMaxJson.putOpt("name", "下行速率最高"); JSONObject txbytMaxJson = new JSONObject(); txbytMaxJson.putOpt("name", "上行速率最高"); String filedStr = ""; for (String dateStr : dateList) { if ("1".equals(days)) { for (int i = 0; i < 24; i++) { String startHour = "" + i; if (i < 10) { startHour = "0" + i; } params.put(StaticKeys.SEARCH_START_TIME, dateStr + " " + startHour + ":00:00"); params.put(StaticKeys.SEARCH_END_TIME, dateStr + " " + startHour + ":59:59"); NetIoState netIoState = netIoStateService.selectMaxByDate(params); Double rxbytMax = 0d; Double txbytMax = 0d; int netConnectionsMax = 0; if (null != netIoState) { String rxbytTmp = netIoState.getRxbyt(); if (!StringUtils.isEmpty(rxbytTmp)) { rxbytMax = Double.valueOf(rxbytTmp); } String txbytTmp = netIoState.getTxbyt(); if (!StringUtils.isEmpty(txbytTmp)) { txbytMax = Double.valueOf(txbytTmp); } String netConnections = netIoState.getNetConnections(); if (!StringUtils.isEmpty(netConnections)) { netConnectionsMax = Integer.valueOf(netConnections); } } filedStr = startHour + "时"; rxbytMaxJson.putOpt(filedStr, FormatUtil.formatDouble(rxbytMax / 1024, 2)); txbytMaxJson.putOpt(filedStr, FormatUtil.formatDouble(txbytMax / 1024, 2)); filedList.add(filedStr); ChartReportInfo chartInfo = new ChartReportInfo(); chartInfo.setName(filedStr); chartInfo.setVote(netConnectionsMax); chartInfoList.add(chartInfo); } } else { params.put(StaticKeys.SEARCH_START_TIME, dateStr + " 00:00:00"); params.put(StaticKeys.SEARCH_END_TIME, dateStr + " 23:59:59"); NetIoState netIoState = netIoStateService.selectMaxByDate(params); Double rxbytMax = 0d; Double txbytMax = 0d; int netConnectionsMax = 0; if (null != netIoState) { String rxbytTmp = netIoState.getRxbyt(); if (!StringUtils.isEmpty(rxbytTmp)) { rxbytMax = Double.valueOf(rxbytTmp); } String txbytTmp = netIoState.getTxbyt(); if (!StringUtils.isEmpty(txbytTmp)) { txbytMax = Double.valueOf(txbytTmp); } String netConnections = netIoState.getNetConnections(); if (!StringUtils.isEmpty(netConnections)) { netConnectionsMax = Integer.valueOf(netConnections); } } filedStr = dateStr.substring(5); rxbytMaxJson.putOpt(filedStr, FormatUtil.formatDouble(rxbytMax / 1024, 2)); txbytMaxJson.putOpt(filedStr, FormatUtil.formatDouble(txbytMax / 1024, 2)); filedList.add(filedStr); ChartReportInfo chartInfo = new ChartReportInfo(); chartInfo.setName(filedStr); chartInfo.setVote(netConnectionsMax); chartInfoList.add(chartInfo); } } jsonObjectList.add(rxbytMaxJson); jsonObjectList.add(txbytMaxJson); model.addAttribute("netIoBarChartValue", JSONUtil.parseArray(jsonObjectList)); model.addAttribute("netIoBarChartFiled", JSONUtil.parseArray(filedList)); model.addAttribute("netConnectionsChart", JSONUtil.parseArray(chartInfoList)); //封装map,给导出excel用 resultMap.put("rxbytMaxJson", rxbytMaxJson); resultMap.put("txbytMaxJson", txbytMaxJson); resultMap.put("netConnections", chartInfoList); } catch (Exception e) { logger.error("统计报表上下行速率柱状图错误", e); } return resultMap; } /** * 主机系统负载值最高柱状图 * * @param model * @param days */ private Map setLoadBarChart(Model model, String days, HttpServletRequest request) throws Exception { Map resultMap = new HashMap(); Map params = new HashMap(); //校验是否需要添加过滤用户查询条件 HostUtil.addAccountquery(request, params); List jsonObjectList = new ArrayList(); List filedList = new ArrayList(); try { List dateList = getDateList(Integer.valueOf(days)); JSONObject oneMaxJson = new JSONObject(); oneMaxJson.putOpt("name", "1分钟负载最高"); JSONObject fiveMaxJson = new JSONObject(); fiveMaxJson.putOpt("name", "5分钟负载最高"); JSONObject fifteenMaxJson = new JSONObject(); fifteenMaxJson.putOpt("name", "15分钟负载最高"); String filedStr = ""; for (String dateStr : dateList) { if ("1".equals(days)) { for (int i = 0; i < 24; i++) { String startHour = "" + i; if (i < 10) { startHour = "0" + i; } params.put(StaticKeys.SEARCH_START_TIME, dateStr + " " + startHour + ":00:00"); params.put(StaticKeys.SEARCH_END_TIME, dateStr + " " + startHour + ":59:59"); SysLoadState sysLoadState = sysLoadStateService.selectMaxByDate(params); Double oneLoad = 0d; Double fiveLoad = 0d; Double fiteenLoad = 0d; if (null != sysLoadState) { oneLoad = sysLoadState.getOneLoad(); fiveLoad = sysLoadState.getFiveLoad(); fiteenLoad = sysLoadState.getFifteenLoad(); } filedStr = startHour + "时"; oneMaxJson.putOpt(filedStr, oneLoad); fiveMaxJson.putOpt(filedStr, fiveLoad); fifteenMaxJson.putOpt(filedStr, fiteenLoad); filedList.add(filedStr); } } else { params.put(StaticKeys.SEARCH_START_TIME, dateStr + " 00:00:00"); params.put(StaticKeys.SEARCH_END_TIME, dateStr + " 23:59:59"); SysLoadState sysLoadState = sysLoadStateService.selectMaxByDate(params); Double oneLoad = 0d; Double fiveLoad = 0d; Double fiteenLoad = 0d; if (null != sysLoadState) { oneLoad = sysLoadState.getOneLoad(); fiveLoad = sysLoadState.getFiveLoad(); fiteenLoad = sysLoadState.getFifteenLoad(); } filedStr = dateStr.substring(5); oneMaxJson.putOpt(filedStr, oneLoad); fiveMaxJson.putOpt(filedStr, fiveLoad); fifteenMaxJson.putOpt(filedStr, fiteenLoad); filedList.add(filedStr); } } jsonObjectList.add(oneMaxJson); jsonObjectList.add(fiveMaxJson); jsonObjectList.add(fifteenMaxJson); model.addAttribute("loadBarChartValue", JSONUtil.parseArray(jsonObjectList)); model.addAttribute("loadBarChartFiled", JSONUtil.parseArray(filedList)); //封装map,给导出excel用 resultMap.put("oneMaxJson", oneMaxJson); resultMap.put("fiveMaxJson", fiveMaxJson); resultMap.put("fifteenMaxJson", fifteenMaxJson); } catch (Exception e) { logger.error("统计报表主机系统负载值最高柱状图错误", e); } return resultMap; } /** * 统计报表导出excel * * @param model * @param request * @return */ @RequestMapping(value = "chartExcel") public void chartExcel(Model model, HttpServletRequest request, HttpServletResponse response) { ServletOutputStream out = null; Map params = new HashMap(); String days = request.getParameter("days"); if (StringUtils.isEmpty(days)) { days = "1"; } try { List dateList = getDateList(Integer.valueOf(days)); //时间段集合 List filedList = new ArrayList(); String filedStr = ""; for (String dateStr : dateList) { if ("1".equals(days)) { for (int i = 0; i < 24; i++) { String startHour = "" + i; if (i < 10) { startHour = "0" + i; } filedStr = startHour + "时"; filedList.add(filedStr); } } else { filedStr = dateStr.substring(5); filedList.add(filedStr); } } //顶部柱状图 List mainList = setMainBarChart(model, days, request); //主机内存使用率最高柱状图,按时间段 List memMaxList = setMemBarChart(model, days, request); //主机cpu使用率最高柱状图,按时间段 List cpuMaxList = setCpuBarChart(model, days, request); //主机上下行速率最高柱状图,转为MB显示 Map netIoMap = setNetIoBarChart(model, days, request); JSONObject rxbytMaxJson = (JSONObject) netIoMap.get("rxbytMaxJson"); JSONObject txbytMaxJson = (JSONObject) netIoMap.get("txbytMaxJson"); List netConnectionsList = (List) netIoMap.get("netConnections"); //主机系统负载值最高柱状图,按时间段 Map loadMap = setLoadBarChart(model, days, request); JSONObject oneMaxJson = (JSONObject) loadMap.get("oneMaxJson"); JSONObject fiveMaxJson = (JSONObject) loadMap.get("fiveMaxJson"); JSONObject fifteenMaxJson = (JSONObject) loadMap.get("fifteenMaxJson"); List excelList = new ArrayList<>(); for (int i = 0; i < filedList.size(); i++) { String filedName = filedList.get(i); ChartReportInfo warnInfo = mainList.get(i); ChartReportInfo memInfo = memMaxList.get(i); ChartReportInfo cpuInfo = cpuMaxList.get(i); Double rxbytMax = rxbytMaxJson.getDouble(filedName); Double txbytMax = txbytMaxJson.getDouble(filedName); Integer netConnectionsMax = netConnectionsList.get(i).getVote(); Double oneMax = oneMaxJson.getDouble(filedName); Double fiveMax = fiveMaxJson.getDouble(filedName); Double fifteenMax = fifteenMaxJson.getDouble(filedName); TjbbExcelChartDto excelChartDto = new TjbbExcelChartDto(); excelChartDto.setDatetime(filedName); excelChartDto.setCpuPer(cpuInfo.getValue()); excelChartDto.setMemPer(memInfo.getValue()); excelChartDto.setWarnCount(warnInfo.getVote()); excelChartDto.setRxbyt(rxbytMax); excelChartDto.setTxbyt(txbytMax); excelChartDto.setNetConnections(netConnectionsMax); excelChartDto.setOneLoad(oneMax); excelChartDto.setFiveLoad(fiveMax); excelChartDto.setFiteenLoad(fifteenMax); excelList.add(excelChartDto); } String fileName = "report_"; if ("1".equals(days)) { fileName += dateList.get(0).substring(5); } else { fileName += "Day" + days; } response.setContentType("application/vnd.ms-exce"); response.setCharacterEncoding("utf-8"); response.addHeader("Content-Disposition", "filename=" + fileName + ".xlsx"); EasyExcel.write(response.getOutputStream(), TjbbExcelChartDto.class).sheet("sheet").doWrite(excelList); } catch (Exception e) { logger.error("统计报表导出excel错误", e); logInfoService.save("统计报表导出excel错误", e.toString(), StaticKeys.LOG_XTCZ); } finally { try { // 关闭输出流 if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * 获取时间列表 * * @param days 几天 * @return */ public List getDateList(int days) { List dateList = new ArrayList(); String sevenDayBefore = DateUtil.getDateBefore(days); for (int i = 1; i < days + 1; i++) { sevenDayBefore = DateUtil.getDateBefore(i); dateList.add(sevenDayBefore.substring(0, 10)); } CollectionUtil.reverse(dateList); return dateList; } }