Browse Source

告警推送++

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

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

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

Loading…
Cancel
Save