From 36f51c954939b9e4e6fa71d5b81e5d6d13e8c2cd Mon Sep 17 00:00:00 2001 From: wuqun Date: Wed, 2 Nov 2022 14:11:41 +0800 Subject: [PATCH] =?UTF-8?q?(*)=E5=91=8A=E8=AD=A6=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E6=8E=A8=E9=80=81=E8=8A=82=E6=B5=81=20=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=9A=82=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/app/lib/utils/alarmHandle.js | 48 +++++++++++++++++-- .../sections/control/containers/control.jsx | 8 +--- 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/api/app/lib/utils/alarmHandle.js b/api/app/lib/utils/alarmHandle.js index c3b8a67..34fef95 100644 --- a/api/app/lib/utils/alarmHandle.js +++ b/api/app/lib/utils/alarmHandle.js @@ -1,4 +1,5 @@ 'use strict'; +const moment = require('moment') module.exports = function (app, opts) { const { models } = app.fs.dc @@ -43,7 +44,8 @@ module.exports = function (app, opts) { alarmGroup//告警类型 }) }) - app.socket.emit('alarmSendSocket', { type: 'alarmAppear', sendData }) + //app.socket.emit('alarmSendSocket', { type: 'alarmAppear', sendData }) + await socketThrottle('appear', sendData); } catch (err) { console.log(`告警(发现)推送失败, error: ${err}`); } @@ -74,7 +76,8 @@ module.exports = function (app, opts) { isAuto//是否为自动恢复,自动恢复时user为null }) }) - app.socket.emit('alarmSendSocket', { type: 'alarmConfirm', sendData })//小飞(处理人) 确认并关闭了A项目(项目) DTU设备(告警源) 状态异常(异常类型)的问题 + //app.socket.emit('alarmSendSocket', { type: 'alarmConfirm', sendData })//小飞(处理人) 确认并关闭了A项目(项目) DTU设备(告警源) 状态异常(异常类型)的问题 + await socketThrottle('confirm', sendData); } catch (err) { console.log(`告警(确认)推送失败, error: ${err}`); } @@ -110,20 +113,55 @@ module.exports = function (app, opts) { let pepPId = projects.find(p => p.id == projectCorrelationId).pepProjectId; //需要 策略名称 处理人 项目 策略和参数 时间 - let sendData = { + let sendData = [{ pushConfig: { cfgName, tactics, tacticsParams },//策略信息 pepUsers, projectCorrelationId: projectCorrelationId, project: projects.find(p => p.id == projectCorrelationId).name || pepProjects.find(pp => pp.id == pepPId).project_name,//前者为自定义项目名称 time - } + }] - app.socket.emit('alarmSendSocket', { type: 'alarmNotice', sendData }) + //app.socket.emit('alarmSendSocket', { type: 'alarmNotice', sendData }) + await socketThrottle('notice', sendData); } catch (err) { console.log(`推送通知失败, error: ${err}`); } } + async function socketThrottle(type, dataList) { + try { + if (!msgSendObj.time || moment() > moment(msgSendObj.time).add(1, 'minute')) {//首次 || 跟上次时间间隔大于1分钟 直接发送 + msgSendObj.dataMap[type] = dataList; + app.socket.emit('alarmSendSocket', { msgDataMap: msgSendObj.dataMap });//发送到前端 + + msgSendObj.time = moment().format();//记录本次发送时间 + msgSendObj.dataMap = { appear: [], confirm: [], notice: [] } + msgSendObj.toSend = false + } else {//放进队列 + msgSendObj.dataMap[type] = msgSendObj.dataMap[type].concat(dataList); + msgSendObj.toSend = true;//有待发送 + } + } catch (err) { + console.log(`推送节流失败, error: ${err}`); + } + } + + //全局变量 + let msgSendObj = { + time: null, + dataMap: { appear: [], confirm: [], notice: [] }, + toSend: false//有待发送 + } + setInterval(async () => { + if (msgSendObj.toSend) {//有待发送 + app.socket.emit('alarmSendSocket', { msgDataMap: msgSendObj.dataMap })//发送到前端 + + msgSendObj.time = moment().format() + msgSendObj.dataMap = { appear: [], confirm: [], notice: [] } + msgSendObj.toSend = false + } + }, 60 * 1000)//1分钟 + return { sendAppearToWeb,//推送告警发现 sendConfirmToWeb,//推送告警确认 diff --git a/web/client/src/sections/control/containers/control.jsx b/web/client/src/sections/control/containers/control.jsx index b7410c2..537c465 100644 --- a/web/client/src/sections/control/containers/control.jsx +++ b/web/client/src/sections/control/containers/control.jsx @@ -53,13 +53,7 @@ const Control = (props) => { if (socket) { socket.on('alarmSendSocket', function (msg) { //console.info(msg); - if (msg.type == "alarmAppear") {//告警出现 - - } else if (msg.type == "alarmConfirm") {//告警确认 - - } else if (msg.type == "alarmNotice") {//通知 - - } + let a = msg; }); return () => { socket.off("alarmSendSocket");