registerCollectors(); } /** * 注册所有收集器 * * @return void */ private function registerCollectors(): void { $this->collectors = [ 'fund' => new FundLogCollector(), 'item' => new ItemLogCollector(), 'farm_harvest' => new FarmHarvestLogCollector(), 'farm_upgrade' => new FarmUpgradeLogCollector(), 'point' => new PointLogCollector(), // 可以在这里添加更多收集器 ]; } /** * 执行所有收集器的日志收集 * * @param int|null $limit 单次处理最大记录数限制 * @return array 收集结果统计 */ public function collectAll(?int $limit = null): array { $results = []; $totalProcessed = 0; $startTime = microtime(true); Log::info("开始执行用户日志收集", [ 'collectors_count' => count($this->collectors) ]); foreach ($this->collectors as $name => $collector) { try { // 检查收集器是否启用 if (!GameConfigService::isCollectorEnabled($name)) { $results[$name] = [ 'processed_count' => 0, 'execution_time' => 0, 'status' => 'disabled' ]; Log::info("收集器已禁用,跳过执行", [ 'collector' => $name ]); continue; } // 如果指定了限制,设置收集器的最大记录数 if ($limit !== null) { $collector->setMaxRecords($limit); } $collectorStartTime = microtime(true); $processedCount = $collector->collect(); $collectorEndTime = microtime(true); $results[$name] = [ 'processed_count' => $processedCount, 'execution_time' => round(($collectorEndTime - $collectorStartTime) * 1000, 2), // 毫秒 'status' => 'success' ]; $totalProcessed += $processedCount; Log::info("收集器执行完成", [ 'collector' => $name, 'processed_count' => $processedCount, 'execution_time_ms' => $results[$name]['execution_time'] ]); } catch (\Exception $e) { $results[$name] = [ 'processed_count' => 0, 'execution_time' => 0, 'status' => 'error', 'error' => $e->getMessage() ]; Log::error("收集器执行失败", [ 'collector' => $name, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); } } $endTime = microtime(true); $totalExecutionTime = round(($endTime - $startTime) * 1000, 2); $summary = [ 'total_processed' => $totalProcessed, 'total_execution_time' => $totalExecutionTime, 'collectors' => $results, 'timestamp' => now()->toDateTimeString() ]; Log::info("用户日志收集完成", $summary); return $summary; } /** * 执行指定收集器的日志收集 * * @param string $collectorName 收集器名称 * @return array 收集结果 */ public function collectByName(string $collectorName): array { if (!isset($this->collectors[$collectorName])) { throw new \InvalidArgumentException("收集器 {$collectorName} 不存在"); } // 检查收集器是否启用 if (!GameConfigService::isCollectorEnabled($collectorName)) { return [ 'collector' => $collectorName, 'processed_count' => 0, 'execution_time' => 0, 'status' => 'disabled', 'timestamp' => now()->toDateTimeString() ]; } $collector = $this->collectors[$collectorName]; $startTime = microtime(true); try { $processedCount = $collector->collect(); $endTime = microtime(true); return [ 'collector' => $collectorName, 'processed_count' => $processedCount, 'execution_time' => round(($endTime - $startTime) * 1000, 2), 'status' => 'success', 'timestamp' => now()->toDateTimeString() ]; } catch (\Exception $e) { $endTime = microtime(true); return [ 'collector' => $collectorName, 'processed_count' => 0, 'execution_time' => round(($endTime - $startTime) * 1000, 2), 'status' => 'error', 'error' => $e->getMessage(), 'timestamp' => now()->toDateTimeString() ]; } } /** * 获取所有收集器的信息 * * @return array */ public function getCollectorsInfo(): array { $info = []; foreach ($this->collectors as $name => $collector) { $info[$name] = [ 'name' => $name, 'class' => get_class($collector), 'source_table' => $collector->getSourceTable(), 'source_type' => $collector->getSourceType(), 'enabled' => GameConfigService::isCollectorEnabled($name), ]; } return $info; } // 注意:基于ID的进度追踪机制不需要重置功能 // resetCollector方法已移除,因为: // 1. BaseLogCollector中没有resetAllProgress方法 // 2. 基于ID的进度追踪是递增的,无需重置 // 3. 进度通过user_logs表中的source_id自动维护 /** * 添加自定义收集器 * * @param string $name 收集器名称 * @param BaseLogCollector $collector 收集器实例 * @return void */ public function addCollector(string $name, BaseLogCollector $collector): void { $this->collectors[$name] = $collector; Log::info("添加自定义收集器", [ 'name' => $name, 'class' => get_class($collector) ]); } /** * 移除收集器 * * @param string $name 收集器名称 * @return void */ public function removeCollector(string $name): void { if (isset($this->collectors[$name])) { unset($this->collectors[$name]); Log::info("移除收集器", [ 'name' => $name ]); } } /** * 获取收集器实例 * * @param string $name 收集器名称 * @return BaseLogCollector|null */ public function getCollector(string $name): ?BaseLogCollector { return $this->collectors[$name] ?? null; } /** * 检查收集器是否存在 * * @param string $name 收集器名称 * @return bool */ public function hasCollector(string $name): bool { return isset($this->collectors[$name]); } /** * 获取收集器的源表最大ID * * @param string $name 收集器名称 * @return int */ public function getCollectorSourceTableMaxId(string $name): int { if (!isset($this->collectors[$name])) { return 0; } return $this->collectors[$name]->getSourceTableMaxId(); } }