RiskOtherController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package com.jjt.risk.controller;
  2. import com.jjt.biz.domain.BizModel;
  3. import com.jjt.biz.domain.BizModelDetail;
  4. import com.jjt.biz.domain.BizObjMetrics;
  5. import com.jjt.biz.domain.BizObjMetricsData;
  6. import com.jjt.biz.service.IBizModelDetailService;
  7. import com.jjt.biz.service.IBizModelService;
  8. import com.jjt.biz.service.IBizObjMetricsDataService;
  9. import com.jjt.biz.service.IBizObjMetricsService;
  10. import com.jjt.biz.vo.ScoreVO;
  11. import com.jjt.common.core.controller.BaseController;
  12. import com.jjt.common.core.domain.AjaxResult;
  13. import com.jjt.common.utils.DateUtils;
  14. import com.jjt.risk.domain.RiskMsConfig;
  15. import com.jjt.risk.service.IRiskMsConfigService;
  16. import io.swagger.annotations.Api;
  17. import io.swagger.annotations.ApiOperation;
  18. import org.springframework.jdbc.core.JdbcTemplate;
  19. import org.springframework.web.bind.annotation.GetMapping;
  20. import org.springframework.web.bind.annotation.PathVariable;
  21. import org.springframework.web.bind.annotation.RequestMapping;
  22. import org.springframework.web.bind.annotation.RestController;
  23. import javax.annotation.Resource;
  24. import java.time.LocalDate;
  25. import java.time.LocalDateTime;
  26. import java.time.format.DateTimeFormatter;
  27. import java.util.*;
  28. import java.util.stream.Collectors;
  29. /**
  30. * 风险分析结果Controller
  31. *
  32. * @author jjt
  33. * @date 2024-09-12
  34. */
  35. @Api(tags = "其他风险分析")
  36. @RestController
  37. @RequestMapping("/risk/other")
  38. public class RiskOtherController extends BaseController {
  39. @Resource
  40. private IBizModelService bizModelService;
  41. @Resource
  42. private IRiskMsConfigService riskMsConfigService;
  43. @Resource
  44. private IBizModelDetailService modelDetailService;
  45. @Resource
  46. private IBizObjMetricsService omService;
  47. @Resource
  48. private IBizObjMetricsDataService dataService;
  49. @Resource
  50. private JdbcTemplate jdbcTemplate;
  51. @ApiOperation("模型列表")
  52. @GetMapping(value = "/model/list")
  53. public AjaxResult modelList() {
  54. List<BizModel> list = bizModelService.selectBizModelList(new BizModel());
  55. return success(list);
  56. }
  57. @ApiOperation("业务主机分析")
  58. @GetMapping(value = "/host/{modelId}")
  59. public AjaxResult host(@PathVariable("modelId") Long modelId) {
  60. RiskMsConfig q = new RiskMsConfig();
  61. q.setConfigType("host");
  62. List<RiskMsConfig> configs = riskMsConfigService.selectRiskMsConfigList(q);
  63. LocalDateTime endTime = LocalDateTime.now();
  64. LocalDateTime beginTime = endTime.minusDays(7);
  65. String begin = beginTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
  66. String end = endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
  67. List<Map<String, Object>> result = new ArrayList<>();
  68. List<BizModelDetail> mdList = modelDetailService.selectBizModelDetailList4ModelId(modelId);
  69. for (RiskMsConfig config : configs) {
  70. Map<String, Object> map = new HashMap<>(16);
  71. map.put("title", config.getViewName());
  72. List<Map<String, Object>> dataList = new ArrayList<>();
  73. for (BizModelDetail md : mdList) {
  74. Map<String, Object> dataMap = new HashMap<>(16);
  75. dataMap.put("objName", md.getBizObj().getObjName());
  76. if ("2".equals(config.getRankingBased())) {
  77. dataMap.put("alarms", new Random().nextInt(50));
  78. }
  79. BizObjMetrics bom = new BizObjMetrics();
  80. bom.setObjId(md.getObjId());
  81. bom.setMetricsId(config.getMetricsId());
  82. List<BizObjMetrics> oms = omService.selectBizObjMetricsList(bom);
  83. if (oms.size() > 0) {
  84. BizObjMetrics om = oms.get(0);
  85. dataMap.put("id", om.getObjMetricsId());
  86. dataMap.put("value", om.getDValue().floatValue());
  87. if ("2".equals(config.getRankingBased())) {
  88. String sql = "SELECT COUNT(*) num,sum(TIMESTAMPDIFF(MINUTE, alarm_time, ifnull(end_time,SYSDATE()))) times FROM alarm_record WHERE alarm_time BETWEEN ? AND ? and obj_metrics_id=?";
  89. Map<String, Object> dMap = jdbcTemplate.queryForMap(sql, begin, end, om.getObjMetricsId());
  90. Long num = (Long) dMap.get("num");
  91. dataMap.put("alarms", num.intValue());
  92. dataMap.put("value", dMap.get("times"));
  93. } else {
  94. dataMap.put("value", om.getDValue().floatValue());
  95. }
  96. dataList.add(dataMap);
  97. }
  98. }
  99. if ("2".equals(config.getRankingBased())) {
  100. dataList = dataList.stream().sorted(
  101. (o1, o2) -> -Integer.compare((int) o1.get("alarms"), (int) o2.get("alarms"))
  102. ).collect(Collectors.toList());
  103. } else {
  104. dataList = dataList.stream().sorted(
  105. (o1, o2) -> -Float.compare((float) o1.get("value"), (float) o2.get("value"))
  106. ).collect(Collectors.toList());
  107. }
  108. if (dataList.size() > 0) {
  109. map.put("data", dataList);
  110. result.add(map);
  111. }
  112. }
  113. return success(result);
  114. }
  115. @ApiOperation("业务主机分析趋势")
  116. @GetMapping(value = "/host/trend/{id}")
  117. public AjaxResult hostTrend(@PathVariable("id") Long id) {
  118. Map<String, Object> result = new HashMap<>(16);
  119. List<String> xData = new ArrayList<>();
  120. List<Float> curr = new ArrayList<>();
  121. LocalDateTime endTime = LocalDateTime.now();
  122. LocalDateTime beginTime = endTime.minusDays(7);
  123. Map<String, Object> params = new HashMap<>(16);
  124. params.put("beginTime", beginTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
  125. params.put("endTime", endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
  126. BizObjMetricsData q = new BizObjMetricsData();
  127. q.setObjMetricsId(id);
  128. q.setParams(params);
  129. List<BizObjMetricsData> list = dataService.selectBizObjMetricsDataList(q);
  130. list.forEach(data -> {
  131. Map<String, Object> map = new HashMap<>(16);
  132. xData.add(DateUtils.dateTime(data.getCreateTime()));
  133. curr.add(data.getdValue().floatValue());
  134. });
  135. result.put("time", xData);
  136. result.put("data", curr);
  137. return success(result);
  138. }
  139. @ApiOperation("网络状况分析")
  140. @GetMapping(value = "/network/{modelId}/{metricsId}")
  141. public AjaxResult network(@PathVariable("modelId") Long modelId, @PathVariable("metricsId") Long metricsId) {
  142. List<Map<String, Object>> result = new ArrayList<>();
  143. modelDetailService.selectBizModelDetailList4ModelId(modelId).forEach(md -> {
  144. Map<String, Object> objectMap = new HashMap<>(16);
  145. objectMap.put("name", md.getBizObj().getObjName());
  146. BizObjMetrics bom = new BizObjMetrics();
  147. bom.setObjId(md.getObjId());
  148. bom.setMetricsId(metricsId);
  149. List<BizObjMetrics> oms = omService.selectBizObjMetricsList(bom);
  150. if (oms.size() > 0) {
  151. BizObjMetrics om = oms.get(0);
  152. List<Map<String, Object>> trendList = new ArrayList<>();
  153. LocalDateTime endTime = LocalDateTime.now();
  154. LocalDateTime beginTime = endTime.minusDays(7);
  155. Map<String, Object> params = new HashMap<>(16);
  156. params.put("beginTime", beginTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
  157. params.put("endTime", endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
  158. BizObjMetricsData q = new BizObjMetricsData();
  159. q.setObjMetricsId(om.getObjMetricsId());
  160. q.setParams(params);
  161. List<BizObjMetricsData> list = dataService.selectBizObjMetricsDataList(q);
  162. list.forEach(data -> {
  163. Map<String, Object> map = new HashMap<>(16);
  164. map.put("time", data.getCreateTime());
  165. map.put("value", data.getdValue().floatValue());
  166. trendList.add(map);
  167. });
  168. objectMap.put("data", trendList);
  169. result.add(objectMap);
  170. }
  171. });
  172. // String[] objs = {"node1", "node2", "node3", "ecs4", "业务对象5", "虚拟主机6", "cluster7"};
  173. // for (String obj : objs) {
  174. // Map<String, Object> map = new HashMap<>(16);
  175. // map.put("name", obj);
  176. //
  177. // List<Map<String, Object>> dataList = new ArrayList<>();
  178. //
  179. // LocalDateTime ed = LocalDateTime.now();
  180. // LocalDateTime st = ed.minusDays(30);
  181. // Random r = new Random();
  182. // DecimalFormat df = new DecimalFormat("#0.00");
  183. // do {
  184. // Map<String, Object> dataMap = new HashMap<>(16);
  185. // long time = st.toEpochSecond(ZoneOffset.ofHours(8)) * 1000;
  186. // dataMap.put("time", time);
  187. // float f = r.nextFloat() * 100;
  188. // dataMap.put("value", Float.parseFloat(df.format(f)));
  189. // st = st.plusDays(1);
  190. // dataList.add(dataMap);
  191. // } while (!st.isAfter(ed));
  192. // map.put("data", dataList);
  193. //
  194. // result.add(map);
  195. // }
  196. return success(result);
  197. }
  198. @ApiOperation("可持续性服务评估")
  199. @GetMapping(value = "/server")
  200. public AjaxResult server() {
  201. Map<String, Object> result = new HashMap<>(16);
  202. List<ScoreVO> list = new ArrayList<>();
  203. List<Map<String, Object>> topList = new ArrayList<>();
  204. String[] names = {"市场服务", "市场出清", "信息发布", "市场合规", "市场结算"};
  205. for (int i = 0; i < names.length; i++) {
  206. Map<String, Object> map = new HashMap<>(16);
  207. map.put("name", names[i]);
  208. map.put("score", Float.valueOf(new Random().nextInt(50) + 50));
  209. topList.add(map);
  210. ScoreVO vo = new ScoreVO();
  211. List<String> xData = new ArrayList<>();
  212. List<Float> scores = new ArrayList<>();
  213. LocalDate time = LocalDate.now().minusDays(31);
  214. for (int j = 0; j < 30; j++) {
  215. time = time.plusDays(1);
  216. xData.add(time.getMonthValue() + "-" + time.getDayOfMonth());
  217. Float score = Float.valueOf(new Random().nextInt(50) + 50);
  218. scores.add(score);
  219. }
  220. vo.setModelId((long) (i + 1));
  221. vo.setModelName(names[i]);
  222. vo.setScores(scores);
  223. vo.setXData(xData);
  224. list.add(vo);
  225. }
  226. topList.sort(Comparator.comparing(map -> (Float) map.get("score")));
  227. result.put("trend", list);
  228. result.put("top", topList);
  229. return success(result);
  230. }
  231. }