From e9bf0c44055c30fe39ae708c8a1f00d6345463be Mon Sep 17 00:00:00 2001 From: "gao.zhiyuan" Date: Thu, 15 Sep 2022 17:54:49 +0800 Subject: [PATCH] DurationCalc --- api/app/lib/controllers/alarm/app.js | 10 +++++----- api/app/lib/controllers/organization/index.js | 9 ++++++--- api/app/lib/controllers/project/bind.js | 2 +- api/app/lib/controllers/project/index.js | 6 +++--- api/app/lib/controllers/push/config.js | 6 +++--- api/app/lib/middlewares/duration-calc.js | 14 ++++++++++++++ api/app/lib/routes/organization/index.js | 1 - 7 files changed, 32 insertions(+), 16 deletions(-) create mode 100644 api/app/lib/middlewares/duration-calc.js diff --git a/api/app/lib/controllers/alarm/app.js b/api/app/lib/controllers/alarm/app.js index 58fe6e8..3313e79 100644 --- a/api/app/lib/controllers/alarm/app.js +++ b/api/app/lib/controllers/alarm/app.js @@ -89,7 +89,7 @@ async function inspectionList (ctx) { ctx.status = 200; ctx.body = inspectionRes } catch (error) { - ctx.fs.logger.error(`path: ${ctx.path}, error: error`); + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined @@ -114,7 +114,7 @@ async function notedInspection (ctx) { ctx.status = 204; } catch (error) { - ctx.fs.logger.error(`path: ${ctx.path}, error: error`); + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined @@ -159,7 +159,7 @@ async function apiError (ctx) { ctx.status = 200; } catch (error) { - ctx.fs.logger.error(`path: ${ctx.path}, error: error`); + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined @@ -176,7 +176,7 @@ async function apiErrorList (ctx) { ctx.status = 200; ctx.body = [] } catch (error) { - ctx.fs.logger.error(`path: ${ctx.path}, error: error`); + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined @@ -199,7 +199,7 @@ async function confirmApiError (ctx) { ctx.status = 204; } catch (error) { - ctx.fs.logger.error(`path: ${ctx.path}, error: error`); + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined diff --git a/api/app/lib/controllers/organization/index.js b/api/app/lib/controllers/organization/index.js index f8c4ba7..fbda0d1 100644 --- a/api/app/lib/controllers/organization/index.js +++ b/api/app/lib/controllers/organization/index.js @@ -5,13 +5,16 @@ async function allDeps (ctx) { try { const models = ctx.fs.dc.models; const { redis } = ctx.app - + const start = moment() let depRes = await redis.get('allDepartments') + console.log(`DurationCalc: 1 用时 ${moment().diff(start, 'milliseconds')}`); if (depRes) { depRes = JSON.parse(depRes) + console.log(`DurationCalc: 2 用时 ${moment().diff(start, 'milliseconds')}`); depRes = depRes.departments + console.log(`DurationCalc: 3 用时 ${moment().diff(start, 'milliseconds')}`); } - + console.log(`DurationCalc: 4 用时 ${moment().diff(start, 'milliseconds')}`); ctx.status = 200; ctx.body = depRes || [] } catch (error) { @@ -147,7 +150,7 @@ async function delAdmin (ctx) { ctx.status = 204; } catch (error) { - ctx.fs.logger.error(`path: ${ctx.path}, error: error`); + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined diff --git a/api/app/lib/controllers/project/bind.js b/api/app/lib/controllers/project/bind.js index f11651e..f6a4bbd 100644 --- a/api/app/lib/controllers/project/bind.js +++ b/api/app/lib/controllers/project/bind.js @@ -9,7 +9,7 @@ async function bindAnxin2pep (ctx) { ctx.status = 20; } catch (error) { - ctx.fs.logger.error(`path: ${ctx.path}, error: error`); + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined diff --git a/api/app/lib/controllers/project/index.js b/api/app/lib/controllers/project/index.js index 70af536..88f62b0 100644 --- a/api/app/lib/controllers/project/index.js +++ b/api/app/lib/controllers/project/index.js @@ -12,7 +12,7 @@ async function appList (ctx) { ctx.status = 200; ctx.body = appRes } catch (error) { - ctx.fs.logger.error(`path: ${ctx.path}, error: error`); + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined @@ -30,7 +30,7 @@ async function projectAnxincloud (ctx) { ctx.status = 200; ctx.body = projectRes } catch (error) { - ctx.fs.logger.error(`path: ${ctx.path}, error: error`); + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined @@ -48,7 +48,7 @@ async function projectPManage (ctx) { ctx.status = 200; ctx.body = projectRes } catch (error) { - ctx.fs.logger.error(`path: ${ctx.path}, error: error`); + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined diff --git a/api/app/lib/controllers/push/config.js b/api/app/lib/controllers/push/config.js index 2af88bb..3cbcdb0 100644 --- a/api/app/lib/controllers/push/config.js +++ b/api/app/lib/controllers/push/config.js @@ -37,7 +37,7 @@ async function list (ctx) { ctx.status = 200; ctx.body = listRes } catch (error) { - ctx.fs.logger.error(`path: ${ctx.path}, error: error`); + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined @@ -68,7 +68,7 @@ async function edit (ctx) { ctx.status = 204; } catch (error) { - ctx.fs.logger.error(`path: ${ctx.path}, error: error`); + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined @@ -100,7 +100,7 @@ async function state (ctx) { ctx.status = 204; } catch (error) { - ctx.fs.logger.error(`path: ${ctx.path}, error: error`); + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined diff --git a/api/app/lib/middlewares/duration-calc.js b/api/app/lib/middlewares/duration-calc.js new file mode 100644 index 0000000..bd338c0 --- /dev/null +++ b/api/app/lib/middlewares/duration-calc.js @@ -0,0 +1,14 @@ +'use strict'; +const moment = require('moment'); + +async function factory (ctx, next) { + try { + const start = moment() + await next() + ctx.fs.logger.log(`DurationCalc: ${ctx.path} 用时 ${moment().diff(start, 'milliseconds')}`); + } catch (error) { + ctx.fs.logger.error(`DurationCalc, error: ${error}`); + } +} + +module.exports = factory; \ No newline at end of file diff --git a/api/app/lib/routes/organization/index.js b/api/app/lib/routes/organization/index.js index 48e17e6..4b26221 100644 --- a/api/app/lib/routes/organization/index.js +++ b/api/app/lib/routes/organization/index.js @@ -1,5 +1,4 @@ 'use strict'; - const organization = require('../../controllers/organization'); module.exports = function (app, router, opts) {