CollectUserLogsCommand.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. <?php
  2. namespace App\Module\Game\Commands;
  3. use App\Module\Game\Logics\UserLogCollectorManager;
  4. use App\Module\Game\Services\GameConfigService;
  5. use UCore\Command\Command;
  6. use UCore\Exception\LogicException;
  7. /**
  8. * 用户日志收集命令
  9. *
  10. * 定时执行的计划任务,每2秒收集一次用户日志
  11. */
  12. class CollectUserLogsCommand extends Command
  13. {
  14. /**
  15. * 命令名称和参数
  16. *
  17. * @var string
  18. */
  19. protected $signature = 'game:collect-user-logs {--info : 显示收集器信息} {--statistics : 显示收集统计信息} {--detail : 显示详细的处理过程} {--limit=1000 : 单次处理最大记录数} {--force : 强制执行,忽略自动收集配置}';
  20. /**
  21. * 命令描述
  22. *
  23. * @var string
  24. */
  25. protected $description = '收集用户日志,将各模块的原始日志转换为用户友好的日志消息
  26. 选项说明:
  27. --info 显示收集器信息
  28. --statistics 显示收集统计信息
  29. --detail 显示详细的处理过程
  30. --limit 单次处理最大记录数(默认1000)
  31. --force 强制执行,忽略自动收集配置
  32. 注意:进度追踪基于user_logs表中的已处理记录,系统会自动从上次处理位置继续收集';
  33. /**
  34. * 执行命令
  35. */
  36. public function handleRun()
  37. {
  38. // 检查是否允许自动收集日志(除非强制执行)
  39. if (!$this->option('force') && !$this->isAutoCollectEnabled()) {
  40. $this->warn("⚠️ 自动收集日志功能已禁用");
  41. $this->line("💡 如需强制执行,请使用 --force 选项");
  42. return 0;
  43. }
  44. // 显示统计信息
  45. if ($this->option('statistics')) {
  46. $manager = new UserLogCollectorManager();
  47. $collectorsInfo = $manager->getCollectorsInfo();
  48. $this->info("📊 收集器统计信息:");
  49. $this->line("");
  50. $this->line("收集器数量: " . count($collectorsInfo));
  51. // 动态显示所有收集器
  52. foreach ($collectorsInfo as $name => $info) {
  53. $icon = $this->getCollectorIcon($info['source_type']);
  54. $description = $this->getCollectorNameDescription($name);
  55. $this->line("- {$name}: {$icon} {$description}");
  56. }
  57. $this->line("");
  58. // 显示用户日志表统计
  59. try {
  60. $totalUserLogs = \App\Module\Game\Models\UserLog::count();
  61. $this->line("📋 用户日志表统计:");
  62. $this->line(" 📝 总日志数: {$totalUserLogs}");
  63. if ($totalUserLogs > 0) {
  64. $latestLog = \App\Module\Game\Models\UserLog::orderBy('created_at', 'desc')->first();
  65. $oldestLog = \App\Module\Game\Models\UserLog::orderBy('created_at', 'asc')->first();
  66. $this->line(" 🕐 最新日志时间: " . $latestLog->created_at);
  67. $this->line(" 🕐 最旧日志时间: " . $oldestLog->created_at);
  68. // 按类型统计 - 动态获取所有收集器类型
  69. $this->line(" 📊 按类型统计:");
  70. foreach ($collectorsInfo as $name => $info) {
  71. $sourceType = $info['source_type'];
  72. $count = \App\Module\Game\Models\UserLog::where('source_type', $sourceType)->count();
  73. $icon = $this->getCollectorIcon($sourceType);
  74. $description = $this->getCollectorDescription($sourceType);
  75. $this->line(" {$icon} {$description}: {$count}");
  76. }
  77. }
  78. } catch (\Exception $e) {
  79. $this->line(" ⚠️ 无法获取用户日志统计: " . $e->getMessage());
  80. }
  81. $this->line("");
  82. return 0;
  83. }
  84. $manager = new UserLogCollectorManager();
  85. // 显示收集器信息
  86. if ($this->option('info')) {
  87. $this->showCollectorsInfo($manager);
  88. return 0;
  89. }
  90. // 执行日志收集
  91. return $this->executeCollection($manager);
  92. }
  93. /**
  94. * 执行日志收集
  95. *
  96. * @param UserLogCollectorManager $manager
  97. * @return int
  98. */
  99. private function executeCollection(UserLogCollectorManager $manager): int
  100. {
  101. $detail = $this->option('detail');
  102. $limit = (int)$this->option('limit');
  103. $force = $this->option('force');
  104. try {
  105. if ($force) {
  106. $this->info("🚀 强制执行用户日志收集...");
  107. $this->line("⚡ 忽略自动收集配置,强制执行收集任务");
  108. } else {
  109. $this->info("🚀 开始按时间线收集用户日志...");
  110. }
  111. if ($detail) {
  112. $this->showTimelineProgress();
  113. }
  114. $result = $this->executeTimelineCollection($manager, $limit, $detail);
  115. $this->displayTimelineResult($result);
  116. return 0;
  117. } catch (\Exception $e) {
  118. $this->error("❌ 日志收集失败: {$e->getMessage()}");
  119. return 1;
  120. }
  121. }
  122. /**
  123. * 从user_logs表获取最后处理的时间戳
  124. *
  125. * @param string $sourceTable
  126. * @param string $sourceType
  127. * @return int
  128. */
  129. private function getLastProcessedTimestampFromUserLogs(string $sourceTable, string $sourceType): int
  130. {
  131. try {
  132. $lastLog = \App\Module\Game\Models\UserLog::where('source_table', $sourceTable)
  133. ->where('source_type', $sourceType)
  134. ->orderBy('created_at', 'desc')
  135. ->first();
  136. if (!$lastLog) {
  137. return 0;
  138. }
  139. // 返回user_logs记录的创建时间戳
  140. return strtotime($lastLog->created_at);
  141. } catch (\Exception $e) {
  142. return 0;
  143. }
  144. }
  145. /**
  146. * 显示收集器信息
  147. *
  148. * @param UserLogCollectorManager $manager
  149. * @return void
  150. */
  151. private function showCollectorsInfo(UserLogCollectorManager $manager): void
  152. {
  153. $this->info("📋 注册的收集器信息:");
  154. $this->line("");
  155. $collectorsInfo = $manager->getCollectorsInfo();
  156. foreach ($collectorsInfo as $info) {
  157. $this->line("收集器: <comment>{$info['name']}</comment>");
  158. $this->line(" 类名: {$info['class']}");
  159. $this->line(" 源表: <info>{$info['source_table']}</info>");
  160. $this->line(" 类型: <info>{$info['source_type']}</info>");
  161. $this->line("");
  162. }
  163. }
  164. /**
  165. * 显示统计信息
  166. *
  167. * @param UserLogCollectorManager $manager
  168. * @return void
  169. */
  170. private function showStats(UserLogCollectorManager $manager): void
  171. {
  172. $collectorsInfo = $manager->getCollectorsInfo();
  173. $this->line("");
  174. $this->line("收集器数量: " . count($collectorsInfo));
  175. // 动态显示所有收集器
  176. foreach ($collectorsInfo as $name => $info) {
  177. $icon = $this->getCollectorIcon($info['source_type']);
  178. $description = $this->getCollectorNameDescription($name);
  179. $this->line("- {$name}: {$icon} {$description}");
  180. }
  181. $this->line("");
  182. }
  183. /**
  184. * 获取收集器图标
  185. *
  186. * @param string $sourceType
  187. * @return string
  188. */
  189. private function getCollectorIcon(string $sourceType): string
  190. {
  191. $icons = [
  192. 'fund' => '💰',
  193. 'item' => '📦',
  194. 'farm' => '🌾',
  195. 'point' => '⭐',
  196. 'pet' => '🐾',
  197. 'system' => '⚙️',
  198. ];
  199. return $icons[$sourceType] ?? '📄';
  200. }
  201. /**
  202. * 获取收集器描述
  203. *
  204. * @param string $sourceType
  205. * @return string
  206. */
  207. private function getCollectorDescription(string $sourceType): string
  208. {
  209. $descriptions = [
  210. 'fund' => '资金日志收集器',
  211. 'item' => '物品日志收集器',
  212. 'farm' => '农场日志收集器',
  213. 'point' => '积分日志收集器',
  214. 'pet' => '宠物日志收集器',
  215. 'system' => '系统日志收集器',
  216. ];
  217. return $descriptions[$sourceType] ?? '未知类型收集器';
  218. }
  219. /**
  220. * 获取收集器名称描述(基于收集器名称而非源类型)
  221. *
  222. * @param string $collectorName
  223. * @return string
  224. */
  225. private function getCollectorNameDescription(string $collectorName): string
  226. {
  227. $descriptions = [
  228. 'fund' => '资金日志收集器',
  229. 'item' => '物品日志收集器',
  230. 'farm_harvest' => '农场收获日志收集器',
  231. 'farm_upgrade' => '农场升级日志收集器',
  232. 'point' => '积分日志收集器',
  233. 'pet' => '宠物日志收集器',
  234. 'system' => '系统日志收集器',
  235. ];
  236. return $descriptions[$collectorName] ?? '未知收集器';
  237. }
  238. /**
  239. * 获取表的记录总数
  240. *
  241. * @param string $tableName
  242. * @return int
  243. */
  244. private function getTableRecordCount(string $tableName): int
  245. {
  246. try {
  247. // 根据表名使用对应的模型
  248. switch ($tableName) {
  249. case 'fund_logs':
  250. return \App\Module\Fund\Models\FundLogModel::count();
  251. case 'item_transaction_logs':
  252. return \App\Module\GameItems\Models\ItemTransactionLog::count();
  253. case 'farm_harvest_logs':
  254. return \App\Module\Farm\Models\FarmHarvestLog::count();
  255. case 'farm_upgrade_logs':
  256. return \App\Module\Farm\Models\FarmUpgradeLog::count();
  257. case 'point_logs':
  258. return \App\Module\Point\Models\PointLogModel::count();
  259. default:
  260. // 回退到直接查询
  261. return \Illuminate\Support\Facades\DB::table($tableName)->count();
  262. }
  263. } catch (\Exception $e) {
  264. return 0;
  265. }
  266. }
  267. /**
  268. * 显示进度条
  269. *
  270. * @param float $percent
  271. * @return void
  272. */
  273. private function displayProgressBar(float $percent): void
  274. {
  275. $barLength = 20;
  276. $filledLength = (int)round(($percent / 100) * $barLength);
  277. $emptyLength = $barLength - $filledLength;
  278. $bar = str_repeat('█', $filledLength) . str_repeat('░', $emptyLength);
  279. $this->line(" 📊 [{$bar}] {$percent}%");
  280. }
  281. /**
  282. * 执行时间线收集
  283. * 改为让每个收集器独立处理,避免全局时间戳的复杂性
  284. *
  285. * @param UserLogCollectorManager $manager
  286. * @param int $limit
  287. * @param bool $detail
  288. * @return array
  289. */
  290. private function executeTimelineCollection(UserLogCollectorManager $manager, int $limit, bool $detail): array
  291. {
  292. $startTime = microtime(true);
  293. if ($detail) {
  294. $this->line("📊 执行各收集器的日志收集...");
  295. }
  296. // 直接执行所有收集器,传递限制参数
  297. $results = $manager->collectAll($limit);
  298. if ($results['total_processed'] == 0) {
  299. return [
  300. 'processed_count' => 0,
  301. 'execution_time' => round((microtime(true) - $startTime) * 1000, 2),
  302. 'status' => 'success',
  303. 'message' => '没有新记录需要处理',
  304. 'timestamp' => now()->toDateTimeString()
  305. ];
  306. }
  307. if ($detail) {
  308. $this->line("📝 开始处理各收集器...");
  309. foreach ($results['collectors'] as $collectorName => $result) {
  310. $this->line(" {$collectorName}: 处理了 {$result['processed_count']} 条记录");
  311. }
  312. }
  313. $endTime = microtime(true);
  314. return [
  315. 'processed_count' => $results['total_processed'],
  316. 'execution_time' => round(($endTime - $startTime) * 1000, 2),
  317. 'status' => 'success',
  318. 'timestamp' => now()->toDateTimeString(),
  319. 'details' => $results['collectors']
  320. ];
  321. }
  322. /**
  323. * 从所有收集器获取按时间线排序的记录
  324. *
  325. * @param UserLogCollectorManager $manager
  326. * @param int $lastTimestamp
  327. * @param int $limit
  328. * @return array
  329. */
  330. private function getAllRecordsByTimeline(UserLogCollectorManager $manager, int $lastTimestamp, int $limit): array
  331. {
  332. $allRecords = [];
  333. $collectorsInfo = $manager->getCollectorsInfo();
  334. foreach ($collectorsInfo as $name => $info) {
  335. $collector = $manager->getCollector($name);
  336. if (!$collector) continue;
  337. try {
  338. $records = $collector->getNewRecordsByTimePublic($lastTimestamp);
  339. foreach ($records as $record) {
  340. $timestamp = $collector->getRecordTimestampPublic($record);
  341. $allRecords[] = [
  342. 'id' => $record->id,
  343. 'timestamp' => $timestamp,
  344. 'collector' => $collector,
  345. 'record' => $record,
  346. 'source_type' => $info['source_type']
  347. ];
  348. }
  349. } catch (\Exception $e) {
  350. \Illuminate\Support\Facades\Log::error("获取收集器记录失败", [
  351. 'collector' => $name,
  352. 'error' => $e->getMessage()
  353. ]);
  354. }
  355. }
  356. // 限制记录数量
  357. if (count($allRecords) > $limit) {
  358. // 先按时间排序,然后取前N条
  359. usort($allRecords, function($a, $b) {
  360. $timeA = $a['timestamp'];
  361. $timeB = $b['timestamp'];
  362. if ($timeA == $timeB) {
  363. return $a['id'] <=> $b['id'];
  364. }
  365. return $timeA <=> $timeB;
  366. });
  367. $allRecords = array_slice($allRecords, 0, $limit);
  368. }
  369. return $allRecords;
  370. }
  371. /**
  372. * 显示时间线进度
  373. *
  374. * @return void
  375. */
  376. private function showTimelineProgress(): void
  377. {
  378. $lastTimestamp = $this->getGlobalLastProcessedTimestamp();
  379. $this->line("📈 时间线处理进度:");
  380. $this->line(" 🕐 最后处理时间: <comment>" . ($lastTimestamp > 0 ? date('Y-m-d H:i:s', $lastTimestamp) : '未开始') . "</comment>");
  381. $this->line("");
  382. }
  383. /**
  384. * 显示时间线收集结果
  385. *
  386. * @param array $result
  387. * @return void
  388. */
  389. private function displayTimelineResult(array $result): void
  390. {
  391. $this->line("");
  392. if ($result['status'] === 'success') {
  393. $this->info("✅ 时间线收集完成!");
  394. $this->line("📊 <comment>处理统计</comment>:");
  395. $this->line(" 📝 处理记录数: <info>{$result['processed_count']}</info>");
  396. $this->line(" ⏱️ 执行时间: <info>{$result['execution_time']}ms</info>");
  397. $this->line(" 🕐 完成时间: <info>{$result['timestamp']}</info>");
  398. if (isset($result['last_timestamp'])) {
  399. $this->line(" 🎯 最新的处理时间: <info>" . date('Y-m-d H:i:s', $result['last_timestamp']) . "</info>");
  400. }
  401. if ($result['processed_count'] > 0) {
  402. $avgTime = round($result['execution_time'] / $result['processed_count'], 2);
  403. $this->line(" 📈 平均处理时间: <info>{$avgTime}ms/条</info>");
  404. }
  405. if (isset($result['message'])) {
  406. $this->line(" 💡 <comment>{$result['message']}</comment>");
  407. }
  408. } else {
  409. $this->error("❌ 时间线收集失败!");
  410. if (isset($result['error'])) {
  411. $this->line("🚨 <comment>错误信息</comment>:");
  412. $this->line(" {$result['error']}");
  413. }
  414. }
  415. $this->line("");
  416. }
  417. /**
  418. * 获取全局最后处理时间戳
  419. * 从各个收集器获取最后处理的原始记录时间戳,取最小值
  420. *
  421. * @return int
  422. */
  423. private function getGlobalLastProcessedTimestamp(): int
  424. {
  425. try {
  426. $manager = new \App\Module\Game\Logics\UserLogCollectorManager();
  427. $collectorsInfo = $manager->getCollectorsInfo();
  428. $minTimestamp = PHP_INT_MAX;
  429. $hasValidTimestamp = false;
  430. foreach ($collectorsInfo as $name => $info) {
  431. $collector = $manager->getCollector($name);
  432. if (!$collector) continue;
  433. // 获取该收集器最后处理的时间戳
  434. $lastProcessedTimestamp = $this->getLastProcessedTimestampFromUserLogs($info['source_table'], $info['source_type']);
  435. if ($lastProcessedTimestamp > 0) {
  436. $minTimestamp = min($minTimestamp, $lastProcessedTimestamp);
  437. $hasValidTimestamp = true;
  438. }
  439. }
  440. return $hasValidTimestamp ? $minTimestamp : 0;
  441. } catch (\Exception $e) {
  442. return 0;
  443. }
  444. }
  445. /**
  446. * 更新全局最后处理时间戳
  447. * 不再需要手动更新,因为进度通过user_logs表自动追踪
  448. *
  449. * @param int $timestamp
  450. * @return void
  451. */
  452. private function updateGlobalLastProcessedTimestamp(int $timestamp): void
  453. {
  454. // 不再需要手动更新,进度通过user_logs表自动追踪
  455. // 这个方法保留是为了兼容性
  456. }
  457. /**
  458. * 显示用户日志表统计
  459. *
  460. * @return void
  461. */
  462. private function showUserLogStats(): void
  463. {
  464. try {
  465. // 使用模型查询,避免表名前缀问题
  466. $totalUserLogs = \App\Module\Game\Models\UserLog::count();
  467. $todayUserLogs = \App\Module\Game\Models\UserLog::whereDate('created_at', now()->toDateString())->count();
  468. $this->line("📋 <comment>用户日志表统计</comment>:");
  469. $this->line(" 📝 总日志数: <info>{$totalUserLogs}</info>");
  470. $this->line(" 📅 今日新增: <info>{$todayUserLogs}</info>");
  471. // 按来源类型统计
  472. $sourceStats = \App\Module\Game\Models\UserLog::select('source_type', \Illuminate\Support\Facades\DB::raw('count(*) as count'))
  473. ->groupBy('source_type')
  474. ->get();
  475. if ($sourceStats->isNotEmpty()) {
  476. $this->line(" 📊 按来源类型统计:");
  477. foreach ($sourceStats as $stat) {
  478. $this->line(" {$stat->source_type}: <info>{$stat->count}</info>");
  479. }
  480. }
  481. } catch (\Exception $e) {
  482. $this->line(" ⚠️ 无法获取用户日志统计信息: " . $e->getMessage());
  483. }
  484. }
  485. /**
  486. * 显示单个收集器结果
  487. *
  488. * @param array $result
  489. * @return void
  490. */
  491. private function displaySingleResult(array $result): void
  492. {
  493. $this->line("");
  494. if ($result['status'] === 'success') {
  495. $this->info("✅ 收集完成!");
  496. $this->line("📊 <comment>处理统计</comment>:");
  497. $this->line(" 📝 处理记录数: <info>{$result['processed_count']}</info>");
  498. $this->line(" ⏱️ 执行时间: <info>{$result['execution_time']}ms</info>");
  499. $this->line(" 🕐 完成时间: <info>{$result['timestamp']}</info>");
  500. if ($result['processed_count'] > 0) {
  501. $avgTime = round($result['execution_time'] / $result['processed_count'], 2);
  502. $this->line(" 📈 平均处理时间: <info>{$avgTime}ms/条</info>");
  503. }
  504. } else {
  505. $this->error("❌ 收集失败!");
  506. $this->line("🚨 <comment>错误信息</comment>:");
  507. $this->line(" {$result['error']}");
  508. }
  509. $this->line("");
  510. }
  511. /**
  512. * 显示所有收集器结果
  513. *
  514. * @param array $results
  515. * @return void
  516. */
  517. private function displayAllResults(array $results): void
  518. {
  519. $this->line("");
  520. $this->info("🎉 所有收集器执行完成!");
  521. $this->line("");
  522. // 显示总体统计
  523. $this->line("📊 <comment>总体统计</comment>:");
  524. $this->line(" 📝 总处理记录数: <info>{$results['total_processed']}</info>");
  525. $this->line(" ⏱️ 总执行时间: <info>{$results['total_execution_time']}ms</info>");
  526. $this->line(" 🕐 完成时间: <info>{$results['timestamp']}</info>");
  527. if ($results['total_processed'] > 0) {
  528. $avgTime = round($results['total_execution_time'] / $results['total_processed'], 2);
  529. $this->line(" 📈 平均处理时间: <info>{$avgTime}ms/条</info>");
  530. }
  531. $this->line("");
  532. // 显示各收集器详情
  533. $this->line("📋 <comment>各收集器详情</comment>:");
  534. $successCount = 0;
  535. $failureCount = 0;
  536. foreach ($results['collectors'] as $name => $result) {
  537. if ($result['status'] === 'success') {
  538. $status = '<info>✅ 成功</info>';
  539. $successCount++;
  540. } else {
  541. $status = '<error>❌ 失败</error>';
  542. $failureCount++;
  543. }
  544. $this->line(" 🔧 <comment>{$name}</comment>: {$status}");
  545. $this->line(" 📝 处理记录: <info>{$result['processed_count']}</info> 条");
  546. $this->line(" ⏱️ 执行时间: <info>{$result['execution_time']}</info> ms");
  547. if ($result['status'] === 'error') {
  548. $this->line(" 🚨 错误信息: <error>{$result['error']}</error>");
  549. } elseif ($result['processed_count'] > 0) {
  550. $avgTime = round($result['execution_time'] / $result['processed_count'], 2);
  551. $this->line(" 📈 平均时间: <info>{$avgTime}</info> ms/条");
  552. }
  553. $this->line("");
  554. }
  555. // 显示执行摘要
  556. $totalCollectors = $successCount + $failureCount;
  557. $this->line("📈 <comment>执行摘要</comment>:");
  558. $this->line(" 🎯 成功收集器: <info>{$successCount}/{$totalCollectors}</info>");
  559. if ($failureCount > 0) {
  560. $this->line(" ⚠️ 失败收集器: <error>{$failureCount}/{$totalCollectors}</error>");
  561. }
  562. $this->line("");
  563. }
  564. /**
  565. * 检查是否允许自动收集日志
  566. *
  567. * @return bool
  568. */
  569. private function isAutoCollectEnabled(): bool
  570. {
  571. return GameConfigService::isAutoCollectEnabled();
  572. }
  573. }