package com.jjt.biz.controller; import com.jjt.biz.domain.*; import com.jjt.biz.service.*; import com.jjt.biz.vo.BizAccessVO; import com.jjt.biz.vo.BizTypeVO; import com.jjt.biz.vo.HlScoreVO; import com.jjt.biz.vo.ScoreVO; 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.utils.DateUtils; 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.web.bind.annotation.*; import javax.annotation.Resource; import java.text.DecimalFormat; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.format.DateTimeFormatter; import java.util.*; import java.util.stream.Collectors; /** * 业务对象Controller * * @author jjt * @date 2024-08-09 */ @Api(tags = "首页") @RestController @RequestMapping("/index") public class IndexController extends BaseController { @Resource private IIndexMetricsService imService; @Resource private IBizObjMetricsService omService; @Resource private IHlScoreService scoreService; @Resource private IBizModelService bizModelService; @Resource private IBizObjMetricsDataService dataService; @Resource private IAlarmRecordService alarmRecordService; String[] MODEL_NAMES = {"市场服务", "市场出清", "信息发布", "市场合规", "市场结算"}; @ApiOperation("巡检提示") @GetMapping("/hl/check") public AjaxResult hlCheck() { Map result = new HashMap<>(16); result.put("time", DateUtils.getTime()); result.put("model", 5); result.put("metrics", new Random().nextInt(200)); result.put("problem", new Random().nextInt(80)); return AjaxResult.success(result); } @ApiOperation("当前健康度") @GetMapping("/hl/curr") public AjaxResult hlCurr() { List list = scoreService.selectHlScoreListLast(); // List> result = new ArrayList<>(); // for (int i = 0; i < MODEL_NAMES.length; i++) { // Map map = new HashMap<>(16); // map.put("name", MODEL_NAMES[i]); // Float score = Float.valueOf(new Random().nextInt(50) + 50); // map.put("score", score); // result.add(map); // } return AjaxResult.success(list); } @ApiOperation("应用健康趋势-按月") @GetMapping("/hl/month/{date}") public AjaxResult hlMonth(@ApiParam(value = "时间 yyyy-mm", required = true) @PathVariable("date") String date) { List> result = new ArrayList<>(); int year = Integer.parseInt(date.split("-")[0]); int month = Integer.parseInt(date.split("-")[1]); LocalDate localDate = LocalDate.of(year, month, 1); for (int i = 0; i < MODEL_NAMES.length; i++) { Map map = new HashMap<>(16); map.put("name", MODEL_NAMES[i]); List times = new ArrayList<>(); List scores = new ArrayList<>(); for (int j = 0; j < localDate.lengthOfMonth(); j++) { times.add((j + 1) + ""); Float score = Float.valueOf(new Random().nextInt(50) + 50); scores.add(score); } map.put("time", times); map.put("score", scores); result.add(map); } return success(result); } @ApiOperation("应用健康趋势-按天") @GetMapping("/hl/day/{date}") public AjaxResult hlDay(@ApiParam(value = "时间(yyyy-mm-dd", required = true) @PathVariable("date") Date date) { List> result = new ArrayList<>(); HlScore search = new HlScore(); search.setHlType("1"); Map params = new HashMap<>(16); params.put("beginTime", DateUtils.parseDateToStr("yyyy-MM-dd", date) + " 00:00:00"); params.put("endTime", DateUtils.parseDateToStr("yyyy-MM-dd", date) + " 23:59:59"); search.setParams(params); List list = scoreService.selectHlScoreList(search); Map> resultMap = list.stream().collect(Collectors.groupingBy(HlScore::getModelId)); for (Long modelId : resultMap.keySet()) { Map map = new HashMap<>(16); ScoreVO vo = new ScoreVO(); BizModel model = bizModelService.selectBizModelByModelId(modelId); map.put("name", model.getModelName()); List times = 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 -> { times.add(DateUtils.parseDateToStr("HH:mm", hs.getHlDate())); scores.add(hs.getHlScore().floatValue()); }); map.put("time", times); map.put("score", scores); result.add(map); } return success(result); } @ApiOperation("运行风险预测结果") @GetMapping("/risk") public AjaxResult risk() { Map result = new HashMap<>(); List> list = new ArrayList<>(); String[] objs = {"出清node1", "出清ecs", "业务应用3", "业务应用4", "业务应用5", "业务应用6", "业务应用7"}; String[] ms = {"cpu使用率", "慢访问次数", "GC次数", "空间交换", "打开文件数", "交换分区访问"}; Random random = new Random(); int x = objs.length * ms.length; int xx = x / 2; int max = random.nextInt(xx) + 12; int num = 0; for (int i = 0; i < max; i++) { boolean flag = random.nextInt(888) % 2 == 0; int mi = random.nextInt(ms.length - 1); Map map = new HashMap<>(16); String objName = objs[i % 6]; String metricsName = ms[mi]; map.put("objName", objName); map.put("metricsName", metricsName); String status = "1"; // if (flag) { //风险 num++; status = "2"; // } map.put("status", status); list.add(map); } result.put("num", num); result.put("data", list); return success(result); } @ApiOperation("业务访问报错排名") @GetMapping("/biz/access") public AjaxResult access() { List> result = new ArrayList<>(); LocalDateTime endTime = LocalDateTime.now(); LocalDateTime beginTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN); String start = beginTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); String end = endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); List list = alarmRecordService.objAccess(null, start, end); list.forEach(vo -> { Map map = new HashMap<>(16); map.put("name", vo.getModelName()); map.put("total", vo.getAccess()); map.put("error", vo.getNum()); result.add(map); }); // String[] names = {"市场出清/mysql", "市场出清/redis", "市场出清/user", "市场服务/auth", "市场结算/mysql"}; // Random random = new Random(); // for (int i = 0; i < names.length; i++) { // Map map = new HashMap<>(16); // map.put("name", names[i]); // map.put("total", random.nextInt(500) + 500); // map.put("error", random.nextInt(20)); // result.add(map); // } return success(result); } @ApiOperation("组件告警数排名") @GetMapping("/obj/alarm") public AjaxResult alarm() { Map result = new HashMap<>(); LocalDateTime endTime = LocalDateTime.now(); LocalDateTime beginTime = endTime.minusDays(7); String start = beginTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); String end = endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); List list = alarmRecordService.listBizObj(null, start, end); list = list.stream().sorted(Comparator.comparing(BizTypeVO::getNum).reversed()).collect(Collectors.toList()); AlarmRecord q = new AlarmRecord(); q.setAlarmType("1"); List al = alarmRecordService.selectAlarmRecordList(q); result.put("curr", al.size()); result.put("data", list); return success(result); } @ApiOperation("指标配置列表") @GetMapping("/ms/config/list") public AjaxResult configList() { List list = imService.selectIndexMetricsList(new IndexMetrics()); return AjaxResult.success(list); } @ApiOperation("重点指标运行趋势列表") @GetMapping("/ms/trend") public AjaxResult msList() { List list = imService.selectIndexMetricsList(new IndexMetrics()); list.forEach(im -> { Random r = new Random(); DecimalFormat df = new DecimalFormat("#0.00"); im.setValue(Float.parseFloat(df.format(r.nextFloat() * 100))); }); return AjaxResult.success(list); } @ApiOperation("重点指标运行趋势图") @GetMapping("/ms/trend/{objMetricsId}") public AjaxResult msChat(@PathVariable("objMetricsId") Long objMetricsId) { List> trendList = new ArrayList<>(); LocalDateTime endTime = LocalDateTime.now(); LocalDateTime beginTime = endTime.minusDays(7); 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"))); BizObjMetricsData q = new BizObjMetricsData(); q.setObjMetricsId(objMetricsId); q.setParams(params); List list = dataService.selectBizObjMetricsDataList(q); list.forEach(data -> { Map map = new HashMap<>(16); map.put("time", data.getCreateTime()); map.put("value", data.getdValue().floatValue()); trendList.add(map); }); // LocalDateTime ed = LocalDateTime.now(); // LocalDateTime st = ed.minusDays(8); // Random r = new Random(); // DecimalFormat df = new DecimalFormat("#0.00"); // do { // Map map = new HashMap<>(16); // long time = st.toEpochSecond(ZoneOffset.ofHours(8)) * 1000; // map.put("time", time); // float f = r.nextFloat() * 100; // map.put("value", Float.parseFloat(df.format(f))); // st = st.plusDays(1); // trendList.add(map); // } while (!st.isAfter(ed)); return AjaxResult.success(trendList); } @ApiOperation("指标配置--选择指标列表") @GetMapping("/ms/config/select") public TableDataInfo list(BizObjMetrics bizObjMetrics) { startPage(); List list = omService.selectBizObjMetricsList(bizObjMetrics); return getDataTable(list); } @ApiOperation("指标配置--添加指标") @GetMapping("/ms/config/add") public AjaxResult selectAdd(@ApiParam(value = "指标ID数组", required = true) Long[] ids) { for (int i = 0; i < ids.length; i++) { Long id = ids[i]; IndexMetrics im = new IndexMetrics(); im.setObjMetricsId(id); List list = imService.selectIndexMetricsList(im); if (list.size() == 0) { BizObjMetrics bom = omService.selectBizObjMetricsByObjMetricsId(id); im.setObjId(bom.getObjId()); im.setObjName(bom.getObjName()); im.setMetricsId(bom.getMetricsId()); im.setMetricsName(bom.getMetricsName()); imService.insertIndexMetrics(im); } } return AjaxResult.success(); } @ApiOperation("指标配置--删除指标") @DeleteMapping("/ms/config/del/{imIds}") public AjaxResult configDel(@ApiParam(value = "指标imId", required = true) @PathVariable Long[] imIds) { return AjaxResult.success(imService.deleteIndexMetricsByImIds(imIds)); } }