app: debug: add hot ips

This commit is contained in:
DIYgod
2018-05-17 15:48:35 +08:00
parent 77014458d0
commit 005670acee
4 changed files with 34 additions and 0 deletions

View File

@@ -38,6 +38,7 @@ app.context.debug = {
hitCache: 0,
request: 0,
routes: [],
ips: [],
};
app.use(debug);

View File

@@ -4,6 +4,12 @@ module.exports = async (ctx, next) => {
}
ctx.debug.routes[ctx.request.path]++;
const ip = ctx.ips[0] || ctx.ip;
if (!ctx.debug.ips[ip]) {
ctx.debug.ips[ip] = 0;
}
ctx.debug.ips[ip]++;
if (ctx.request.path !== '/') {
ctx.debug.request++;
}

View File

@@ -18,6 +18,7 @@ router.get('/', async (ctx) => {
});
const time = (+new Date() - startTime) / 1000;
const routes = Object.keys(ctx.debug.routes).sort((a, b) => ctx.debug.routes[b] - ctx.debug.routes[a]);
const hotRoutes = routes.slice(0, 10);
let hotRoutesValue = '';
@@ -25,6 +26,13 @@ router.get('/', async (ctx) => {
hotRoutesValue += `${ctx.debug.routes[item]}&nbsp;&nbsp;${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, 10);
let hotIPsValue = '';
hotIPs.forEach((item) => {
hotIPsValue += `${ctx.debug.ips[item]}&nbsp;&nbsp;${item}<br>`;
});
ctx.body = art(path.resolve(__dirname, './views/welcome.art'), {
debug: [
{
@@ -55,6 +63,10 @@ router.get('/', async (ctx) => {
name: '热门路由',
value: hotRoutesValue,
},
{
name: '热门IP',
value: hotIPsValue,
},
],
});
});

View File

@@ -25,6 +25,21 @@
details {
text-align: left;
max-height: 400px;
overflow: scroll;
}
details::-webkit-scrollbar {
width: 5px;
}
details::-webkit-scrollbar-thumb {
border-radius: 3px;
background-color: #eee;
}
details::-webkit-scrollbar-thumb:hover {
background-color: #ccc;
}
summary {