|
|
@ -34,76 +34,81 @@ async function list (ctx) { |
|
|
|
const { utils: { judgeSuper, anxinStrucIdRange } } = ctx.app.fs |
|
|
|
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) |
|
|
|
let anxinStrucIds = await anxinStrucIdRange({ ctx, pepProjectId }) |
|
|
|
let anxinStrucIds = await anxinStrucIdRange({ |
|
|
|
ctx, pepProjectId, projectOrStructKeyword, |
|
|
|
}) |
|
|
|
let whereOption = [] |
|
|
|
if (anxinStrucIds) { |
|
|
|
if (anxinStrucIds.length) { |
|
|
|
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(` |
|
|
|
SELECT |
|
|
|
alarms.AlarmId AS AlarmId, |
|
|
|
alarms.State AS State, |
|
|
|
SourceName, StartTime, EndTime, |
|
|
|
alarms.CurrentLevel AS CurrentLevel, |
|
|
|
SourceTypeId, |
|
|
|
AlarmAdviceProblem, AlarmGroup, AlarmGroupUnit, AlarmAdviceProblem, |
|
|
|
${anxinyun}.t_structure.name AS StructureName, |
|
|
|
StructureId, |
|
|
|
${anxinyun}.t_alarm_code.name AS AlarmCodeName |
|
|
|
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}' |
|
|
|
) |
|
|
|
) |
|
|
|
`)
|
|
|
|
} |
|
|
|
|
|
|
|
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();
|
|
|
|
const alarmRes = await clickHouse.dataAlarm.query(` |
|
|
|
SELECT |
|
|
|
alarms.AlarmId AS AlarmId, |
|
|
|
alarms.State AS State, |
|
|
|
SourceName, StartTime, EndTime, |
|
|
|
alarms.CurrentLevel AS CurrentLevel, |
|
|
|
SourceTypeId, |
|
|
|
AlarmAdviceProblem, AlarmGroup, AlarmGroupUnit, AlarmAdviceProblem, |
|
|
|
${anxinyun}.t_structure.name AS StructureName, |
|
|
|
StructureId, |
|
|
|
${anxinyun}.t_alarm_code.name AS AlarmCodeName |
|
|
|
|
|
|
|
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
|
|
|
|
// ON alarms.AlarmId = alarm_details.AlarmId
|
|
|
|
// AND alarm_details.Time = (
|
|
|
|
// SELECT MAX(alarm_details.Time) from alarm_details WHERE AlarmId = alarms.AlarmId
|
|
|
|
// )
|
|
|
|
// LEFT JOIN alarm_details
|
|
|
|
// ON alarms.AlarmId = alarm_details.AlarmId
|
|
|
|
// AND alarm_details.Time = (
|
|
|
|
// SELECT MAX(alarm_details.Time) from alarm_details WHERE AlarmId = alarms.AlarmId
|
|
|
|
// )
|
|
|
|
|
|
|
|
// State = 3 是 自动恢复 / 4 是 人工恢复 / 其他数字 是 需要恢复
|
|
|
|
const SourceType = { 0: 'DTU', 1: '传感器', 2: '测点' }; |
|
|
|
// State = 3 是 自动恢复 / 4 是 人工恢复 / 其他数字 是 需要恢复
|
|
|
|
// const SourceType = { 0: 'DTU', 1: '传感器', 2: '测点' };
|
|
|
|
|
|
|
|
ctx.body = alarmRes |
|
|
|
} else { |
|
|
|
ctx.body = [] |
|
|
|
} |
|
|
|
ctx.status = 200; |
|
|
|
ctx.body = alarmRes |
|
|
|
} catch (error) { |
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: error`); |
|
|
|
ctx.status = 400; |
|
|
@ -207,7 +212,7 @@ async function detailAggregation (ctx) { |
|
|
|
FROM |
|
|
|
alarm_details |
|
|
|
WHERE |
|
|
|
AlarmId=${alarmId} |
|
|
|
AlarmId='${alarmId}' |
|
|
|
GROUP BY |
|
|
|
hours; |
|
|
|
`).toPromise();
|
|
|
|