From 46287ac2f5bed28e1743bb609f55bb7b9961ee50 Mon Sep 17 00:00:00 2001 From: aixianling Date: Fri, 10 Feb 2023 16:07:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=89=8D=E7=AB=AF=E6=8E=A5=E5=8F=A3=E7=9B=91?= =?UTF-8?q?=E6=8E=A7=E7=BB=9F=E8=AE=A1=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/rest/monitorApi/add.js | 11 ++++++----- src/rest/monitorApi/sta.js | 17 +++++++++++++++++ src/utils/dbUitls.js | 2 +- 3 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 src/rest/monitorApi/sta.js diff --git a/src/rest/monitorApi/add.js b/src/rest/monitorApi/add.js index ba6f0e0..9c76803 100644 --- a/src/rest/monitorApi/add.js +++ b/src/rest/monitorApi/add.js @@ -1,13 +1,14 @@ const dbUtils = require("../../utils/dbUitls"); +const addLog = ({id, status, path, device, url, createTime, nodeProcess, method, code, userName, error}) => dbUtils.addOrUpdate({ + table: 'node_api_logs', + form: {id, status, path, device, url, createTime, nodeProcess, method, code, userName, error} +}) + module.exports = { action: "/node/monitorApi/addOrUpdate", method: "post", execute: (request, response) => { - let {id, status, path, device, url, createTime, nodeProcess, userId, userName, error} = request.body - dbUtils.addOrUpdate({ - table: 'node_api_logs', - form: {id, status, path, device, url, createTime, nodeProcess, userId, userName, error} - }).then(data => { + Promise.all([request.body].flat().map(e => addLog(e))).then(data => { response.send({code: 0, data}) }).catch(err => { response.send({code: 1, err: err?.sqlMessage || err || ""}) diff --git a/src/rest/monitorApi/sta.js b/src/rest/monitorApi/sta.js new file mode 100644 index 0000000..006d0ec --- /dev/null +++ b/src/rest/monitorApi/sta.js @@ -0,0 +1,17 @@ +const dbUtils = require("../../utils/dbUitls"); +module.exports = { + action: "/node/monitorApi/sta", + method: "post", + execute: (request, response) => { + const data = {} + Promise.all([ + dbUtils.query(`select path,method,SUM(1) as total,createTime FROM node_api_logs where status not like '200' GROUP BY path ORDER BY total DESC limit 0,10`).then(res => data.top10 = res), + dbUtils.query(`select status as name,SUM(1) as value FROM node_api_logs where status not like '200' GROUP BY status`).then(res => data.distribution = res) + ]).then(() => { + response.send({code: 0, data}) + }).catch(err => { + response.send({code: 1, err: err?.sqlMessage || err || ""}) + }) + + } +} diff --git a/src/utils/dbUitls.js b/src/utils/dbUitls.js index 79a872c..b38feee 100644 --- a/src/utils/dbUitls.js +++ b/src/utils/dbUitls.js @@ -31,7 +31,7 @@ module.exports = { let total = 0, records = [] if (table) { const {current, size = 10} = search, params = JSON.parse(JSON.stringify(search)) - const conValue = params[con] + const conValue = params[con] || "" delete params.current delete params.size delete params[con]