translate comment
This commit is contained in:
@@ -3,7 +3,7 @@ const Redis = require('ioredis');
|
||||
const fs = require('fs/promises');
|
||||
const path = require('path');
|
||||
|
||||
// Redis 客户端配置
|
||||
// Redis client configuration
|
||||
const redis = new Redis({
|
||||
host: 'localhost',
|
||||
port: 6379,
|
||||
@@ -15,16 +15,16 @@ async function exportTrackingData() {
|
||||
const fileName = `tracking-data-${timestamp}.txt`;
|
||||
const filePath = path.join(__dirname, '../logs', fileName);
|
||||
|
||||
// 创建输出内容
|
||||
// Create output content
|
||||
let output = '=== Tracking Data Export ===\n';
|
||||
output += `Generated at: ${new Date().toISOString()}\n\n`;
|
||||
|
||||
// 1. 获取所有来源
|
||||
// 1. Get all sources
|
||||
const sources = await redis.smembers('referrers:sources');
|
||||
output += '=== Referral Sources ===\n';
|
||||
output += sources.join(', ') + '\n\n';
|
||||
|
||||
// 2. 获取总计数
|
||||
// 2. Get total counts
|
||||
const totalCounts = await redis.hgetall('referrers:count');
|
||||
output += '=== Total Counts by Source ===\n';
|
||||
for (const [source, count] of Object.entries(totalCounts)) {
|
||||
@@ -32,21 +32,21 @@ async function exportTrackingData() {
|
||||
}
|
||||
output += '\n';
|
||||
|
||||
// 3. 获取每日统计数据并按日期排序
|
||||
// 3. Get daily statistics and sort by date
|
||||
output += '=== Daily Statistics ===\n';
|
||||
const dailyKeys = await redis.keys('referrers:daily:*');
|
||||
|
||||
// 将 key 转换为包含日期对象的数组,并按日期排序
|
||||
// Convert keys to an array of objects with dates and sort by date
|
||||
const dailyData = await Promise.all(dailyKeys.map(async (key) => {
|
||||
const date = key.split(':')[2];
|
||||
const dailyStats = await redis.hgetall(key);
|
||||
return { date: new Date(date), data: dailyStats };
|
||||
}));
|
||||
|
||||
dailyData.sort((a, b) => b.date.getTime() - a.date.getTime()); // 按日期降序排序(最近到最远)
|
||||
dailyData.sort((a, b) => b.date.getTime() - a.date.getTime()); // Sort by date in descending order (most recent to oldest)
|
||||
|
||||
for (const item of dailyData) {
|
||||
const dateString = item.date.toISOString().split('T')[0]; //日期格式化字符串
|
||||
const dateString = item.date.toISOString().split('T')[0]; // Format date string
|
||||
output += `\nDate: ${dateString}\n`;
|
||||
for (const [source, count] of Object.entries(item.data)) {
|
||||
output += ` ${source}: ${count}\n`;
|
||||
@@ -55,14 +55,14 @@ async function exportTrackingData() {
|
||||
|
||||
output += '\n';
|
||||
|
||||
// 5. 添加基本统计信息
|
||||
// 5. Add basic statistics
|
||||
output += '\n=== Summary ===\n';
|
||||
output += `Total Sources: ${sources.length}\n`;
|
||||
|
||||
// 确保日志目录存在
|
||||
// Ensure the log directory exists
|
||||
await fs.mkdir(path.join(__dirname, '../logs'), { recursive: true });
|
||||
|
||||
// 写入文件
|
||||
// Write to file
|
||||
await fs.writeFile(filePath, output, 'utf8');
|
||||
|
||||
console.log(`Data exported successfully to: ${filePath}`);
|
||||
@@ -71,7 +71,7 @@ async function exportTrackingData() {
|
||||
console.log(output.slice(0, 500) + '...');
|
||||
console.log('='.repeat(50));
|
||||
|
||||
// 关闭 Redis 连接
|
||||
// Close Redis connection
|
||||
await redis.quit();
|
||||
|
||||
} catch (error) {
|
||||
@@ -80,5 +80,5 @@ async function exportTrackingData() {
|
||||
}
|
||||
}
|
||||
|
||||
// 执行导出
|
||||
// Execute export
|
||||
exportTrackingData();
|
||||
|
||||
Reference in New Issue
Block a user