From a96dd28d0b0ef01061a36177edd43a42febcd7d6 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Thu, 17 May 2018 12:22:52 +0800 Subject: [PATCH] app: debug info --- index.js | 18 +++++--- middleware/debug.js | 10 +++++ package.json | 1 + router.js | 37 +++++++++++++++- views/welcome.art | 103 ++++++++++++++++++++++++++++++-------------- yarn.lock | 34 +++++++++++++-- 6 files changed, 161 insertions(+), 42 deletions(-) create mode 100644 middleware/debug.js diff --git a/index.js b/index.js index 3d6f8ab604..7171762c63 100644 --- a/index.js +++ b/index.js @@ -11,6 +11,7 @@ const redisCache = require('./middleware/redis-cache.js'); const filter = require('./middleware/filter.js'); const template = require('./middleware/template.js'); const favicon = require('koa-favicon'); +const debug = require('./middleware/debug.js'); const router = require('./router'); @@ -28,19 +29,26 @@ app.use(favicon(__dirname + '/favicon.png')); // global error handing app.use(onerror); -// set header +// 1 set header app.use(header); -// fix incorrect `utf-8` characters +// 6 debug +app.context.debug = { + hitCache: 0, + request: 0, +}; +app.use(debug); + +// 5 fix incorrect `utf-8` characters app.use(utf8); -// generate body +// 4 generate body app.use(template); -// filter content +// 3 filter content app.use(filter); -// cache +// 2 cache if (config.cacheType === 'memory') { app.use( memoryCache({ diff --git a/middleware/debug.js b/middleware/debug.js new file mode 100644 index 0000000000..2b0e0b5114 --- /dev/null +++ b/middleware/debug.js @@ -0,0 +1,10 @@ +module.exports = async (ctx, next) => { + await next(); + + if (ctx.request.path !== '/') { + ctx.debug.request++; + if (ctx.response.get('X-Koa-Redis-Cache') || ctx.response.get('X-Koa-Memory-Cache')) { + ctx.debug.hitCache++; + } + } +}; diff --git a/package.json b/package.json index 555c5b858e..e70fded190 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "co-redis": "2.1.1", "crypto": "1.0.1", "form-data": "^2.3.2", + "git-rev-sync": "1.12.0", "googleapis": "30.0.0", "iconv-lite": "0.4.23", "json-bigint": "0.2.3", diff --git a/router.js b/router.js index 12a12e49d2..55a62f6f49 100644 --- a/router.js +++ b/router.js @@ -5,11 +5,46 @@ const path = require('path'); const config = require('./config'); const logger = require('./utils/logger'); +let gitHash; +try { + gitHash = require('git-rev-sync').short(); +} catch (e) { + gitHash = process.env.HEROKU_SLUG_COMMIT.slice(0, 7) || 'unknown'; +} +const startTime = +new Date(); router.get('/', async (ctx) => { ctx.set({ 'Content-Type': 'text/html; charset=UTF-8', }); - ctx.body = art(path.resolve(__dirname, './views/welcome.art'), {}); + const time = (+new Date() - startTime) / 1000; + ctx.body = art(path.resolve(__dirname, './views/welcome.art'), { + debug: [ + { + name: 'git hash', + value: gitHash, + }, + { + name: '请求数', + value: ctx.debug.request, + }, + { + name: '请求频率', + value: (ctx.debug.request / time * 60).toFixed(3) + ' 次/分钟', + }, + { + name: '缓存命中率', + value: ctx.debug.request ? (ctx.debug.hitCache / ctx.debug.request).toFixed(3) : 0, + }, + { + name: '内存占用', + value: process.memoryUsage().rss + ' Byte', + }, + { + name: '运行时间', + value: time + ' 秒', + }, + ], + }); }); // bilibili diff --git a/views/welcome.art b/views/welcome.art index 87f3e4650a..39ba0aa273 100644 --- a/views/welcome.art +++ b/views/welcome.art @@ -1,43 +1,82 @@ + -Welcome to RSSHub! - + .content { + position: absolute; + top: 40%; + left: 50%; + transform: translate(-50%, -50%); + min-width: 400px; + text-align: center; + } + + details { + text-align: left; + } + + summary { + margin-bottom: 20px; + outline: none; + cursor: pointer; + } + + .debug-key { + width: 100px; + text-align: right; + display: inline-block; + margin-right: 10px; + } + + .debug-item { + margin: 10px 0; + font-size: 14px; + } + + + -
-

RSSHub

+
+

+ RSSHub +

-

Welcome to RSSHub!

+

Welcome to + RSSHub!

-

If you see this page, the RSSHub{{ if hash }} ({{ hash }}){{ /if }} is successfully installed and working.

+

If you see this page, the RSSHub{{ if hash }} ({{ hash }}){{ /if }} is successfully installed and working.

-

For online documentation and support please refer to - rsshub.js.org.

+

For online documentation and support please refer to + rsshub.js.org.

-

-- Made with - by - DIYgod -

-
+

-- Made with + by + DIYgod +

+ +
+ debug + {{ each debug }} +
+ {{ $value.name }}: + {{ $value.value }} +
+ {{ /each }} +
+
- \ No newline at end of file + + diff --git a/yarn.lock b/yarn.lock index 0af01aa86a..30cbeec81f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1880,7 +1880,7 @@ escape-html@^1.0.3, escape-html@~1.0.1: version "1.0.3" resolved "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz#0258eae4d3d0c0974de1c169188ef0051d1d1988" -escape-string-regexp@^1.0.2, escape-string-regexp@^1.0.5: +escape-string-regexp@1.0.5, escape-string-regexp@^1.0.2, escape-string-regexp@^1.0.5: version "1.0.5" resolved "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4" @@ -2412,6 +2412,14 @@ getpass@^0.1.1: dependencies: assert-plus "^1.0.0" +git-rev-sync@1.12.0: + version "1.12.0" + resolved "https://registry.npmjs.org/git-rev-sync/-/git-rev-sync-1.12.0.tgz#4468406c7e6c3ba4cf4587999e1adb28d9d1af55" + dependencies: + escape-string-regexp "1.0.5" + graceful-fs "4.1.11" + shelljs "0.7.7" + glob-base@^0.3.0: version "0.3.0" resolved "https://registry.npmjs.org/glob-base/-/glob-base-0.3.0.tgz#dbb164f6221b1c0b1ccf82aea328b497df0ea3c4" @@ -2447,7 +2455,7 @@ glob@7.0.x: once "^1.3.0" path-is-absolute "^1.0.0" -glob@^7.0.3, glob@^7.0.5, glob@^7.1.1, glob@^7.1.2: +glob@^7.0.0, glob@^7.0.3, glob@^7.0.5, glob@^7.1.1, glob@^7.1.2: version "7.1.2" resolved "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15" dependencies: @@ -2560,7 +2568,7 @@ got@^6.7.1: unzip-response "^2.0.1" url-parse-lax "^1.0.0" -graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.6: +graceful-fs@4.1.11, graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.6: version "4.1.11" resolved "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658" @@ -2886,6 +2894,10 @@ inquirer@^3.0.6: strip-ansi "^4.0.0" through "^2.3.6" +interpret@^1.0.0: + version "1.1.0" + resolved "https://registry.npmjs.org/interpret/-/interpret-1.1.0.tgz#7ed1b1410c6a0e0f78cf95d3b8440c63f78b8614" + is-absolute-url@^2.0.0: version "2.1.0" resolved "https://registry.npmjs.org/is-absolute-url/-/is-absolute-url-2.1.0.tgz#50530dfb84fcc9aa7dbe7852e83a37b93b9f2aa6" @@ -5286,6 +5298,12 @@ readdirp@^2.0.0: readable-stream "^2.0.2" set-immediate-shim "^1.0.1" +rechoir@^0.6.2: + version "0.6.2" + resolved "https://registry.npmjs.org/rechoir/-/rechoir-0.6.2.tgz#85204b54dba82d5742e28c96756ef43af50e3384" + dependencies: + resolve "^1.1.6" + redent@^2.0.0: version "2.0.0" resolved "https://registry.npmjs.org/redent/-/redent-2.0.0.tgz#c1b2007b42d57eb1389079b3c8333639d5e1ccaa" @@ -5509,7 +5527,7 @@ resolve@1.1.7: version "1.1.7" resolved "https://registry.npmjs.org/resolve/-/resolve-1.1.7.tgz#203114d82ad2c5ed9e8e0411b3932875e889e97b" -resolve@^1.2.0, resolve@^1.6.0: +resolve@^1.1.6, resolve@^1.2.0, resolve@^1.6.0: version "1.7.1" resolved "https://registry.npmjs.org/resolve/-/resolve-1.7.1.tgz#aadd656374fd298aee895bc026b8297418677fd3" dependencies: @@ -5687,6 +5705,14 @@ shebang-regex@^1.0.0: version "1.0.0" resolved "https://registry.npmjs.org/shebang-regex/-/shebang-regex-1.0.0.tgz#da42f49740c0b42db2ca9728571cb190c98efea3" +shelljs@0.7.7: + version "0.7.7" + resolved "https://registry.npmjs.org/shelljs/-/shelljs-0.7.7.tgz#b2f5c77ef97148f4b4f6e22682e10bba8667cff1" + dependencies: + glob "^7.0.0" + interpret "^1.0.0" + rechoir "^0.6.2" + signal-exit@^3.0.0, signal-exit@^3.0.2: version "3.0.2" resolved "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d"