Browse Source

告警数据查询接口

dev
CODE 1 year ago
parent
commit
17d92ae3f4
  1. 80
      api/app/lib/controllers/project/group.js

80
api/app/lib/controllers/project/group.js

@ -116,7 +116,7 @@ async function groupStatistic (ctx) {
}
})
// 获取全部的 poms 项目id 并构建关系
// 获取全部的 poms 项目id
let pomsProjectIds = new Set()
for (let group of progectGroupList) {
for (let projectId of group.pomsProjectIds) {
@ -322,8 +322,84 @@ async function groupStatisticAlarm (ctx) {
try {
const { models } = ctx.fs.dc;
const { groupId } = ctx.query
const sequelize = ctx.fs.dc.orm
const { clickHouse } = ctx.app.fs
const pomsProjectRes = await sequelize.query(`
SELECT project_correlation.anxin_project_id
FROM project_group
JOIN project_correlation
ON project_correlation.id = ANY(project_group.poms_project_ids)
WHERE project_group.id = ${groupId};
`)
const anxinProjectIds = new Set()
for (let pomsProject of (pomsProjectRes[0] || [])) {
for (let pid of pomsProject.anxin_project_id)
anxinProjectIds.add(pid)
}
const strucIdRes = anxinProjectIds.size ? await clickHouse.anxinyun.query(
`
SELECT *
FROM t_project_structure
WHERE project IN (${[...anxinProjectIds].join(',')}, -1)
`
).toPromise() : []
let strucIds = new Set()
for (let struc of strucIdRes) {
strucIds.add(struc.structure)
}
let strucIdArr = Array.from(strucIds)
const strucRes = strucIdArr.length ? await clickHouse.anxinyun.query(
`
SELECT name, id FROM t_structure WHERE id IN (${[...strucIdArr].join(',')});
`
).toPromise() : []
// 查一周内超阈值告警的个数
strucIdArr = [1]
const alarmRes = strucIdArr.length ? await clickHouse.dataAlarm.query(
`
SELECT StructureId,count(StructureId) AS alarmCount
FROM alarms
WHERE StructureId IN (${[...strucIdArr].join(',')})
AND AlarmGroupUnit = 8
AND StartTime >= '${moment().subtract(7, 'days').format('YYYY-MM-DD HH:mm:ss')}'
group by StructureId
`
).toPromise() : []
const alarmDealRes = strucIdArr.length ? await clickHouse.dataAlarm.query(
`
SELECT StructureId,count(StructureId) AS alarmCount
FROM alarms
WHERE StructureId IN (${[...strucIdArr].join(',')})
AND AlarmGroupUnit = 8
AND State = 4
AND StartTime >= '${moment().subtract(30, 'days').format('YYYY-MM-DD HH:mm:ss')}'
group by StructureId
`
).toPromise() : []
for (let struc of strucRes) {
let corAlarm = alarmRes.find((o) => o.StructureId == struc.id)
let corDealAlarm = alarmDealRes.find((o) => o.StructureId == struc.id)
struc.alarmCount = corAlarm ? corAlarm.alarmCount : 0
struc.dealAlarmCount = corDealAlarm ? corDealAlarm.alarmCount : 0
}
strucRes.sort((a, b) => b.alarmCount - a.alarmCount)
ctx.status = 200;
ctx.body = []
ctx.body = strucRes;
// ctx.body = [{
// id: 1,
// name: '测试结构物1',
// alarmCount: 128,
// dealAlarmCount: 23
// }];
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;

Loading…
Cancel
Save