From 17d92ae3f4c2caa95397caf1f8ec8b051e7f0b65 Mon Sep 17 00:00:00 2001 From: CODE <1650192445@qq.com> Date: Wed, 30 Aug 2023 11:47:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=91=8A=E8=AD=A6=E6=95=B0=E6=8D=AE=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/app/lib/controllers/project/group.js | 80 +++++++++++++++++++++++- 1 file changed, 78 insertions(+), 2 deletions(-) diff --git a/api/app/lib/controllers/project/group.js b/api/app/lib/controllers/project/group.js index 7bbc8ae..c9f0fbb 100644 --- a/api/app/lib/controllers/project/group.js +++ b/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;