From 4fd1b0ccc023a08d7242e1e23cce46488adfd008 Mon Sep 17 00:00:00 2001 From: wuqun Date: Fri, 28 Oct 2022 15:53:38 +0800 Subject: [PATCH 1/4] =?UTF-8?q?(*)=E5=91=8A=E8=AD=A6=E5=A4=84=E7=BD=AE?= =?UTF-8?q?=E6=95=88=E7=8E=87=E6=95=B0=E6=8D=AE=E8=AE=A1=E7=AE=97=E5=85=A5?= =?UTF-8?q?=E5=BA=93=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1=E4=BF=AE=E6=94=B9?= =?UTF-8?q?-=E6=95=B0=E6=8D=AE=E7=B1=BB=E5=91=8A=E8=AD=A6=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib/schedule/alarms_handle_statistics.js | 233 +++++++++++------- 1 file changed, 150 insertions(+), 83 deletions(-) diff --git a/api/app/lib/schedule/alarms_handle_statistics.js b/api/app/lib/schedule/alarms_handle_statistics.js index 04fab12..b63293f 100644 --- a/api/app/lib/schedule/alarms_handle_statistics.js +++ b/api/app/lib/schedule/alarms_handle_statistics.js @@ -7,7 +7,7 @@ module.exports = function (app, opts) { const { database: anxinyun } = clickHouse.anxinyun.opts.config const alarmHandleStatistics = app.fs.scheduleInit( { - interval: '0 58 9 * * *', + interval: '0 48 1 ? * MON',//0 48 1 ? * MON 每周一凌晨1点48开始执行 // immediate: true, //proRun: true, }, @@ -16,18 +16,18 @@ module.exports = function (app, opts) { let anxinStruc = await getAxyStructs() let pomsProject = await pomsProjectRange() if (anxinStruc.length) { - let dataAlarms = await getDataAlarms(anxinStruc);//数据告警 + let { dataSum, dataMaps } = await getDataAlarms(anxinStruc);//数据告警 let appAlarms = await getAppAlarms(pomsProject);//应用告警 let videoAlarms = await getVideoAlarms(anxinStruc);//视频告警 let time = moment().format() //算全局 - let dataArrToSave = [] - let dataMap = calculate(dataAlarms, appAlarms, videoAlarms) - let sum = dataAlarms.length + appAlarms.length + videoAlarms.length; + //let dataArrToSave = [] + let dataMap = calculate(dataMaps, appAlarms, videoAlarms) + let sum = dataSum + appAlarms.length + videoAlarms.length; if (sum) { - dataArrToSave.push({ + let data = { time: time, projectCorrelationId: null,//全局 day1: parseFloat((100 * dataMap.day1 / sum).toFixed(2)), @@ -36,32 +36,60 @@ module.exports = function (app, opts) { day15: parseFloat((100 * dataMap.day15 / sum).toFixed(2)), day30: parseFloat((100 * dataMap.day30 / sum).toFixed(2)), day30m: parseFloat((100 * dataMap.day30m / sum).toFixed(2)), - }) + } + await models.AlarmHandleStatistics.create(data) } //算单个项目 - pomsProject.map(p => { + pomsProject.map(async 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 pid = 11 + let pAnxinStruc = await getAxyStructs(pid) + let pPomsProject = await pomsProjectRange(pid) + if (pAnxinStruc.length) { + let { dataSum, dataMaps } = await getDataAlarms(pAnxinStruc);//数据告警 + let pAppAlarms = await getAppAlarms(pPomsProject);//应用告警 + let pVideoAlarms = await getVideoAlarms(pAnxinStruc);//视频告警 + let pDataMap = calculate(dataMaps, pAppAlarms, pVideoAlarms) + let sm = dataSum + pAppAlarms.length + pVideoAlarms.length; + if (sm) { + let data = { + time: time, + projectCorrelationId: pid,//单个项目 + day1: parseFloat((100 * pDataMap.day1 / sm).toFixed(2)), + day3: parseFloat((100 * pDataMap.day3 / sm).toFixed(2)), + day7: parseFloat((100 * pDataMap.day7 / sm).toFixed(2)), + day15: parseFloat((100 * pDataMap.day15 / sm).toFixed(2)), + day30: parseFloat((100 * pDataMap.day30 / sm).toFixed(2)), + day30m: parseFloat((100 * pDataMap.day30m / sm).toFixed(2)), + } + await models.AlarmHandleStatistics.create(data) + } } }) - await models.AlarmHandleStatistics.bulkCreate(dataArrToSave) + //算单个项目 + // 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)), + // }) + // } + // }) + //await models.AlarmHandleStatistics.bulkCreate(dataArrToSave) } } catch (error) { console.error(error); @@ -69,33 +97,34 @@ module.exports = function (app, opts) { } ) - function calculate(dataAlarms, appAlarms, videoAlarms) { + function calculate(dataMaps, appAlarms, videoAlarms) { try { + let { day1, day3, day7, day15, day30, day30m } = dataMaps; //算全局 let dataMap = { - day1: 0,//当日处理 - day3: 0,//3日内 - day7: 0,//7日内 - day15: 0,//15日内 - day30: 0,//30日内 - day30m: 0//超过30日 + day1: day1[0].count,//当日处理 + day3: day3[0].count,//3日内 + day7: day7[0].count,//7日内 + day15: day15[0].count,//15日内 + day30: day30[0].count,//30日内 + day30m: day30m[0].count//超过30日 } - dataAlarms.filter(d => d.State > 3).map(da => { - let range = moment(da.confirmTime).diff(moment(da.StartTime), 'day') - if (range <= 1) { - dataMap.day1++ - } else if (range > 1 && range <= 3) { - dataMap.day3++ - } else if (range > 3 && range <= 7) { - dataMap.day7++ - } else if (range > 7 && range <= 15) { - dataMap.day15++ - } else if (range > 15 && range <= 30) { - dataMap.day30++ - } else if (range > 30) { - dataMap.day30m++ - } - }) + // dataAlarms.filter(d => d.State > 3).map(da => { + // let range = moment(da.confirmTime).diff(moment(da.StartTime), 'day') + // if (range <= 1) { + // dataMap.day1++ + // } else if (range > 1 && range <= 3) { + // dataMap.day3++ + // } else if (range > 3 && range <= 7) { + // dataMap.day7++ + // } else if (range > 7 && range <= 15) { + // dataMap.day15++ + // } else if (range > 15 && range <= 30) { + // dataMap.day30++ + // } else if (range > 30) { + // dataMap.day30m++ + // } + // }) appAlarms.filter(d => d.confirmTime).map(da => { let range = moment(da.confirmTime).diff(moment(da.createTime), 'day') if (range < 1) { @@ -134,6 +163,17 @@ module.exports = function (app, opts) { } } + function getStr(alarmQueryOptionStr, paramStr) { + return ` + select count(AlarmId) count from + (SELECT + alarms.AlarmId AS AlarmId, + alarms.State AS State, + alarms.StructureId AS StructureId, + StartTime, EndTime + ${alarmQueryOptionStr}) where State>3 and ${paramStr}` + } + async function getDataAlarms(anxinStruc) { try { const anxinStrucIds = anxinStruc.map(a => a.strucId) @@ -158,35 +198,53 @@ module.exports = function (app, opts) { StartTime, EndTime ${alarmQueryOptionStr}`).toPromise(); - console.log('数据-数据-数据告警查询结束---' + moment().format('YYYY-MM-DD HH:mm:ss') + `---一共${alarmRes.length}条`) - - const confirmedAlarm = alarmRes.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() : []; - - alarmRes.forEach(ar => { - ar.pomsProject = ( - anxinStruc.find(as => as.strucId == ar.StructureId) || - { - pomsProject: [ - // TODO: 开发临时添加 - ] - } - ).pomsProject.map(p => p.id) + let day1Str = getStr(alarmQueryOptionStr, 'Date(EndTime) - Date(StartTime)<=1'); + let day1 = await clickHouse.dataAlarm.query(day1Str).toPromise(); - // 最新告警详情 - 确认信息 - let corConfirmedData = (confirmedAlarmDetailMax.find(cdm => cdm.AlarmId == ar.AlarmId) || {}); - ar.confirmTime = corConfirmedData.Time || ar.EndTime - }) - return alarmRes; + let day3Str = getStr(alarmQueryOptionStr, 'Date(EndTime) - Date(StartTime)>1 and Date(EndTime) - Date(StartTime)<=3'); + let day3 = await clickHouse.dataAlarm.query(day3Str).toPromise(); + + let day7Str = getStr(alarmQueryOptionStr, 'Date(EndTime) - Date(StartTime)>3 and Date(EndTime) - Date(StartTime)<=7'); + let day7 = await clickHouse.dataAlarm.query(day7Str).toPromise(); + + let day15Str = getStr(alarmQueryOptionStr, 'Date(EndTime) - Date(StartTime)>7 and Date(EndTime) - Date(StartTime)<=15'); + let day15 = await clickHouse.dataAlarm.query(day15Str).toPromise(); + + let day30Str = getStr(alarmQueryOptionStr, 'Date(EndTime) - Date(StartTime)>15 and Date(EndTime) - Date(StartTime)<=30'); + let day30 = await clickHouse.dataAlarm.query(day30Str).toPromise(); + + let day30mStr = getStr(alarmQueryOptionStr, 'Date(EndTime) - Date(StartTime)>30'); + let day30m = await clickHouse.dataAlarm.query(day30mStr).toPromise(); + + // console.log('数据-数据-数据告警查询结束---' + moment().format('YYYY-MM-DD HH:mm:ss') + `---一共${alarmRes.length}条`) + + // const confirmedAlarm = alarmRes.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() : []; + + // alarmRes.forEach(ar => { + // ar.pomsProject = ( + // anxinStruc.find(as => as.strucId == ar.StructureId) || + // { + // pomsProject: [ + // // TODO: 开发临时添加 + // ] + // } + // ).pomsProject.map(p => p.id) + + // // 最新告警详情 - 确认信息 + // let corConfirmedData = (confirmedAlarmDetailMax.find(cdm => cdm.AlarmId == ar.AlarmId) || {}); + // ar.confirmTime = corConfirmedData.Time || ar.EndTime + // }) + return { dataSum: alarmRes.length, dataMaps: { day1, day3, day7, day15, day30, day30m } }; } catch (error) { console.error(error); } @@ -318,9 +376,9 @@ module.exports = function (app, opts) { console.error(error); } } - async function pomsProjectRange() { + async function pomsProjectRange(pepProjectId) { try { - const { pepProjectRes, bindRes } = await pomsWithPepRangeParams() + const { pepProjectRes, bindRes } = await pomsWithPepRangeParams(pepProjectId) let pomsProject = [] for (let b of bindRes) { if (b.pepProjectId) { @@ -341,9 +399,18 @@ module.exports = function (app, opts) { } } - async function pomsWithPepRangeParams() { + async function pomsWithPepRangeParams(pepProjectId) { try { - const bindRes = await models.ProjectCorrelation.findAll({ where: { del: false } }); + let findOption = { + where: { + del: false + } + } + if (pepProjectId) { + // 有 特定的项目id 就按此查询 + findOption.where.id = pepProjectId + } + const bindRes = await models.ProjectCorrelation.findAll(findOption); // 获取不重复的 项企项目id let pepProjectIds = [] for (let b of bindRes) { @@ -379,9 +446,9 @@ module.exports = function (app, opts) { } } - async function getAxyStructs() { + async function getAxyStructs(pepProjectId) { try { - const { pepProjectRes, bindRes } = await pomsWithPepRangeParams() + const { pepProjectRes, bindRes } = await pomsWithPepRangeParams(pepProjectId) // 获取不重复的 安心云项目 id const anxinProjectIds = [ ...(bindRes).reduce( From 6aa7dcd343c9dd16b840b6f6f3c34f866c888b45 Mon Sep 17 00:00:00 2001 From: wuqun Date: Fri, 28 Oct 2022 15:56:04 +0800 Subject: [PATCH 2/4] =?UTF-8?q?(*)=E5=91=8A=E8=AD=A6=E5=A4=84=E7=BD=AE?= =?UTF-8?q?=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1proRun:true?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/app/lib/schedule/alarms_handle_statistics.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/app/lib/schedule/alarms_handle_statistics.js b/api/app/lib/schedule/alarms_handle_statistics.js index b63293f..e2fe67f 100644 --- a/api/app/lib/schedule/alarms_handle_statistics.js +++ b/api/app/lib/schedule/alarms_handle_statistics.js @@ -9,7 +9,7 @@ module.exports = function (app, opts) { { interval: '0 48 1 ? * MON',//0 48 1 ? * MON 每周一凌晨1点48开始执行 // immediate: true, - //proRun: true, + proRun: true, }, async () => { try { From c8d59856c9fcecf6e5e7f6752f59e3160d8d6e45 Mon Sep 17 00:00:00 2001 From: wuqun Date: Fri, 28 Oct 2022 16:02:26 +0800 Subject: [PATCH 3/4] =?UTF-8?q?(*)=E6=95=B0=E6=8D=AE=E5=91=8A=E8=AD=A6?= =?UTF-8?q?=E5=A4=84=E7=BD=AE=E6=95=88=E7=8E=87=E6=9F=A5=E8=AF=A2=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/app/lib/schedule/alarms_handle_statistics.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/api/app/lib/schedule/alarms_handle_statistics.js b/api/app/lib/schedule/alarms_handle_statistics.js index e2fe67f..dcfa55d 100644 --- a/api/app/lib/schedule/alarms_handle_statistics.js +++ b/api/app/lib/schedule/alarms_handle_statistics.js @@ -191,12 +191,13 @@ module.exports = function (app, opts) { console.log('开始查数据-数据-数据类告警---' + moment().format('YYYY-MM-DD HH:mm:ss')) const alarmRes = await clickHouse.dataAlarm.query(` - SELECT + select count(AlarmId) count from + (SELECT alarms.AlarmId AS AlarmId, alarms.State AS State, alarms.StructureId AS StructureId, StartTime, EndTime - ${alarmQueryOptionStr}`).toPromise(); + ${alarmQueryOptionStr})`).toPromise(); let day1Str = getStr(alarmQueryOptionStr, 'Date(EndTime) - Date(StartTime)<=1'); let day1 = await clickHouse.dataAlarm.query(day1Str).toPromise(); @@ -244,7 +245,7 @@ module.exports = function (app, opts) { // let corConfirmedData = (confirmedAlarmDetailMax.find(cdm => cdm.AlarmId == ar.AlarmId) || {}); // ar.confirmTime = corConfirmedData.Time || ar.EndTime // }) - return { dataSum: alarmRes.length, dataMaps: { day1, day3, day7, day15, day30, day30m } }; + return { dataSum: alarmRes[0].count, dataMaps: { day1, day3, day7, day15, day30, day30m } }; } catch (error) { console.error(error); } From e2628098edf1a8e522683962e7ee69d8879aa27c Mon Sep 17 00:00:00 2001 From: wuqun Date: Mon, 31 Oct 2022 10:51:54 +0800 Subject: [PATCH 4/4] =?UTF-8?q?(*)=E5=91=8A=E8=AD=A6=E7=A1=AE=E8=AE=A4?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E6=8E=A8=E9=80=81=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/.vscode/launch.json | 2 +- api/app/lib/controllers/alarm/alarmConfirmLog.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/api/.vscode/launch.json b/api/.vscode/launch.json index f58978c..69a9028 100644 --- a/api/.vscode/launch.json +++ b/api/.vscode/launch.json @@ -56,7 +56,7 @@ // "--clickHouseDataAlarm default", // 测试 - "--clickHouseAnxincloud Anxinyun22", + "--clickHouseAnxincloud Anxinyun23", "--clickHousePepEmis pepca8", "--clickHouseProjectManage peppm8", "--clickHouseVcmp video_access_dev", diff --git a/api/app/lib/controllers/alarm/alarmConfirmLog.js b/api/app/lib/controllers/alarm/alarmConfirmLog.js index 5a91aea..378c584 100644 --- a/api/app/lib/controllers/alarm/alarmConfirmLog.js +++ b/api/app/lib/controllers/alarm/alarmConfirmLog.js @@ -32,7 +32,8 @@ async function alarmConfirmLog(ctx, confirmPost, content) { }) await models.LatestDynamicList.bulkCreate(dynamics); - //TODO 消息推送到前端 + //消息推送到前端 + //ctx.app.socket.emit('TEST', { someProperty: '【广播】呼叫青铜时代号!!!', }) } catch (error) {