From 510b60f87021f6b4c371f394d1a75443167bf3c4 Mon Sep 17 00:00:00 2001 From: david_bai Date: Fri, 23 May 2025 20:47:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=B8=AA=E5=AF=BC?= =?UTF-8?q?=E5=87=BA=E8=AE=BF=E9=97=AE=E6=AC=A1=E6=95=B0=E7=9A=84=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/scripts/export-tracking-data.js | 84 +++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 backend/scripts/export-tracking-data.js diff --git a/backend/scripts/export-tracking-data.js b/backend/scripts/export-tracking-data.js new file mode 100644 index 0000000..1621018 --- /dev/null +++ b/backend/scripts/export-tracking-data.js @@ -0,0 +1,84 @@ +// scripts/export-tracking-data.js +const Redis = require('ioredis'); +const fs = require('fs/promises'); +const path = require('path'); + +// Redis 客户端配置 +const redis = new Redis({ + host: 'localhost', + port: 6379, +}); + +async function exportTrackingData() { + try { + const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); + const fileName = `tracking-data-${timestamp}.txt`; + const filePath = path.join(__dirname, '../logs', fileName); + + // 创建输出内容 + let output = '=== Tracking Data Export ===\n'; + output += `Generated at: ${new Date().toISOString()}\n\n`; + + // 1. 获取所有来源 + const sources = await redis.smembers('referrers:sources'); + output += '=== Referral Sources ===\n'; + output += sources.join(', ') + '\n\n'; + + // 2. 获取总计数 + const totalCounts = await redis.hgetall('referrers:count'); + output += '=== Total Counts by Source ===\n'; + for (const [source, count] of Object.entries(totalCounts)) { + output += `${source}: ${count}\n`; + } + output += '\n'; + + // 3. 获取每日统计数据并按日期排序 + output += '=== Daily Statistics ===\n'; + const dailyKeys = await redis.keys('referrers:daily:*'); + + // 将 key 转换为包含日期对象的数组,并按日期排序 + 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()); // 按日期降序排序(最近到最远) + + for (const item of dailyData) { + const dateString = item.date.toISOString().split('T')[0]; //日期格式化字符串 + output += `\nDate: ${dateString}\n`; + for (const [source, count] of Object.entries(item.data)) { + output += ` ${source}: ${count}\n`; + } + } + + output += '\n'; + + // 5. 添加基本统计信息 + output += '\n=== Summary ===\n'; + output += `Total Sources: ${sources.length}\n`; + + // 确保日志目录存在 + await fs.mkdir(path.join(__dirname, '../logs'), { recursive: true }); + + // 写入文件 + await fs.writeFile(filePath, output, 'utf8'); + + console.log(`Data exported successfully to: ${filePath}`); + console.log('\nFile Preview:'); + console.log('='.repeat(50)); + console.log(output.slice(0, 500) + '...'); + console.log('='.repeat(50)); + + // 关闭 Redis 连接 + await redis.quit(); + + } catch (error) { + console.error('Error exporting data:', error); + process.exit(1); + } +} + +// 执行导出 +exportTrackingData();