修正统计

This commit is contained in:
2024-12-16 06:26:36 +08:00
parent c8a01cdf7e
commit d0602c4bcf
2 changed files with 88 additions and 62 deletions

89
main.js
View File

@@ -3,13 +3,33 @@ import { Tail } from 'tail'
import { update } from './update.js'
const articles = new Map() // 文章统计
const screenshots = new Map() // 截图统计
const collections = new Map() // 收藏统计
class 计数 {
= new Map()
= new Map()
= new Map()
= new Map()
}
class 类型 {
浏览数 = new 计数()
评论数 = new 计数()
点赞数 = new 计数()
收藏数 = new 计数()
}
const 文章 = new 类型()
const 作品 = new 类型()
const 游戏 = new 类型()
const 截图 = new 类型()
const 收藏 = new 类型()
const 综合 = new 类型()
// 日期格式化工具
const dateFormat = new Intl.DateTimeFormat('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit' })
const 日期格式 = new Intl.DateTimeFormat('zh-CN', {
year: 'numeric',
month: '2-digit',
day: '2-digit'
})
// 匹配文章和截图的正则表达式
@@ -18,43 +38,42 @@ 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 // 是否正在存档标记
var 当前日期 = 日期格式.format(new Date()).replace(/\//g, '-')
var 正在存档 = false
// 存档函数:将数据存入文件
const archive = async (message = '正在存档...') => {
if (isArchiving) return // 防止重复存档
isArchiving = true
const 存档 = async (message = '正在存档...') => {
if (正在存档) return
正在存档 = 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),
await fs.promises.writeFile(`./data/${当前日期}.json`, JSON.stringify({
文章: Object.fromEntries(文章),
截图: Object.fromEntries(截图),
收藏: Object.fromEntries(收藏),
}, null, 2))
isArchiving = false
正在存档 = false
}
// 主函数
export default function () {
if (fs.existsSync(`./data/${currentDate}.json`)) {
console.log(currentDate, '启动时加载当日数据...')
const file = fs.readFileSync(`./data/${currentDate}.json`, 'utf8')
if (fs.existsSync(`./data/${当前日期}.json`)) {
console.log(当前日期, '启动时加载当日数据...')
const file = fs.readFileSync(`./data/${当前日期}.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))
Object.entries(data.文章).forEach(([key, value]) => 文章.浏览数..set(key, value))
Object.entries(data.截图).forEach(([key, value]) => 截图.浏览数..set(key, value))
Object.entries(data.收藏).forEach(([key, value]) => 收藏.浏览数..set(key, value))
}
}
console.log(currentDate, '开始收集日志...')
console.log(当前日期, '开始收集日志...')
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)))
存档('每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)
// 实时读取日志文件
@@ -65,13 +84,13 @@ export default function () {
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
const logDate = 日期格式.format(new Date(item.ts * 1000)).replace(/\//g, '-')
if (当前日期 !== logDate) {
await 存档('跨日期存档...')
文章.浏览数..clear()
截图.浏览数..clear()
收藏.浏览数..clear()
当前日期 = logDate
}
// 处理文章日志