diff --git a/.gitignore b/.gitignore index f2cc884..042f600 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -access.log \ No newline at end of file +access.log +node_modules +package-lock.json diff --git a/README.md b/README.md index bbb72b3..004af6e 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,7 @@ # statistics +数据为日志, 指标亦为日志 + +引入数据 +创建指标 +导出接口 diff --git a/main.js b/main.js new file mode 100644 index 0000000..d2e6011 --- /dev/null +++ b/main.js @@ -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() +}) diff --git a/package.json b/package.json index 60fcfd5..aa7169e 100644 --- a/package.json +++ b/package.json @@ -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" + } }