diff --git a/api/app/lib/controllers/alarm/video.js b/api/app/lib/controllers/alarm/video.js index 8adf26b..46fa882 100644 --- a/api/app/lib/controllers/alarm/video.js +++ b/api/app/lib/controllers/alarm/video.js @@ -21,7 +21,7 @@ async function deviceType (ctx) { } } -async function alarmList (ctx, agg) { +async function alarmList (ctx) { try { const { models } = ctx.fs.dc; const { clickHouse } = ctx.app.fs @@ -251,12 +251,8 @@ async function alarmList (ctx, agg) { } } - if (agg == 'day') {//控制台 按日聚集 - return returnD - } else { - ctx.status = 200; - ctx.body = returnD - } + ctx.status = 200; + ctx.body = returnD } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; diff --git a/api/app/lib/controllers/control/data.js b/api/app/lib/controllers/control/data.js index f66e691..f894575 100644 --- a/api/app/lib/controllers/control/data.js +++ b/api/app/lib/controllers/control/data.js @@ -1,6 +1,5 @@ 'use strict'; const moment = require('moment'); -const { alarmList } = require('../alarm/video'); //BI分析-数据 async function getDataAlarmsAggDay(ctx) { @@ -94,14 +93,10 @@ async function getAppAlarmsAggDay(ctx) { attributes: ['id', 'createTime', 'confirmTime'], include: [{ model: models.App, - attributes: { - exclude: ['projectId'] - }, + attributes: ['id'], include: [{ model: models.ProjectCorrelation, - attributes: { - exclude: ['id'] - }, + attributes: ['id'] }] }] } @@ -136,9 +131,99 @@ async function getAppAlarmsAggDay(ctx) { //BI分析-视频异常 async function getVideoAlarmsAggDay(ctx) { try { - let videoAlarms = await alarmList(ctx, 'day'); - let aggDayMap = []; + const { clickHouse, utils: { anxinStrucIdRange } } = ctx.app.fs + const { database: anxinyun } = clickHouse.anxinyun.opts.config + const { pepProjectId } = ctx.query + let anxinStruc = await anxinStrucIdRange({ + ctx, pepProjectId + }) + const anxinStrucIds = anxinStruc.map(a => a.strucId) + let statusAlarmWhereOption = [] + let start = moment().add(-1, 'year').format('YYYY-MM-DD HH:mm:ss');//最近一年 + statusAlarmWhereOption.push(`camera_status_alarm.create_time >= '${start}'`) + + const videoAlarms = anxinStrucIds.length ? await clickHouse.vcmp.query( + `SELECT + cameraAlarm.cameraId AS cameraId, + cameraAlarm.alarmId AS alarmId, + cameraAlarm.createTime AS createTime, + cameraAlarm.confirmTime AS confirmTime, + ${'cameraAlarm.autoRestore AS autoRestore,'} + anxinStruc.id AS strucId + FROM + ( + SELECT + camera.id AS cameraId, + camera_status_alarm.id AS alarmId, + camera_status_alarm.create_time AS createTime, + camera_status_alarm.platform AS platform, + camera_status_alarm.status_id AS statusId, + camera_status_alarm.serial_no AS cameraSerialNo, + camera_status_alarm.channel_no AS cameraChannelNo, + ${'camera_status_alarm.auto_restore AS autoRestore,'} + camera_status_alarm.confirm_time AS confirmTime + FROM camera_status_alarm + INNER JOIN camera + ON camera.serial_no = camera_status_alarm.serial_no + AND camera.channel_no = camera_status_alarm.channel_no + WHERE + camera.delete = false + AND camera.recycle_time is null + ${statusAlarmWhereOption.length ? 'AND ' + statusAlarmWhereOption.join(' AND ') : ''} + AND alarmId IN ( + SELECT camera_status_alarm.id AS alarmId + FROM camera_status_alarm + RIGHT JOIN ${anxinyun}.t_video_ipc + ON toString(${anxinyun}.t_video_ipc.channel_no) = camera_status_alarm.channel_no + AND ${anxinyun}.t_video_ipc.serial_no = camera_status_alarm.serial_no + ${`WHERE ${anxinyun}.t_video_ipc.structure IN (${anxinStrucIds.join(',')})`} + ) + ) AS cameraAlarm + LEFT JOIN camera_status + ON cameraAlarm.platform = camera_status.platform + AND cameraAlarm.statusId = camera_status.id + LEFT JOIN camera_status_resolve + ON camera_status_resolve.status_id = camera_status.id + + LEFT JOIN ${anxinyun}.t_video_ipc AS anxinIpc + ON toString(anxinIpc.channel_no) = cameraAlarm.cameraChannelNo + AND anxinIpc.serial_no = cameraAlarm.cameraSerialNo + LEFT JOIN ${anxinyun}.t_structure AS anxinStruc + ON anxinStruc.id = anxinIpc.structure + AND anxinStruc.id IN (${anxinStrucIds.join(',')}) + LEFT JOIN ${anxinyun}.t_video_ipc_station AS anxinIpcStation + ON anxinIpcStation.ipc = anxinIpc.id + `).toPromise() : [] + + let returnD = [] + let positionD = {} + // 每个设备一个告警 for (let a of videoAlarms) { + if (positionD[a.cameraId]) { + } else { + let d = { + cameraId: a.cameraId, + autoRestore: a.autoRestore, + createTime: a.createTime, + alarmId: a.alarmId, + confirmTime: a.confirmTime, + } + d.pomsProject = ( + anxinStruc.find(as => as.strucId == a.strucId) || + { + pomsProject: [ + + ] + } + ).pomsProject.map(d => d.id) + returnD.push(d) + positionD[a.cameraId] = { + positionReturnD: returnD.length - 1 + } + } + } + let aggDayMap = []; + for (let a of returnD) { let exist = aggDayMap.find(ad => ad.day == moment(a.createTime).format('YYYY-MM-DD')); if (exist) { exist.total++;//总数 diff --git a/api/app/lib/schedule/alarms_handle_statistics.js b/api/app/lib/schedule/alarms_handle_statistics.js index 7b02acd..a3b7d87 100644 --- a/api/app/lib/schedule/alarms_handle_statistics.js +++ b/api/app/lib/schedule/alarms_handle_statistics.js @@ -1,20 +1,18 @@ 'use strict'; const moment = require('moment'); -const request = require('superagent'); module.exports = function (app, opts) { const { models } = app.fs.dc - const { clickHouse, utils: { anxinStrucIdRange } } = app.fs + const { clickHouse } = app.fs const { database: anxinyun } = clickHouse.anxinyun.opts.config const alarmHandleStatistics = app.fs.scheduleInit( { - interval: '10 41 17 * * *', + interval: '30 59 14 * * *', // immediate: true, //proRun: true, }, async () => { try { - let time = moment().format() let anxinStruc = await getAxyStructs() let pomsProject = await pomsProjectRange() if (anxinStruc.length) { @@ -22,6 +20,8 @@ module.exports = function (app, opts) { let appAlarms = await getAppAlarms(pomsProject);//应用告警 let videoAlarms = await getVideoAlarms(anxinStruc);//视频告警 + + let time = moment().format() //算全局 let dataArrToSave = [] let dataMap = calculate(dataAlarms, appAlarms, videoAlarms) @@ -40,28 +40,27 @@ module.exports = function (app, opts) { } //算单个项目 - //pomsProject.map(p => { - //let pid = p.id; - let pid = 22 - let pDataAlarms = dataAlarms.filter(da => da.pomsProject.indexOf(pid) != -1) - let pAppAlarms = appAlarms.filter(aa => aa.app.projectCorrelations.map(ap => ap.id).indexOf(pid) != -1) - let pVideoAlarms = videoAlarms.filter(va => va.pomsProject.indexOf(pid) != -1) + pomsProject.map(p => { + let pid = p.id; + let pDataAlarms = dataAlarms.filter(da => da.pomsProject.indexOf(pid) != -1) + let pAppAlarms = appAlarms.filter(aa => aa.app.projectCorrelations.map(ap => ap.id).indexOf(pid) != -1) + let pVideoAlarms = videoAlarms.filter(va => va.pomsProject.indexOf(pid) != -1) - let pDataMap = calculate(pDataAlarms, pAppAlarms, pVideoAlarms) - let sm = pDataAlarms.length + pAppAlarms.length + pVideoAlarms.length; - if (sm) { - dataArrToSave.push({ - time: time, - projectCorrelationId: pid,//单个项目 - day1: parseFloat((100 * pDataMap.day1 / sum).toFixed(2)), - day3: parseFloat((100 * pDataMap.day3 / sum).toFixed(2)), - day7: parseFloat((100 * pDataMap.day7 / sum).toFixed(2)), - day15: parseFloat((100 * pDataMap.day15 / sum).toFixed(2)), - day30: parseFloat((100 * pDataMap.day30 / sum).toFixed(2)), - day30m: parseFloat((100 * pDataMap.day30m / sum).toFixed(2)), - }) - } - //}) + let pDataMap = calculate(pDataAlarms, pAppAlarms, pVideoAlarms) + let sm = pDataAlarms.length + pAppAlarms.length + pVideoAlarms.length; + if (sm) { + dataArrToSave.push({ + time: time, + projectCorrelationId: pid,//单个项目 + day1: parseFloat((100 * pDataMap.day1 / sum).toFixed(2)), + day3: parseFloat((100 * pDataMap.day3 / sum).toFixed(2)), + day7: parseFloat((100 * pDataMap.day7 / sum).toFixed(2)), + day15: parseFloat((100 * pDataMap.day15 / sum).toFixed(2)), + day30: parseFloat((100 * pDataMap.day30 / sum).toFixed(2)), + day30m: parseFloat((100 * pDataMap.day30m / sum).toFixed(2)), + }) + } + }) await models.AlarmHandleStatistics.bulkCreate(dataArrToSave) } } catch (error) {