wukai 9 сар өмнө
parent
commit
a02262493c

+ 119 - 42
jjt-biz/src/main/java/com/jjt/biz/controller/AlarmRecordController.java

@@ -22,7 +22,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 import javax.annotation.Resource;
-import java.math.BigDecimal;
 import java.util.*;
 
 /**
@@ -136,7 +135,7 @@ public class AlarmRecordController extends BaseController {
     @GetMapping("/list/getMetrics")
     public AjaxResult getMetrics(@ApiParam(value = "对象ID") Long objId, @ApiParam(value = "开始时间yyyy-mm-dd") @DateTimeFormat(pattern = "yyyy-MM-dd") Date start, @ApiParam(value = "结束时间yyyy-mm-dd") @DateTimeFormat(pattern = "yyyy-MM-dd") Date end) {
         List<BizTypeVO> list = new ArrayList();
-        String sql = "SELECT b.metrics_name,COUNT(*) num FROM alarm_record a,biz_obj_metrics b WHERE a.obj_metrics_id=b.obj_metrics_id";
+        String sql = "SELECT a.obj_metrics_id,b.metrics_name,COUNT(*) num FROM alarm_record a,biz_obj_metrics b WHERE a.obj_metrics_id=b.obj_metrics_id";
         Vector<Object> v = new Vector<>();
         if (objId != null) {
             sql += " AND b.obj_id=?";
@@ -147,10 +146,12 @@ public class AlarmRecordController extends BaseController {
             v.add(start);
             v.add(end);
         }
-        sql += "  GROUP by b.metrics_name order by num desc";
+        sql += "  GROUP by a.obj_metrics_id,b.metrics_name order by num desc";
         List<Map<String, Object>> results = jdbcTemplate.queryForList(sql, v.toArray());
         results.forEach(map -> {
             BizTypeVO ls = new BizTypeVO();
+            Integer obj_id = (Integer) map.get("obj_metrics_id");
+            ls.setId(obj_id + "");
             String obj_name = (String) map.get("metrics_name");
             ls.setName(obj_name);
             ls.setNum((Long) map.get("num"));
@@ -198,40 +199,46 @@ public class AlarmRecordController extends BaseController {
     @ApiOperation("系统压力排名")
     @GetMapping("/list/bizAccess")
     public AjaxResult listBizAccess(@ApiParam(value = "开始时间yyyy-mm-dd") @DateTimeFormat(pattern = "yyyy-MM-dd") Date start, @ApiParam(value = "结束时间yyyy-mm-dd") @DateTimeFormat(pattern = "yyyy-MM-dd") Date end) {
-        List<BizAccessVO> list = new ArrayList();
-        String sql = "SELECT a.model_id,a.model_name,ifnull(sum(c.ACCESS_TIMES),0) times FROM biz_model a,biz_model_detail b,biz_obj c" +
-                " WHERE a.model_id=b.model_id AND b.obj_id=c.obj_id GROUP BY a.model_id,a.model_name ORDER BY times desc";
-        List<Map<String, Object>> mdList = jdbcTemplate.queryForList(sql);
-        mdList.forEach(map -> {
-            BizAccessVO vo = new BizAccessVO();
-            Integer modelId = (Integer) map.get("model_id");
-            String modelName = (String) map.get("model_name");
-            BigDecimal times = (BigDecimal) map.get("times");
-            vo.setAccess(times.longValue());
-            vo.setModelId(Long.valueOf(modelId));
-            vo.setModelName(modelName);
-            Vector<Object> v = new Vector<>();
-            String sql1 = "SELECT COUNT(*) FROM alarm_record WHERE obj_id IN(SELECT obj_id FROM biz_model_detail WHERE model_id=?)";
-            v.add(modelId);
-            if (start != null && end != null) {
-                sql1 += " AND alarm_time BETWEEN ? AND ?";
-                v.add(start);
-                v.add(end);
-            }
-            Long num = jdbcTemplate.queryForObject(sql1, Long.class, v.toArray());
-            vo.setNum(num);
-            list.add(vo);
-        });
-//        String[] names = {"市场服务", "市场出清", "市场结算", "市场合规", "信息发布"};
-//        for (int i = 0; i < names.length; i++) {
+        List<BizAccessVO> list = new ArrayList<>();
+//        //TODO 还是得先用模拟数据。
+//        String sql = "SELECT a.model_id,a.model_name,ifnull(sum(c.ACCESS_TIMES),0) times FROM biz_model a,biz_model_detail b,biz_obj c" +
+//                " WHERE a.model_id=b.model_id AND b.obj_id=c.obj_id GROUP BY a.model_id,a.model_name ORDER BY times desc";
+//        List<Map<String, Object>> mdList = jdbcTemplate.queryForList(sql);
+//        mdList.forEach(map -> {
 //            BizAccessVO vo = new BizAccessVO();
-//            vo.setModelName(names[i]);
-//            long access = Long.valueOf(new Random().nextInt(600) + 10);
-//            long num = Long.valueOf(new Random().nextInt(200) + 10);
-//            vo.setAccess(access);
+//            Integer modelId = (Integer) map.get("model_id");
+//            String modelName = (String) map.get("model_name");
+//            BigDecimal times = (BigDecimal) map.get("times");
+//            vo.setAccess(times.longValue());
+//            vo.setModelId(Long.valueOf(modelId));
+//            vo.setModelName(modelName);
+//            Vector<Object> v = new Vector<>();
+//            String sql1 = "SELECT COUNT(*) FROM alarm_record WHERE obj_id IN(SELECT obj_id FROM biz_model_detail WHERE model_id=?)";
+//            v.add(modelId);
+//            if (start != null && end != null) {
+//                sql1 += " AND alarm_time BETWEEN ? AND ?";
+//                v.add(start);
+//                v.add(end);
+//            }
+//            Long num = jdbcTemplate.queryForObject(sql1, Long.class, v.toArray());
 //            vo.setNum(num);
 //            list.add(vo);
-//        }
+//        });
+        String[] names = {"市场服务", "市场出清", "市场结算", "市场合规", "信息发布"};
+        int[] as = new int[5];
+        int[] nums = new int[5];
+        for (int i = 0; i < as.length; i++) {
+            as[i] = new Random().nextInt(1000) + 200;
+            nums[i] = new Random().nextInt(500);
+        }
+        for (int i = 0; i < names.length; i++) {
+            BizAccessVO vo = new BizAccessVO();
+            vo.setModelId((long) (i + 1));
+            vo.setModelName(names[i]);
+            vo.setAccess((long) as[i]);
+            vo.setNum((long) nums[i]);
+            list.add(vo);
+        }
 
         list.sort(Comparator.comparing(BizAccessVO::getAccess).reversed());
         return AjaxResult.success(list);
@@ -240,12 +247,16 @@ public class AlarmRecordController extends BaseController {
     @ApiOperation("组件告警数量排名")
     @GetMapping("/list/objAlarm")
     public AjaxResult objAlarm(@ApiParam(value = "对象ID") Long modelId, @ApiParam(value = "开始时间yyyy-mm-dd") @DateTimeFormat(pattern = "yyyy-MM-dd") Date start, @ApiParam(value = "结束时间yyyy-mm-dd") @DateTimeFormat(pattern = "yyyy-MM-dd") Date end) {
+
         List<BizSortVO> list = new ArrayList();
         String sql = "SELECT a.obj_id,c.obj_name,COUNT(*) num FROM alarm_record a,biz_model_detail b,biz_obj c WHERE a.obj_id=b.obj_id AND b.obj_id=c.obj_id";
         Vector<Object> v = new Vector<>();
         if (modelId != null) {
             sql += " AND b.MODEL_ID=? ";
-            v.add(modelId);
+            //TODO 因为是模拟数据,所以这里要做个映射,之后要取消掉
+            int[] idRela = {4, 5, 15, 20, 23};
+            v.add(idRela[modelId.intValue() - 1]);
+//            v.add(modelId);
         }
         if (start != null && end != null) {
             sql += " AND a.alarm_time BETWEEN ? AND ?";
@@ -253,8 +264,8 @@ public class AlarmRecordController extends BaseController {
             v.add(end);
         }
         sql += " GROUP BY a.obj_id,c.obj_name order by num desc";
-        List<Map<String, Object>> results =  jdbcTemplate.queryForList(sql, v.toArray());
-       results.forEach(map -> {
+        List<Map<String, Object>> results = jdbcTemplate.queryForList(sql, v.toArray());
+        results.forEach(map -> {
             BizSortVO ls = new BizSortVO();
             ls.setObjId(Long.valueOf((Integer) map.get("obj_id")));
             ls.setObjName((String) map.get("obj_name"));
@@ -267,11 +278,59 @@ public class AlarmRecordController extends BaseController {
     @ApiOperation("组件压力排名")
     @GetMapping("/list/objAccess")
     public AjaxResult objAccess(@ApiParam(value = "对象ID") Long modelId, @ApiParam(value = "开始时间yyyy-mm-dd") @DateTimeFormat(pattern = "yyyy-MM-dd") Date start, @ApiParam(value = "结束时间yyyy-mm-dd") @DateTimeFormat(pattern = "yyyy-MM-dd") Date end) {
-        String sql = "SELECT b.OBJ_ID,c.OBJ_NAME,ifnull(sum(c.ACCESS_TIMES),0) TIMES FROM biz_model_detail b,biz_obj c WHERE b.obj_id=c.obj_id AND c.OBJ_TYPE=1";
-        sql += " AND b.model_id=?";
-        sql += " GROUP BY b.OBJ_ID,c.obj_name order by TIMES desc";
-        List<Map<String, Object>> mdList = jdbcTemplate.queryForList(sql, modelId);
-        return AjaxResult.success(mdList);
+//        TODO 依然得用模拟数据
+//        String sql = "SELECT b.OBJ_ID,c.OBJ_NAME,ifnull(sum(c.ACCESS_TIMES),0) TIMES FROM biz_model_detail b,biz_obj c WHERE b.obj_id=c.obj_id AND c.OBJ_TYPE=1";
+//        sql += " AND b.model_id=?";
+//        sql += " GROUP BY b.OBJ_ID,c.obj_name order by TIMES desc";
+//        List<Map<String, Object>> mdList = jdbcTemplate.queryForList(sql, modelId);
+        Map<String, Object> result = new HashMap<>(16);
+        List<BizAccessVO> list = new ArrayList<>();
+        String[] names = {"服务", "出清", "结算", "合规", "发布"};
+        String[] objs = {"USER", "mysql", "redis", "job", "auth", "biz"};
+        int[] as = new int[6];
+        int[] es = new int[6];
+        for (int i = 0; i < as.length; i++) {
+            as[i] = new Random().nextInt(300);
+            es[i] = new Random().nextInt(80);
+        }
+        for (int i = 0; i < objs.length; i++) {
+            BizAccessVO vo = new BizAccessVO();
+            String name = names[modelId.intValue() - 1];
+            vo.setModelName(name + "/" + objs[i]);
+            vo.setAccess((long) as[i]);
+            vo.setNum((long) es[i]);
+            list.add(vo);
+        }
+        list.sort(Comparator.comparing(BizAccessVO::getAccess).reversed());
+
+        List<Map<String, Object>> dataList = new ArrayList<>();
+        int[] max = new int[20];
+        int[] avg = new int[20];
+        int[] error = new int[20];
+        int hour = 14;
+        int mi = 0;
+        for (int i = 0; i < 20; i++) {
+            mi = mi + i;
+            if (mi >= 60) {
+                mi = 0;
+                hour++;
+            }
+            String time = hour + ":" + (mi < 10 ? "0" + mi : mi + "");
+            Map<String, Object> map = new HashMap<>();
+            map.put("time", time);
+            max[i] = new Random().nextInt(300);
+            avg[i] = new Random().nextInt(100);
+            error[i] = new Random().nextInt(15);
+            map.put("max", max[i]);
+            map.put("avg", avg[i]);
+            map.put("error", error[i]);
+
+            dataList.add(map);
+        }
+
+        result.put("objs", list);
+        result.put("datas", dataList);
+        return AjaxResult.success(result);
     }
 
     @ApiOperation("等级级别排名")
@@ -300,6 +359,24 @@ public class AlarmRecordController extends BaseController {
         return AjaxResult.success(list);
     }
 
+    @ApiOperation("按小时统计趋势")
+    @GetMapping("/list/hours")
+    public AjaxResult listHours(AlarmRecord alarmRecord) {
+        List<Map<String, Object>> results = alarmRecordService.listHours(alarmRecord);
+        return AjaxResult.success(results);
+    }
+
+    @ApiOperation("按指标统计")
+    @GetMapping("/calc/ms")
+    public AjaxResult calcMs(AlarmRecord alarmRecord) {
+        List<AlarmRecord> list = alarmRecordService.selectAlarmRecordList(alarmRecord);
+        List<Map<String, Object>> hours = alarmRecordService.listHours(alarmRecord);
+        Map<String, Object> map = new HashMap<>();
+        map.put("detail", list);
+        map.put("hour", hours);
+        return AjaxResult.success(map);
+    }
+
 //    /**
 //     * 导出告警记录列表
 //     */

+ 5 - 2
jjt-biz/src/main/java/com/jjt/biz/controller/BizModelController.java

@@ -295,13 +295,16 @@ public class BizModelController extends BaseController {
         List<Map<String, Object>> list = new ArrayList<>();
         String[] names = {"CPU使用率", "内存使用率", "磁盘使用率", "mysql连接数", "mysql慢查询数量"};
         String[] remarks = {"超过80%,且持续1小时", "超过60,且持续1小时", "超过90%", "超过200,且持续1小时", "当前值超过5"};
-        int[] times = {25, 35, 45, 38, 25};
+        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<String, Object> map = new HashMap<>();
             map.put("name", names[i]);
             map.put("remark", remarks[i]);
             map.put("value", times[i]);
-            if (i % 2 == 0) {
+            if (times[i] % 2 == 0) {
                 map.put("status", "正常");
             } else {
                 map.put("status", "异常");

+ 7 - 0
jjt-biz/src/main/java/com/jjt/biz/mapper/AlarmRecordMapper.java

@@ -68,4 +68,11 @@ public interface AlarmRecordMapper extends BaseMapper<AlarmRecord> {
      * @return 结果
      */
     List<Map<String, Object>> levelSort(AlarmRecord alarmRecord);
+    /**
+     * 按小时告警趋势
+     *
+     * @param alarmRecord 对象
+     * @return 结果
+     */
+    List<Map<String, Object>> listHours(AlarmRecord alarmRecord);
 }

+ 7 - 0
jjt-biz/src/main/java/com/jjt/biz/service/IAlarmRecordService.java

@@ -66,4 +66,11 @@ public interface IAlarmRecordService {
      * @return 结果
      */
     List<Map<String, Object>> levelSort(AlarmRecord alarmRecord);
+
+    /**
+     * 按小时告警趋势
+     * @param alarmRecord 对象
+     * @return 结果
+     */
+    List<Map<String, Object>> listHours(AlarmRecord alarmRecord);
 }

+ 11 - 0
jjt-biz/src/main/java/com/jjt/biz/service/impl/AlarmRecordServiceImpl.java

@@ -99,4 +99,15 @@ public class AlarmRecordServiceImpl implements IAlarmRecordService {
     public List<Map<String, Object>> levelSort(AlarmRecord alarmRecord) {
         return alarmRecordMapper.levelSort(alarmRecord);
     }
+
+    /**
+     * 按小时告警趋势
+     *
+     * @param alarmRecord 对象
+     * @return 结果
+     */
+    @Override
+    public List<Map<String, Object>> listHours(AlarmRecord alarmRecord) {
+        return alarmRecordMapper.listHours(alarmRecord);
+    }
 }

+ 68 - 0
jjt-biz/src/main/resources/mapper/alarm/AlarmRecordMapper.xml

@@ -167,6 +167,74 @@
         </where>
         GROUP BY alarm_level
     </select>
+    <select id="listHours" resultType="java.util.Map">
+        select hour,COUNT(*) num
+        from (SELECT A.ALARM_ID,
+        A.OBJ_ID,
+        DATE_FORMAT(alarm_time, '%m-%d %H:00') AS hour,
+        A.OBJ_METRICS_ID,
+        A.ALARM_TYPE,
+        A.ALARM_LEVEL,
+        A.ALARM_VALUE,
+        A.ALARM_TIME,
+        A.END_TIME,
+        A.ALARM_STATUS,
+        A.CREATE_BY,
+        A.CREATE_TIME,
+        A.UPDATE_BY,
+        A.UPDATE_TIME,
+        A.REMARK,
+        B.OBJ_TYPE,
+        B.APP_ID,
+        B.ACCESS_TIMES,
+        B.OBJ_NAME,
+        B.OBJ_ADDR,
+        B.OBJ_PORT,
+        c.TPL_ID,
+        c.METRICS_ID,
+        c.METRICS_NAME,
+        c.METRICS_CODE,
+        c.DATA_EXP,
+        c.ALARM_LOW,
+        c.ALARM_MID,
+        c.ALARM_HIGH,
+        c.D_VALUE
+        FROM alarm_record a,
+        biz_obj b,
+        biz_obj_metrics c
+        WHERE a.obj_id = b.obj_id
+        AND a.OBJ_METRICS_ID = c.OBJ_METRICS_ID) t
+        <where>
+            <if test="objId != null ">
+                and OBJ_ID = #{objId}
+            </if>
+            <if test="objMetricsId != null ">
+                and OBJ_METRICS_ID = #{objMetricsId}
+            </if>
+            <if test="alarmType != null  and alarmType != ''">
+                and ALARM_TYPE = #{alarmType}
+            </if>
+            <if test="alarmLevel != null  and alarmLevel != ''">
+                and ALARM_LEVEL = #{alarmLevel}
+            </if>
+            <if test="alarmValue != null  and alarmValue != ''">
+                and ALARM_VALUE = #{alarmValue}
+            </if>
+            <if test="alarmStatus != null  and alarmStatus != ''">
+                and ALARM_Status = #{alarmStatus}
+            </if>
+            <if test="params.beginAlarmTime != null and params.beginAlarmTime != '' and params.endAlarmTime != null and params.endAlarmTime != ''">
+                and ALARM_TIME between #{params.beginAlarmTime} and #{params.endAlarmTime}
+            </if>
+            <if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''">
+                and CREATE_TIME between #{params.beginCreateTime} and #{params.endCreateTime}
+            </if>
+            <if test="params.beginUpdateTime != null and params.beginUpdateTime != '' and params.endUpdateTime != null and params.endUpdateTime != ''">
+                and UPDATE_TIME between #{params.beginUpdateTime} and #{params.endUpdateTime}
+            </if>
+        </where>
+        GROUP BY hour order by hour
+    </select>
 
     <insert id="insertAlarmRecord" parameterType="AlarmRecord" useGeneratedKeys="true"
             keyProperty="alarmId">