diff --git a/api/.vscode/launch.json b/api/.vscode/launch.json index 42d30dc..5018623 100644 --- a/api/.vscode/launch.json +++ b/api/.vscode/launch.json @@ -56,7 +56,7 @@ // "--clickHouseDataAlarm default", // 测试 - "--clickHouseAnxincloud Anxinyun21", + "--clickHouseAnxincloud Anxinyun22", "--clickHousePepEmis pepca8", "--clickHouseProjectManage peppm8", "--clickHouseVcmp video_access_dev", diff --git a/api/app/lib/controllers/control/data.js b/api/app/lib/controllers/control/data.js index 2285e66..d3b8511 100644 --- a/api/app/lib/controllers/control/data.js +++ b/api/app/lib/controllers/control/data.js @@ -1,6 +1,7 @@ 'use strict'; const moment = require('moment'); const { alarmList } = require('../alarm/video'); +const { inspectionList } = require('../alarm/app'); //工作台 async function getWorkbench(ctx) { try { @@ -67,7 +68,97 @@ async function getProjectsInfo(ctx) { } } -//BI分析 +//BI分析-数据 +async function getDataAlarmsAggDay(ctx) { + try { + const { utils: { anxinStrucIdRange } } = ctx.app.fs + const { pepProjectId } = ctx.query + + let dataAbnormal = [], dataInterrupt = [], policyHit = [], deviceAbnormal = []; + let anxinStruc = await anxinStrucIdRange({ + ctx, pepProjectId + }) + let whereOption = [] + // ! 1 开发临时增加 + if (anxinStruc.length) { + const anxinStrucIds = anxinStruc.map(a => a.strucId) + // ! 开发临时注释 + whereOption.push(`alarms.StructureId IN (${anxinStrucIds.join(",")})`) + + let start = moment().add(-1, 'year').format('YYYY-MM-DD HH:mm:ss');//最近一年 + whereOption.push(`alarms.StartTime >= '${start}'`) + + let alarmQueryOptionStr = ` + FROM alarms + ${whereOption.length ? 'WHERE ' + whereOption.join(' AND ') : ''} + ` + dataAbnormal = await queryAlarm(ctx, alarmQueryOptionStr, [2]);//数据异常 + dataInterrupt = await queryAlarm(ctx, alarmQueryOptionStr, [1]);//数据中断 + policyHit = await queryAlarm(ctx, alarmQueryOptionStr, [3]);//策略命中 + deviceAbnormal = await queryAlarm(ctx, alarmQueryOptionStr, [4, 5]);//设备异常 + } + ctx.status = 200; + ctx.body = { dataAbnormal, dataInterrupt, policyHit, deviceAbnormal }; + } catch (error) { + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); + ctx.status = 400; + ctx.body = { + message: typeof error == 'string' ? error : undefined + } + } +} + +async function queryAlarm(ctx, alarmQueryOptionStr, type) { + const { clickHouse } = ctx.app.fs + try { + const alarmRes = await clickHouse.dataAlarm.query(` + select days, sum(count) total, sum(done) done from ( + + SELECT formatDateTime(StartTime,'%F') days, count(AlarmId) count, 0 done from + (SELECT + alarms.AlarmId AS AlarmId, + alarms.State AS State, + StartTime, + AlarmGroup + ${alarmQueryOptionStr} and alarms.AlarmGroup in [${type}]) group by days + + union all (SELECT formatDateTime(StartTime,'%F') days, 0 count, count(AlarmId) done from + (SELECT + alarms.AlarmId AS AlarmId, + alarms.State AS State, + StartTime, + AlarmGroup + ${alarmQueryOptionStr} and alarms.AlarmGroup in [${type}] and alarms.State > 3) group by days) + + ) group by days ORDER BY days + `).toPromise(); + return alarmRes; + } catch (error) { + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); + ctx.status = 400; + ctx.body = { + message: typeof error == 'string' ? error : undefined + } + } +} + +//BI分析-应用异常 +async function getAppAlarmsAggDay(ctx) { + try { + let appAlarms = await inspectionList(ctx, 'day'); + let aggDayMap = []; + ctx.status = 200; + ctx.body = aggDayMap; + } catch (error) { + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); + ctx.status = 400; + ctx.body = { + message: typeof error == 'string' ? error : undefined + } + } +} + +//BI分析-视频异常 async function getVideoAlarmsAggDay(ctx) { try { let videoAlarms = await alarmList(ctx, 'day'); @@ -190,6 +281,9 @@ async function getLatestDynamic(ctx) { module.exports = { getWorkbench, getProjectsInfo, + + getDataAlarmsAggDay, + getAppAlarmsAggDay, getVideoAlarmsAggDay, getLatestDynamic }; \ No newline at end of file diff --git a/api/app/lib/routes/control/index.js b/api/app/lib/routes/control/index.js index 162f7f6..95aa8e5 100644 --- a/api/app/lib/routes/control/index.js +++ b/api/app/lib/routes/control/index.js @@ -27,6 +27,12 @@ module.exports = function (app, router, opts) { router.get('/projects/info', csData.getProjectsInfo); //BI分析模块 + app.fs.api.logAttr['GET/data/alarms/agg/day'] = { content: '查询BI分析数据-数据', visible: false }; + router.get('/data/alarms/agg/day', csData.getDataAlarmsAggDay); + + app.fs.api.logAttr['GET/app/alarms/agg/day'] = { content: '查询BI分析数据-应用异常', visible: false }; + router.get('/app/alarms/agg/day', csData.getAppAlarmsAggDay); + app.fs.api.logAttr['GET/video/alarms/agg/day'] = { content: '查询BI分析数据-视频异常', visible: false }; router.get('/video/alarms/agg/day', csData.getVideoAlarmsAggDay);