1
1
const sendEmail = require ( '../util_cus/sendEmail' ) ;
2
- const dingDing = require ( '../alarm/dingding' )
3
- const weiXin = require ( '../alarm/weixin' )
4
- const feiSHu = require ( '../alarm/feishu' )
5
- const Utils = require ( '../util/utils' )
6
- const AccountConfig = require ( '../config/AccountConfig' )
7
- const { accountInfo } = AccountConfig
8
- const AlarmNames = require ( './alarmName' )
9
-
10
- const alarmCallback = ( project , rule , users ) => {
11
- const { projectName, projectType, chooseHook } = project
12
- const { type, happenCount, compareType, limitValue} = rule
13
- const compareStr = compareType === "up" ? ">=" : "<"
14
- const projectHook = chooseHook ? JSON . parse ( chooseHook ) : { value : "" , name : "" , webHook : "" }
15
- /**生成警报配置 */
16
- // 添加用户手机号
17
- users . forEach ( ( user ) => {
18
- dingDing . config . at . atMobiles . push ( user . phone )
19
- weiXin . config . text . mentioned_mobile_list . push ( user . phone )
20
- } )
21
- // 生成警报内容
22
- const contentStr = type + "警报!" +
23
- "您的" + projectType + "项目【" + projectName + "】发出警报:" +
24
- type + "数量 " + compareStr + " " + limitValue + " 已经发生" + happenCount + "次了,请及时处理。"
25
- dingDing . config . text . content = contentStr
26
- weiXin . config . text . content = contentStr
27
- feiSHu . config . content . text = contentStr
28
- switch ( projectHook . value ) {
29
- case "dingding" :
30
- // 1. 通知钉钉机器人
31
- Utils . postJson ( projectHook . webHook , dingDing . config ) // 钉钉机器人
32
- break
33
- case "weixin" :
34
- // 2. 通知微信机器人
35
- Utils . postJson ( projectHook . webHook , weiXin . config ) // 微信机器人
36
- break
37
- case "feishu" :
38
- // 3. 通知飞书机器人
39
- Utils . postJson ( projectHook . webHook , feiSHu . config ) // 飞书机器人
40
- break
41
- default :
42
- break
43
- }
44
-
45
- // 4. 发送邮件通知
46
- if ( users && users . length && accountInfo . emailUser && accountInfo . emailPassword ) {
47
- users . forEach ( ( user ) => {
48
- const email = user . emailName
49
- sendEmail ( email , AlarmNames [ type ] + "警报!" , contentStr , accountInfo . emailUser , accountInfo . emailPassword )
50
- } )
51
- }
52
- }
53
- module . exports = {
54
- alarmCallback
55
- }
2
+ const dingDing = require ( '../alarm/dingding' )
3
+ const weiXin = require ( '../alarm/weixin' )
4
+ const feiSHu = require ( '../alarm/feishu' )
5
+ const Utils = require ( '../util/utils' )
6
+ const AccountConfig = require ( '../config/AccountConfig' )
7
+ const { accountInfo } = AccountConfig
8
+ const AlarmNames = require ( './alarmName' )
9
+
10
+ const alarmCallback = ( project , rule , users ) => {
11
+ const { projectName, projectType, chooseHook } = project
12
+ const { type, happenCount, compareType, limitValue} = rule
13
+ const compareStr = compareType === "up" ? ">=" : "<"
14
+ const projectHook = JSON . parse ( chooseHook )
15
+ /**生成警报配置 */
16
+ // 添加用户手机号
17
+ users . forEach ( ( user ) => {
18
+ const dingMobiles = dingDing . config . at . atMobiles
19
+ const weiXinMobiles = weiXin . config . text . mentioned_mobile_list
20
+ if ( dingMobiles . indexOf ( user . phone ) === - 1 ) {
21
+ dingMobiles . push ( user . phone )
22
+ }
23
+ if ( weiXinMobiles . indexOf ( user . phone ) === - 1 ) {
24
+ weiXinMobiles . push ( user . phone )
25
+ }
26
+ // dingDing.config.at.atMobiles.push(user.phone)
27
+ // weiXin.config.text.mentioned_mobile_list.push(user.phone)
28
+ } )
29
+ // 生成警报内容
30
+ const contentStr = type + "警报!" +
31
+ "您的" + projectType + "项目【" + projectName + "】发出警报:" +
32
+ type + "数量 " + compareStr + " " + limitValue + " 已经发生" + happenCount + "次了,请及时处理。"
33
+ dingDing . config . text . content = contentStr
34
+ weiXin . config . text . content = contentStr
35
+ feiSHu . config . content . text = contentStr
36
+ switch ( projectHook . value ) {
37
+ case "dingding" :
38
+ // 1. 通知钉钉机器人
39
+ Utils . postJson ( projectHook . webHook , dingDing . config ) // 钉钉机器人
40
+ break
41
+ case "weixin" :
42
+ // 2. 通知微信机器人
43
+ Utils . postJson ( projectHook . webHook , weiXin . config ) // 微信机器人
44
+ break
45
+ case "feishu" :
46
+ // 3. 通知飞书机器人
47
+ Utils . postJson ( projectHook . webHook , feiSHu . config ) // 飞书机器人
48
+ break
49
+ }
50
+
51
+ // 4. 发送邮件通知
52
+ if ( users && users . length && accountInfo . emailUser && accountInfo . emailPassword ) {
53
+ users . forEach ( ( user ) => {
54
+ const email = user . emailName
55
+ sendEmail ( email , AlarmNames [ type ] + "警报!" , contentStr , accountInfo . emailUser , accountInfo . emailPassword )
56
+ } )
57
+ }
58
+ }
59
+ module . exports = {
60
+ alarmCallback
61
+ }
0 commit comments