|
|
@ -1,4 +1,5 @@ |
|
|
|
'use strict'; |
|
|
|
const moment = require('moment'); |
|
|
|
|
|
|
|
async function groupList (ctx) { |
|
|
|
try { |
|
|
@ -33,7 +34,7 @@ async function list (ctx) { |
|
|
|
const { utils: { judgeSuper, anxinStrucIdRange } } = ctx.app.fs |
|
|
|
const { database: anxinyun } = clickHouse.anxinyun.opts.config |
|
|
|
|
|
|
|
const { pepProjectId, groupId, groupUnitId, sustainTimeStart, sustainTimeEnd, limit, page } = ctx.query |
|
|
|
const { pepProjectId, keyword, groupId, groupUnitId, sustainTimeStart, sustainTimeEnd, limit, page } = ctx.query |
|
|
|
|
|
|
|
const isSuper = judgeSuper(ctx) |
|
|
|
let anxinStrucIds = null |
|
|
@ -67,6 +68,7 @@ async function list (ctx) { |
|
|
|
) |
|
|
|
`)
|
|
|
|
} |
|
|
|
|
|
|
|
const alarmRes = await clickHouse.dataAlarm.query(` |
|
|
|
SELECT |
|
|
|
alarms.AlarmId AS AlarmId, |
|
|
@ -100,6 +102,9 @@ async function list (ctx) { |
|
|
|
// SELECT MAX(alarm_details.Time) from alarm_details WHERE AlarmId = alarms.AlarmId
|
|
|
|
// )
|
|
|
|
|
|
|
|
// State = 3 是 自动恢复 / 4 是 人工恢复 / 其他数字 是 需要恢复
|
|
|
|
|
|
|
|
|
|
|
|
ctx.status = 200; |
|
|
|
ctx.body = alarmRes |
|
|
|
} catch (error) { |
|
|
@ -132,23 +137,86 @@ async function detail (ctx) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
async function confirm (ctx) { |
|
|
|
function confirm (opts) { |
|
|
|
return async function (ctx) { |
|
|
|
try { |
|
|
|
const { models } = ctx.fs.dc; |
|
|
|
const { utils: { kfkSendAsync } } = ctx.app.fs |
|
|
|
const { clickHouse } = ctx.app.fs |
|
|
|
const { content = '', alarmId } = ctx.request.body |
|
|
|
// 发送告警恢复通知
|
|
|
|
// Topic: alarm
|
|
|
|
/* |
|
|
|
* { |
|
|
|
* messageMode: "AlarmManElimination", |
|
|
|
* sourceId: "", |
|
|
|
* alarmTypeCode: "", |
|
|
|
* sponsor: userId, |
|
|
|
* content: "确认消息", |
|
|
|
* time: "YYYY-MM-DDTHH:mm:ss.SSSZ" |
|
|
|
* } |
|
|
|
*/ |
|
|
|
|
|
|
|
const alarmRes = await clickHouse.dataAlarm.query(` |
|
|
|
SELECT * FROM alarms WHERE AlarmId = '${alarmId}' |
|
|
|
`).toPromise();
|
|
|
|
|
|
|
|
if (!alarmRes.length) { |
|
|
|
throw '没有查询到对应的告警信息' |
|
|
|
} |
|
|
|
|
|
|
|
const [corAlarm] = alarmRes |
|
|
|
if ([3, 4].some(s => s == corAlarm.State)) { |
|
|
|
throw '告警信息已确认' |
|
|
|
} |
|
|
|
const message = { |
|
|
|
messageMode: "AlarmManElimination", |
|
|
|
sourceId: corAlarm.SourceId, |
|
|
|
alarmTypeCode: corAlarm.AlarmTypeCode, |
|
|
|
sponsor: opts.anxinCloud.confirmAlarmAnxinUserId, |
|
|
|
content: content, |
|
|
|
time: moment().toISOString() |
|
|
|
}; |
|
|
|
|
|
|
|
const payloads = [{ |
|
|
|
topic: `${opts.kafka.topicPrefix}_alarm`, |
|
|
|
messages: [JSON.stringify(message)], |
|
|
|
partition: 0 |
|
|
|
}]; |
|
|
|
|
|
|
|
await kfkSendAsync(payloads) |
|
|
|
|
|
|
|
ctx.status = 204; |
|
|
|
} catch (error) { |
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: error`); |
|
|
|
ctx.status = 400; |
|
|
|
ctx.body = { |
|
|
|
message: typeof error == 'string' ? error : undefined |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function detailAggregation (ctx) { |
|
|
|
try { |
|
|
|
const { models } = ctx.fs.dc; |
|
|
|
// 发送告警恢复通知
|
|
|
|
// Topic: alarm
|
|
|
|
/* |
|
|
|
* { |
|
|
|
* messageMode: "AlarmManElimination", |
|
|
|
* sourceId: "", |
|
|
|
* alarmTypeCode: "", |
|
|
|
* sponsor: userId, |
|
|
|
* content: "确认消息", |
|
|
|
* time: "YYYY-MM-DDTHH:mm:ss.SSSZ" |
|
|
|
* } |
|
|
|
*/ |
|
|
|
|
|
|
|
ctx.status = 204; |
|
|
|
const { alarmId } = ctx.query |
|
|
|
const { clickHouse } = ctx.app.fs |
|
|
|
|
|
|
|
const alarmDetailAggRes = await clickHouse.dataAlarm.query(` |
|
|
|
SELECT |
|
|
|
formatDateTime(Time,'%F %H') hours, count(AlarmId) count |
|
|
|
FROM |
|
|
|
alarm_details |
|
|
|
WHERE |
|
|
|
AlarmId=${alarmId} |
|
|
|
GROUP BY |
|
|
|
hours; |
|
|
|
`).toPromise();
|
|
|
|
|
|
|
|
ctx.status = 200; |
|
|
|
ctx.body = alarmDetailAggRes |
|
|
|
} catch (error) { |
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: error`); |
|
|
|
ctx.status = 400; |
|
|
@ -158,18 +226,17 @@ async function confirm (ctx) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
async function detailAggregation (ctx) { |
|
|
|
async function alarmCount (ctx) { |
|
|
|
try { |
|
|
|
const { models } = ctx.fs.dc; |
|
|
|
const { alarmId } = ctx.query |
|
|
|
const { clickHouse } = ctx.app.fs |
|
|
|
|
|
|
|
const alarmDetailAggRes = await clickHouse.dataAlarm.query(` |
|
|
|
SELECT formatDateTime(Time,'%F %H') days, count(AlarmId) count from alarm_details WHERE AlarmId=${AlarmId} group by days; |
|
|
|
const alarmUnconfirmedAggRes = await clickHouse.dataAlarm.query(` |
|
|
|
SELECT count(AlarmId) count, AlarmGroup from alarms GROUP BY AlarmGroup; |
|
|
|
`).toPromise();
|
|
|
|
|
|
|
|
ctx.status = 200; |
|
|
|
ctx.body = alarmDetailAggRes |
|
|
|
ctx.body = alarmUnconfirmedAggRes |
|
|
|
} catch (error) { |
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: error`); |
|
|
|
ctx.status = 400; |
|
|
@ -185,4 +252,5 @@ module.exports = { |
|
|
|
groupList, |
|
|
|
confirm, |
|
|
|
detailAggregation, |
|
|
|
alarmCount, |
|
|
|
}; |