'use strict'; const moment = require('moment') async function alarmConfirmLog(ctx, confirmPost, content) { try { const { models } = ctx.fs.dc; const { clickHouse } = ctx.app.fs; //存日志 let logDatas = []; confirmPost.map(cp => { let { pepUserId, projectCorrelationIds, alarmInfo } = cp; projectCorrelationIds.map(id => { logDatas.push({ pepUserId, projectCorrelationId: id, alarmInfo,//包含告警id,type,source confirmTime: moment().format(), confirmContent: content }) }) }) let rslt = await models.AlarmConfirmLog.bulkCreate(logDatas, { returning: true }); //存最新动态 let dynamics = rslt.map(r => { return { time: r.confirmTime, alarmConfirmId: r.id, projectCorrelationId: r.projectCorrelationId, type: 4//告警确认 } }) await models.LatestDynamicList.bulkCreate(dynamics); //消息推送到前端 if (logDatas.length) { await sendConfirmToWeb(ctx.app, models, clickHouse, logDatas, false); } } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } let constAlarmGroups = { 1: '数据中断', 2: '数据异常', 3: '策略命中', 4: '设备异常', 5: '设备异常', 'video': '视频异常', 'app': '应用异常' } async function sendAppearToWeb(app, models, clickHouse, datas, ttype) { try { //告警类型 let alarmGroup = null //项目信息 let { projects, pepProjects } = await getProjectsInfo(models, clickHouse, datas); //数据类区分alarmGroup if (ttype == 'data') { let alarm_group = await clickHouse.anxinyun.query( `SELECT alarm_group FROM t_alarm_code WHERE code='${datas[0].alarmInfo.alarmTypeCode}'`).toPromise(); alarmGroup = alarm_group.length ? constAlarmGroups[alarm_group[0].alarm_group] : null } else { alarmGroup = constAlarmGroups[ttype] } let sendData = [] datas.map(ld => { let pepPId = projects.find(p => p.id == ld.projectCorrelationId).pepProjectId; sendData.push({ projectCorrelationId: ld.projectCorrelationId, project: projects.find(p => p.id == ld.projectCorrelationId).name || pepProjects.find(pp => pp.id == pepPId).project_name,//前者为自定义项目名称 source: ld.alarmInfo.sourceName, type: ld.type, time: ld.time, alarmGroup//告警类型 }) }) app.socket.emit('alarmSendSocket', { type: 'alarmAppear', sendData }) } catch (err) { console.log(`告警(发现)推送失败, error: ${err}`); } } async function getProjectsInfo(models, clickHouse, logDatas) { try { let pIds = logDatas.map(l => l.projectCorrelationId);//所有的项目的id let projects = await models.ProjectCorrelation.findAll({ where: { id: { $in: pIds } }, attributes: ['id', 'name', 'pepProjectId'] }); let pepPojectIds = new Set(); for (let p of projects) { pepPojectIds.add(p.pepProjectId); } let pepProjects = pepPojectIds.size ? await clickHouse.projectManage.query(` SELECT id, project_name FROM t_pim_project WHERE id IN (${[...pepPojectIds]})`).toPromise() : []; return { projects, pepProjects }; } catch (err) { console.log(`获取项目信息失败, error: ${err}`); } } async function sendConfirmToWeb(app, models, clickHouse, logDatas, isAuto) { try { //用户信息 let userName = null if (!isAuto) { let userPepRes = await clickHouse.pepEmis.query( `SELECT DISTINCT user.id AS id, "user"."name" AS name FROM user WHERE user.id=${logDatas[0].pepUserId}`).toPromise(); userName = userPepRes.length ? userPepRes[0].name : null } //项目信息 let { projects, pepProjects } = await getProjectsInfo(models, clickHouse, logDatas); let sendData = [] logDatas.map(ld => { let pepPId = projects.find(p => p.id == ld.projectCorrelationId).pepProjectId; sendData.push({ user: userName, projectCorrelationId: ld.projectCorrelationId, project: projects.find(p => p.id == ld.projectCorrelationId).name || pepProjects.find(pp => pp.id == pepPId).project_name,//前者为自定义项目名称 source: ld.alarmInfo.source, type: ld.alarmInfo.type, time: ld.confirmTime, isAuto//是否为自动恢复,自动恢复时user为null }) }) app.socket.emit('alarmSendSocket', { type: 'alarmConfirm', sendData })//小飞(处理人) 确认并关闭了A项目(项目) DTU设备(告警源) 状态异常(异常类型)的问题 } catch (err) { console.log(`告警(确认)推送失败, error: ${err}`); } } module.exports = { alarmConfirmLog, sendAppearToWeb,//推送告警发现 sendConfirmToWeb//推送告警确认 };