Browse Source

(*)修复启动控制台报错; kafka告警处理加异常类型字段

dev
wuqun 2 years ago
parent
commit
81598a0105
  1. 21
      api/app/lib/service/kafka.js
  2. 19
      api/app/lib/utils/helper.js

21
api/app/lib/service/kafka.js

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

19
api/app/lib/utils/helper.js

@ -2,13 +2,14 @@
const moment = require('moment') const moment = require('moment')
async function getAxyStructs(app, pepProjectId) { module.exports = function (app, opts) {
const { models } = app.fs.dc const { models } = app.fs.dc
const { clickHouse } = app.fs const { clickHouse } = app.fs
const { database: anxinyun } = clickHouse.anxinyun.opts.config const { database: anxinyun } = clickHouse.anxinyun.opts.config
async function getAxyStructs(pepProjectId) {
try { try {
try { try {
const { pepProjectRes, bindRes } = await pomsWithPepRangeParams(app, pepProjectId) const { pepProjectRes, bindRes } = await pomsWithPepRangeParams(pepProjectId)
// 获取不重复的 安心云项目 id // 获取不重复的 安心云项目 id
const anxinProjectIds = [ const anxinProjectIds = [
...(bindRes).reduce( ...(bindRes).reduce(
@ -103,12 +104,9 @@ async function getAxyStructs(app, pepProjectId) {
} catch (error) { } catch (error) {
console.log(error) console.log(error)
} }
} }
async function pomsWithPepRangeParams(app, pepProjectId) { async function pomsWithPepRangeParams(pepProjectId) {
const { models } = app.fs.dc
const { clickHouse } = app.fs
const { database: anxinyun } = clickHouse.anxinyun.opts.config
try { try {
let findOption = { let findOption = {
where: { where: {
@ -153,8 +151,9 @@ async function pomsWithPepRangeParams(app, pepProjectId) {
} catch (error) { } catch (error) {
console.error(error); console.error(error);
} }
} }
module.exports = { return {
getAxyStructs getAxyStructs
}; }
}
Loading…
Cancel
Save