提交
This commit is contained in:
118
main.js
118
main.js
@@ -1,101 +1,119 @@
|
|||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
import { Tail } from 'tail'
|
import { Tail } from 'tail'
|
||||||
import { updateDatabase } from './put_database.js'
|
import { update } from './update.js'
|
||||||
|
|
||||||
const 作品 = new Map()
|
// 用于存储文章、游戏、截图相关的统计数据
|
||||||
const 游戏 = new Map()
|
const articles = new Map() // 文章统计
|
||||||
const 截图 = new Map()
|
const games = new Map() // 游戏统计
|
||||||
const 格式 = new Intl.DateTimeFormat('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit' })
|
const screenshots = new Map() // 截图统计
|
||||||
|
|
||||||
|
// 日期格式化工具
|
||||||
|
const dateFormat = new Intl.DateTimeFormat('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit' })
|
||||||
|
|
||||||
|
// 匹配文章和截图的正则表达式
|
||||||
const articleRegex = /\/web\/v1\/article\/get\?id=(\d+)&userId=(\d+)/
|
const articleRegex = /\/web\/v1\/article\/get\?id=(\d+)&userId=(\d+)/
|
||||||
const imagesRegex = /\/api\/images\?similar=(\d+)/
|
const imagesRegex = /\/api\/images\?similar=(\d+)/
|
||||||
|
|
||||||
|
let currentDate = dateFormat.format(new Date()).replace(/\//g, '-') // 当前日期
|
||||||
|
let isArchiving = false // 是否正在存档标记
|
||||||
|
|
||||||
var 当前日期 = 格式.format(new Date()).replace(/\//g, '-')
|
// 存档函数:将数据存入文件
|
||||||
var 正在存档 = false
|
const archive = async (message = '正在存档...') => {
|
||||||
|
if (isArchiving) return // 防止重复存档
|
||||||
const 存档 = async (message = '存档中..') => {
|
isArchiving = true
|
||||||
if (正在存档) return
|
|
||||||
正在存档 = true
|
|
||||||
console.log(message)
|
console.log(message)
|
||||||
await fs.promises.writeFile(`./data/${当前日期}.json`, JSON.stringify({
|
await fs.promises.writeFile(`./data/${currentDate}.json`, JSON.stringify({
|
||||||
作品: Object.fromEntries(作品),
|
articles: Object.fromEntries(articles), // 将 Map 转换为普通对象
|
||||||
游戏: Object.fromEntries(游戏),
|
games: Object.fromEntries(games),
|
||||||
截图: Object.fromEntries(截图),
|
screenshots: Object.fromEntries(screenshots),
|
||||||
}, null, 2))
|
}, null, 2))
|
||||||
正在存档 = false
|
isArchiving = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 主函数
|
||||||
export default function () {
|
export default function () {
|
||||||
if (fs.existsSync(`./data/${当前日期}.json`)) {
|
// 启动时加载当天的数据文件
|
||||||
console.log(当前日期, '启动时读入数据..')
|
if (fs.existsSync(`./data/${currentDate}.json`)) {
|
||||||
const file = fs.readFileSync(`./data/${当前日期}.json`, 'utf8')
|
console.log(currentDate, '启动时加载数据...')
|
||||||
|
const file = fs.readFileSync(`./data/${currentDate}.json`, 'utf8')
|
||||||
if (file) {
|
if (file) {
|
||||||
const data = JSON.parse(file)
|
const data = JSON.parse(file)
|
||||||
Object.entries(data.作品).forEach(([key, value]) => 作品.set(key, value))
|
// 将文件中的数据加载到对应的 Map 中
|
||||||
Object.entries(data.游戏).forEach(([key, value]) => 游戏.set(key, value))
|
Object.entries(data.articles).forEach(([key, value]) => articles.set(key, value))
|
||||||
Object.entries(data.截图).forEach(([key, value]) => 截图.set(key, value))
|
Object.entries(data.games).forEach(([key, value]) => games.set(key, value))
|
||||||
|
Object.entries(data.screenshots).forEach(([key, value]) => screenshots.set(key, value))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(当前日期, '开始收集日志..')
|
console.log(currentDate, '开始收集日志...')
|
||||||
|
|
||||||
|
// 定时存档(每10分钟)
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
存档('10 分钟存档一次..')
|
archive('每10分钟自动存档...')
|
||||||
updateDatabase(Object.fromEntries(截图))
|
update('day_rank', Object.fromEntries(screenshots)) // 更新数据库中 day_rank 字段
|
||||||
}, 600000)
|
}, 600000)
|
||||||
|
|
||||||
|
// 实时读取日志文件
|
||||||
const tail = new Tail('/opt/log/caddy/access.log')
|
const tail = new Tail('/opt/log/caddy/access.log')
|
||||||
tail.on('line', async (line) => {
|
tail.on('line', async (line) => {
|
||||||
const item = JSON.parse(line)
|
const item = JSON.parse(line) // 解析日志行
|
||||||
if (item.level !== 'debug') return
|
if (item.level !== 'debug') return // 仅处理 debug 级别的日志
|
||||||
if (item.msg !== 'upstream roundtrip') return
|
if (item.msg !== 'upstream roundtrip') return // 仅处理指定类型的日志
|
||||||
|
|
||||||
const 日志日期 = 格式.format(new Date(item.ts * 1000)).replace(/\//g, '-')
|
// 检查日志的日期是否跨天
|
||||||
if (当前日期 !== 日志日期) {
|
const logDate = dateFormat.format(new Date(item.ts * 1000)).replace(/\//g, '-')
|
||||||
await 存档('跨日期存档..')
|
if (currentDate !== logDate) {
|
||||||
作品.clear()
|
await archive('跨日期存档...') // 保存当前数据
|
||||||
游戏.clear()
|
articles.clear() // 清空统计数据
|
||||||
截图.clear()
|
games.clear()
|
||||||
当前日期 = 日志日期
|
screenshots.clear()
|
||||||
|
currentDate = logDate // 更新当前日期
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 处理文章相关日志
|
||||||
if (item.request.uri.startsWith('/web/v1/article/get')) {
|
if (item.request.uri.startsWith('/web/v1/article/get')) {
|
||||||
const [uri, id, user_id] = item.request.uri.match(articleRegex) ?? []
|
const [uri, id, userId] = item.request.uri.match(articleRegex) ?? []
|
||||||
if (uri && id && user_id && item.request.headers.Referer) {
|
if (uri && id && userId && item.request.headers.Referer) {
|
||||||
if (item.request.headers.Referer[0].includes('/articleDetails/')) {
|
const referer = item.request.headers.Referer[0]
|
||||||
作品.set(id, 作品.has(id) ? 作品.get(id) + 1 : 1)
|
// 根据 Referer 的不同路径,分类计数
|
||||||
|
if (referer.includes('/articleDetails/')) {
|
||||||
|
articles.set(id, articles.has(id) ? articles.get(id) + 1 : 1)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (item.request.headers.Referer[0].includes('/inspirationInfo/')) {
|
if (referer.includes('/inspirationInfo/')) {
|
||||||
游戏.set(id, 游戏.has(id) ? 游戏.get(id) + 1 : 1)
|
games.set(id, games.has(id) ? games.get(id) + 1 : 1)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (item.request.headers.Referer[0].includes('/game/')) {
|
if (referer.includes('/game/')) {
|
||||||
游戏.set(id, 游戏.has(id) ? 游戏.get(id) + 1 : 1)
|
games.set(id, games.has(id) ? games.get(id) + 1 : 1)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
console.log('未处理 Referer:', item.request.headers.Referer)
|
console.log('未处理的 Referer:', referer) // 打印未处理的 Referer
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 处理截图相关日志
|
||||||
if (item.request.uri.startsWith('/api/images')) {
|
if (item.request.uri.startsWith('/api/images')) {
|
||||||
if (item.request.uri.includes('page=')) {
|
if (item.request.uri.includes('page=')) {
|
||||||
return
|
return // 忽略分页请求
|
||||||
}
|
}
|
||||||
const [uri, id] = item.request.uri.match(imagesRegex) ?? []
|
const [uri, id] = item.request.uri.match(imagesRegex) ?? []
|
||||||
if (uri && id) {
|
if (uri && id) {
|
||||||
截图.set(id, 截图.has(id) ? 截图.get(id) + 1 : 1)
|
screenshots.set(id, screenshots.has(id) ? screenshots.get(id) + 1 : 1)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
tail.on('end', () => console.log('没有更多内容,结束读取'))
|
// 日志尾部事件监听
|
||||||
|
tail.on('end', () => console.log('没有更多内容,停止读取'))
|
||||||
tail.on('error', (error) => console.error('错误:', error))
|
tail.on('error', (error) => console.error('错误:', error))
|
||||||
|
|
||||||
|
// 捕获退出信号,确保数据存档
|
||||||
process.on('SIGINT', async () => {
|
process.on('SIGINT', async () => {
|
||||||
tail.unwatch()
|
tail.unwatch() // 停止监听日志
|
||||||
await 存档('退出前储存数据..')
|
await archive('退出前存档数据...')
|
||||||
process.exit()
|
process.exit()
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
import mysql from 'mysql2/promise'
|
import mysql from 'mysql2/promise'
|
||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
|
|
||||||
export async function updateDatabase(list, field) {
|
export async function update(list, field) {
|
||||||
const { mysql: config } = JSON.parse(fs.readFileSync('config.json', 'utf8'))
|
const { mysql: config } = JSON.parse(fs.readFileSync('config.json', 'utf8'))
|
||||||
const conn = await mysql.createConnection(config)
|
const conn = await mysql.createConnection(config)
|
||||||
|
|
Reference in New Issue
Block a user