()
| 242 | * 定期发送使用摘要数据 |
| 243 | */ |
| 244 | const scheduleSyncStats = () => { |
| 245 | // 每周发送一次摘要数据 |
| 246 | const ONE_WEEK = 7 * 24 * 60 * 60 * 1000; |
| 247 | setInterval(async () => { |
| 248 | // 发送工具使用排名 |
| 249 | const toolRanking = Object.entries(usageData.tools) |
| 250 | .sort((a, b) => b[1] - a[1]) |
| 251 | .slice(0, 5) |
| 252 | .map(([name, count]) => ({name, count})); |
| 253 | |
| 254 | sendToServer('usage_summary', { |
| 255 | top_tools: JSON.stringify(toolRanking) |
| 256 | }); |
| 257 | |
| 258 | // 清理过旧的日期数据(保留30天数据) |
| 259 | const now = new Date(); |
| 260 | const thirtyDaysAgo = new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000); |
| 261 | const thirtyDaysAgoStr = thirtyDaysAgo.toISOString().split('T')[0]; |
| 262 | |
| 263 | Object.keys(usageData.dailyUsage).forEach(date => { |
| 264 | if (date < thirtyDaysAgoStr) { |
| 265 | delete usageData.dailyUsage[date]; |
| 266 | } |
| 267 | }); |
| 268 | |
| 269 | // 保存清理后的数据 |
| 270 | await saveUsageData(); |
| 271 | }, ONE_WEEK); |
| 272 | }; |
| 273 | |
| 274 | /** |
| 275 | * 初始化统计模块 |
no test coverage detected