Browse Source

告警详情聚集

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

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

@ -34,76 +34,81 @@ 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) {
whereOption.push(`alarms.AlarmGroup IN (${groupId})`)
}
if (groupUnitId) {
whereOption.push(`alarms.AlarmGroupUnit=${groupId}`)
}
if (sustainTimeStart && sustainTimeEnd) {
let momentStart = moment(sustainTimeStart).format()
let momentEnd = moment(sustainTimeEnd).format()
whereOption.push(`
(
alarms."StartTime"
BETWEEN '${momentStart}' AND '${momentEnd}'
OR
"alarms"."EndTime" BETWEEN '${momentStart}' AND '${momentEnd}'
OR (
"alarms"."StartTime" <= '${momentStart}'
AND
"alarms"."EndTime" >= '${momentEnd}'
)
)
`)
}
const alarmRes = await clickHouse.dataAlarm.query(` if (groupId) {
SELECT whereOption.push(`alarms.AlarmGroup IN (${groupId})`)
alarms.AlarmId AS AlarmId, }
alarms.State AS State, if (groupUnitId) {
SourceName, StartTime, EndTime, whereOption.push(`alarms.AlarmGroupUnit=${groupId}`)
alarms.CurrentLevel AS CurrentLevel, }
SourceTypeId, if (sustainTimeStart && sustainTimeEnd) {
AlarmAdviceProblem, AlarmGroup, AlarmGroupUnit, AlarmAdviceProblem, let momentStart = moment(sustainTimeStart).format()
${anxinyun}.t_structure.name AS StructureName, let momentEnd = moment(sustainTimeEnd).format()
StructureId, whereOption.push(`
${anxinyun}.t_alarm_code.name AS AlarmCodeName (
alarms."StartTime"
BETWEEN '${momentStart}' AND '${momentEnd}'
OR
"alarms"."EndTime" BETWEEN '${momentStart}' AND '${momentEnd}'
OR (
"alarms"."StartTime" <= '${momentStart}'
AND
"alarms"."EndTime" >= '${momentEnd}'
)
)
`)
}
FROM const alarmRes = await clickHouse.dataAlarm.query(`
alarms SELECT
LEFT JOIN ${anxinyun}.t_structure alarms.AlarmId AS AlarmId,
ON ${anxinyun}.t_structure.id = alarms.StructureId alarms.State AS State,
LEFT JOIN ${anxinyun}.t_alarm_code SourceName, StartTime, EndTime,
ON ${anxinyun}.t_alarm_code.code = alarms.AlarmTypeCode alarms.CurrentLevel AS CurrentLevel,
SourceTypeId,
${whereOption.length ? 'WHERE ' + whereOption.join(' AND ') : ''} AlarmAdviceProblem, AlarmGroup, AlarmGroupUnit, AlarmAdviceProblem,
ORDER BY alarms.StartTime DESC ${anxinyun}.t_structure.name AS StructureName,
${limit ? 'LIMIT ' + limit : ''} StructureId,
${limit && page ? 'OFFSET ' + parseInt(limit) * parseInt(page) : ''} ${anxinyun}.t_alarm_code.name AS AlarmCodeName
`).toPromise();
FROM
alarms
LEFT JOIN ${anxinyun}.t_structure
ON ${anxinyun}.t_structure.id = alarms.StructureId
LEFT JOIN ${anxinyun}.t_alarm_code
ON ${anxinyun}.t_alarm_code.code = alarms.AlarmTypeCode
${whereOption.length ? 'WHERE ' + whereOption.join(' AND ') : ''}
ORDER BY alarms.StartTime DESC
${limit ? 'LIMIT ' + limit : ''}
${limit && page ? 'OFFSET ' + parseInt(limit) * parseInt(page) : ''}
`).toPromise();
// alarm_details.Time, alarm_details.Content // alarm_details.Time, alarm_details.Content
// LEFT JOIN alarm_details // LEFT JOIN alarm_details
// ON alarms.AlarmId = alarm_details.AlarmId // ON alarms.AlarmId = alarm_details.AlarmId
// AND alarm_details.Time = ( // AND alarm_details.Time = (
// SELECT MAX(alarm_details.Time) from alarm_details WHERE AlarmId = alarms.AlarmId // SELECT MAX(alarm_details.Time) from alarm_details WHERE AlarmId = alarms.AlarmId
// ) // )
// State = 3 是 自动恢复 / 4 是 人工恢复 / 其他数字 是 需要恢复 // State = 3 是 自动恢复 / 4 是 人工恢复 / 其他数字 是 需要恢复
const SourceType = { 0: 'DTU', 1: '传感器', 2: '测点' }; // const SourceType = { 0: 'DTU', 1: '传感器', 2: '测点' };
ctx.body = alarmRes
} else {
ctx.body = []
}
ctx.status = 200; ctx.status = 200;
ctx.body = alarmRes
} 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();

53
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
WHERE LEFT JOIN
project_state != -1 t_project_structure
AND ON t_project_structure.project = t_project.id
id IN (${anxinStrucIds.join(',')}) RIGHT JOIN
`).toPromise() : t_structure
ON t_structure.id = t_project_structure.structure
WHERE
project_state != -1
AND
t_project.id IN (${anxinProjectIds.join(',')})
${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