mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-04 19:59:54 +08:00
feat: debug info for hot route and path
This commit is contained in:
@@ -57,6 +57,7 @@ if (cluster.isMaster && !config.disableCluster && process.env.NODE_ENV !== 'test
|
||||
app.context.debug = {
|
||||
hitCache: 0,
|
||||
request: 0,
|
||||
paths: [],
|
||||
routes: [],
|
||||
ips: [],
|
||||
};
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
module.exports = async (ctx, next) => {
|
||||
if (!ctx.debug.routes[ctx.request.path]) {
|
||||
ctx.debug.routes[ctx.request.path] = 0;
|
||||
if (!ctx.debug.paths[ctx.request.path]) {
|
||||
ctx.debug.paths[ctx.request.path] = 0;
|
||||
}
|
||||
ctx.debug.routes[ctx.request.path]++;
|
||||
ctx.debug.paths[ctx.request.path]++;
|
||||
|
||||
const ip = ctx.ips[0] || ctx.ip;
|
||||
if (!ctx.debug.ips[ip]) {
|
||||
@@ -13,6 +13,11 @@ module.exports = async (ctx, next) => {
|
||||
|
||||
await next();
|
||||
|
||||
if (!ctx.debug.routes[ctx._matchedRoute]) {
|
||||
ctx.debug.routes[ctx._matchedRoute] = 0;
|
||||
}
|
||||
ctx.debug.routes[ctx._matchedRoute]++;
|
||||
|
||||
if (ctx.response.get('X-Koa-Redis-Cache') || ctx.response.get('X-Koa-Memory-Cache')) {
|
||||
ctx.debug.hitCache++;
|
||||
}
|
||||
|
||||
@@ -16,12 +16,19 @@ module.exports = async (ctx) => {
|
||||
});
|
||||
|
||||
const routes = Object.keys(ctx.debug.routes).sort((a, b) => ctx.debug.routes[b] - ctx.debug.routes[a]);
|
||||
const hotRoutes = routes.slice(0, 50);
|
||||
const hotRoutes = routes.slice(0, 30);
|
||||
let hotRoutesValue = '';
|
||||
hotRoutes.forEach((item) => {
|
||||
hotRoutesValue += `${ctx.debug.routes[item]} ${item}<br>`;
|
||||
});
|
||||
|
||||
const paths = Object.keys(ctx.debug.paths).sort((a, b) => ctx.debug.paths[b] - ctx.debug.paths[a]);
|
||||
const hotPaths = paths.slice(0, 30);
|
||||
let hotPathsValue = '';
|
||||
hotPaths.forEach((item) => {
|
||||
hotPathsValue += `${ctx.debug.paths[item]} ${item}<br>`;
|
||||
});
|
||||
|
||||
const ips = Object.keys(ctx.debug.ips).sort((a, b) => ctx.debug.ips[b] - ctx.debug.ips[a]);
|
||||
const hotIPs = ips.slice(0, 50);
|
||||
let hotIPsValue = '';
|
||||
@@ -76,6 +83,10 @@ module.exports = async (ctx) => {
|
||||
name: '热门路由',
|
||||
value: hotRoutesValue,
|
||||
},
|
||||
{
|
||||
name: '热门路径',
|
||||
value: hotPathsValue,
|
||||
},
|
||||
{
|
||||
name: '热门IP',
|
||||
value: hotIPsValue,
|
||||
|
||||
@@ -41,6 +41,9 @@ describe('debug', () => {
|
||||
expect(value).toBe('6');
|
||||
break;
|
||||
case '热门路由:':
|
||||
expect(value).toBe(`5 /test/:id<br>`);
|
||||
break;
|
||||
case '热门路径:':
|
||||
expect(value).toBe(`3 /test/1<br>2 /test/2<br>1 /<br>`);
|
||||
break;
|
||||
case '热门IP:':
|
||||
|
||||
Reference in New Issue
Block a user