监听日志

This commit is contained in:
2024-11-23 15:30:32 +08:00
parent 793b1a0fd2
commit 8b3fde1ae3
4 changed files with 64 additions and 3 deletions

2
.gitignore vendored
View File

@@ -1 +1,3 @@
access.log
node_modules
package-lock.json

View File

@@ -1,2 +1,7 @@
# statistics
数据为日志, 指标亦为日志
引入数据
创建指标
导出接口

50
main.js Normal file
View File

@@ -0,0 +1,50 @@
import { Tail } from 'tail'
const tail = new Tail('./access.log')
// 指标(日榜, 周榜, 月榜, 总榜)
const 作品 = new Map()
const 游戏 = new Map()
const 截图 = new Map()
tail.on('line', (line) => {
const item = JSON.parse(line)
if (item.level !== 'debug') return
if (item.msg !== 'upstream roundtrip') return
if (item.request.uri.startsWith('/web/v1/article/get')) {
const reg = /\/web\/v1\/article\/get\?id=(\d+)&userId=(\d+)/
const [uri, id, user_id] = item.request.uri.match(reg) ?? []
if (uri && id && user_id) {
// 判断类型
游戏.set(id, 游戏.has(id) ? 游戏.get(id) + 1 : 1)
}
return
}
if (item.request.uri.startsWith('/api/images')) {
if (item.request.uri.includes('page=')) {
return
}
const reg = /\/api\/images\?similar=(\d+)/
const [uri, id] = item.request.uri.match(reg) ?? []
if (uri && id) {
截图.set(id, 截图.has(id) ? 截图.get(id) + 1 : 1)
}
return
}
})
tail.on('end', () => {
console.log('没有更多内容,结束读取')
const sorted = new Map([...游戏.entries()].sort((a, b) => b[1] - a[1]))
const top10 = Array.from(sorted).slice(0, 10)
console.log(top10)
})
tail.on('error', (error) => {
console.error('错误:', error)
})
process.on('SIGINT', () => {
tail.unwatch()
process.exit()
})

View File

@@ -2,10 +2,14 @@
"name": "statistics",
"version": "1.0.0",
"description": "统计",
"main": "index.js",
"type": "module",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "satori",
"license": "GPL-3.0-only"
"license": "GPL-3.0-only",
"dependencies": {
"tail": "^2.2.6"
}
}