UserLogCollectorManager.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. namespace App\Module\Game\Logics;
  3. use App\Module\Game\Logics\UserLogCollectors\BaseLogCollector;
  4. use App\Module\Game\Logics\UserLogCollectors\FundLogCollector;
  5. use App\Module\Game\Logics\UserLogCollectors\ItemLogCollector;
  6. use App\Module\Game\Logics\UserLogCollectors\FarmHarvestLogCollector;
  7. use App\Module\Game\Logics\UserLogCollectors\FarmUpgradeLogCollector;
  8. use App\Module\Game\Logics\UserLogCollectors\PointLogCollector;
  9. use Illuminate\Support\Facades\Log;
  10. /**
  11. * 用户日志收集管理器
  12. *
  13. * 管理所有的日志收集器,协调日志收集工作
  14. */
  15. class UserLogCollectorManager
  16. {
  17. /**
  18. * 注册的收集器列表
  19. *
  20. * @var array
  21. */
  22. private array $collectors = [];
  23. /**
  24. * 构造函数
  25. */
  26. public function __construct()
  27. {
  28. $this->registerCollectors();
  29. }
  30. /**
  31. * 注册所有收集器
  32. *
  33. * @return void
  34. */
  35. private function registerCollectors(): void
  36. {
  37. $this->collectors = [
  38. 'fund' => new FundLogCollector(),
  39. 'item' => new ItemLogCollector(),
  40. 'farm_harvest' => new FarmHarvestLogCollector(),
  41. 'farm_upgrade' => new FarmUpgradeLogCollector(),
  42. 'point' => new PointLogCollector(),
  43. // 可以在这里添加更多收集器
  44. ];
  45. }
  46. /**
  47. * 执行所有收集器的日志收集
  48. *
  49. * @param int|null $limit 单次处理最大记录数限制
  50. * @return array 收集结果统计
  51. */
  52. public function collectAll(?int $limit = null): array
  53. {
  54. $results = [];
  55. $totalProcessed = 0;
  56. $startTime = microtime(true);
  57. Log::info("开始执行用户日志收集", [
  58. 'collectors_count' => count($this->collectors)
  59. ]);
  60. foreach ($this->collectors as $name => $collector) {
  61. try {
  62. // 如果指定了限制,设置收集器的最大记录数
  63. if ($limit !== null) {
  64. $collector->setMaxRecords($limit);
  65. }
  66. $collectorStartTime = microtime(true);
  67. $processedCount = $collector->collect();
  68. $collectorEndTime = microtime(true);
  69. $results[$name] = [
  70. 'processed_count' => $processedCount,
  71. 'execution_time' => round(($collectorEndTime - $collectorStartTime) * 1000, 2), // 毫秒
  72. 'status' => 'success'
  73. ];
  74. $totalProcessed += $processedCount;
  75. Log::info("收集器执行完成", [
  76. 'collector' => $name,
  77. 'processed_count' => $processedCount,
  78. 'execution_time_ms' => $results[$name]['execution_time']
  79. ]);
  80. } catch (\Exception $e) {
  81. $results[$name] = [
  82. 'processed_count' => 0,
  83. 'execution_time' => 0,
  84. 'status' => 'error',
  85. 'error' => $e->getMessage()
  86. ];
  87. Log::error("收集器执行失败", [
  88. 'collector' => $name,
  89. 'error' => $e->getMessage(),
  90. 'trace' => $e->getTraceAsString()
  91. ]);
  92. }
  93. }
  94. $endTime = microtime(true);
  95. $totalExecutionTime = round(($endTime - $startTime) * 1000, 2);
  96. $summary = [
  97. 'total_processed' => $totalProcessed,
  98. 'total_execution_time' => $totalExecutionTime,
  99. 'collectors' => $results,
  100. 'timestamp' => now()->toDateTimeString()
  101. ];
  102. Log::info("用户日志收集完成", $summary);
  103. return $summary;
  104. }
  105. /**
  106. * 执行指定收集器的日志收集
  107. *
  108. * @param string $collectorName 收集器名称
  109. * @return array 收集结果
  110. */
  111. public function collectByName(string $collectorName): array
  112. {
  113. if (!isset($this->collectors[$collectorName])) {
  114. throw new \InvalidArgumentException("收集器 {$collectorName} 不存在");
  115. }
  116. $collector = $this->collectors[$collectorName];
  117. $startTime = microtime(true);
  118. try {
  119. $processedCount = $collector->collect();
  120. $endTime = microtime(true);
  121. return [
  122. 'collector' => $collectorName,
  123. 'processed_count' => $processedCount,
  124. 'execution_time' => round(($endTime - $startTime) * 1000, 2),
  125. 'status' => 'success',
  126. 'timestamp' => now()->toDateTimeString()
  127. ];
  128. } catch (\Exception $e) {
  129. $endTime = microtime(true);
  130. return [
  131. 'collector' => $collectorName,
  132. 'processed_count' => 0,
  133. 'execution_time' => round(($endTime - $startTime) * 1000, 2),
  134. 'status' => 'error',
  135. 'error' => $e->getMessage(),
  136. 'timestamp' => now()->toDateTimeString()
  137. ];
  138. }
  139. }
  140. /**
  141. * 获取所有收集器的信息
  142. *
  143. * @return array
  144. */
  145. public function getCollectorsInfo(): array
  146. {
  147. $info = [];
  148. foreach ($this->collectors as $name => $collector) {
  149. $info[$name] = [
  150. 'name' => $name,
  151. 'class' => get_class($collector),
  152. 'source_table' => $collector->getSourceTable(),
  153. 'source_type' => $collector->getSourceType(),
  154. ];
  155. }
  156. return $info;
  157. }
  158. // 注意:基于ID的进度追踪机制不需要重置功能
  159. // resetCollector方法已移除,因为:
  160. // 1. BaseLogCollector中没有resetAllProgress方法
  161. // 2. 基于ID的进度追踪是递增的,无需重置
  162. // 3. 进度通过user_logs表中的source_id自动维护
  163. /**
  164. * 添加自定义收集器
  165. *
  166. * @param string $name 收集器名称
  167. * @param BaseLogCollector $collector 收集器实例
  168. * @return void
  169. */
  170. public function addCollector(string $name, BaseLogCollector $collector): void
  171. {
  172. $this->collectors[$name] = $collector;
  173. Log::info("添加自定义收集器", [
  174. 'name' => $name,
  175. 'class' => get_class($collector)
  176. ]);
  177. }
  178. /**
  179. * 移除收集器
  180. *
  181. * @param string $name 收集器名称
  182. * @return void
  183. */
  184. public function removeCollector(string $name): void
  185. {
  186. if (isset($this->collectors[$name])) {
  187. unset($this->collectors[$name]);
  188. Log::info("移除收集器", [
  189. 'name' => $name
  190. ]);
  191. }
  192. }
  193. /**
  194. * 获取收集器实例
  195. *
  196. * @param string $name 收集器名称
  197. * @return BaseLogCollector|null
  198. */
  199. public function getCollector(string $name): ?BaseLogCollector
  200. {
  201. return $this->collectors[$name] ?? null;
  202. }
  203. /**
  204. * 检查收集器是否存在
  205. *
  206. * @param string $name 收集器名称
  207. * @return bool
  208. */
  209. public function hasCollector(string $name): bool
  210. {
  211. return isset($this->collectors[$name]);
  212. }
  213. }