Browse Source

数据告警信息获取 92%

dev
巴林闲侠 2 years ago
parent
commit
af7605dcfc
  1. 72
      api/app/lib/controllers/alarm/data.js
  2. 146
      api/app/lib/utils/dataRange.js

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

@ -34,15 +34,16 @@ 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, projectOrStructKeyword } = ctx.query
const { pepProjectId, keywordTarget, keyword, groupId, groupUnitId, sustainTimeStart, sustainTimeEnd, limit, page, projectOrStructKeyword } = ctx.query
const isSuper = judgeSuper(ctx)
let anxinStrucIds = await anxinStrucIdRange({
ctx, pepProjectId, projectOrStructKeyword,
let anxinStruc = await anxinStrucIdRange({
ctx, pepProjectId, keywordTarget, keyword
})
let whereOption = []
if (anxinStrucIds.length ||1) {
whereOption.push(`alarms.StructureId IN (${anxinStrucIds.join(",")})`)
if (anxinStruc.length || 1) {
const anxinStrucIds = anxinStruc.map(a => a.strucId)
// TODO: 开发临时注释
// whereOption.push(`alarms.StructureId IN (${anxinStrucIds.join(",")})`)
if (groupId) {
whereOption.push(`alarms.AlarmGroup IN (${groupId})`)
@ -67,6 +68,9 @@ async function list (ctx) {
)
`)
}
if (keywordTarget == 'source' && keyword) {
whereOption.push(`SourceName LIKE '%${keyword}%'`)
}
const alarmRes = await clickHouse.dataAlarm.query(`
SELECT
@ -76,31 +80,61 @@ async function list (ctx) {
alarms.CurrentLevel AS CurrentLevel,
SourceTypeId,
AlarmAdviceProblem, AlarmGroup, AlarmGroupUnit, AlarmAdviceProblem,
alarms.StructureId AS StructureId,
${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
// 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
// )
const confirmedAlarm = alarmRes
// TODO: 开发临时注释
// .filter(ar => ar.State && ar.State > 2)
.map(ar => "'" + ar.AlarmId + "'")
const confirmedAlarmDetailMax = confirmedAlarm.length ?
await clickHouse.dataAlarm.query(`
SELECT
max(Time) AS Time,AlarmId
FROM
alarm_details
WHERE
AlarmId IN (${confirmedAlarm.join(',')})
GROUP BY AlarmId
`).toPromise() :
[];
// State = 3 是 自动恢复 / 4 是 人工恢复 / 其他数字 是 需要恢复
// const SourceType = { 0: 'DTU', 1: '传感器', 2: '测点' };
// state = 2 是 等级提升 / 1 是持续产生 / 0 是首次产生
// SourceType 0: 'DTU' / 1: '传感器' / 2: '测点'
alarmRes.forEach(ar => {
ar.pepProject = (anxinStruc.find(as => as.strucId == ar.StructureId) ||
{
// TODO: 开发临时添加
pepProject: [{
id: 999,
name: '这是假的开发の数据,看到请拨打110',
isdelete: 0,
}]
}).pepProject
let corConfirmedData = (confirmedAlarmDetailMax.find(cdm => cdm.AlarmId == ar.AlarmId) || {});
if (corConfirmedData.AlarmState && corConfirmedData.AlarmState > 2) {
} else {
corConfirmedData = {}
}
ar.confirmedContent = 'corConfirmedData.Content'
ar.confirmedTime = 'corConfirmedData.Time'
})
ctx.body = alarmRes
} else {
ctx.body = []

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

@ -8,66 +8,90 @@ module.exports = function (app, opts) {
try {
const { userInfo = {} } = ctx.fs.api || {};
const { role = [] } = userInfo
return role.includes('SuperAdmin')
return role.some(r => r == 'SuperAdmin' || r == 'admin')
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
}
}
async function anxinStrucIdRange ({ ctx, pepProjectId, projectOrStructKeyword }) {
try {
const { models } = ctx.fs.dc;
const { userInfo = {} } = ctx.fs.api || {};
const { clickHouse } = ctx.app.fs
const { correlationProject = [] } = userInfo
async function anxinStrucIdRange ({ ctx, pepProjectId, keywordTarget, keyword }) {
const { models } = ctx.fs.dc;
const { userInfo = {} } = ctx.fs.api || {};
const { clickHouse } = ctx.app.fs
const { correlationProject = [] } = userInfo
const isSuper = judgeSuper(ctx)
let findOption = {
where: {
del: false
}
}
if (pepProjectId) {
findOption.where.pepProjectId = pepProjectId
} else if (!isSuper) {
findOption.where.id = { $in: correlationProject }
const isSuper = judgeSuper(ctx)
let findOption = {
where: {
del: false
}
}
if (pepProjectId) {
// 有 特定的项目id 就按此查询
findOption.where.pepProjectId = pepProjectId
} else if (!isSuper) {
// 还不是超管或管理员就按关联的项目id的数据范围查
findOption.where.id = { $in: correlationProject }
}
// TODO 这儿也许需要判断传进来的 pepProjectId 在不在当前用户的关注范围内
const bindRes = await models.ProjectCorrelation.findAll(findOption)
// TODO 这儿也许需要判断传进来的 pepProjectId 在不在当前用户的关注范围内
// 根据 poms 的项目绑定关系查相关联的项企项目、安心云项目id信息
const bindRes = await models.ProjectCorrelation.findAll(findOption)
let pepProjectIds = []
// 获取不重复的 安心云项目id
let pepProjectIds = bindRes.map(b => b.pepProjectId)
const anxinProjectIds = [
...bindRes.reduce(
(arr, b) => {
pepProjectIds.push(b.pepProjectId)
for (let sid of b.anxinProjectId) {
arr.add(sid);
}
return arr;
},
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() :
[]
// 查询项企项目的信息
let pepProjectWhereOptions = []
if (keywordTarget == 'pepProject' && keyword) {
pepProjectWhereOptions.push(`name LIKE '%${keyword}%')`)
}
const pepProjectRes = pepProjectIds.length ?
await clickHouse.projectManage.query(
`
SELECT
id, project_name AS name, isdelete
FROM
t_pim_project
WHERE
id IN (${pepProjectIds.join(',')})
${pepProjectWhereOptions.length ? `AND ${pepProjectWhereOptions.join(' AND ')}`
: ''}
`
).toPromise() :
[]
const undelStrucRes = anxinProjectIds.length ?
await clickHouse.anxinyun.query(
`
const anxinProjectIds = [
...(
bindRes.filter(b => pepProjectRes.some(pp => pp.id == b.pepProjectId))
).reduce(
(arr, b) => {
for (let sid of b.anxinProjectId) {
arr.add(sid);
}
return arr;
},
new Set()
)
]
// 查询安心云项目及结构物信息
let undelStrucWhereOptions = []
if (keywordTarget && keyword) {
if (keywordTarget == 'struc') {
undelStrucWhereOptions.push(`t_structure.name LIKE '%${keyword}%'`)
}
}
const undelStrucRes = anxinProjectIds.length ?
await clickHouse.anxinyun.query(
`
SELECT
t_project.id, t_structure.id AS strucId, project_state
t_project.id AS projectId,
t_structure.id AS strucId,
t_structure.name AS strucName,
project_state
FROM
t_project
LEFT JOIN
@ -80,16 +104,28 @@ module.exports = function (app, opts) {
project_state != -1
AND
t_project.id IN (${anxinProjectIds.join(',')})
${projectOrStructKeyword ? `AND (t_project.name LIKE '%${projectOrStructKeyword}%' OR t_structure.name LIKE '%${projectOrStructKeyword}%')` : ''}
${undelStrucWhereOptions.length ? `AND ${undelStrucWhereOptions.join(' AND ')}` : ''}
`
).toPromise() :
[]
).toPromise() :
[]
const undelStrucIds = [...new Set(...undelStrucRes.map(s => s.strucId))]
return {}
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
// 构建安心云结构物和项企项目的关系
// 并保存信息至数据
let undelStruc = []
for (let s of undelStrucRes) {
if (!undelStruc.some(us => us.strucId == s.strucId)) {
undelStruc.push({
strucId: s.strucId,
strucName: s.strucName,
pepProject: pepProjectRes.filter(pp => {
return bindRes.find(br => {
return br.pepProjectId == pp.id && br.anxinProjectId.some(braId => braId == s.strucId)
})
})
})
}
}
return undelStruc
}
return {

Loading…
Cancel
Save