Pārlūkot izejas kodu

告警统计相关业务功能

wukai 1 gadu atpakaļ
vecāks
revīzija
e7e947cb60

+ 27 - 3
jjt-admin/src/test/java/com/test/AlarmTest.java

@@ -6,11 +6,13 @@ import com.jjt.biz.domain.BizObjMetrics;
 import com.jjt.biz.mapper.AlarmRecordMapper;
 import com.jjt.biz.service.IAlarmRecordService;
 import com.jjt.biz.service.IBizObjMetricsService;
+import com.jjt.common.utils.DateUtils;
 import org.apache.ibatis.session.ExecutorType;
 import org.apache.ibatis.session.SqlSession;
 import org.apache.ibatis.session.SqlSessionFactory;
 import org.junit.jupiter.api.Test;
 import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.ActiveProfiles;
 
 import javax.annotation.Resource;
 import java.math.BigDecimal;
@@ -21,7 +23,7 @@ import java.util.stream.Collectors;
  *
  */
 @SpringBootTest(classes = JjtApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
-//@ActiveProfiles("test")
+@ActiveProfiles("test")
 public class AlarmTest {
     @Resource
     private IBizObjMetricsService objMetricsService;
@@ -32,9 +34,20 @@ public class AlarmTest {
 
     public static void main(String[] args) {
         long end = new Date().getTime();
+        System.err.println(end);
+        Random random = new Random();
         System.err.println(new Date(end));
+        List<Date> list = new ArrayList<>();
         for (int i = 0; i < 1000; i++) {
-
+            long xx = random.nextInt(2592000) * 1000l;
+            long randomMillis = new Date().getTime() - xx;
+            list.add(new Date(randomMillis));
+//            System.err.println(new Date(randomMillis));
+        }
+//
+        list.sort(Comparator.reverseOrder());
+        for (Date d : list) {
+            System.err.println(DateUtils.dateTime(d));
         }
     }
 
@@ -52,8 +65,13 @@ public class AlarmTest {
             record.setObjMetricsId(om.getObjMetricsId());
             String level = levels[random.nextInt(3)];
             record.setAlarmLevel(level);
+            record.setAlarmType("1");
+            if (flag()) {
+                record.setAlarmType("2");
+                record.setEndTime(new Date());
+            }
             record.setAlarmValue(BigDecimal.valueOf(random.nextInt(100)));
-            long randomMillis = new Date().getTime() - Math.abs(random.nextInt(2592000)) * 1000;
+            long randomMillis = new Date().getTime() - random.nextInt(2592000) * 1000l;
             record.setAlarmTime(new Date(randomMillis));
             records.add(record);
         }
@@ -66,4 +84,10 @@ public class AlarmTest {
             sqlSession.commit();
         }
     }
+
+    private boolean flag() {
+        Random r = new Random();
+        int x = r.nextInt(8889) + 1;
+        return x % 2 == 1;
+    }
 }

+ 87 - 63
jjt-biz/src/main/java/com/jjt/biz/controller/AlarmRecordController.java

@@ -9,6 +9,8 @@ import com.jjt.biz.vo.LevelSortVO;
 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.StringUtils;
+import com.jjt.system.service.ISysDictDataService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import io.swagger.annotations.ApiParam;
@@ -36,6 +38,8 @@ public class AlarmRecordController extends BaseController {
     private IAlarmRecordService alarmRecordService;
     @Resource
     private JdbcTemplate jdbcTemplate;
+    @Resource
+    private ISysDictDataService sysDictDataService;
 
     /**
      * 查询告警记录列表
@@ -64,31 +68,64 @@ public class AlarmRecordController extends BaseController {
     @GetMapping("/list/bizType")
     public AjaxResult listBizType(@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();
-//      TODO 暂时使用模拟数据
+        String sql = "SELECT b.obj_type,COUNT(*) num FROM alarm_record a,biz_obj b WHERE a.obj_id=b.obj_id";
+        Vector<Object> v = new Vector<>();
+        if (start != null && end != null) {
+            sql += " AND a.alarm_time BETWEEN ? AND ?";
+            v.add(start);
+            v.add(end);
+        }
+        sql += " GROUP BY b.obj_type";
+        List<Map<String, Object>> results = jdbcTemplate.queryForList(sql, v.toArray());
+        results.forEach(map -> {
+            BizTypeVO ls = new BizTypeVO();
 
-//        String sql = "SELECT b.obj_type,COUNT(*) num FROM alarm_record a,biz_obj b WHERE a.obj_id=b.obj_id";
-//        Vector<Object> v = new Vector<>();
-//        if (start != null && end != null) {
-//            sql += " AND a.alarm_time BETWEEN ? AND ?";
-//            v.add(start);
-//            v.add(end);
+            String obj_type = (String) map.get("obj_type");
+            ls.setId(obj_type);
+            ls.setName(sysDictDataService.selectDictLabel("biz_type", obj_type));
+            ls.setNum((Long) map.get("num"));
+            list.add(ls);
+        });
+//        测试数据
+//        String[] names = {"mysql", "基础支撑", "运行环境", "ES搜索服务", "nginx", "redis", "负载均衡", "其他", "JAVA应用"};
+//        for (int i = 0; i < names.length; i++) {
+//            BizTypeVO vo = new BizTypeVO();
+//            vo.setBizType(names[i]);
+//            long num = Long.valueOf(new Random().nextInt(200) + 10);
+//            vo.setNum(num);
+//            list.add(vo);
 //        }
-//        sql += " GROUP BY b.obj_type";
-//        List<Map<String, Object>> results = jdbcTemplate.queryForList(sql, v.toArray());
-//        results.forEach(map -> {
-//            BizTypeVO ls = new BizTypeVO();
-//            ls.setBizType((String) map.get("obj_type"));
-//            ls.setNum((Long) map.get("num"));
-//            list.add(ls);
-//        });
-        String[] names = {"mysql", "基础支撑", "运行环境", "ES搜索服务", "nginx", "redis", "负载均衡", "其他", "JAVA应用"};
-        for (int i = 0; i < names.length; i++) {
-            BizTypeVO vo = new BizTypeVO();
-            vo.setBizType(names[i]);
-            long num = Long.valueOf(new Random().nextInt(200) + 10);
-            vo.setNum(num);
-            list.add(vo);
+
+        list.sort(Comparator.comparing(BizTypeVO::getNum));
+        return AjaxResult.success(list);
+    }
+
+    @ApiOperation("业务类型统计")
+    @GetMapping("/list/bizObj")
+    public AjaxResult listBizObj(@ApiParam(value = "业务类型") String type, @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.obj_id,b.obj_name,COUNT(*) num FROM alarm_record a,biz_obj b WHERE a.obj_id=b.obj_id";
+        Vector<Object> v = new Vector<>();
+        if (StringUtils.isNotEmpty(type)) {
+            sql += " AND b.obj_type=?";
+            v.add(type);
+        }
+        if (start != null && end != null) {
+            sql += " AND a.alarm_time BETWEEN ? AND ?";
+            v.add(start);
+            v.add(end);
         }
+        sql += " GROUP BY b.obj_id,b.obj_name";
+        List<Map<String, Object>> results = jdbcTemplate.queryForList(sql, v.toArray());
+        results.forEach(map -> {
+            BizTypeVO ls = new BizTypeVO();
+            String obj_name = (String) map.get("obj_name");
+            int obj_id = (int) map.get("obj_id");
+            ls.setId(obj_id + "");
+            ls.setName(obj_name);
+            ls.setNum((Long) map.get("num"));
+            list.add(ls);
+        });
 
         list.sort(Comparator.comparing(BizTypeVO::getNum));
         return AjaxResult.success(list);
@@ -99,32 +136,32 @@ public class AlarmRecordController extends BaseController {
     public AjaxResult listBizSort(@ApiParam(value = "分类") String bizType, @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();
 //          TODO 暂时使用模拟数据
-//        String sql = "SELECT a.obj_id,b.obj_name,b.obj_type,COUNT(*) num FROM alarm_record a,biz_obj b WHERE a.obj_id=b.obj_id";
-//        Vector<Object> v = new Vector<>();
-//        if (StringUtils.isNotEmpty(bizType)) {
-//            sql += " AND b.obj_type=?";
-//            v.add(bizType);
-//        }
-//        if (start != null && end != null) {
-//            sql += " AND a.alarm_time BETWEEN ? AND ?";
-//            v.add(start);
-//            v.add(end);
-//        }
-//        sql += " GROUP BY a.obj_id,b.obj_name,b.obj_type";
-//        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"));
-//            ls.setBizType((String) map.get("obj_type"));
-//            ls.setNum((Long) map.get("num"));
-//            list.add(ls);
-//        });
-
-        for (int i = 0; i < 10; i++) {
-            BizSortVO vo = new BizSortVO(true);
-            list.add(vo);
+        String sql = "SELECT a.obj_id,b.obj_name,b.obj_type,COUNT(*) num FROM alarm_record a,biz_obj b WHERE a.obj_id=b.obj_id";
+        Vector<Object> v = new Vector<>();
+        if (StringUtils.isNotEmpty(bizType)) {
+            sql += " AND b.obj_type=?";
+            v.add(bizType);
         }
+        if (start != null && end != null) {
+            sql += " AND a.alarm_time BETWEEN ? AND ?";
+            v.add(start);
+            v.add(end);
+        }
+        sql += " GROUP BY a.obj_id,b.obj_name,b.obj_type";
+        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"));
+            ls.setBizType((String) map.get("obj_type"));
+            ls.setNum((Long) map.get("num"));
+            list.add(ls);
+        });
+
+//        for (int i = 0; i < 10; i++) {
+//            BizSortVO vo = new BizSortVO(true);
+//            list.add(vo);
+//        }
         list.sort(Comparator.comparing(BizSortVO::getNum));
         return AjaxResult.success(list);
     }
@@ -176,23 +213,10 @@ public class AlarmRecordController extends BaseController {
     @GetMapping("/list/levelSort")
     public AjaxResult listLevelSort(AlarmRecord alarmRecord) {
         List<LevelSortVO> list = new ArrayList();
-        Vector<Object> v = new Vector<>();
-        String sql = "SELECT alarm_level,COUNT(*) num FROM alarm_record where 1=1";
-        if (alarmRecord.getObjId() != null) {
-            sql += " and OBJ_ID = ?";
-            v.add(alarmRecord.getObjId());
-        }
-        if (alarmRecord.getParams().get("beginAlarmTime") != null && alarmRecord.getParams().get("endAlarmTime") != null) {
-            sql += " AND alarm_time BETWEEN ? AND ?";
-            v.add(alarmRecord.getParams().get("beginAlarmTime"));
-            v.add(alarmRecord.getParams().get("endAlarmTime") != null);
-        }
-        sql += " GROUP BY alarm_level";
-        List<Map<String, Object>> results = jdbcTemplate.queryForList(sql, v.toArray());
-
+        List<Map<String, Object>> results = alarmRecordService.levelSort(alarmRecord);
         results.forEach(map -> {
             LevelSortVO ls = new LevelSortVO();
-            ls.setLevel((String) map.get("alarm_level"));
+            ls.setLevel((String) map.get("ALARM_LEVEL"));
             switch (ls.getLevel()) {
                 case "low":
                     ls.setName("低");
@@ -205,7 +229,7 @@ public class AlarmRecordController extends BaseController {
                     break;
                 default:
             }
-            ls.setNum((Long) map.get("num"));
+            ls.setNum((Long) map.get("NUM"));
             list.add(ls);
         });
         return AjaxResult.success(list);

+ 7 - 7
jjt-biz/src/main/java/com/jjt/biz/domain/AlarmRecord.java

@@ -36,17 +36,17 @@ public class AlarmRecord extends BaseEntity {
     @Excel(name = "告警对象")
     private Long objId;
 
-    @ApiModelProperty("对象详情")
-    private BizObj bizobj;
+    @ApiModelProperty("对象类型")
+    private String objType;
+
+    @ApiModelProperty("对象名称")
+    private String objName;
 
-    /**
-     * 告警指标
-     */
     @ApiModelProperty("告警指标")
     @Excel(name = "告警指标")
     private Long objMetricsId;
-    @ApiModelProperty("指标详情")
-    private BizObjMetrics bizObjMetrics;
+    @Excel(name = "指标名称")
+    private String metricsName;
 
     /**
      * 告警类型

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

@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.jjt.biz.domain.AlarmRecord;
 
 import java.util.List;
+import java.util.Map;
 
 /**
  * 告警记录Mapper接口
@@ -59,4 +60,12 @@ public interface AlarmRecordMapper extends BaseMapper<AlarmRecord> {
      * @return 结果
      */
     public int deleteAlarmRecordByAlarmIds(Long[] alarmIds);
+
+    /**
+     * 告警等级排行
+     *
+     * @param alarmRecord 对象
+     * @return 结果
+     */
+    List<Map<String, Object>> levelSort(AlarmRecord alarmRecord);
 }

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

@@ -3,6 +3,7 @@ package com.jjt.biz.service;
 import com.jjt.biz.domain.AlarmRecord;
 
 import java.util.List;
+import java.util.Map;
 
 /**
  * 告警记录Service接口
@@ -58,4 +59,11 @@ public interface IAlarmRecordService {
      * @return 结果
      */
     public int deleteAlarmRecordByAlarmId(Long alarmId);
+
+    /**
+     * 告警等级排行
+     * @param alarmRecord 对象
+     * @return 结果
+     */
+    List<Map<String, Object>> levelSort(AlarmRecord alarmRecord);
 }

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

@@ -8,6 +8,7 @@ import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
 import java.util.List;
+import java.util.Map;
 
 /**
  * 告警记录Service业务层处理
@@ -87,4 +88,15 @@ public class AlarmRecordServiceImpl implements IAlarmRecordService {
     public int deleteAlarmRecordByAlarmId(Long alarmId) {
         return alarmRecordMapper.deleteAlarmRecordByAlarmId(alarmId);
     }
+
+    /**
+     * 告警等级排行
+     *
+     * @param alarmRecord 对象
+     * @return 结果
+     */
+    @Override
+    public List<Map<String, Object>> levelSort(AlarmRecord alarmRecord) {
+        return alarmRecordMapper.levelSort(alarmRecord);
+    }
 }

+ 3 - 11
jjt-biz/src/main/java/com/jjt/biz/vo/BizTypeVO.java

@@ -5,8 +5,6 @@ import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 
-import java.util.Random;
-
 /**
  * 业务类型统计
  *
@@ -17,16 +15,10 @@ import java.util.Random;
 @Data
 @NoArgsConstructor
 public class BizTypeVO {
+    @ApiModelProperty("ID")
+    private String id;
     @ApiModelProperty("业务类型")
-    private String bizType;
+    private String name;
     @ApiModelProperty("告警数量")
     private Long num;
-
-    public BizTypeVO(boolean flag) {
-        if (flag) {
-            this.bizType = "" + (new Random().nextInt(10) + 1);
-            this.num = Long.valueOf(new Random().nextInt(200) + 10);
-        }
-
-    }
 }

+ 70 - 19
jjt-biz/src/main/resources/mapper/alarm/AlarmRecordMapper.xml

@@ -19,25 +19,9 @@
         <result property="updateBy" column="UPDATE_BY"/>
         <result property="updateTime" column="UPDATE_TIME"/>
         <result property="remark" column="REMARK"/>
-        <association property="bizobj" javaType="BizObj">
-            <result property="objType" column="OBJ_TYPE"/>
-            <result property="appId" column="APP_ID"/>
-            <result property="accessTimes" column="ACCESS_TIMES"/>
-            <result property="objName" column="OBJ_NAME"/>
-            <result property="objAddr" column="OBJ_ADDR"/>
-            <result property="objPort" column="OBJ_PORT"/>
-        </association>
-        <association property="bizObjMetrics" javaType="BizObjMetrics">
-            <result property="tplId" column="TPL_ID"/>
-            <result property="metricsId" column="METRICS_ID"/>
-            <result property="metricsName" column="METRICS_NAME"/>
-            <result property="metricsCode" column="METRICS_CODE"/>
-            <result property="alarmLow" column="ALARM_LOW"/>
-            <result property="alarmMid" column="ALARM_MID"/>
-            <result property="alarmHigh" column="ALARM_HIGH"/>
-            <result property="dValue" column="D_VALUE"/>
-            <result property="dataExp" column="DATA_EXP"/>
-        </association>
+        <result property="objType" column="OBJ_TYPE"/>
+        <result property="objName" column="OBJ_NAME"/>
+        <result property="metricsName" column="METRICS_NAME"/>
     </resultMap>
 
     <sql id="selectAlarmRecordVo">
@@ -116,6 +100,73 @@
         <include refid="selectAlarmRecordVo"/>
         where ALARM_ID = #{alarmId}
     </select>
+    <select id="levelSort" resultType="java.util.Map">
+        select ALARM_LEVEL,COUNT(*) NUM
+        from (SELECT A.ALARM_ID,
+                     A.OBJ_ID,
+                     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 alarm_level
+    </select>
 
     <insert id="insertAlarmRecord" parameterType="AlarmRecord" useGeneratedKeys="true"
             keyProperty="alarmId">