CollectUserLogsCommand.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace App\Module\Game\Commands;
  3. use App\Module\Game\Logics\UserLogCollectorManager;
  4. use UCore\Command\Command;
  5. /**
  6. * 用户日志收集命令
  7. *
  8. * 定时执行的计划任务,每2秒收集一次用户日志
  9. */
  10. class CollectUserLogsCommand extends Command
  11. {
  12. /**
  13. * 命令名称和参数
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'game:collect-user-logs
  18. {--collector= : 指定收集器名称,不指定则执行所有收集器}
  19. {--reset : 重置收集器进度,从头开始收集}
  20. {--info : 显示收集器信息}
  21. {--stats : 显示收集统计信息}';
  22. /**
  23. * 命令描述
  24. *
  25. * @var string
  26. */
  27. protected $description = '收集用户日志,将各模块的原始日志转换为用户友好的日志消息';
  28. /**
  29. * 执行命令
  30. */
  31. public function handleRun()
  32. {
  33. $manager = new UserLogCollectorManager();
  34. // 显示收集器信息
  35. if ($this->option('info')) {
  36. $this->showCollectorsInfo($manager);
  37. return 0;
  38. }
  39. // 重置收集器进度
  40. if ($this->option('reset')) {
  41. $this->resetCollectors($manager);
  42. return 0;
  43. }
  44. // 显示统计信息
  45. if ($this->option('stats')) {
  46. $this->showStats($manager);
  47. return 0;
  48. }
  49. // 执行日志收集
  50. return $this->executeCollection($manager);
  51. }
  52. /**
  53. * 执行日志收集
  54. *
  55. * @param UserLogCollectorManager $manager
  56. * @return int
  57. */
  58. private function executeCollection(UserLogCollectorManager $manager): int
  59. {
  60. $collectorName = $this->option('collector');
  61. try {
  62. if ($collectorName) {
  63. // 执行指定收集器
  64. $this->info("执行收集器: {$collectorName}");
  65. $result = $manager->collectByName($collectorName);
  66. $this->displaySingleResult($result);
  67. } else {
  68. // 执行所有收集器
  69. $this->info("执行所有收集器...");
  70. $results = $manager->collectAll();
  71. $this->displayAllResults($results);
  72. }
  73. return 0;
  74. } catch (\Exception $e) {
  75. $this->error("日志收集失败: {$e->getMessage()}");
  76. return 1;
  77. }
  78. }
  79. /**
  80. * 显示收集器信息
  81. *
  82. * @param UserLogCollectorManager $manager
  83. * @return void
  84. */
  85. private function showCollectorsInfo(UserLogCollectorManager $manager): void
  86. {
  87. $this->info("注册的收集器信息:");
  88. $this->line("");
  89. $collectorsInfo = $manager->getCollectorsInfo();
  90. foreach ($collectorsInfo as $info) {
  91. $this->line("收集器: <comment>{$info['name']}</comment>");
  92. $this->line(" 类名: {$info['class']}");
  93. $this->line(" 源表: {$info['source_table']}");
  94. $this->line(" 类型: {$info['source_type']}");
  95. $this->line("");
  96. }
  97. }
  98. /**
  99. * 重置收集器进度
  100. *
  101. * @param UserLogCollectorManager $manager
  102. * @return void
  103. */
  104. private function resetCollectors(UserLogCollectorManager $manager): void
  105. {
  106. $collectorName = $this->option('collector');
  107. if ($collectorName) {
  108. if (!$manager->hasCollector($collectorName)) {
  109. $this->error("收集器 {$collectorName} 不存在");
  110. return;
  111. }
  112. $manager->resetCollector($collectorName);
  113. $this->info("已重置收集器 {$collectorName} 的进度");
  114. } else {
  115. if ($this->confirm('确定要重置所有收集器的进度吗?这将从头开始收集所有日志。')) {
  116. $manager->resetAllCollectors();
  117. $this->info("已重置所有收集器的进度");
  118. }
  119. }
  120. }
  121. /**
  122. * 显示统计信息
  123. *
  124. * @param UserLogCollectorManager $manager
  125. * @return void
  126. */
  127. private function showStats(UserLogCollectorManager $manager): void
  128. {
  129. $this->info("收集器统计信息:");
  130. $this->line("");
  131. // 这里可以添加更详细的统计信息
  132. // 比如每个收集器的处理进度、最后处理时间等
  133. $collectorsInfo = $manager->getCollectorsInfo();
  134. foreach ($collectorsInfo as $info) {
  135. $this->line("收集器: <comment>{$info['name']}</comment>");
  136. // 获取最后处理的ID
  137. $cacheKey = "user_log_collector:last_processed_id:{$info['source_table']}";
  138. $lastProcessedId = \Illuminate\Support\Facades\Cache::get($cacheKey, 0);
  139. $this->line(" 最后处理ID: {$lastProcessedId}");
  140. $this->line("");
  141. }
  142. }
  143. /**
  144. * 显示单个收集器结果
  145. *
  146. * @param array $result
  147. * @return void
  148. */
  149. private function displaySingleResult(array $result): void
  150. {
  151. if ($result['status'] === 'success') {
  152. $this->info("收集完成:");
  153. $this->line(" 处理记录数: {$result['processed_count']}");
  154. $this->line(" 执行时间: {$result['execution_time']}ms");
  155. } else {
  156. $this->error("收集失败:");
  157. $this->line(" 错误信息: {$result['error']}");
  158. }
  159. }
  160. /**
  161. * 显示所有收集器结果
  162. *
  163. * @param array $results
  164. * @return void
  165. */
  166. private function displayAllResults(array $results): void
  167. {
  168. $this->info("收集完成:");
  169. $this->line(" 总处理记录数: {$results['total_processed']}");
  170. $this->line(" 总执行时间: {$results['total_execution_time']}ms");
  171. $this->line("");
  172. $this->info("各收集器详情:");
  173. foreach ($results['collectors'] as $name => $result) {
  174. $status = $result['status'] === 'success' ? '<info>成功</info>' : '<error>失败</error>';
  175. $this->line(" {$name}: {$status} - 处理{$result['processed_count']}条 - {$result['execution_time']}ms");
  176. if ($result['status'] === 'error') {
  177. $this->line(" 错误: {$result['error']}");
  178. }
  179. }
  180. }
  181. }