115 lines
4.2 KiB
JavaScript
115 lines
4.2 KiB
JavaScript
import fs from 'fs'
|
|
import { Tail } from 'tail'
|
|
import { update } from './update.js'
|
|
|
|
|
|
const articles = new Map() // 文章统计
|
|
const screenshots = new Map() // 截图统计
|
|
const collections = new Map() // 收藏统计
|
|
|
|
|
|
// 日期格式化工具
|
|
const dateFormat = new Intl.DateTimeFormat('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit' })
|
|
|
|
|
|
// 匹配文章和截图的正则表达式
|
|
const imagesRegex = /\/web\/v1\/images\/detail\/v2\?imagesId=(\d+)¤tUserId=(\d+)/
|
|
const articleRegex = /\/web\/v1\/article\/get\?id=(\d+)&userId=(\d+)/
|
|
const collectionRegex = /\/web\/v1\/member\/explorer\/article\/get\?id=(\d+)/
|
|
|
|
|
|
let currentDate = dateFormat.format(new Date()).replace(/\//g, '-') // 当前日期
|
|
let isArchiving = false // 是否正在存档标记
|
|
|
|
|
|
// 存档函数:将数据存入文件
|
|
const archive = async (message = '正在存档...') => {
|
|
if (isArchiving) return // 防止重复存档
|
|
isArchiving = true
|
|
console.log(message)
|
|
await fs.promises.writeFile(`./data/${currentDate}.json`, JSON.stringify({
|
|
articles: Object.fromEntries(articles),
|
|
screenshots: Object.fromEntries(screenshots),
|
|
collections: Object.fromEntries(collections),
|
|
}, null, 2))
|
|
isArchiving = false
|
|
}
|
|
|
|
// 主函数
|
|
export default function () {
|
|
if (fs.existsSync(`./data/${currentDate}.json`)) {
|
|
console.log(currentDate, '启动时加载当日数据...')
|
|
const file = fs.readFileSync(`./data/${currentDate}.json`, 'utf8')
|
|
if (file) {
|
|
const data = JSON.parse(file)
|
|
Object.entries(data.articles).forEach(([key, value]) => articles.set(key, value))
|
|
Object.entries(data.screenshots).forEach(([key, value]) => screenshots.set(key, value))
|
|
Object.entries(data.collections).forEach(([key, value]) => collections.set(key, value))
|
|
}
|
|
}
|
|
|
|
console.log(currentDate, '开始收集日志...')
|
|
|
|
setInterval(() => {
|
|
archive('每10分钟自动存档...')
|
|
update('web_images', 'day_rank', Object.entries(Object.fromEntries(screenshots))),
|
|
update('web_article', 'day_rank', Object.entries(Object.fromEntries(articles))),
|
|
update('web_member_explorer', 'day_rank', Object.entries(Object.fromEntries(collections)))
|
|
}, 600000)
|
|
|
|
// 实时读取日志文件
|
|
const tail = new Tail('/opt/log/caddy/access.log')
|
|
tail.on('line', async (line) => {
|
|
const item = JSON.parse(line)
|
|
if (item.level !== 'debug') return
|
|
if (item.msg !== 'upstream roundtrip') return
|
|
|
|
// 检查日志的日期是否跨天
|
|
const logDate = dateFormat.format(new Date(item.ts * 1000)).replace(/\//g, '-')
|
|
if (currentDate !== logDate) {
|
|
await archive('跨日期存档...')
|
|
articles.clear()
|
|
screenshots.clear()
|
|
collections.clear()
|
|
currentDate = logDate
|
|
}
|
|
|
|
// 处理文章日志
|
|
if (item.request.uri.startsWith('/web/v1/article/get')) {
|
|
const [uri, id] = item.request.uri.match(articleRegex) ?? []
|
|
if (uri && id) {
|
|
articles.set(id, articles.has(id) ? articles.get(id) + 1 : 1)
|
|
}
|
|
}
|
|
|
|
// 处理截图日志
|
|
if (item.request.uri.startsWith('/web/v1/images/detail/v2')) {
|
|
const [uri, id] = item.request.uri.match(imagesRegex) ?? []
|
|
if (uri && id) {
|
|
screenshots.set(id, screenshots.has(id) ? screenshots.get(id) + 1 : 1)
|
|
}
|
|
return
|
|
}
|
|
|
|
// 处理收藏日志
|
|
if (item.request.uri.startsWith('/web/v1/member/explorer/article/get')) {
|
|
const [uri, id] = item.request.uri.match(collectionRegex) ?? []
|
|
if (uri && id) {
|
|
collections.set(id, collections.has(id) ? collections.get(id) + 1 : 1)
|
|
}
|
|
return
|
|
}
|
|
})
|
|
|
|
// 日志尾部事件监听
|
|
tail.on('end', () => console.log('没有更多内容,停止读取'))
|
|
tail.on('error', (error) => console.error('错误:', error))
|
|
|
|
// 捕获退出信号,确保数据存档
|
|
process.on('SIGINT', async () => {
|
|
tail.unwatch() // 停止监听日志
|
|
await archive('退出前存档数据...')
|
|
process.exit()
|
|
})
|
|
}
|