Browse Source

告警推送重构

dev
巴林闲侠 2 years ago
parent
commit
c5ac7a13cd
  1. 84
      api/app/lib/schedule/alarms_push.js

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

@ -20,18 +20,22 @@ module.exports = function (app, opts) {
disable: false
},
order: ['id'],
include: [{
model: models.ProjectCorrelation,
where: {
del: false,
},
required: true
}],
})
let pomsProjectId = new Set()
let pepProjectIds = new Set()
for (let { dataValues: c } of configListRes) {
if (c.projectCorrelation.pepProjectId) {
pepProjectIds.add(c.projectCorrelation.pepProjectId)
if (c.pomsProjectId) {
c.pomsProjectId.forEach(pid => pomsProjectId.add(pid))
}
}
const pomsProjectRes = pomsProjectId.size ? await models.ProjectCorrelation.findAll({
where: {
id: { $in: [...pomsProjectId] }
}
}) : []
for (let { dataValues: c } of pomsProjectRes) {
if (c.pepProjectId) {
pepProjectIds.add(c.pepProjectId)
}
}
const pepProjectRes = pepProjectIds.size ?
@ -55,24 +59,39 @@ module.exports = function (app, opts) {
for (let { dataValues: c } of configListRes) {
if (c.tacticsParams && c.tactics) {
// pomsProjectId 是个数组 []
const { projectCorrelation, strucId, pomsProjectId, } = c
const { interval, deviceProportion } = c.tacticsParams
if (
curMinOfYear % parseInt(interval) == 0
) {
const corPepProject = projectCorrelation.pepProjectId ?
pepProjectRes.find(p => p.id == projectCorrelation.pepProjectId)
: null
if (
!projectCorrelation.pepProjectId
|| (
corPepProject
&& c.timeType.some(ct => ct == corPepProject.construction_status_id)
)
) {
const { anxinProjectId, pepProjectId } = projectCorrelation
const corPomsProject = pomsProjectRes.filter(poms => pomsProjectId.includes(poms.id))
let curAnxinProjectId = new Set()
let pepProjectName_ = []
for (let { dataValues: poms } of corPomsProject) {
if (poms.pepProjectId) {
// 找对应的项企项目
const corPepProject =
pepProjectRes.find(p => p.id == poms.pepProjectId)
if (corPepProject && c.timeType.some(ct => ct == corPepProject.construction_status_id)) {
pepProjectName_.push(corPepProject.project_name)
} else {
// 不符合当前项目的时间节点
continue
}
} else {
// 是自定义项目
poms.name ? pepProjectName_.push(poms.name) : null
}
// 筛选全部的 anxinProjectId pepProjectId
for (let axId of poms.anxinProjectId) {
curAnxinProjectId.add(axId)
}
}
const anxinProjectId = [...curAnxinProjectId]
// 查当前 poms 下的结构物 并把不包含的去掉
// 可能有结构物已解绑
const strucListRes = strucId.length && anxinProjectId.length ?
@ -109,7 +128,6 @@ module.exports = function (app, opts) {
project_state != -1
AND t_project.id IN (${anxinProjectId.join(',')})
AND t_structure.id IN (${strucId.join(',')})
`
).toPromise() :
[]
@ -121,16 +139,23 @@ module.exports = function (app, opts) {
return s.id
})
// 开发测试用的数据
// !开发测试用的数据
// searchStrucIds = searchStrucIds.concat([991, 1052, 700])
if (searchStrucIds.length) {
searchStrucIds.unshift(-1)
} else {
// 没有结构物可查
continue
}
let pepProjectName = pepProjectId ?
pepProjectRes.find(p => p.id == pepProjectId).project_name
: projectCorrelation.name
let emailTitle = `${pepProjectName}-${c.name}-`
let pepProjectName =
pepProjectName_.length ?
pepProjectName_.join('<br/>')
: ''
let emailTitle = `${pepProjectName_.length ?
pepProjectName_.join('、')
: ''}-${c.name}-`
let emailSubTitle = ''
let dataAlarmOption = []
@ -217,7 +242,6 @@ module.exports = function (app, opts) {
if (c.alarmType.includes('data_exception')) {
dataAlarmGroupOption.push(2)
if (c.alarmSubType) dataAlarmSubType = dataAlarmSubType.concat(c.alarmSubType['data_exception'])
}
if (c.alarmType.includes('strategy_hit')) {
dataAlarmGroupOption.push(3)
@ -373,7 +397,7 @@ module.exports = function (app, opts) {
include: [{
model: models.ProjectApp,
where: {
projectId: pomsProjectId
projectId: { $in: pomsProjectId }
},
required: true,
}]
@ -703,13 +727,14 @@ module.exports = function (app, opts) {
`).toPromise()
: []
let receiverId = []
const emails = receiverRes.reduce((arr, r) => {
let emails = receiverRes.reduce((arr, r) => {
if (r.email) {
arr.push(r.email)
receiverId.push({ id: r.id, name: r.name })
}
return arr
}, [])
// emails = ['1650192445@qq.com']
if (emails.length) {
await pushByEmail({
email: emails,
@ -746,7 +771,6 @@ module.exports = function (app, opts) {
}
}
}
}
} catch (error) {
console.error(error);

Loading…
Cancel
Save