diff --git a/api/app/lib/controllers/push/config.js b/api/app/lib/controllers/push/config.js index 7e8325e..01eedd2 100644 --- a/api/app/lib/controllers/push/config.js +++ b/api/app/lib/controllers/push/config.js @@ -22,7 +22,7 @@ async function list (ctx) { if (keywordTarget == 'tactics') { findOption.where.name = { $like: `%${keyword}%` } } else if (keywordTarget == 'struc') { - let bindAnixinStrucRes = await clickHouse.anxinyun.query(` + let bindAnixinStrucRes = await clickHouse.projectManage.query(` SELECT id, name FROM t_structure WHERE name LIKE '%${keyword}%' `).toPromise() diff --git a/api/app/lib/index.js b/api/app/lib/index.js index d33bbb7..dcc655c 100644 --- a/api/app/lib/index.js +++ b/api/app/lib/index.js @@ -77,7 +77,8 @@ module.exports.models = function (dc) { // dc = { orm: Sequelize对象, ORM: Seq AppAlarm.belongsTo(App, { foreignKey: 'projectAppId', targetKey: 'id' }); App.hasMany(AppAlarm, { foreignKey: 'projectAppId', sourceKey: 'id' }); - + AlarmPushConfig.belongsTo(ProjectCorrelation, { foreignKey: 'pomsProjectId', targetKey: 'id' }); + ProjectCorrelation.hasMany(AlarmPushConfig, { foreignKey: 'pomsProjectId', sourceKey: 'id' }); AlarmAppearRecord.belongsTo(ProjectCorrelation, { foreignKey: 'projectCorrelationId', targetKey: 'id' }); ProjectCorrelation.hasMany(AlarmAppearRecord, { foreignKey: 'projectCorrelationId', sourceKey: 'id' }); diff --git a/api/app/lib/schedule/alarms_push.js b/api/app/lib/schedule/alarms_push.js new file mode 100644 index 0000000..3cd3eeb --- /dev/null +++ b/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, + } +} \ No newline at end of file