From 9f36be5025e084b32a01e34b3342f07359e13df3 Mon Sep 17 00:00:00 2001 From: CODE <1650192445@qq.com> Date: Mon, 28 Aug 2023 14:06:09 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A6=BB=E7=BA=BF=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/app/lib/controllers/project/group.js | 33 +++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/api/app/lib/controllers/project/group.js b/api/app/lib/controllers/project/group.js index 150da65..21a518e 100644 --- a/api/app/lib/controllers/project/group.js +++ b/api/app/lib/controllers/project/group.js @@ -241,10 +241,19 @@ async function groupStatisticOnline (ctx) { const strucRes = strucIdArr.length ? await clickHouse.anxinyun.query( ` - SELECT * FROM t_structure WHERE id IN (${[...strucIdArr].join(',')}); + SELECT name, id FROM t_structure WHERE id IN (${[...strucIdArr].join(',')}); ` ).toPromise() : [] + // 查中断时间 + const maxOfflineTimeRes = strucIdArr.length ? await models.StructureOff.findAll({ + where: { + structure: { $in: strucIdArr }, + state: 0 + } + }) : [] + + // 查在线率 const strucOnlineClient = ctx.app.fs.esclient.strucOnline const onlineRes = await strucOnlineClient.search({ index: strucOnlineClient.config.index, @@ -264,7 +273,7 @@ async function groupStatisticOnline (ctx) { { "terms": { "structure": strucIdArr, - "structure": [1, 2, 3] + // "structure": [1, 2, 3] } } ] @@ -280,8 +289,26 @@ async function groupStatisticOnline (ctx) { } }) + for (let struc of strucRes) { + let curOnline = + onlineRes.hits.hits + .filter((h) => h._source.structure == struc.id) + // .sort((a, b) => { + // return a._source.collect_time - b._source.collect_time + // }) + .map(s => { + return { + ...s._source, + rate: s._source.online / s._source.total * 100 + } + }) + let curOffline = maxOfflineTimeRes.find((o) => o.structure == struc.id) + struc.online = curOnline + struc.offline = curOffline || {} + } + ctx.status = 200; - ctx.body = onlineRes.hits.hits; + ctx.body = strucRes; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: error`); ctx.status = 400;