mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-04 11:07: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 = {
|
app.context.debug = {
|
||||||
hitCache: 0,
|
hitCache: 0,
|
||||||
request: 0,
|
request: 0,
|
||||||
|
paths: [],
|
||||||
routes: [],
|
routes: [],
|
||||||
ips: [],
|
ips: [],
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
module.exports = async (ctx, next) => {
|
module.exports = async (ctx, next) => {
|
||||||
if (!ctx.debug.routes[ctx.request.path]) {
|
if (!ctx.debug.paths[ctx.request.path]) {
|
||||||
ctx.debug.routes[ctx.request.path] = 0;
|
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;
|
const ip = ctx.ips[0] || ctx.ip;
|
||||||
if (!ctx.debug.ips[ip]) {
|
if (!ctx.debug.ips[ip]) {
|
||||||
@@ -13,6 +13,11 @@ module.exports = async (ctx, next) => {
|
|||||||
|
|
||||||
await 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')) {
|
if (ctx.response.get('X-Koa-Redis-Cache') || ctx.response.get('X-Koa-Memory-Cache')) {
|
||||||
ctx.debug.hitCache++;
|
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 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 = '';
|
let hotRoutesValue = '';
|
||||||
hotRoutes.forEach((item) => {
|
hotRoutes.forEach((item) => {
|
||||||
hotRoutesValue += `${ctx.debug.routes[item]} ${item}<br>`;
|
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 ips = Object.keys(ctx.debug.ips).sort((a, b) => ctx.debug.ips[b] - ctx.debug.ips[a]);
|
||||||
const hotIPs = ips.slice(0, 50);
|
const hotIPs = ips.slice(0, 50);
|
||||||
let hotIPsValue = '';
|
let hotIPsValue = '';
|
||||||
@@ -76,6 +83,10 @@ module.exports = async (ctx) => {
|
|||||||
name: '热门路由',
|
name: '热门路由',
|
||||||
value: hotRoutesValue,
|
value: hotRoutesValue,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: '热门路径',
|
||||||
|
value: hotPathsValue,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: '热门IP',
|
name: '热门IP',
|
||||||
value: hotIPsValue,
|
value: hotIPsValue,
|
||||||
|
|||||||
@@ -41,6 +41,9 @@ describe('debug', () => {
|
|||||||
expect(value).toBe('6');
|
expect(value).toBe('6');
|
||||||
break;
|
break;
|
||||||
case '热门路由:':
|
case '热门路由:':
|
||||||
|
expect(value).toBe(`5 /test/:id<br>`);
|
||||||
|
break;
|
||||||
|
case '热门路径:':
|
||||||
expect(value).toBe(`3 /test/1<br>2 /test/2<br>1 /<br>`);
|
expect(value).toBe(`3 /test/1<br>2 /test/2<br>1 /<br>`);
|
||||||
break;
|
break;
|
||||||
case '热门IP:':
|
case '热门IP:':
|
||||||
|
|||||||
Reference in New Issue
Block a user