package com.jjt.biz.controller; import com.deepoove.poi.XWPFTemplate; import com.github.pagehelper.PageHelper; import com.jjt.biz.domain.BizModel; import com.jjt.biz.domain.BizObj; import com.jjt.biz.service.IBizModelService; import com.jjt.biz.util.DataUtil; import com.jjt.biz.vo.CheckExportVO; import com.jjt.biz.vo.CheckVO; import com.jjt.biz.vo.ScoreVO; import com.jjt.common.annotation.Log; import com.jjt.common.core.controller.BaseController; import com.jjt.common.core.domain.AjaxResult; import com.jjt.common.core.page.TableDataInfo; import com.jjt.common.enums.BusinessType; import com.jjt.common.utils.DateUtils; import com.jjt.common.utils.poi.ExcelUtil; import com.jjt.hl.domain.HlClass; import com.jjt.hl.domain.HlScore; import com.jjt.hl.service.IHlScoreService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.*; import java.util.stream.Collectors; /** * 业务模型Controller * * @author jjt * @date 2024-08-26 */ @Api(tags = "业务模型") @RestController @RequestMapping("/hl/bm") public class BizModelController extends BaseController { @Resource private IBizModelService bizModelService; @Resource private IHlScoreService hlScoreService; @ApiOperation("选择对象") @GetMapping("/obj/select/{modelId}") public TableDataInfo objSelect(@ApiParam(value = "模型ID", required = true) @PathVariable("modelId") Long modelId) { PageHelper.startPage(1, 1000, "").setReasonable(true); List list = bizModelService.selectObjList(modelId); return getDataTable(list); } @ApiOperation("添加对象") @GetMapping("/obj/add/{modelId}") public AjaxResult objAdd(@ApiParam(value = "模型ID", required = true) @PathVariable("modelId") Long modelId, @ApiParam(value = "对象ID数组", required = true) Long[] objIds) { return success(bizModelService.insertObj(modelId, objIds)); } @ApiOperation("健康度配置") @GetMapping("/config/{modelId}") public AjaxResult config(@ApiParam(value = "模型ID", required = true) @PathVariable("modelId") Long modelId) { List list = bizModelService.selectHlClassList4modelId(modelId); return success(list); } @ApiOperation("导入分类") @GetMapping("/imp/{modelId}") public AjaxResult imp(@ApiParam(value = "模型ID", required = true) @PathVariable("modelId") Long modelId) { bizModelService.impClass(modelId); return success(); } @ApiOperation("历史健康度得分情况") @GetMapping("/time/history") public List timeHistory() { List result = new ArrayList<>(); LocalDateTime endTime = LocalDateTime.now().plusHours(1).withMinute(0).withSecond(0).withNano(0); LocalDateTime beginTime = endTime.minusHours(24); HlScore search = new HlScore(); search.setHlType("1"); Map params = new HashMap<>(16); params.put("beginTime", beginTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); params.put("endTime", endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); search.setParams(params); List list = hlScoreService.selectHlScoreList(search); Map> resultMap = list.stream().collect(Collectors.groupingBy(HlScore::getModelId)); int len = 0; List standTime = new ArrayList<>(); for (Long modelId : resultMap.keySet()) { ScoreVO vo = new ScoreVO(); BizModel model = bizModelService.selectBizModelByModelId(modelId); vo.setModelId(modelId); vo.setModelName(model.getModelName()); List xData = new ArrayList<>(); List scores = new ArrayList<>(); List scoreList = resultMap.get(modelId); scoreList = scoreList.stream().sorted(Comparator.comparing(HlScore::getHlDate)).collect(Collectors.toList()); scoreList.forEach(hs -> { xData.add(DateUtils.parseDateToStr("HH:mm", hs.getHlDate())); if (hs.getHlScore() == null) { scores.add(100f); } else { scores.add(hs.getHlScore().floatValue()); } }); if (scoreList.size() > len) { len = scoreList.size(); standTime = xData; } vo.setScores(scores); vo.setXData(xData); result.add(vo); } DataUtil.standTimeVO(standTime, result); return result; } @ApiOperation("日健康度得分情况") @GetMapping("/day/history") public List dayHistory() { List result = new ArrayList<>(); LocalDate endTime = LocalDate.now(); LocalDate beginTime = endTime.minusDays(8); HlScore search = new HlScore(); search.setHlType("2"); Map params = new HashMap<>(16); params.put("beginTime", beginTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); params.put("endTime", endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); search.setParams(params); List list = hlScoreService.selectHlScoreList(search); Map> resultMap = list.stream().collect(Collectors.groupingBy(HlScore::getModelId)); int len = 0; List standTime = new ArrayList<>(); for (Long modelId : resultMap.keySet()) { ScoreVO vo = new ScoreVO(); BizModel model = bizModelService.selectBizModelByModelId(modelId); vo.setModelId(modelId); vo.setModelName(model.getModelName()); List xData = new ArrayList<>(); List scores = new ArrayList<>(); List scoreList = resultMap.get(modelId); scoreList = scoreList.stream().sorted(Comparator.comparing(HlScore::getHlDate)).collect(Collectors.toList()); scoreList.forEach(hs -> { xData.add(DateUtils.parseDateToStr("MM-dd", hs.getHlDate())); scores.add(hs.getHlScore().floatValue()); }); if (scoreList.size() > len) { len = scoreList.size(); standTime = xData; } vo.setScores(scores); vo.setXData(xData); result.add(vo); } DataUtil.standTimeVO(standTime, result); return result; } // @ApiOperation("选择健康度指标") // @GetMapping("/metrics/select/{modelId}") // public TableDataInfo metricsSelect(@ApiParam(value = "模型ID", required = true) @PathVariable("modelId") Long modelId) { // startPage(); // List list = bizModelService.selectObjList(modelId); // return getDataTable(list); // } // // @ApiOperation("添加对象") // @GetMapping("/metrics/add/{modelId}") // public AjaxResult metricsAdd(@ApiParam(value = "模型ID", required = true) @PathVariable("modelId") Long modelId, @ApiParam(value = "指标ID数组", required = true) Long[] metricsIds) { // return success(bizModelService.insertObj(modelId, metricsIds)); // } /** * 查询业务模型列表 */ @ApiOperation("查询业务模型列表") @PreAuthorize("@ss.hasPermi('hl:bm:list')") @GetMapping("/list") public TableDataInfo list(BizModel bizModel) { startPage(); List list = bizModelService.selectBizModelList(bizModel); return getDataTable(list); } /** * 导出业务模型列表 */ @ApiOperation("导出业务模型列表") @PreAuthorize("@ss.hasPermi('hl:bm:export')") @Log(title = "业务模型", businessType = BusinessType.EXPORT) @PostMapping("/export") public void export(HttpServletResponse response, BizModel bizModel) { List list = bizModelService.selectBizModelList(bizModel); ExcelUtil util = new ExcelUtil(BizModel.class); util.exportExcel(response, list, "业务模型数据"); } /** * 获取业务模型详细信息 */ @ApiOperation("获取业务模型详细信息") @PreAuthorize("@ss.hasPermi('hl:bm:query')") @GetMapping(value = "/{modelId}") public AjaxResult getInfo(@PathVariable("modelId") Long modelId) { return success(bizModelService.selectBizModelByModelId(modelId)); } /** * 新增业务模型 */ @ApiOperation("新增业务模型") @PreAuthorize("@ss.hasPermi('hl:bm:add')") @Log(title = "业务模型", businessType = BusinessType.INSERT) @PostMapping public AjaxResult add(@RequestBody BizModel bizModel) { bizModelService.insertBizModel(bizModel); return AjaxResult.success(bizModel); } /** * 修改业务模型 */ @ApiOperation("修改业务模型") @PreAuthorize("@ss.hasPermi('hl:bm:edit')") @Log(title = "业务模型", businessType = BusinessType.UPDATE) @PostMapping("/edit") public AjaxResult edit(@RequestBody BizModel bizModel) { return toAjax(bizModelService.updateBizModel(bizModel)); } /** * 删除业务模型 */ @ApiOperation("删除业务模型") @PreAuthorize("@ss.hasPermi('hl:bm:remove')") @Log(title = "业务模型", businessType = BusinessType.DELETE) @GetMapping("/del/{modelIds}") public AjaxResult remove(@PathVariable Long[] modelIds) { return toAjax(bizModelService.deleteBizModelByModelIds(modelIds)); } @ApiOperation("一健巡检--模型列表") @GetMapping("/check/list") public AjaxResult checkList() { List> classList = new ArrayList<>(); Map map = new HashMap<>(); map.put("class", "基础支撑"); map.put("option", "容器服务1、容器服务2、node-1、node-2"); classList.add(map); map = new HashMap<>(); map.put("class", "mysql"); map.put("option", "mysql主服务器、mysql从服务器"); classList.add(map); map = new HashMap<>(); map.put("class", "JAVA应用"); map.put("option", "服务网关、系统服务、权限服务、业务服务、定时器服务"); classList.add(map); map = new HashMap<>(); map.put("class", "运行环境"); map.put("option", "ECS-1、ECS-cluster-2、ECS-node-3"); classList.add(map); map.put("class", "中间件"); map.put("option", "ES搜索服务、文件服务、其他"); classList.add(map); List list = new ArrayList<>(); String[] names = {"市场服务", "市场出清", "信息发布", "市场合规", "市场结算"}; for (int i = 0; i < names.length; i++) { CheckVO model = new CheckVO(); model.setModelId((long) (i + 1)); model.setModelName(names[i]); model.setClassList(classList); list.add(model); } return AjaxResult.success(list); } @ApiOperation("一健巡检--查看报告") @GetMapping("/check/history/report") public AjaxResult checkHistory() { List> list = new ArrayList<>(); for (int i = 0; i < 10; i++) { Map map = new HashMap<>(); String time = "2024-09-08 " + (21 - i) + ":00:00"; map.put("id", (i + 1)); map.put("time", time); list.add(map); } return AjaxResult.success(list); } @ApiOperation("一健巡检--查看报告模型明细") @GetMapping("/check/model/{id}") public AjaxResult checkDetail(@PathVariable("id") Long id) { List> list = new ArrayList<>(); String[] names = {"市场服务", "市场出清", "信息发布", "市场合规", "市场结算"}; int[] times = {25, 35, 45, 38, 25}; for (int i = 0; i < names.length; i++) { Map map = new HashMap<>(); map.put("id", (i + 1)); map.put("modelName", names[i]); map.put("total", times[i]); map.put("num", new Random().nextInt(times[i])); list.add(map); } return AjaxResult.success(list); } @ApiOperation("一健巡检--导出报告") @GetMapping("/check/export") public void checkExport(HttpServletResponse response) { CheckExportVO vo = new CheckExportVO(); vo.mock(); // 定义模板对应的数据 HashMap data = new HashMap<>(); data.put("test", "测试dsx哈"); for (int i = 1; i <= 28; i++) { data.put("l" + i, vo.getLeft()[i - 1]); data.put("r" + i, vo.getRight()[i - 1]); } // 渲染模板 try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("tpl/check.docx"); OutputStream outputStream = new BufferedOutputStream(response.getOutputStream()); ByteArrayOutputStream out = new ByteArrayOutputStream();) { // 写出到文件 XWPFTemplate template = XWPFTemplate.compile(inputStream).render(data); template.writeAndClose(out); // 清空response response.reset(); // 设置response的Header response.setCharacterEncoding("UTF-8"); //Content-Disposition的作用:告知浏览器以何种方式显示响应返回的文件,用浏览器打开还是以附件的形式下载到本地保存 //attachment表示以附件方式下载 inline表示在线打开 "Content-Disposition: inline; filename=文件名.mp3" // filename表示文件的默认名称,因为网络传输只支持URL编码的相关支付,因此需要将文件名URL编码后进行传输,前端收到后需要反编码才能获取到真正的名称 response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("新一代电力交易平台巡检报告" + DateUtils.dateTimeNow() + ".docx", "UTF-8")); response.setContentType("application/octet-stream"); outputStream.write(out.toByteArray()); outputStream.flush(); } catch (IOException e) { throw new RuntimeException(e); } } @ApiOperation("一健巡检--查看报告分类明细") @GetMapping("/check/proj/{id}") public AjaxResult checkProj(@PathVariable("id") Long id) { List> list = new ArrayList<>(); String[] names = {"JAVA应用", "数据库", "运行环境", "基础支撑", "中间件"}; String[] options = {"服务网关、系统服务、权限服务、业务服务、定时器服务", "mysql主服务器、mysql从服务器", "ECS-1、ECS-cluster-2、ECS-node-3", "容器服务1、容器服务2、node-1、node-2", "ES搜索服务、文件服务、其他"}; int[] times = {25, 35, 45, 38, 25}; for (int i = 0; i < names.length; i++) { Map map = new HashMap<>(); map.put("id", (i + 1)); map.put("class", names[i]); map.put("option", options[i]); map.put("total", times[i]); map.put("num", new Random().nextInt(times[i])); list.add(map); } return AjaxResult.success(list); } @ApiOperation("一健巡检--查看报告指标明细") @GetMapping("/check/metrics/{id}") public AjaxResult checkMetrics(@PathVariable("id") Long id) { List> list = new ArrayList<>(); String[] names = {"CPU使用率", "内存使用率", "磁盘使用率", "mysql连接数", "mysql慢查询数量"}; String[] remarks = {"超过80%,且持续1小时", "超过60,且持续1小时", "超过90%", "超过200,且持续1小时", "当前值超过5"}; int[] times = new int[5]; for (int i = 0; i < times.length; i++) { times[i] = new Random().nextInt(100); } for (int i = 0; i < names.length; i++) { Map map = new HashMap<>(); map.put("name", names[i]); map.put("remark", remarks[i]); map.put("value", times[i]); if (times[i] % 2 == 0) { map.put("status", "正常"); } else { map.put("status", "异常"); } list.add(map); } return AjaxResult.success(list); } @ApiOperation("一健巡检--按模型ID巡检") @GetMapping("/check/{modelId}") public AjaxResult checkModel(@PathVariable("modelId") Long modelId) { try { Thread.sleep(3000); } catch (InterruptedException e) { throw new RuntimeException(e); } return AjaxResult.success(); } }