Răsfoiți Sursa

告警推送相关

wukai 7 luni în urmă
părinte
comite
1348c8a32f

+ 40 - 0
jjt-admin/src/test/java/com/test/PushTest.java

@@ -0,0 +1,40 @@
+package com.test;
+
+import com.jjt.JjtApplication;
+import com.jjt.biz.domain.AlarmRecord;
+import com.jjt.push.service.IPushConfigService;
+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;
+import java.util.Date;
+import java.util.Random;
+
+/**
+ *
+ */
+@SpringBootTest(classes = JjtApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
+@ActiveProfiles("tt")
+public class PushTest {
+    @Resource
+    private IPushConfigService pushConfigService;
+
+    @Test
+    void test() {
+        AlarmRecord record = new AlarmRecord();
+        record.setObjName("测试对象");
+        record.setMetricsName("测试指标");
+        record.setAlarmTime(new Date());
+        record.setAlarmLevel("mid");
+        record.setAlarmValue(BigDecimal.valueOf(100.88));
+        pushConfigService.push(record);
+    }
+
+    private boolean flag() {
+        Random r = new Random();
+        int x = r.nextInt(8889) + 1;
+        return x % 2 == 1;
+    }
+}

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

@@ -9,6 +9,7 @@ import com.jjt.biz.vo.BizTypeVO;
 import com.jjt.common.utils.DateUtils;
 import com.jjt.common.utils.IntervalUtil;
 import com.jjt.common.utils.StringUtils;
+import com.jjt.push.service.IPushConfigService;
 import com.jjt.system.service.ISysDictDataService;
 import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.stereotype.Service;
@@ -32,6 +33,8 @@ public class AlarmRecordServiceImpl implements IAlarmRecordService {
     private JdbcTemplate jdbcTemplate;
     @Resource
     private ISysDictDataService sysDictDataService;
+    @Resource
+    private IPushConfigService pushConfigService;
 
     /**
      * 查询告警记录
@@ -168,13 +171,16 @@ public class AlarmRecordServiceImpl implements IAlarmRecordService {
         }
         record = new AlarmRecord();
         record.setObjId(om.getObjId());
+        record.setObjName(om.getObjName());
         record.setObjMetricsId(om.getObjMetricsId());
+        record.setMetricsName(om.getMetricsName());
         record.setAlarmLevel(alarmLevel);
         record.setAlarmValue(om.getDValue());
         record.setAlarmType("1");
         record.setAlarmTime(date);
         record.setCreateTime(new Date());
         result.put("add", record);
+        pushConfigService.push(record);
         return result;
 //        insertAlarmRecord(record);
     }

+ 3 - 6
jjt-biz/src/main/java/com/jjt/push/controller/PushConfigController.java

@@ -32,10 +32,7 @@ import java.util.List;
 public class PushConfigController extends BaseController {
     @Resource
     private IPushConfigService pushConfigService;
-    @Resource
-    private IMailService mailService;
-    @Resource
-    private ISmsService smsService;
+
 
     /**
      * 查询推送配置列表
@@ -53,7 +50,7 @@ public class PushConfigController extends BaseController {
     @GetMapping("/sms")
     public AjaxResult sms(@ApiParam(value = "手机号") String phone, @ApiParam(value = "短信内容") String content) {
         try {
-            smsService.send(phone, content);
+            pushConfigService.sendSms(phone, content);
         } catch (Exception e) {
             return AjaxResult.error(e.getMessage());
         }
@@ -64,7 +61,7 @@ public class PushConfigController extends BaseController {
     @GetMapping("/mail")
     public AjaxResult mail(@ApiParam(value = "邮箱") String mail, @ApiParam(value = "邮件标题") String title, @ApiParam(value = "邮件内容") String content) {
         try {
-            mailService.send(mail, title, content);
+            pushConfigService.sendMail(mail, title, content);
         } catch (Exception e) {
             return AjaxResult.error(e.getMessage());
         }

+ 35 - 10
jjt-biz/src/main/java/com/jjt/push/service/IPushConfigService.java

@@ -1,19 +1,20 @@
 package com.jjt.push.service;
 
-import java.util.List;
+import com.jjt.biz.domain.AlarmRecord;
 import com.jjt.push.domain.PushConfig;
 
+import java.util.List;
+
 /**
  * 推送配置Service接口
- * 
+ *
  * @author jjt
  * @date 2024-10-08
  */
-public interface IPushConfigService 
-{
+public interface IPushConfigService {
     /**
      * 查询推送配置
-     * 
+     *
      * @param pcId 推送配置主键
      * @return 推送配置
      */
@@ -21,7 +22,7 @@ public interface IPushConfigService
 
     /**
      * 查询推送配置列表
-     * 
+     *
      * @param pushConfig 推送配置
      * @return 推送配置集合
      */
@@ -29,7 +30,7 @@ public interface IPushConfigService
 
     /**
      * 新增推送配置
-     * 
+     *
      * @param pushConfig 推送配置
      * @return 结果
      */
@@ -37,7 +38,7 @@ public interface IPushConfigService
 
     /**
      * 修改推送配置
-     * 
+     *
      * @param pushConfig 推送配置
      * @return 结果
      */
@@ -45,7 +46,7 @@ public interface IPushConfigService
 
     /**
      * 批量删除推送配置
-     * 
+     *
      * @param pcIds 需要删除的推送配置主键集合
      * @return 结果
      */
@@ -53,9 +54,33 @@ public interface IPushConfigService
 
     /**
      * 删除推送配置信息
-     * 
+     *
      * @param pcId 推送配置主键
      * @return 结果
      */
     public int deletePushConfigByPcId(Long pcId);
+
+    /**
+     * 发送短信
+     *
+     * @param phone   手机号
+     * @param content 内容
+     */
+    void sendSms(String phone, String content) throws Exception;
+
+    /**
+     * 推送告警信息
+     *
+     * @param record 告警记录
+     */
+    void push(AlarmRecord record);
+
+    /**
+     * 发送邮件
+     *
+     * @param mail    邮箱
+     * @param title   标题
+     * @param content 内容
+     */
+    void sendMail(String mail, String title, String content) throws Exception;
 }

+ 13 - 1
jjt-biz/src/main/java/com/jjt/push/service/ISmsService.java

@@ -8,7 +8,7 @@ package com.jjt.push.service;
  */
 public interface ISmsService {
     /**
-     * 发送邮件
+     * 发送短信
      *
      * @param phone   手机号
      * @param content 内容
@@ -16,4 +16,16 @@ public interface ISmsService {
      * @throws Exception 异常
      */
     boolean send(String phone, String content) throws Exception;
+
+    /**
+     * 发送邮件
+     * 发送短信
+     *
+     * @param uri     地址
+     * @param phone   手机号
+     * @param content 内容
+     * @return 结果
+     * @throws Exception 异常
+     */
+    boolean send(String uri, String phone, String content) throws Exception;
 }

+ 115 - 4
jjt-biz/src/main/java/com/jjt/push/service/impl/PushConfigServiceImpl.java

@@ -1,11 +1,20 @@
 package com.jjt.push.service.impl;
 
-import java.util.List;
-import org.springframework.stereotype.Service;
-import com.jjt.push.mapper.PushConfigMapper;
+import cn.hutool.json.JSONUtil;
+import com.jjt.biz.domain.AlarmRecord;
+import com.jjt.common.utils.DateUtils;
+import com.jjt.common.utils.StringUtils;
 import com.jjt.push.domain.PushConfig;
+import com.jjt.push.mapper.PushConfigMapper;
+import com.jjt.push.service.IMailService;
 import com.jjt.push.service.IPushConfigService;
+import com.jjt.push.service.ISmsService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Service;
+
 import javax.annotation.Resource;
+import java.util.List;
+import java.util.Map;
 
 /**
  * 推送配置Service业务层处理
@@ -14,9 +23,14 @@ import javax.annotation.Resource;
  * @date 2024-10-08
  */
 @Service
+@Slf4j
 public class PushConfigServiceImpl implements IPushConfigService {
     @Resource
     private PushConfigMapper pushConfigMapper;
+    @Resource
+    private IMailService mailService;
+    @Resource
+    private ISmsService smsService;
 
     /**
      * 查询推送配置
@@ -48,7 +62,7 @@ public class PushConfigServiceImpl implements IPushConfigService {
      */
     @Override
     public int insertPushConfig(PushConfig pushConfig) {
-            return pushConfigMapper.insertPushConfig(pushConfig);
+        return pushConfigMapper.insertPushConfig(pushConfig);
     }
 
     /**
@@ -83,4 +97,101 @@ public class PushConfigServiceImpl implements IPushConfigService {
     public int deletePushConfigByPcId(Long pcId) {
         return pushConfigMapper.deletePushConfigByPcId(pcId);
     }
+
+    /**
+     * 发送短信
+     *
+     * @param phone   手机号
+     * @param content 内容
+     */
+    @Override
+    public void sendSms(String phone, String content) throws Exception {
+        smsService.send(phone, content);
+    }
+
+    /**
+     * 推送告警信息
+     *
+     * @param record 告警记录
+     */
+    @Override
+    public void push(AlarmRecord record) {
+        //获取短信接口发送地址
+        PushConfig pc = pushConfigMapper.selectPushConfigByPcId(1L);
+        String title = record.getObjName() + "-" + record.getMetricsName() + "超过阈值";
+        String content = "";
+        String phones = "";
+        String mails = "";
+        content += "告警指标:" + record.getMetricsName() + "\n";
+        content += "告警对象:" + record.getObjName() + "\n";
+        content += "告警时间:" + DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,record.getAlarmTime()) + "\n";
+        switch (record.getAlarmLevel()) {
+            case "low":
+                title = "轻度告警:" + title;
+                phones = pc.getLowSms();
+                mails = pc.getLowMail();
+                content += "告警等级:低\n";
+                break;
+            case "mid":
+                title = "一般告警:" + title;
+                phones = pc.getMidSms();
+                mails = pc.getMidMail();
+                content += "告警等级:中\n";
+                break;
+            case "high":
+                title = "严重告警:" + title;
+                phones = pc.getHighSms();
+                mails = pc.getHighMail();
+                content += "告警等级:高\n";
+                break;
+            default:
+                phones = "";
+        }
+        content += "当前状态:" + record.getAlarmValue() + "\n";
+        String open = "Y";
+        if (open.equals(pc.getFlagSms())) {
+            //短信开关打开
+            try {
+                Map map = JSONUtil.toBean(pc.getConfigSms(), Map.class);
+                String uri = (String) map.get("uri");
+                if (StringUtils.isNotEmpty(phones)) {
+                    String[] ps = phones.split("\n");
+                    for (String p : ps) {
+                        try {
+                            smsService.send(uri, p, title);
+                        } catch (Exception e) {
+                            log.error("短信发送失败:{}", e.getMessage());
+                        }
+                    }
+                }
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+
+        if (open.equals(pc.getFlagMail())) {
+            //邮件开关打开
+            try {
+                if (StringUtils.isNotEmpty(mails)) {
+                    mails = mails.replace("\n", ",");
+                    mailService.send(mails, title, content);
+                }
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+//        String
+    }
+
+    /**
+     * 发送邮件
+     *
+     * @param mail    邮箱
+     * @param title   标题
+     * @param content 内容
+     */
+    @Override
+    public void sendMail(String mail, String title, String content) throws Exception {
+        mailService.send(mail, title, content);
+    }
 }

+ 14 - 0
jjt-biz/src/main/java/com/jjt/push/service/impl/SmsServiceImpl.java

@@ -38,6 +38,20 @@ public class SmsServiceImpl implements ISmsService {
         PushConfig pc = pushConfigMapper.selectPushConfigByPcId(1L);
         Map<String, Object> map = JSONUtil.toBean(pc.getConfigSms(), Map.class);
         String uri = (String) map.get("uri");
+        return send(uri, phone, content);
+    }
+
+    /**
+     * 发送邮件
+     * 发送短信
+     *
+     * @param uri     地址
+     * @param phone   手机号
+     * @param content 内容
+     * @return 结果
+     */
+    @Override
+    public boolean send(String uri, String phone, String content) throws Exception {
         // 封装请求体
         JSONObject json = new JSONObject();
         json.putOnce("phone", phone);