zhaobing’ 1 year ago
parent
commit
9ee4355f15
  1. 161
      api/app/lib/controllers/project/group.js
  2. 24
      api/app/lib/schedule/alarms_push.js

161
api/app/lib/controllers/project/group.js

@ -108,6 +108,7 @@ async function groupStatistic (ctx) {
const { models } = ctx.fs.dc; const { models } = ctx.fs.dc;
const { userId } = ctx.fs.api const { userId } = ctx.fs.api
const { clickHouse } = ctx.app.fs const { clickHouse } = ctx.app.fs
const { utils: { judgeSuper, anxinStrucIdRange } } = ctx.app.fs
const sequelize = ctx.fs.dc.orm const sequelize = ctx.fs.dc.orm
const progectGroupList = await models.ProjectGroup.findAll({ const progectGroupList = await models.ProjectGroup.findAll({
@ -140,18 +141,46 @@ async function groupStatistic (ctx) {
} }
let anxinProjectIdArr = Array.from(anxinProjectIds) let anxinProjectIdArr = Array.from(anxinProjectIds)
// 统计安心云项目下的结构物id // 统计安心云项目下的结构物
const strucIdRes = await clickHouse.anxinyun.query( const strucRes = anxinProjectIdArr.length ? await clickHouse.anxinyun.query(
` `
SELECT * SELECT
FROM t_project_structure t_project.id AS projectId,
WHERE project IN (${[...anxinProjectIdArr].join(',')}, -1) t_structure.id AS strucId,
t_structure.name AS strucName,
project_state
FROM
t_project
LEFT JOIN
t_project_structure
ON t_project_structure.project = t_project.id
LEFT JOIN
t_project_structuregroup
ON t_project_structuregroup.project = t_project.id
LEFT JOIN
t_structuregroup_structure
ON t_structuregroup_structure.structuregroup = t_project_structuregroup.structuregroup
LEFT JOIN
t_project_construction
ON t_project_construction.project = t_project.id
LEFT JOIN
t_structure_site
ON t_structure_site.siteid = t_project_construction.construction
RIGHT JOIN
t_structure
ON t_structure.id = t_project_structure.structure
OR t_structure.id = t_structuregroup_structure.structure
OR t_structure.id = t_structure_site.structid
WHERE
project_state != -1
AND
t_project.id IN (${anxinProjectIdArr.join(',')},-1)
` `
).toPromise() ).toPromise() : []
let strucIds = new Set() let strucIds = new Set()
for (let struc of strucIdRes) { for (let struc of strucRes) {
strucIds.add(struc.structure) strucIds.add(struc.strucId)
} }
let strucIdArr = Array.from(strucIds) let strucIdArr = Array.from(strucIds)
@ -175,7 +204,7 @@ async function groupStatistic (ctx) {
for (let pg of progectGroupList) { for (let pg of progectGroupList) {
let strucCount = 0 let strucCount = 0
let maxOffLineTime = 0 let maxOffLineTime = 0
let todayAlarms=0 let todayAlarms = 0
let anxinProjectCount = groupProjectRes.reduce((count, gp) => { let anxinProjectCount = groupProjectRes.reduce((count, gp) => {
if (pg.pomsProjectIds.some(id => gp.id == id)) { if (pg.pomsProjectIds.some(id => gp.id == id)) {
// 如果 pg 分组信息 存储的 pomsProjectIds 运维项目id 和 真正的 groupProjectRes 运维项目id 相等 // 如果 pg 分组信息 存储的 pomsProjectIds 运维项目id 和 真正的 groupProjectRes 运维项目id 相等
@ -183,16 +212,16 @@ async function groupStatistic (ctx) {
count += gp.anxinProjectId.length count += gp.anxinProjectId.length
// //
let strucIdArr_ = let strucIdArr_ =
strucIdRes.filter(s => gp.anxinProjectId.includes(s.project)) strucRes.filter(s => gp.anxinProjectId.includes(s.projectId))
strucCount += strucIdArr_.length strucCount += strucIdArr_.length
//所有结构物的告警 //所有结构物的告警
const ss=alarmRes.filter(p=>strucIdArr_.some(q=>q.structure==p.StructureId)) const ss = alarmRes.filter(p => strucIdArr_.some(q => q.structure == p.StructureId))
todayAlarms= ss.reduce((p,c)=>{ todayAlarms = ss.reduce((p, c) => {
return p+c.alarmCount return p + c.alarmCount
},0) }, 0)
// //
for (let { dataValues: off } of deviceOffTimeRes) { for (let { dataValues: off } of deviceOffTimeRes) {
if (strucIdArr_.some((s) => s.structure == off.structure)) { if (strucIdArr_.some((s) => s.strucId == off.structure)) {
if (off.offline > maxOffLineTime) { if (off.offline > maxOffLineTime) {
maxOffLineTime = off.offline maxOffLineTime = off.offline
} }
@ -243,25 +272,49 @@ async function groupStatisticOnline (ctx) {
anxinProjectIds.add(pid) anxinProjectIds.add(pid)
} }
const strucIdRes = await clickHouse.anxinyun.query( // 统计安心云项目下的结构物
const strucRes = anxinProjectIds.length ? await clickHouse.anxinyun.query(
` `
SELECT * SELECT
FROM t_project_structure t_project.id AS projectId,
WHERE project IN (${[...anxinProjectIds].join(',')}, -1) t_structure.id AS id,
t_structure.name AS name,
project_state
FROM
t_project
LEFT JOIN
t_project_structure
ON t_project_structure.project = t_project.id
LEFT JOIN
t_project_structuregroup
ON t_project_structuregroup.project = t_project.id
LEFT JOIN
t_structuregroup_structure
ON t_structuregroup_structure.structuregroup = t_project_structuregroup.structuregroup
LEFT JOIN
t_project_construction
ON t_project_construction.project = t_project.id
LEFT JOIN
t_structure_site
ON t_structure_site.siteid = t_project_construction.construction
RIGHT JOIN
t_structure
ON t_structure.id = t_project_structure.structure
OR t_structure.id = t_structuregroup_structure.structure
OR t_structure.id = t_structure_site.structid
WHERE
project_state != -1
AND
t_project.id IN (${anxinProjectIds.join(',')},-1)
` `
).toPromise() ).toPromise() : []
let strucIds = new Set() let strucIds = new Set()
for (let struc of strucIdRes) { for (let struc of strucRes) {
strucIds.add(struc.structure) strucIds.add(struc.id)
} }
let strucIdArr = Array.from(strucIds) 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() : []
// 查中断时间 // 查中断时间
const maxOfflineTimeRes = strucIdArr.length ? await models.StructureOff.findAll({ const maxOfflineTimeRes = strucIdArr.length ? await models.StructureOff.findAll({
where: { where: {
@ -272,8 +325,6 @@ async function groupStatisticOnline (ctx) {
// 查在线率 // 查在线率
const strucOnlineClient = ctx.app.fs.esclient.strucOnline const strucOnlineClient = ctx.app.fs.esclient.strucOnline
console.log('es参数,', strucOnlineClient.config)
console.log('strucIdArr', strucIdArr)
const onlineRes = await strucOnlineClient.search({ const onlineRes = await strucOnlineClient.search({
index: strucOnlineClient.config.index, index: strucOnlineClient.config.index,
@ -309,7 +360,6 @@ async function groupStatisticOnline (ctx) {
"size": 10000 "size": 10000
} }
}) })
console.log('es数据,', onlineRes.hits.hits.length, onlineRes.hits.hits)
for (let struc of strucRes) { for (let struc of strucRes) {
let curOnline = let curOnline =
@ -362,27 +412,50 @@ async function groupStatisticAlarm (ctx) {
anxinProjectIds.add(pid) anxinProjectIds.add(pid)
} }
const strucIdRes = anxinProjectIds.size ? await clickHouse.anxinyun.query( // 统计安心云项目下的结构物
const strucRes = anxinProjectIds.length ? await clickHouse.anxinyun.query(
` `
SELECT * SELECT
FROM t_project_structure t_project.id AS projectId,
WHERE project IN (${[...anxinProjectIds].join(',')}, -1) t_structure.id AS id,
t_structure.name AS name,
project_state
FROM
t_project
LEFT JOIN
t_project_structure
ON t_project_structure.project = t_project.id
LEFT JOIN
t_project_structuregroup
ON t_project_structuregroup.project = t_project.id
LEFT JOIN
t_structuregroup_structure
ON t_structuregroup_structure.structuregroup = t_project_structuregroup.structuregroup
LEFT JOIN
t_project_construction
ON t_project_construction.project = t_project.id
LEFT JOIN
t_structure_site
ON t_structure_site.siteid = t_project_construction.construction
RIGHT JOIN
t_structure
ON t_structure.id = t_project_structure.structure
OR t_structure.id = t_structuregroup_structure.structure
OR t_structure.id = t_structure_site.structid
WHERE
project_state != -1
AND
t_project.id IN (${anxinProjectIds.join(',')},-1)
` `
).toPromise() : [] ).toPromise() : []
let strucIds = new Set() let strucIds = new Set()
for (let struc of strucIdRes) { for (let struc of strucRes) {
strucIds.add(struc.structure) strucIds.add(struc.id)
} }
let strucIdArr = Array.from(strucIds) 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( const alarmRes = strucIdArr.length ? await clickHouse.dataAlarm.query(
` `
SELECT StructureId,count(StructureId) AS alarmCount SELECT StructureId,count(StructureId) AS alarmCount

24
api/app/lib/schedule/alarms_push.js

@ -1144,13 +1144,12 @@ module.exports = function (app, opts) {
ON t_factor.id = t_structure_factor.factor ON t_factor.id = t_structure_factor.factor
WHERE WHERE
t_structure_factor.structure IN (${[...dataAlarmG1StructureId]}) t_structure_factor.structure IN (${[...dataAlarmG1StructureId]})
` `
).toPromise() || [] ).toPromise() || []
let factorId = factorData.map(f => f.id) let factorId = factorData.map(f => f.id)
//查询结构物对应的设备 //查询结构物对应的测点
const equipment = await clickHouse.anxinyun.query( const equipment = await clickHouse.anxinyun.query(
` `
SELECT SELECT
@ -1180,7 +1179,8 @@ module.exports = function (app, opts) {
alarms.SourceId AS sourceId, alarms.SourceId AS sourceId,
alarms.StartTime AS StartTime, alarms.StartTime AS StartTime,
alarms.SourceName AS SourceName, alarms.SourceName AS SourceName,
alarms.AlarmCode AS AlarmCode alarms.AlarmCode AS AlarmCode,
alarms.SourceTypeId AS SourceTypeId
FROM FROM
alarms alarms
WHERE WHERE
@ -1192,10 +1192,7 @@ module.exports = function (app, opts) {
` `
).toPromise() || [] ).toPromise() || []
// console.log('当前结构物下所有告警', alarmDatas); // 为设备分配自己产生的告警数据
// console.log('监测因素', factorData);
// console.log('结构物对应的设备', equipment);
equipment.map(f => { equipment.map(f => {
f.alarmDatas = [] f.alarmDatas = []
alarmDatas.map(r => { alarmDatas.map(r => {
@ -1204,6 +1201,8 @@ module.exports = function (app, opts) {
} }
}) })
}) })
// 为监测因素分配绑定的设备
factorData.map(v => { factorData.map(v => {
v.devices = [] v.devices = []
equipment.map(f => { equipment.map(f => {
@ -1212,14 +1211,16 @@ module.exports = function (app, opts) {
} }
}) })
if (strucMap[v.structureId]) { if (strucMap[v.structureId]) {
// 并为 strucMap 补充 factor 监测因素信息
strucMap[v.structureId].factor.push({ ...v }) strucMap[v.structureId].factor.push({ ...v })
} }
}) })
let projectList = [] let projectList = []
for (let key in strucMap) { for (let key in strucMap) {
if (strucMap[key].factor.length > 0) { if (strucMap[key].factor.length > 0) {
// 如果有监测因素信息
// 存结构物信息至 projectList
projectList.push(strucMap[key]) projectList.push(strucMap[key])
} }
} }
@ -1228,6 +1229,8 @@ module.exports = function (app, opts) {
pepProject_name.map(s => { pepProject_name.map(s => {
s.projects = [] s.projects = []
projectList.map(f => { projectList.map(f => {
// 匹配 pepProject_name 中的项目
// 匹配到则向 projects 中插入 f 也就是结构物信息 + 项目的id
if (s.anxinProjectId.includes(f.projectId)) { if (s.anxinProjectId.includes(f.projectId)) {
s.projects.push(f) s.projects.push(f)
} }
@ -1274,6 +1277,7 @@ module.exports = function (app, opts) {
delete c.alarmDatas delete c.alarmDatas
} }
}) })
// 进一步筛选了有告警的设备
f.devices = f.devices.filter(b => b.alarmDatas && b.alarmDatas.length > 0) f.devices = f.devices.filter(b => b.alarmDatas && b.alarmDatas.length > 0)
} else { } else {
delete f.devices delete f.devices
@ -1384,10 +1388,7 @@ module.exports = function (app, opts) {
} else { } else {
for (let [index, a] of alarmData.entries()) { for (let [index, a] of alarmData.entries()) {
alarmContent += packageTableData({ data: a, titleArr: alarmTitleArr }) alarmContent += packageTableData({ data: a, titleArr: alarmTitleArr })
if (a[keyOfStartTime] && moment(a[keyOfStartTime]).isBetween(newAddStartTime, newAddEndTime)) { if (a[keyOfStartTime] && moment(a[keyOfStartTime]).isBetween(newAddStartTime, newAddEndTime)) {
newAddCount++ newAddCount++
} }
@ -1395,7 +1396,6 @@ module.exports = function (app, opts) {
accumulate = alarmData.length accumulate = alarmData.length
} }
tableTitlePrefix += tableTitlePrefix +=
titlePrefix != '数据异常&视频异常' ? titlePrefix != '数据异常&视频异常' ?
c.tactics == 'abnormal_rate' ? c.tactics == 'abnormal_rate' ?

Loading…
Cancel
Save