|
|
@ -1,7 +1,7 @@ |
|
|
|
'use strict'; |
|
|
|
const moment = require('moment'); |
|
|
|
const Kafka = require('kafka-node'); |
|
|
|
const { getAxyStructs } = require('../utils/helper'); |
|
|
|
//const { getAxyStructs } = require('../utils/helper');
|
|
|
|
|
|
|
|
module.exports = async function factory(app, opts) { |
|
|
|
|
|
|
@ -11,7 +11,6 @@ module.exports = async function factory(app, opts) { |
|
|
|
expireTime: null//10分钟更新一次结构物列表
|
|
|
|
} |
|
|
|
|
|
|
|
//const { utils: { getAxyStructs } } = app.fs
|
|
|
|
const client = new Kafka.KafkaClient({ kafkaHost: opts.kafka.rootURL }); |
|
|
|
const producer = new Kafka.HighLevelProducer(client); |
|
|
|
producer.on('error', function (err) { |
|
|
@ -70,10 +69,11 @@ module.exports = async function factory(app, opts) { |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
async function getStructsAche(app) { |
|
|
|
async function getStructsAche() { |
|
|
|
const { utils: { getAxyStructs } } = app.fs |
|
|
|
try { |
|
|
|
if (!structsAche.dataList.length || moment() > moment(structsAche.expireTime)) { |
|
|
|
let structList = await getAxyStructs(app); |
|
|
|
let structList = await getAxyStructs(); |
|
|
|
structsAche.dataList = structList; |
|
|
|
structsAche.expireTime = moment().add(10, 'minute').format('YYYY-MM-DD HH:mm:ss'); |
|
|
|
} |
|
|
@ -85,22 +85,27 @@ module.exports = async function factory(app, opts) { |
|
|
|
|
|
|
|
//保存告警[发现]
|
|
|
|
async function savePullAlarm(msg) { |
|
|
|
const { clickHouse } = app.fs |
|
|
|
try { |
|
|
|
let { messageMode, structureId, sourceName, alarmTypeCode, alarmCode, content, time } = msg; |
|
|
|
let structsAche = await getStructsAche(app); |
|
|
|
let structsAche = await getStructsAche(); |
|
|
|
if (structsAche) { |
|
|
|
let structs = structsAche.dataList;//结构物列表
|
|
|
|
const { models } = app.fs.dc |
|
|
|
let exist = structs.find(s => s.strucId == structureId); |
|
|
|
if (exist) { |
|
|
|
let alarmType = await clickHouse.anxinyun.query( |
|
|
|
`SELECT name FROM t_alarm_type WHERE code='${alarmTypeCode}'`).toPromise() |
|
|
|
let type = alarmType.length ? alarmType[0].name : '' |
|
|
|
|
|
|
|
let projects = exist.pomsProject.filter(d => !d.del).map(p => p.id); |
|
|
|
if (messageMode == 'AlarmGeneration') {//告警产生--------------------------------------------------1
|
|
|
|
let datas = projects.map(d => {//需要 项目,告警源,异常类型,时间
|
|
|
|
return { |
|
|
|
projectCorrelationId: d, |
|
|
|
alarmInfo: { structureId, sourceName, alarmTypeCode, alarmCode, content }, |
|
|
|
alarmInfo: { sourceName, alarmTypeCode }, |
|
|
|
time: time, |
|
|
|
type: 1//异常类型 TODO
|
|
|
|
type//异常类型
|
|
|
|
} |
|
|
|
}) |
|
|
|
let rslt = await models.AlarmAppearRecord.bulkCreate(datas, { returning: true }); |
|
|
@ -118,7 +123,7 @@ module.exports = async function factory(app, opts) { |
|
|
|
return { |
|
|
|
pepUserId: null, |
|
|
|
projectCorrelationId: d, |
|
|
|
alarmInfo: { sourceName, alarmTypeCode, alarmCode },//包含告警id,type,source TODO
|
|
|
|
alarmInfo: { source: sourceName, type },//包含告警id,type,source
|
|
|
|
confirmTime: time, |
|
|
|
confirmContent: '自动恢复' |
|
|
|
} |
|
|
|