Browse Source

告警推送++

dev
CODE 1 year ago
parent
commit
60f720be45
  1. 95
      api/app/lib/schedule/alarms_push.js

95
api/app/lib/schedule/alarms_push.js

@ -4,7 +4,7 @@ let isDev = false
isDev = true isDev = true
let proDebug = false let proDebug = false
// proDebug = true proDebug = true
module.exports = function (app, opts) { module.exports = function (app, opts) {
const alarmsPush = app.fs.scheduleInit( const alarmsPush = app.fs.scheduleInit(
@ -24,6 +24,7 @@ module.exports = function (app, opts) {
const { clickHouse } = app.fs const { clickHouse } = app.fs
const { database: anxinyun } = clickHouse.anxinyun.opts.config const { database: anxinyun } = clickHouse.anxinyun.opts.config
const { database: dataAlarm } = clickHouse.dataAlarm.opts.config const { database: dataAlarm } = clickHouse.dataAlarm.opts.config
const { database: iota } = clickHouse.iot.opts.config
const { pushBySms, pushByEmail, sendNoticeToWeb } = app.fs.utils const { pushBySms, pushByEmail, sendNoticeToWeb } = app.fs.utils
const curMinOfYear = moment().diff(moment().startOf('year'), 'minutes') const curMinOfYear = moment().diff(moment().startOf('year'), 'minutes')
@ -77,6 +78,7 @@ module.exports = function (app, opts) {
[] []
const calcMinute2DHM = (minute) => { const calcMinute2DHM = (minute) => {
pLog('calcMinute2DHM ', minute)
if (!minute && minute != 0) { if (!minute && minute != 0) {
return minute return minute
} }
@ -94,6 +96,7 @@ module.exports = function (app, opts) {
if (h) { if (h) {
title = title + m + '分' title = title + m + '分'
} }
pLog('calcMinute2DHM Rslt', title)
return title return title
} }
@ -102,9 +105,9 @@ module.exports = function (app, opts) {
pLog(`curMinOfYear / ${curMinOfYear}`) pLog(`curMinOfYear / ${curMinOfYear}`)
for (let { dataValues: c } of configListRes) { for (let { dataValues: c } of configListRes) {
if (c.id != 94) { // if (isDev && c.id != 94) {
continue // continue
} // }
if (c.tacticsParams && c.tactics) { if (c.tacticsParams && c.tactics) {
pLog(`当前运行EM配置:id=${c.id} name=${c.name}`); pLog(`当前运行EM配置:id=${c.id} name=${c.name}`);
@ -558,6 +561,7 @@ module.exports = function (app, opts) {
${`State NOT IN (3, 4) AND `} ${`State NOT IN (3, 4) AND `}
StructureId IN (${searchStrucIds.join(',')}) StructureId IN (${searchStrucIds.join(',')})
${dataAlarmOption.length ? ' AND ' + dataAlarmOption.join(' AND ') : ''} ${dataAlarmOption.length ? ' AND ' + dataAlarmOption.join(' AND ') : ''}
AND Project IS NOT null
ORDER BY StartTime DESC ORDER BY StartTime DESC
`).toPromise() : [] `).toPromise() : []
//合并本地化和安心云的告警 //合并本地化和安心云的告警
@ -623,8 +627,12 @@ module.exports = function (app, opts) {
n: '持续时间', n: '持续时间',
k: '', k: '',
f: (d) => { f: (d) => {
return d.StartTime ? let str = ''
'超过' + calcMinute2DHM(moment().diff(moment(d.StartTime), 'minutes')) : '' if (d.StartTime) {
str = calcMinute2DHM(moment().diff(moment(d.StartTime), 'minutes'))
}
return str ?
'超过' + str : ''
} }
},] },]
@ -679,8 +687,12 @@ module.exports = function (app, opts) {
n: '持续时间', n: '持续时间',
k: '', k: '',
f: (d) => { f: (d) => {
return d.StartTime ? let str = ''
'超过' + calcMinute2DHM(moment().diff(moment(d.StartTime), 'minutes')) : '' if (d.StartTime) {
str = calcMinute2DHM(moment().diff(moment(d.StartTime), 'minutes'))
}
return str ?
'超过' + str : ''
} }
},] },]
@ -745,8 +757,12 @@ module.exports = function (app, opts) {
// n: '持续时间', // n: '持续时间',
// k: '', // k: '',
// f: (d) => { // f: (d) => {
// return d.StartTime ? // let str = ''
// '超过' + calcMinute2DHM(moment().diff(moment(d.StartTime), 'minutes')) : '' // if (d.StartTime) {
// str = calcMinute2DHM(moment().diff(moment(d.StartTime), 'minutes'))
// }
// return str ?
// '超过' + str : ''
// } // }
// }, // },
// { // {
@ -832,8 +848,12 @@ module.exports = function (app, opts) {
n: '持续时间', n: '持续时间',
k: '', k: '',
f: (d) => { f: (d) => {
return d.createTime ? let str = ''
'超过' + calcMinute2DHM(moment().diff(moment(d.createTime), 'minutes')) : '' if (d.createTime) {
str = calcMinute2DHM(moment().diff(moment(d.createTime), 'minutes'))
}
return str ?
'超过' + str : ''
} }
},] },]
@ -872,8 +892,12 @@ module.exports = function (app, opts) {
n: '持续时间', n: '持续时间',
k: '', k: '',
f: (d) => { f: (d) => {
return d.createTime ? let str = ''
'超过' + calcMinute2DHM(moment().diff(moment(d.createTime), 'minutes')) : '' if (d.createTime) {
str = calcMinute2DHM(moment().diff(moment(d.createTime), 'minutes'))
}
return str ?
'超过' + str : ''
} }
},] },]
@ -1150,14 +1174,18 @@ module.exports = function (app, opts) {
<h5 style="margin-bottom:12px">${emailSubTitle}</h5> <h5 style="margin-bottom:12px">${emailSubTitle}</h5>
` `
let dataAlarmG1Data = dataAlarms.filter(a => a.AlarmGroup == 1) let dataAlarmG145Data = dataAlarms.filter(a => {
pLog('数据中断', dataAlarmG1Data.length) return a.AlarmGroup == 1
if (dataAlarmG1Data.length) { // || a.AlarmGroup == 4
// || a.AlarmGroup == 5
})
pLog('数据中断', dataAlarmG145Data.length)
if (dataAlarmG145Data.length) {
//安心云项目是structureId数组 //安心云项目是structureId数组
let dataAlarmG1StructureId = new Set() let dataAlarmG1StructureId = new Set()
//本地化项目structureId数组 //本地化项目structureId数组
let dataAlarmStructureId = new Set() let dataAlarmStructureId = new Set()
dataAlarmG1Data.map(c => { dataAlarmG145Data.map(c => {
if (c.project) { if (c.project) {
dataAlarmStructureId.add(c.StructureId) dataAlarmStructureId.add(c.StructureId)
} else { } else {
@ -1197,17 +1225,21 @@ module.exports = function (app, opts) {
let factorIdLocal = factorDataLocal.map(f => f.id) let factorIdLocal = factorDataLocal.map(f => f.id)
pLog('安心云+本地监测因素', factorData.length) pLog('安心云+本地监测因素', factorData.length)
//查询结构物对应的测点(安心云) //查询结构物对应的测点(安心云)
// ! 测点
let equipment = await clickHouse.anxinyun.query( let equipment = await clickHouse.anxinyun.query(
` `
SELECT SELECT
t_sensor.name AS name, t_sensor.name AS name,
t_sensor.structure AS structureId, t_sensor.structure AS structureId,
t_sensor.factor AS factorId, t_sensor.factor AS factorId,
t_device_sensor.iota_device_id AS iotaDeviceId t_device_sensor.iota_device_id AS iotaDeviceId,
Device.name AS deviceName
FROM FROM
t_sensor t_sensor
LEFT JOIN t_device_sensor LEFT JOIN t_device_sensor
ON t_device_sensor.sensor = t_sensor.id ON t_device_sensor.sensor = t_sensor.id
LEFT JOIN ${iota}.Device
ON Device.id = toString(t_device_sensor.iota_device_id)
WHERE WHERE
t_sensor.structure IN (${[...dataAlarmG1StructureId, -1].join(',')}) t_sensor.structure IN (${[...dataAlarmG1StructureId, -1].join(',')})
AND AND
@ -1218,9 +1250,9 @@ module.exports = function (app, opts) {
let equipmentLocal = await clickHouse.dataAlarm.query( let equipmentLocal = await clickHouse.dataAlarm.query(
` SELECT ` SELECT
SensorLocationDescription AS name, SensorLocationDescription AS name,
StructId AS structureId, StructId AS structureId,
SafetyFactorTypeId AS factorId, SafetyFactorTypeId AS factorId,
SensorId AS iotaDeviceId SensorId AS iotaDeviceId
FROM sensors FROM sensors
WHERE sensors.StructId IN (${[...dataAlarmStructureId, -1].join(',')}) WHERE sensors.StructId IN (${[...dataAlarmStructureId, -1].join(',')})
AND sensors.SafetyFactorTypeId IN (${[...factorIdLocal, -1].join(',')}) AND sensors.SafetyFactorTypeId IN (${[...factorIdLocal, -1].join(',')})
@ -1249,7 +1281,7 @@ module.exports = function (app, opts) {
WHERE WHERE
alarms.StructureId In (${[...dataAlarmG1StructureId, ...dataAlarmStructureId, -1].join(',')}) alarms.StructureId In (${[...dataAlarmG1StructureId, ...dataAlarmStructureId, -1].join(',')})
AND AND
alarms.AlarmGroup = 1 alarms.AlarmGroup IN (1,4,5)
AND AND
alarms.State < 3 alarms.State < 3
` `
@ -1262,7 +1294,8 @@ module.exports = function (app, opts) {
for (let SubDevice of alarms.SubDevices) { for (let SubDevice of alarms.SubDevices) {
alarmDatas.push({ alarmDatas.push({
...alarms, ...alarms,
alarmId: SubDevice alarmId: SubDevice,
sourceId: SubDevice,
}) })
} }
} else { } else {
@ -1273,11 +1306,22 @@ module.exports = function (app, opts) {
// 为设备分配自己产生的告警数据 // 为设备分配自己产生的告警数据
let matchedAlarmIds = [] let matchedAlarmIds = []
// 这一步能被分配的告警应该也能最终体现在邮件中 // 这一步能被分配的告警应该也能最终体现在邮件中
equipment.map(f => { equipment.map(f => {
f.alarmDatas = [] f.alarmDatas = []
let deviceRepeatMap = {}
alarmDatas.map(r => { alarmDatas.map(r => {
if (r.sourceId == f.iotaDeviceId) { if (r.sourceId == f.iotaDeviceId) {
f.alarmDatas.push({ ...r }) if (!deviceRepeatMap[r.sourceId]) {
// 没有这个deviceId则不重复
deviceRepeatMap[r.sourceId] = true
f.alarmDatas.push({
...r,
// 可能有的设备绑定了不同的测点 所以这里展示测点名称
SourceName: r.SubDevices && r.SubDevices.length ? f.
deviceName : r.SourceName
})
}
matchedAlarmIds.push(r.alarmId) matchedAlarmIds.push(r.alarmId)
} }
}) })
@ -1666,7 +1710,6 @@ module.exports = function (app, opts) {
`).toPromise() `).toPromise()
: [] : []
let receiverId = [] let receiverId = []
console.log('接收邮件的人', receiverRes)
let emails = receiverRes.reduce((arr, r) => { let emails = receiverRes.reduce((arr, r) => {
if (r.email) { if (r.email) {
arr.push(r.email) arr.push(r.email)

Loading…
Cancel
Save