collectByName($collectorName); } else { return $manager->collectAll(); } } catch (\Exception $e) { Log::error('同步执行日志收集失败', [ 'collector' => $collectorName, 'error' => $e->getMessage() ]); return [ 'status' => 'error', 'error' => $e->getMessage(), 'timestamp' => now()->toDateTimeString() ]; } } /** * 调度日志收集任务(异步方式) * * @param string|null $collectorName 收集器名称,null表示执行所有收集器 * @return void */ public static function scheduleCollection(?string $collectorName = null): void { CollectUserLogJob::dispatchCollection($collectorName); } /** * 获取收集器信息 * * @return array */ public static function getCollectorsInfo(): array { try { $manager = new UserLogCollectorManager(); return $manager->getCollectorsInfo(); } catch (\Exception $e) { Log::error('获取收集器信息失败', [ 'error' => $e->getMessage() ]); return []; } } /** * 重置收集器进度 * * @param string|null $collectorName 收集器名称,null表示重置所有收集器 * @return bool */ public static function resetCollector(?string $collectorName = null): bool { try { $manager = new UserLogCollectorManager(); if ($collectorName) { $manager->resetCollector($collectorName); } else { $manager->resetAllCollectors(); } return true; } catch (\Exception $e) { Log::error('重置收集器进度失败', [ 'collector' => $collectorName, 'error' => $e->getMessage() ]); return false; } } /** * 检查收集器状态 * * @return array */ public static function checkCollectorsStatus(): array { try { $manager = new UserLogCollectorManager(); $collectorsInfo = $manager->getCollectorsInfo(); $status = []; foreach ($collectorsInfo as $name => $info) { $cacheKey = "user_log_collector:last_processed_id:{$info['source_table']}"; $lastProcessedId = \Illuminate\Support\Facades\Cache::get($cacheKey, 0); $status[$name] = [ 'name' => $name, 'source_table' => $info['source_table'], 'source_type' => $info['source_type'], 'last_processed_id' => $lastProcessedId, 'last_check_time' => now()->toDateTimeString(), ]; } return $status; } catch (\Exception $e) { Log::error('检查收集器状态失败', [ 'error' => $e->getMessage() ]); return []; } } /** * 获取收集统计信息 * * @param int $days 统计天数 * @return array */ public static function getCollectionStats(int $days = 7): array { try { $stats = [ 'period' => "{$days}天", 'start_date' => now()->subDays($days)->toDateString(), 'end_date' => now()->toDateString(), 'total_logs' => 0, 'daily_stats' => [], 'source_type_stats' => [], ]; // 获取总日志数 $totalLogs = \App\Module\Game\Models\UserLog::where('created_at', '>=', now()->subDays($days)) ->count(); $stats['total_logs'] = $totalLogs; // 获取每日统计 $dailyStats = \App\Module\Game\Models\UserLog::where('created_at', '>=', now()->subDays($days)) ->selectRaw('DATE(created_at) as date, COUNT(*) as count') ->groupBy('date') ->orderBy('date') ->get() ->pluck('count', 'date') ->toArray(); $stats['daily_stats'] = $dailyStats; // 获取来源类型统计 $sourceTypeStats = \App\Module\Game\Models\UserLog::where('created_at', '>=', now()->subDays($days)) ->selectRaw('source_type, COUNT(*) as count') ->whereNotNull('source_type') ->groupBy('source_type') ->get() ->pluck('count', 'source_type') ->toArray(); $stats['source_type_stats'] = $sourceTypeStats; return $stats; } catch (\Exception $e) { Log::error('获取收集统计信息失败', [ 'error' => $e->getMessage() ]); return []; } } /** * 健康检查 * * @return array */ public static function healthCheck(): array { try { $health = [ 'status' => 'healthy', 'checks' => [], 'timestamp' => now()->toDateTimeString(), ]; // 检查收集器是否正常注册 $manager = new UserLogCollectorManager(); $collectorsInfo = $manager->getCollectorsInfo(); $health['checks']['collectors_registered'] = [ 'status' => !empty($collectorsInfo) ? 'pass' : 'fail', 'count' => count($collectorsInfo), 'details' => array_keys($collectorsInfo), ]; // 检查最近是否有日志生成 $recentLogsCount = \App\Module\Game\Models\UserLog::where('created_at', '>=', now()->subMinutes(10)) ->count(); $health['checks']['recent_logs'] = [ 'status' => 'info', 'count' => $recentLogsCount, 'message' => "最近10分钟生成了 {$recentLogsCount} 条日志", ]; // 检查数据库连接 try { \App\Module\Game\Models\UserLog::count(); $health['checks']['database'] = [ 'status' => 'pass', 'message' => '数据库连接正常', ]; } catch (\Exception $e) { $health['checks']['database'] = [ 'status' => 'fail', 'message' => '数据库连接失败: ' . $e->getMessage(), ]; $health['status'] = 'unhealthy'; } return $health; } catch (\Exception $e) { return [ 'status' => 'unhealthy', 'error' => $e->getMessage(), 'timestamp' => now()->toDateTimeString(), ]; } } }