Browse Source

AlarmPushConfig

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

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

@ -0,0 +1,143 @@
const moment = require('moment')
module.exports = function (app, opts) {
const alarmsPush = app.fs.scheduleInit(
{
interval: '12 */1 * * * *',
immediate: true, // dev
// proRun: true,
},
async () => {
try {
const { models, ORM: sequelize } = app.fs.dc
const { clickHouse } = app.fs
const { pushBySms, pushByEmail } = app.fs.utils
const curMinOfYear = moment().diff(moment().startOf('year'), 'minutes')
const configListRes = await models.AlarmPushConfig.findAll({
where: {
del: false
},
include: [{
model: models.ProjectCorrelation,
where: {
del: false
},
required: true
}]
})
let pepProjectIds = new Set()
for (let { dataValues: c } of configListRes) {
if (c.projectCorrelation.pepProjectId) {
pepProjectIds.add(c.projectCorrelation.pepProjectId)
}
}
const pepProjectRes = pepProjectIds.size ?
await clickHouse.projectManage.query(
`
SELECT
t_pim_project.id AS id,
t_pim_project.project_name AS project_name,
t_pim_project.isdelete AS isdelete,
t_pim_project_construction.construction_status_id AS construction_status_id,
t_pim_project_state.construction_status AS construction_status
FROM t_pim_project
LEFT JOIN t_pim_project_construction
ON t_pim_project.id = t_pim_project_construction.project_id
LEFT JOIN t_pim_project_state
ON t_pim_project_construction.construction_status_id = t_pim_project_state.id
WHERE id IN (${[...pepProjectIds].join(',')})
`
).toPromise() :
[]
for (let { dataValues: c } of configListRes) {
if (c.tacticsParams && c.tactics) {
const { projectCorrelation } = c
const { interval, deviceProportion } = c.tacticsParams
if (
curMinOfYear % 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)
)
) {
let dataAlarmOption = []
let dataAlarmGroupOption = []
let dataAlarms = []
// 判断推送策略
let pointTime = moment().subtract(interval, 'minute').format('YYYY-MM-DD HH:mm:ss')
if (c.tactics.includes('immediately')) {
dataAlarmOption.push(`StartTime >= '${pointTime}'`);
} else if (c.tactics.includes('continue')) {
dataAlarmOption.push(`StartTime <= '${pointTime}'`);
} else if (c.tactics.includes('abnormal_rate')) {
dataAlarmOption.push(`StartTime <= '${pointTime}'`);
if (c.alarmType.includes('data_outages') || c.alarmType.includes('data_exception')) {
// 查了设备异常率 去安心云查当前项目下的设备数量
// TODO 等郑兴同步以太数据再查
const deviceCount = 9999
// await clickHouse.anxinyun.query(`
// SELECT count(*) FROM
// `).toPromise()
}
if (c.alarmType.includes('video_exception')) {
// 查了视频异常 去安心云查 接入的 萤石 设备数量
const cameraCount = await clickHouse.anxinyun.query(`
SELECT count(*) FROM
`).toPromise()
}
}
// 判断告警数据范围
if (c.alarmType.includes('data_outages')) {
dataAlarmGroupOption.push(1)
}
if (c.alarmType.includes('data_exception')) {
dataAlarmGroupOption.push(2)
}
if (c.alarmType.includes('strategy_hit')) {
dataAlarmGroupOption.push(3)
}
if (c.alarmType.includes('video_exception')) {
}
if (c.alarmType.includes('app_exception')) {
}
if (c.alarmType.includes('device_exception')) {
dataAlarmGroupOption.push(4)
dataAlarmGroupOption.push(5)
}
//
dataAlarmGroupOption.length ?
dataAlarmOption.push(`AlarmGroup IN ${dataAlarmGroupOption.join(',')}`) : ''
dataAlarms = await clickHouse.dataAlarm.query(`
SELECT * FROM alarms
WHERE
State NOT IN (3, 4)
${dataAlarmOption.length ? ' AND ' + dataAlarmOption.join(' AND ') : ''}
`).toPromise()
console.log(dataAlarms);
}
}
}
}
} catch (error) {
console.error(error);
}
}
)
return {
alarmsPush,
}
}
Loading…
Cancel
Save