123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- package com.jjt.risk.controller;
- import com.jjt.biz.domain.BizModel;
- import com.jjt.biz.domain.BizModelDetail;
- import com.jjt.biz.domain.BizObjMetrics;
- import com.jjt.biz.domain.BizObjMetricsData;
- import com.jjt.biz.service.IBizModelDetailService;
- import com.jjt.biz.service.IBizModelService;
- import com.jjt.biz.service.IBizObjMetricsDataService;
- import com.jjt.biz.service.IBizObjMetricsService;
- import com.jjt.biz.vo.ScoreVO;
- import com.jjt.common.core.controller.BaseController;
- import com.jjt.common.core.domain.AjaxResult;
- import com.jjt.common.utils.DateUtils;
- import com.jjt.risk.domain.RiskMsConfig;
- import com.jjt.risk.service.IRiskMsConfigService;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiOperation;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import javax.annotation.Resource;
- 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-09-12
- */
- @Api(tags = "其他风险分析")
- @RestController
- @RequestMapping("/risk/other")
- public class RiskOtherController extends BaseController {
- @Resource
- private IBizModelService bizModelService;
- @Resource
- private IRiskMsConfigService riskMsConfigService;
- @Resource
- private IBizModelDetailService modelDetailService;
- @Resource
- private IBizObjMetricsService omService;
- @Resource
- private IBizObjMetricsDataService dataService;
- @Resource
- private JdbcTemplate jdbcTemplate;
- @ApiOperation("模型列表")
- @GetMapping(value = "/model/list")
- public AjaxResult modelList() {
- List<BizModel> list = bizModelService.selectBizModelList(new BizModel());
- return success(list);
- }
- @ApiOperation("业务主机分析")
- @GetMapping(value = "/host/{modelId}")
- public AjaxResult host(@PathVariable("modelId") Long modelId) {
- RiskMsConfig q = new RiskMsConfig();
- q.setConfigType("host");
- List<RiskMsConfig> configs = riskMsConfigService.selectRiskMsConfigList(q);
- LocalDateTime endTime = LocalDateTime.now();
- LocalDateTime beginTime = endTime.minusDays(7);
- String begin = beginTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
- String end = endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
- List<Map<String, Object>> result = new ArrayList<>();
- List<BizModelDetail> mdList = modelDetailService.selectBizModelDetailList4ModelId(modelId);
- for (RiskMsConfig config : configs) {
- Map<String, Object> map = new HashMap<>(16);
- map.put("title", config.getViewName());
- List<Map<String, Object>> dataList = new ArrayList<>();
- for (BizModelDetail md : mdList) {
- Map<String, Object> dataMap = new HashMap<>(16);
- dataMap.put("objName", md.getBizObj().getObjName());
- if ("2".equals(config.getRankingBased())) {
- dataMap.put("alarms", new Random().nextInt(50));
- }
- BizObjMetrics bom = new BizObjMetrics();
- bom.setObjId(md.getObjId());
- bom.setMetricsId(config.getMetricsId());
- List<BizObjMetrics> oms = omService.selectBizObjMetricsList(bom);
- if (oms.size() > 0) {
- BizObjMetrics om = oms.get(0);
- dataMap.put("id", om.getObjMetricsId());
- dataMap.put("value", om.getDValue().floatValue());
- if ("2".equals(config.getRankingBased())) {
- 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=?";
- Map<String, Object> dMap = jdbcTemplate.queryForMap(sql, begin, end, om.getObjMetricsId());
- Long num = (Long) dMap.get("num");
- dataMap.put("alarms", num.intValue());
- dataMap.put("value", dMap.get("times"));
- } else {
- dataMap.put("value", om.getDValue().floatValue());
- }
- dataList.add(dataMap);
- }
- }
- if ("2".equals(config.getRankingBased())) {
- dataList = dataList.stream().sorted(
- (o1, o2) -> -Integer.compare((int) o1.get("alarms"), (int) o2.get("alarms"))
- ).collect(Collectors.toList());
- } else {
- dataList = dataList.stream().sorted(
- (o1, o2) -> -Float.compare((float) o1.get("value"), (float) o2.get("value"))
- ).collect(Collectors.toList());
- }
- if (dataList.size() > 0) {
- map.put("data", dataList);
- result.add(map);
- }
- }
- return success(result);
- }
- @ApiOperation("业务主机分析趋势")
- @GetMapping(value = "/host/trend/{id}")
- public AjaxResult hostTrend(@PathVariable("id") Long id) {
- Map<String, Object> result = new HashMap<>(16);
- List<String> xData = new ArrayList<>();
- List<Float> curr = new ArrayList<>();
- LocalDateTime endTime = LocalDateTime.now();
- LocalDateTime beginTime = endTime.minusDays(7);
- Map<String, Object> 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(id);
- q.setParams(params);
- List<BizObjMetricsData> list = dataService.selectBizObjMetricsDataList(q);
- list.forEach(data -> {
- Map<String, Object> map = new HashMap<>(16);
- xData.add(DateUtils.dateTime(data.getCreateTime()));
- curr.add(data.getdValue().floatValue());
- });
- result.put("time", xData);
- result.put("data", curr);
- return success(result);
- }
- @ApiOperation("网络状况分析")
- @GetMapping(value = "/network/{modelId}/{metricsId}")
- public AjaxResult network(@PathVariable("modelId") Long modelId, @PathVariable("metricsId") Long metricsId) {
- List<Map<String, Object>> result = new ArrayList<>();
- modelDetailService.selectBizModelDetailList4ModelId(modelId).forEach(md -> {
- Map<String, Object> objectMap = new HashMap<>(16);
- objectMap.put("name", md.getBizObj().getObjName());
- BizObjMetrics bom = new BizObjMetrics();
- bom.setObjId(md.getObjId());
- bom.setMetricsId(metricsId);
- List<BizObjMetrics> oms = omService.selectBizObjMetricsList(bom);
- if (oms.size() > 0) {
- BizObjMetrics om = oms.get(0);
- List<Map<String, Object>> trendList = new ArrayList<>();
- LocalDateTime endTime = LocalDateTime.now();
- LocalDateTime beginTime = endTime.minusDays(7);
- Map<String, Object> 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(om.getObjMetricsId());
- q.setParams(params);
- List<BizObjMetricsData> list = dataService.selectBizObjMetricsDataList(q);
- list.forEach(data -> {
- Map<String, Object> map = new HashMap<>(16);
- map.put("time", data.getCreateTime());
- map.put("value", data.getdValue().floatValue());
- trendList.add(map);
- });
- objectMap.put("data", trendList);
- result.add(objectMap);
- }
- });
- // String[] objs = {"node1", "node2", "node3", "ecs4", "业务对象5", "虚拟主机6", "cluster7"};
- // for (String obj : objs) {
- // Map<String, Object> map = new HashMap<>(16);
- // map.put("name", obj);
- //
- // List<Map<String, Object>> dataList = new ArrayList<>();
- //
- // LocalDateTime ed = LocalDateTime.now();
- // LocalDateTime st = ed.minusDays(30);
- // Random r = new Random();
- // DecimalFormat df = new DecimalFormat("#0.00");
- // do {
- // Map<String, Object> dataMap = new HashMap<>(16);
- // long time = st.toEpochSecond(ZoneOffset.ofHours(8)) * 1000;
- // dataMap.put("time", time);
- // float f = r.nextFloat() * 100;
- // dataMap.put("value", Float.parseFloat(df.format(f)));
- // st = st.plusDays(1);
- // dataList.add(dataMap);
- // } while (!st.isAfter(ed));
- // map.put("data", dataList);
- //
- // result.add(map);
- // }
- return success(result);
- }
- @ApiOperation("可持续性服务评估")
- @GetMapping(value = "/server")
- public AjaxResult server() {
- Map<String, Object> result = new HashMap<>(16);
- List<ScoreVO> list = new ArrayList<>();
- List<Map<String, Object>> topList = new ArrayList<>();
- String[] names = {"市场服务", "市场出清", "信息发布", "市场合规", "市场结算"};
- for (int i = 0; i < names.length; i++) {
- Map<String, Object> map = new HashMap<>(16);
- map.put("name", names[i]);
- map.put("score", Float.valueOf(new Random().nextInt(50) + 50));
- topList.add(map);
- ScoreVO vo = new ScoreVO();
- List<String> xData = new ArrayList<>();
- List<Float> scores = new ArrayList<>();
- LocalDate time = LocalDate.now().minusDays(31);
- for (int j = 0; j < 30; j++) {
- time = time.plusDays(1);
- xData.add(time.getMonthValue() + "-" + time.getDayOfMonth());
- Float score = Float.valueOf(new Random().nextInt(50) + 50);
- scores.add(score);
- }
- vo.setModelId((long) (i + 1));
- vo.setModelName(names[i]);
- vo.setScores(scores);
- vo.setXData(xData);
- list.add(vo);
- }
- topList.sort(Comparator.comparing(map -> (Float) map.get("score")));
- result.put("trend", list);
- result.put("top", topList);
- return success(result);
- }
- }
|