diff --git a/api/app/lib/schedule/alarms_push.js b/api/app/lib/schedule/alarms_push.js index ef7d60c..21e7afa 100644 --- a/api/app/lib/schedule/alarms_push.js +++ b/api/app/lib/schedule/alarms_push.js @@ -1,7 +1,7 @@ const moment = require('moment') -let isDev = false -// let isDev = true +// let isDev = false +let isDev = true let proDebug = false proDebug = true @@ -9,7 +9,8 @@ proDebug = true module.exports = function (app, opts) { const alarmsPush = app.fs.scheduleInit( { - interval: '12 */1 * * * *', + // interval: '12 */1 * * * *', + interval: '12 0 0 0 */1 *', immediate: isDev, proRun: !isDev, }, @@ -87,6 +88,7 @@ module.exports = function (app, opts) { } for (let { dataValues: c } of configListRes) { + if (c.tacticsParams && c.tactics) { if (proDebug) { console.log(`当前运行EM配置:id=${c.id} name=${c.name}`); @@ -95,13 +97,12 @@ module.exports = function (app, opts) { const { strucId, pomsProjectId, pomsStrucFactorId } = c const { interval, deviceProportion } = c.tacticsParams - if ( - curMinOfYear % parseInt(interval) == 0 || isDev - ) { + if (curMinOfYear % parseInt(interval) == 0 || isDev) { const corPomsProject = pomsProjectRes.filter(poms => pomsProjectId.includes(poms.id)) let curAnxinProjectId = new Set() let pepProjectName_ = [] + let pepProject_name = [] for (let { dataValues: poms } of corPomsProject) { if (poms.pepProjectId) { // 找对应的项企项目 @@ -109,13 +110,18 @@ module.exports = function (app, opts) { pepProjectRes.find(p => p.id == poms.pepProjectId) if (corPepProject && c.timeType.some(ct => ct == corPepProject.construction_status_id)) { pepProjectName_.push(corPepProject.project_name) + pepProject_name.push({ id: poms.id, anxinProjectId: poms.anxinProjectId, name: corPepProject.project_name }) } else { // 不符合当前项目的时间节点 continue } } else { // 是自定义项目 - poms.name ? pepProjectName_.push(poms.name) : null + if (poms.name) { + pepProjectName_.push(poms.name) + pepProject_name.push({ id: poms.id, anxinProjectId: poms.anxinProjectId, name: poms.name }) + } + } // 筛选全部的 anxinProjectId pepProjectId for (let axId of poms.anxinProjectId) { @@ -164,6 +170,18 @@ module.exports = function (app, opts) { ` ).toPromise() : [] + + //在数据里加入项企项目或自定义项目id + + strucListRes.map(f => { + f.pepProject = [] + pepProject_name.map(s => { + if (s.anxinProjectId.includes(f.projectId)) { + f.pepProject.push(s.id) + } + }) + }) + let strucThingId = [] let strucMap = {} let searchStrucIds = strucListRes.map(s => { @@ -171,7 +189,7 @@ module.exports = function (app, opts) { strucThingId.push(s.iotaThingId) } strucMap[s.id] = { - ...s + ...s, factor: [] } return s.id }) @@ -179,7 +197,7 @@ module.exports = function (app, opts) { // !开发测试用的数据 if (isDev) { // searchStrucIds = searchStrucIds.concat([991, 1052, 700]) - searchStrucIds = searchStrucIds.concat([27, 4003, 700]) + searchStrucIds = searchStrucIds.concat([27, 991, 1040, 50, 59.128, 407, 409, 877, 1035, 1077]) } if (searchStrucIds.length) { @@ -554,70 +572,104 @@ module.exports = function (app, opts) { } },] + // let dataLnterruptTitle = [{ + // n: '项目', + // k: '', + // v: pepProjectName + // }, { + // n: '结构物', + // k: '', + // f: (d) => { + // return (strucMap[d.StructureId] || { name: '' }).name + // // return (strucListRes.find(s => s.id == d.StructureId) || { name: '' }).name + // } + // }, { + // n: '监测因素(中断比例)', + // k: '', + // f: (d) => { + // // d.factor.join('') + // // console.log(21211231131,d.factor); + // let data = [] + // d.factor.map(f => data.push(f.name + '(' + f.breakData + '/' + f.sum + ')')) + // return data.join('
') + // } + // }, { + // n: '告警源名称', + // k: 'SourceName' + // }, { + // n: '告警源类型', + // k: '', + // f: (d) => { + // switch (d.SourceTypeId) { + // case 0: + // return 'DTU' + // case 1: + // return '传感器' + // case 2: + // return '测点' + // default: + // return '' + // } + // } + // }, { + // n: '告警信息', + // k: 'AlarmContent' + // }, { + // n: '告警等级(当前)', + // k: '', + // f: (d) => { + // switch (d.CurrentLevel) { + // case 1: + // return '一级' + // case 2: + // return '二级' + // case 3: + // return '三级' + // default: + // return '' + // } + // } + // }, { + // n: '持续时间', + // k: '', + // f: (d) => { + // return d.StartTime ? + // '超过' + calcMinute2DHM(moment().diff(moment(d.StartTime), 'minutes')) : '' + // } + // }, + // { + // n: '中断时间', + // k: '', + // f: (d) => { + // return d.StartTime ? + // moment(d.StartTime).format('YYYY-MM-DD HH:mm:ss') : '' + // } + // },] + let dataLnterruptTitle = [{ n: '项目', - k: '', - v: pepProjectName + k: 'name', + // v: pepProjectName }, { n: '结构物', - k: '', - f: (d) => { - return (strucMap[d.StructureId] || { name: '' }).name - // return (strucListRes.find(s => s.id == d.StructureId) || { name: '' }).name - } + k: 'Structure', + // f: (d) => { + // return (strucMap[d.StructureId] || { name: '' }).name + // // return (strucListRes.find(s => s.id == d.StructureId) || { name: '' }).name + // } }, { n: '监测因素(中断比例)', - k: '', - f: (d) => { - // d.factor.join('') - // console.log(21211231131,d.factor); - let data = [] - d.factor.map(f => data.push(f.name + '(' + f.breakData + '/' + f.sum + ')')) - return data.join('
') - } + k: 'factor', + // f: (d) => { + // // d.factor.join('') + // // console.log(21211231131,d.factor); + // let data = [] + // d.factor.map(f => data.push(f.name + '(' + f.breakData + '/' + f.sum + ')')) + // return data.join('
') + // } }, { - n: '告警源名称', + n: '告警源', k: 'SourceName' - }, { - n: '告警源类型', - k: '', - f: (d) => { - switch (d.SourceTypeId) { - case 0: - return 'DTU' - case 1: - return '传感器' - case 2: - return '测点' - default: - return '' - } - } - }, { - n: '告警信息', - k: 'AlarmContent' - }, { - n: '告警等级(当前)', - k: '', - f: (d) => { - switch (d.CurrentLevel) { - case 1: - return '一级' - case 2: - return '二级' - case 3: - return '三级' - default: - return '' - } - } - }, { - n: '持续时间', - k: '', - f: (d) => { - return d.StartTime ? - '超过' + calcMinute2DHM(moment().diff(moment(d.StartTime), 'minutes')) : '' - } }, { n: '中断时间', @@ -802,84 +854,86 @@ module.exports = function (app, opts) { // } if (d.AlarmGroup == 1) { - if (d.StructureId) { - //查询结构物的监测因素 - const factorData = await clickHouse.anxinyun.query( - ` - SELECT - t_structure_factor.structure AS structureId, - t_factor.name AS name, - t_factor.id AS id - FROM - t_structure_factor - LEFT JOIN t_factor - ON t_factor.id = t_structure_factor.factor - WHERE - t_structure_factor.structure = (${d.StructureId}) - - ` - ).toPromise() || [] - let factorId = factorData.map(f => f.id) - - //查询结构物对应的设备 - const equipment = await clickHouse.anxinyun.query( - ` - SELECT - t_sensor.id AS id, - t_sensor.name AS name, - t_sensor.structure AS structureId, - t_sensor.factor AS factorId, - t_device_sensor.iota_device_id AS iotaDeviceId - FROM - t_sensor - LEFT JOIN t_device_sensor - ON t_device_sensor.sensor = t_sensor.id - WHERE - t_sensor.structure = (${d.StructureId}) - AND - t_sensor.factor IN (${factorId.join(',')}) - ` - ).toPromise() || [] - - const alarmDatas = await clickHouse.dataAlarm.query( - ` - SELECT - alarms.AlarmId AS alarmId, - alarms.State AS state, - alarms.AlarmGroup AS alarmGroup, - alarms.SourceId AS sourceId - FROM - alarms - WHERE - alarms.StructureId = (${d.StructureId}) - AND - alarms.AlarmGroup = 1 - AND - alarms.State < 3 - ` - ).toPromise() || [] - - equipment.map(r => { - alarmDatas.map(u => { - if (r.iotaDeviceId == u.sourceId) { - r.sourceId = u.sourceId - } - }) - }) - - d.factor = [] - factorData.map(c => { - let breakData = equipment.filter(m => (m.factorId == c.id && m.sourceId)) - d.factor.push({ - name: c.name, - sum: equipment.length, - breakData: breakData.length - }) - }) - - } - - dataAlarmG1.push(d) + // if (d.StructureId) { + // //查询结构物的监测因素 + // const factorData = await clickHouse.anxinyun.query( + // ` + // SELECT + // t_structure_factor.structure AS structureId, + // t_factor.name AS name, + // t_factor.id AS id + // FROM + // t_structure_factor + // LEFT JOIN t_factor + // ON t_factor.id = t_structure_factor.factor + // WHERE + // t_structure_factor.structure = (${d.StructureId}) + + // ` + // ).toPromise() || [] + // let factorId = factorData.map(f => f.id) + + // //查询结构物对应的设备 + // const equipment = await clickHouse.anxinyun.query( + // ` + // SELECT + // t_sensor.id AS id, + // t_sensor.name AS name, + // t_sensor.structure AS structureId, + // t_sensor.factor AS factorId, + // t_device_sensor.iota_device_id AS iotaDeviceId + // FROM + // t_sensor + // LEFT JOIN t_device_sensor + // ON t_device_sensor.sensor = t_sensor.id + // WHERE + // t_sensor.structure = (${d.StructureId}) + // AND + // t_sensor.factor IN (${factorId.join(',')}) + // ` + // ).toPromise() || [] + + // const alarmDatas = await clickHouse.dataAlarm.query( + // ` + // SELECT + // alarms.AlarmId AS alarmId, + // alarms.State AS state, + // alarms.AlarmGroup AS alarmGroup, + // alarms.SourceId AS sourceId + // FROM + // alarms + // WHERE + // alarms.StructureId = (${d.StructureId}) + // AND + // alarms.AlarmGroup = 1 + // AND + // alarms.State < 3 + // ` + // ).toPromise() || [] + + // equipment.map(r => { + // alarmDatas.map(u => { + // if (r.iotaDeviceId == u.sourceId) { + // r.sourceId = u.sourceId + // } + // }) + // }) + + // d.factor = [] + // factorData.map(c => { + // let breakData = equipment.filter(m => (m.factorId == c.id && m.sourceId)) + // d.factor.push({ + // name: c.name, + // sum: equipment.filter(d => d.factorId == c.id).length, + // breakData: breakData.length + // }) + // d.factors = equipment.filter(d => d.factorId == c.id) + // d.breakData = breakData + // }) + + // } + + // dataAlarmG1.push(d) } else if (d.AlarmGroup == 2) { dataAlarmG2.push(d) @@ -947,6 +1001,7 @@ module.exports = function (app, opts) { } else if (d.AlarmGroup == 4 || d.AlarmGroup == 5) { dataAlarmG45.push(d) } + deviceStatistic.add(d.SourceId) } if (c.tactics == 'abnormal_rate') { @@ -990,12 +1045,158 @@ module.exports = function (app, opts) {
${emailSubTitle}
` + let dataAlarmG1Data = dataAlarms.filter(a => a.AlarmGroup == 1) + if (dataAlarmG1Data.length) { + let dataAlarmG1StructureId = new Set() + dataAlarmG1Data.map(c => { + dataAlarmG1StructureId.add(c.StructureId) + }) + //查询结构物的监测因素 + const factorData = await clickHouse.anxinyun.query( + ` + SELECT + t_structure_factor.structure AS structureId, + t_factor.name AS name, + t_factor.id AS id + FROM + t_structure_factor + LEFT JOIN t_factor + ON t_factor.id = t_structure_factor.factor + WHERE + t_structure_factor.structure IN (${[...dataAlarmG1StructureId]}) + + ` + ).toPromise() || [] + + + + let factorId = factorData.map(f => f.id) + + //查询结构物对应的设备 + const equipment = await clickHouse.anxinyun.query( + ` + SELECT + t_sensor.id AS id, + t_sensor.name AS name, + t_sensor.structure AS structureId, + t_sensor.factor AS factorId, + t_device_sensor.iota_device_id AS iotaDeviceId + FROM + t_sensor + LEFT JOIN t_device_sensor + ON t_device_sensor.sensor = t_sensor.id + WHERE + t_sensor.structure IN (${[...dataAlarmG1StructureId]}) + AND + t_sensor.factor IN (${factorId.join(',')}) + ` + ).toPromise() || [] + + + const alarmDatas = await clickHouse.dataAlarm.query( + ` + SELECT + alarms.AlarmId AS alarmId, + alarms.State AS state, + alarms.AlarmGroup AS alarmGroup, + alarms.SourceId AS sourceId, + alarms.StartTime AS StartTime, + alarms.SourceName AS SourceName + FROM + alarms + WHERE + alarms.StructureId In (${[...dataAlarmG1StructureId]}) + AND + alarms.AlarmGroup = 1 + AND + alarms.State < 3 + ` + ).toPromise() || [] + + + equipment.map(f => { + f.alarmDatas = [] + alarmDatas.map(r => { + if (r.sourceId == f.iotaDeviceId) { + f.alarmDatas.push({ ...r }) + } + }) + }) + factorData.map(v => { + v.devices = [] + equipment.map(f => { + if (v.id == f.factorId) { + v.devices.push({ ...f }) + } + }) + }) + + factorData.map(a => { + if (strucMap[a.structureId]) { + strucMap[a.structureId].factor.push({ ...a }) + } + }) + + pepProject_name.map(s => { + + }) + let projectList = [] + for (let key in strucMap) { + if (strucMap[key].factor.length > 0) { + projectList.push(strucMap[key]) + } + } + + if (projectList.length) { + pepProject_name.map(s => { + s.projects = [] + projectList.map(f => { + if (s.anxinProjectId.includes(f.projectId)) { + s.projects.push(f) + } + }) + }) + } + pepProject_name.map(h => { + if (h.projects.length) { + h.projects.map(x => { + if (x.factor.length) { + x.factor.map(f => { + if (f.devices.length) { + f.devices.map(c => { + if (c.alarmDatas.length) { + c.alarmDatas.map(a => { + dataAlarmG1.push({ + name: h.name, + Structure:x.name, + factor:f.name, + SourceName:a.SourceName, + StartTime:a.StartTime + }) + }) + } + }) + } + + }) + } + }) + } + }) + pepProject_name.map(h => { + + }) + } + + function packageAlarmData2Table ({ - titlePrefix, alarmData, alarmTitleArr, keyOfStartTime = 'StartTime' + titlePrefix, alarmData, alarmTitleArr, keyOfStartTime = 'StartTime', type }) { if (!alarmData.length) { return '' } + strucMap = { ...strucMap } + pepProject_name = [...pepProject_name] ifEmailSend = true let tableTitlePrefix = titlePrefix + '告警源' let newAddCount = 0 @@ -1003,8 +1204,57 @@ module.exports = function (app, opts) { let alarmContent = '' let alarmHtmlTitle = packageTableTitle(alarmTitleArr) + // // if (type == 1) { + // let projectData = [] + // //按项目分类 + // pepProject_name.map(c => { + + // let OneProjectdata = [] + + // alarmData.map(a => { + // let OneStructure = (strucMap[a.StructureId] || {}).pepProject || [] + // if (OneStructure.includes(c.id)) { + // OneProjectdata.push({ + // ...a, + // pepProjectId: c.id, + // pepProjectName: c.name + // }) + // } + // }) + + // if (OneProjectdata.length) { + // projectData.push(OneProjectdata) + // } + // }) + + // let projectType = [] + // projectData.map(p => { + + // }) + // // } + + for (let [index, a] of alarmData.entries()) { - alarmContent += packageTableData({ data: a, titleArr: alarmTitleArr }) + // if (type == 1) { + + // let tableData = '' + // for (let t of titleArr) { + // if (t.v) { + // tableData += `${t.v || ''}` + // } else if (t.f) { + // tableData += `${t.f(data) || ''}` + // } else if (t.k) { + // tableData += `${data[t.k] || ''}` + // } else { + // tableData += `` + // } + // } + + // tableData += '' + // alarmContent += tableData + // } else { + alarmContent += packageTableData({ data: a, titleArr: alarmTitleArr }) + // } if (a[keyOfStartTime] && moment(a[keyOfStartTime]).isBetween(newAddStartTime, newAddEndTime)) { newAddCount++ @@ -1023,11 +1273,13 @@ module.exports = function (app, opts) { return alarmHtml } + if (c.alarmType.includes('data_outages')) { html += packageAlarmData2Table({ titlePrefix: '数据中断', alarmData: dataAlarmG1, alarmTitleArr: dataLnterruptTitle, + type: 1 }) } if (c.alarmType.includes('data_exception')) { diff --git a/web/client/src/sections/control/containers/control.jsx b/web/client/src/sections/control/containers/control.jsx index acba920..0060703 100644 --- a/web/client/src/sections/control/containers/control.jsx +++ b/web/client/src/sections/control/containers/control.jsx @@ -1201,7 +1201,7 @@ const Control = ({ dispatch, actions, user, history, loading, socket, pepProject // opts={} /> - })} + })} : ""}