|
@ -2,181 +2,183 @@ |
|
|
const moment = require('moment') |
|
|
const moment = require('moment') |
|
|
|
|
|
|
|
|
module.exports = function (app, opts) { |
|
|
module.exports = function (app, opts) { |
|
|
const { models } = app.fs.dc |
|
|
const { models } = app.fs.dc |
|
|
const { clickHouse } = app.fs |
|
|
const { clickHouse } = app.fs |
|
|
|
|
|
|
|
|
let constAlarmGroups = { |
|
|
let constAlarmGroups = { |
|
|
1: '数据中断', |
|
|
1: '数据中断', |
|
|
2: '数据异常', |
|
|
2: '数据异常', |
|
|
3: '策略命中', |
|
|
3: '策略命中', |
|
|
4: '设备异常', |
|
|
4: '设备异常', |
|
|
5: '设备异常', |
|
|
5: '设备异常', |
|
|
'video': '视频异常', |
|
|
'video': '视频异常', |
|
|
'app': '应用异常' |
|
|
'app': '应用异常' |
|
|
} |
|
|
} |
|
|
let constAlarmUrls = { |
|
|
let constAlarmUrls = { |
|
|
1: '/problem/dataAlarm/dataLnterrupt', |
|
|
1: '/problem/dataAlarm/dataLnterrupt', |
|
|
2: '/problem/dataAlarm/dataAbnormal', |
|
|
2: '/problem/dataAlarm/dataAbnormal', |
|
|
3: '/problem/dataAlarm/strategyHit', |
|
|
3: '/problem/dataAlarm/strategyHit', |
|
|
4: '/problem/deviceAlarm/deviceAbnormal', |
|
|
4: '/problem/deviceAlarm/deviceAbnormal', |
|
|
5: '/problem/deviceAlarm/deviceAbnormal', |
|
|
5: '/problem/deviceAlarm/deviceAbnormal', |
|
|
'video': '/problem/dataAlarm/videoAbnormal', |
|
|
'video': '/problem/dataAlarm/videoAbnormal', |
|
|
'app': '/problem/useAlarm/useAbnormal' |
|
|
'app': '/problem/useAlarm/useAbnormal' |
|
|
} |
|
|
} |
|
|
async function sendAppearToWeb(datas, ttype) { |
|
|
async function sendAppearToWeb (datas, ttype) { |
|
|
try { |
|
|
try { |
|
|
let alarmTypee = null, jumpUrl = null//告警类型 跳转地址
|
|
|
let alarmTypee = null, jumpUrl = null//告警类型 跳转地址
|
|
|
|
|
|
|
|
|
//项目信息
|
|
|
//项目信息
|
|
|
let { projects, pepProjects } = await getProjectsInfo(datas); |
|
|
let { projects, pepProjects } = await getProjectsInfo(datas); |
|
|
|
|
|
|
|
|
//数据类区分alarmGroup
|
|
|
//数据类区分alarmGroup
|
|
|
if (ttype == 'data') { |
|
|
if (ttype == 'data') { |
|
|
let alarm_group = await clickHouse.anxinyun.query( |
|
|
let alarm_group = await clickHouse.anxinyun.query( |
|
|
`SELECT alarm_group FROM t_alarm_code WHERE code='${datas[0].alarmInfo.alarmCode}'`).toPromise(); |
|
|
`SELECT alarm_group FROM t_alarm_code WHERE code='${datas[0].alarmInfo.alarmCode}'`).toPromise(); |
|
|
|
|
|
|
|
|
alarmTypee = alarm_group.length ? constAlarmGroups[alarm_group[0].alarm_group] : null |
|
|
alarmTypee = alarm_group.length ? constAlarmGroups[alarm_group[0].alarm_group] : null |
|
|
jumpUrl = alarm_group.length ? constAlarmUrls[alarm_group[0].alarm_group] : null |
|
|
jumpUrl = alarm_group.length ? constAlarmUrls[alarm_group[0].alarm_group] : null |
|
|
} else { |
|
|
} else { |
|
|
alarmTypee = constAlarmGroups[ttype] |
|
|
alarmTypee = constAlarmGroups[ttype] |
|
|
jumpUrl = constAlarmUrls[ttype] |
|
|
jumpUrl = constAlarmUrls[ttype] |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
let sendData = [] |
|
|
let sendData = [] |
|
|
datas.map(ld => { |
|
|
datas.map(ld => { |
|
|
let pepPId = projects.find(p => p.id == ld.projectCorrelationId).pepProjectId; |
|
|
let pepPId = projects.find(p => p.id == ld.projectCorrelationId).pepProjectId; |
|
|
sendData.push({ |
|
|
sendData.push({ |
|
|
projectCorrelationId: ld.projectCorrelationId, |
|
|
projectCorrelationId: ld.projectCorrelationId, |
|
|
project: projects.find(p => p.id == ld.projectCorrelationId).name || pepProjects.find(pp => pp.id == pepPId).project_name,//前者为自定义项目名称
|
|
|
project: projects.find(p => p.id == ld.projectCorrelationId).name || pepProjects.find(pp => pp.id == pepPId).project_name,//前者为自定义项目名称
|
|
|
source: ld.alarmInfo.sourceName, |
|
|
source: ld.alarmInfo.sourceName, |
|
|
alarmGroup: ld.alarmInfo.type,//异常类型
|
|
|
alarmGroup: ld.alarmInfo.type,//异常类型
|
|
|
time: ld.time, |
|
|
time: ld.time, |
|
|
type: alarmTypee,//告警类型
|
|
|
type: alarmTypee,//告警类型
|
|
|
jumpUrl//跳转地址
|
|
|
jumpUrl//跳转地址
|
|
|
}) |
|
|
|
|
|
}) |
|
|
}) |
|
|
//app.socket.emit('alarmSendSocket', { type: 'alarmAppear', sendData })
|
|
|
}) |
|
|
await socketThrottle('appear', sendData); |
|
|
//app.socket.emit('alarmSendSocket', { type: 'alarmAppear', sendData })
|
|
|
} catch (err) { |
|
|
await socketThrottle('appear', sendData); |
|
|
console.log(`告警(发现)推送失败, error: ${err}`); |
|
|
} catch (err) { |
|
|
} |
|
|
console.log(`告警(发现)推送失败, error: ${err}`); |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
async function sendConfirmToWeb(logDatas, isAuto) { |
|
|
|
|
|
try { |
|
|
async function sendConfirmToWeb (logDatas, isAuto) { |
|
|
//用户信息
|
|
|
try { |
|
|
let userName = null |
|
|
//用户信息
|
|
|
if (!isAuto) { |
|
|
let userName = null |
|
|
let userPepRes = await clickHouse.pepEmis.query( |
|
|
if (!isAuto) { |
|
|
`SELECT DISTINCT user.id AS id, "user"."name" AS name FROM user WHERE user.id=${logDatas[0].pepUserId}`).toPromise(); |
|
|
let userPepRes = await clickHouse.pepEmis.query( |
|
|
userName = userPepRes.length ? userPepRes[0].name : null |
|
|
`SELECT DISTINCT user.id AS id, "user"."name" AS name FROM user WHERE user.id=${logDatas[0].pepUserId}`).toPromise(); |
|
|
} |
|
|
userName = userPepRes.length ? userPepRes[0].name : null |
|
|
|
|
|
} |
|
|
//项目信息
|
|
|
|
|
|
let { projects, pepProjects } = await getProjectsInfo(logDatas); |
|
|
//项目信息
|
|
|
let sendData = [] |
|
|
let { projects, pepProjects } = await getProjectsInfo(logDatas); |
|
|
logDatas.map(ld => { |
|
|
let sendData = [] |
|
|
let pepPId = projects.find(p => p.id == ld.projectCorrelationId).pepProjectId; |
|
|
logDatas.map(ld => { |
|
|
sendData.push({ |
|
|
let pepPId = projects.find(p => p.id == ld.projectCorrelationId).pepProjectId; |
|
|
user: userName, |
|
|
sendData.push({ |
|
|
projectCorrelationId: ld.projectCorrelationId, |
|
|
user: userName, |
|
|
project: projects.find(p => p.id == ld.projectCorrelationId).name || pepProjects.find(pp => pp.id == pepPId).project_name,//前者为自定义项目名称
|
|
|
projectCorrelationId: ld.projectCorrelationId, |
|
|
source: ld.alarmInfo.source, |
|
|
project: projects.find(p => p.id == ld.projectCorrelationId).name || pepProjects.find(pp => pp.id == pepPId).project_name,//前者为自定义项目名称
|
|
|
type: ld.alarmInfo.type, |
|
|
source: ld.alarmInfo.source, |
|
|
time: ld.confirmTime, |
|
|
type: ld.alarmInfo.type, |
|
|
isAuto//是否为自动恢复,自动恢复时user为null
|
|
|
time: ld.confirmTime, |
|
|
}) |
|
|
isAuto//是否为自动恢复,自动恢复时user为null
|
|
|
}) |
|
|
}) |
|
|
//app.socket.emit('alarmSendSocket', { type: 'alarmConfirm', sendData })//小飞(处理人) 确认并关闭了A项目(项目) DTU设备(告警源) 状态异常(异常类型)的问题
|
|
|
}) |
|
|
await socketThrottle('confirm', sendData); |
|
|
//app.socket.emit('alarmSendSocket', { type: 'alarmConfirm', sendData })//小飞(处理人) 确认并关闭了A项目(项目) DTU设备(告警源) 状态异常(异常类型)的问题
|
|
|
} catch (err) { |
|
|
await socketThrottle('confirm', sendData); |
|
|
console.log(`告警(确认)推送失败, error: ${err}`); |
|
|
} catch (err) { |
|
|
} |
|
|
console.log(`告警(确认)推送失败, error: ${err}`); |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
async function getProjectsInfo(datas) { |
|
|
|
|
|
try { |
|
|
async function getProjectsInfo (datas) { |
|
|
let pIds = datas.map(l => l.projectCorrelationId);//所有的项目的id
|
|
|
try { |
|
|
let projects = await models.ProjectCorrelation.findAll({ |
|
|
let pIds = datas.map(l => l.projectCorrelationId);//所有的项目的id
|
|
|
where: { id: { $in: pIds } }, |
|
|
let projects = await models.ProjectCorrelation.findAll({ |
|
|
attributes: ['id', 'name', 'pepProjectId'] |
|
|
where: { id: { $in: pIds } }, |
|
|
}); |
|
|
attributes: ['id', 'name', 'pepProjectId'] |
|
|
|
|
|
}); |
|
|
let pepPojectIds = new Set(); |
|
|
|
|
|
for (let p of projects) { |
|
|
let pepPojectIds = new Set(); |
|
|
pepPojectIds.add(p.pepProjectId); |
|
|
for (let p of projects) { |
|
|
|
|
|
if (p.pepProjectId) { |
|
|
|
|
|
pepPojectIds.add(p.pepProjectId); |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
let pepProjects = pepPojectIds.size ? await clickHouse.projectManage.query(` |
|
|
let pepProjects = pepPojectIds.size ? await clickHouse.projectManage.query(` |
|
|
SELECT id, project_name FROM t_pim_project WHERE id IN (${[...pepPojectIds]}, -1)` |
|
|
SELECT id, project_name FROM t_pim_project WHERE id IN (${[...pepPojectIds]}, -1)` |
|
|
).toPromise() : []; |
|
|
).toPromise() : []; |
|
|
|
|
|
|
|
|
return { projects, pepProjects }; |
|
|
return { projects, pepProjects }; |
|
|
} catch (err) { |
|
|
} catch (err) { |
|
|
console.log(`获取项目信息失败, error: ${err}`); |
|
|
console.log(`获取项目信息失败, error: ${err}`); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async function sendNoticeToWeb(pepUsers, data) { |
|
|
async function sendNoticeToWeb (pepUsers, data) { |
|
|
try { |
|
|
try { |
|
|
let { cfgName, tactics, tacticsParams, projectCorrelationId, time } = data; |
|
|
let { cfgName, tactics, tacticsParams, projectCorrelationId, time } = data; |
|
|
//项目信息
|
|
|
//项目信息
|
|
|
let { projects, pepProjects } = await getProjectsInfo([data]); |
|
|
let { projects, pepProjects } = await getProjectsInfo([data]); |
|
|
let pepPId = projects.find(p => p.id == projectCorrelationId).pepProjectId; |
|
|
let pepPId = projects.find(p => p.id == projectCorrelationId).pepProjectId; |
|
|
|
|
|
|
|
|
//需要 策略名称 处理人 项目 策略和参数 时间
|
|
|
//需要 策略名称 处理人 项目 策略和参数 时间
|
|
|
let sendData = [{ |
|
|
let sendData = [{ |
|
|
pushConfig: { cfgName, tactics, tacticsParams },//策略信息
|
|
|
pushConfig: { cfgName, tactics, tacticsParams },//策略信息
|
|
|
pepUsers, |
|
|
pepUsers, |
|
|
projectCorrelationId: projectCorrelationId, |
|
|
projectCorrelationId: projectCorrelationId, |
|
|
project: projects.find(p => p.id == projectCorrelationId).name || pepProjects.find(pp => pp.id == pepPId).project_name,//前者为自定义项目名称
|
|
|
project: projects.find(p => p.id == projectCorrelationId).name || pepProjects.find(pp => pp.id == pepPId).project_name,//前者为自定义项目名称
|
|
|
time |
|
|
time |
|
|
}] |
|
|
}] |
|
|
|
|
|
|
|
|
//app.socket.emit('alarmSendSocket', { type: 'alarmNotice', sendData })
|
|
|
//app.socket.emit('alarmSendSocket', { type: 'alarmNotice', sendData })
|
|
|
await socketThrottle('notice', sendData); |
|
|
await socketThrottle('notice', sendData); |
|
|
} catch (err) { |
|
|
} catch (err) { |
|
|
console.log(`推送通知失败, error: ${err}`); |
|
|
console.log(`推送通知失败, error: ${err}`); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async function socketThrottle(type, dataList) { |
|
|
async function socketThrottle (type, dataList) { |
|
|
try { |
|
|
try { |
|
|
if (!msgSendObj.time || moment() > moment(msgSendObj.time).add(1, 'minute')) {//首次 || 跟上次时间间隔大于1分钟 直接发送
|
|
|
if (!msgSendObj.time || moment() > moment(msgSendObj.time).add(1, 'minute')) {//首次 || 跟上次时间间隔大于1分钟 直接发送
|
|
|
msgSendObj.dataMap[type] = dataList; |
|
|
msgSendObj.dataMap[type] = dataList; |
|
|
app.socket.emit('alarmSendSocket', { msgDataMap: msgSendObj.dataMap });//发送到前端
|
|
|
app.socket.emit('alarmSendSocket', { msgDataMap: msgSendObj.dataMap });//发送到前端
|
|
|
|
|
|
|
|
|
msgSendObj.time = moment().format();//记录本次发送时间
|
|
|
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.dataMap = { appear: [], confirm: [], notice: [] } |
|
|
msgSendObj.toSend = false |
|
|
msgSendObj.toSend = false |
|
|
} |
|
|
} else {//放进队列
|
|
|
}, 60 * 1000)//1分钟
|
|
|
msgSendObj.dataMap[type] = msgSendObj.dataMap[type].concat(dataList); |
|
|
|
|
|
msgSendObj.toSend = true;//有待发送
|
|
|
return { |
|
|
} |
|
|
sendAppearToWeb,//推送告警发现
|
|
|
} catch (err) { |
|
|
sendConfirmToWeb,//推送告警确认
|
|
|
console.log(`推送节流失败, error: ${err}`); |
|
|
sendNoticeToWeb//推送通知
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//全局变量
|
|
|
|
|
|
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,//推送告警确认
|
|
|
|
|
|
sendNoticeToWeb//推送通知
|
|
|
|
|
|
} |
|
|
} |
|
|
} |