Browse Source

告警详情聚集

dev
巴林闲侠 2 years ago
parent
commit
a80535df60
  1. 19
      api/app/lib/controllers/alarm/data.js
  2. 41
      api/app/lib/utils/dataRange.js

19
api/app/lib/controllers/alarm/data.js

@ -34,14 +34,16 @@ async function list (ctx) {
const { utils: { judgeSuper, anxinStrucIdRange } } = ctx.app.fs const { utils: { judgeSuper, anxinStrucIdRange } } = ctx.app.fs
const { database: anxinyun } = clickHouse.anxinyun.opts.config const { database: anxinyun } = clickHouse.anxinyun.opts.config
const { pepProjectId, keyword, groupId, groupUnitId, sustainTimeStart, sustainTimeEnd, limit, page } = ctx.query const { pepProjectId, keyword, groupId, groupUnitId, sustainTimeStart, sustainTimeEnd, limit, page, projectOrStructKeyword } = ctx.query
const isSuper = judgeSuper(ctx) const isSuper = judgeSuper(ctx)
let anxinStrucIds = await anxinStrucIdRange({ ctx, pepProjectId }) let anxinStrucIds = await anxinStrucIdRange({
ctx, pepProjectId, projectOrStructKeyword,
})
let whereOption = [] let whereOption = []
if (anxinStrucIds) { if (anxinStrucIds.length) {
whereOption.push(`alarms.StructureId IN (${anxinStrucIds.join(",")})`) whereOption.push(`alarms.StructureId IN (${anxinStrucIds.join(",")})`)
}
if (groupId) { if (groupId) {
whereOption.push(`alarms.AlarmGroup IN (${groupId})`) whereOption.push(`alarms.AlarmGroup IN (${groupId})`)
} }
@ -100,10 +102,13 @@ async function list (ctx) {
// ) // )
// State = 3 是 自动恢复 / 4 是 人工恢复 / 其他数字 是 需要恢复 // State = 3 是 自动恢复 / 4 是 人工恢复 / 其他数字 是 需要恢复
const SourceType = { 0: 'DTU', 1: '传感器', 2: '测点' }; // const SourceType = { 0: 'DTU', 1: '传感器', 2: '测点' };
ctx.status = 200;
ctx.body = alarmRes ctx.body = alarmRes
} else {
ctx.body = []
}
ctx.status = 200;
} catch (error) { } catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: error`); ctx.fs.logger.error(`path: ${ctx.path}, error: error`);
ctx.status = 400; ctx.status = 400;
@ -207,7 +212,7 @@ async function detailAggregation (ctx) {
FROM FROM
alarm_details alarm_details
WHERE WHERE
AlarmId=${alarmId} AlarmId='${alarmId}'
GROUP BY GROUP BY
hours; hours;
`).toPromise(); `).toPromise();

41
api/app/lib/utils/dataRange.js

@ -14,7 +14,7 @@ module.exports = function (app, opts) {
} }
} }
async function anxinStrucIdRange ({ ctx, pepProjectId }) { async function anxinStrucIdRange ({ ctx, pepProjectId, projectOrStructKeyword }) {
try { try {
const { models } = ctx.fs.dc; const { models } = ctx.fs.dc;
const { userInfo = {} } = ctx.fs.api || {}; const { userInfo = {} } = ctx.fs.api || {};
@ -36,9 +36,12 @@ module.exports = function (app, opts) {
// TODO 这儿也许需要判断传进来的 pepProjectId 在不在当前用户的关注范围内 // TODO 这儿也许需要判断传进来的 pepProjectId 在不在当前用户的关注范围内
const bindRes = await models.ProjectCorrelation.findAll(findOption) const bindRes = await models.ProjectCorrelation.findAll(findOption)
const anxinStrucIds = [ let pepProjectIds = []
const anxinProjectIds = [
...bindRes.reduce( ...bindRes.reduce(
(arr, b) => { (arr, b) => {
pepProjectIds.push(b.pepProjectId)
for (let sid of b.anxinProjectId) { for (let sid of b.anxinProjectId) {
arr.add(sid); arr.add(sid);
} }
@ -47,22 +50,42 @@ module.exports = function (app, opts) {
new Set() new Set()
) )
] ]
const pepProjectRes = pepProjectIds.length ?
await clickHouse.projectManage.query(
`
SELECT
id, project_name, isdelete
FROM
t_pim_project
WHERE
id IN (${pepProjectIds.join(',')})
`
).toPromise() :
[]
// 查结构物 左联 项目 ,项目 id 在关注的项目里 const undelStrucRes = anxinProjectIds.length ?
const undelStrucRes = anxinStrucIds.length ? await clickHouse.anxinyun.query(
await clickHouse.anxinyun.query(` `
SELECT SELECT
id t_project.id, t_structure.id AS strucId, project_state
FROM FROM
t_project t_project
LEFT JOIN
t_project_structure
ON t_project_structure.project = t_project.id
RIGHT JOIN
t_structure
ON t_structure.id = t_project_structure.structure
WHERE WHERE
project_state != -1 project_state != -1
AND AND
id IN (${anxinStrucIds.join(',')}) t_project.id IN (${anxinProjectIds.join(',')})
`).toPromise() : ${projectOrStructKeyword ? `AND (t_project.name LIKE '%${projectOrStructKeyword}%' OR t_structure.name LIKE '%${projectOrStructKeyword}%')` : ''}
`
).toPromise() :
[] []
return undelStrucRes.map(s => s.id) return [...new Set(...undelStrucRes.map(s => s.strucId))]
} catch (error) { } catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
} }

Loading…
Cancel
Save