| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713 |
- <?php
- namespace App\Module\Game\Commands;
- use App\Module\Game\Logics\UserLogCollectorManager;
- use App\Module\Game\Services\GameConfigService;
- use UCore\Command\Command;
- /**
- * 用户日志收集命令
- *
- * 定时执行的计划任务,每2秒收集一次用户日志
- */
- class CollectUserLogsCommand extends Command
- {
- /**
- * 命令名称和参数
- *
- * @var string
- */
- protected $signature = 'game:collect-user-logs {--info : 显示收集器信息} {--statistics : 显示收集统计信息} {--detail : 显示详细的处理过程} {--limit=1000 : 单次处理最大记录数} {--force : 强制执行,忽略自动收集配置}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '收集用户日志,将各模块的原始日志转换为用户友好的日志消息
- 选项说明:
- --info 显示收集器信息
- --statistics 显示收集统计信息
- --detail 显示详细的处理过程
- --limit 单次处理最大记录数(默认1000)
- --force 强制执行,忽略自动收集配置
- 进度追踪机制:
- - 基于原始日志ID进行进度追踪,确保不遗漏任何记录
- - 通过user_logs表中的source_id字段自动维护进度
- - 每个收集器独立追踪进度,无需手动重置
- - 系统会自动从上次处理的最大ID继续收集';
- /**
- * 执行命令
- */
- public function handleRun()
- {
- // 检查是否允许自动收集日志(除非强制执行)
- if (!$this->option('force') && !$this->isAutoCollectEnabled()) {
- $this->warn("⚠️ 自动收集日志功能已禁用");
- $this->line("💡 如需强制执行,请使用 --force 选项");
- return 0;
- }
- // 显示统计信息
- if ($this->option('statistics')) {
- $manager = new UserLogCollectorManager();
- $collectorsInfo = $manager->getCollectorsInfo();
- $this->info("📊 收集器统计信息:");
- $this->line("");
- $this->line("收集器数量: " . count($collectorsInfo));
- // 动态显示所有收集器
- foreach ($collectorsInfo as $name => $info) {
- $icon = $this->getCollectorIcon($info['source_type']);
- $description = $this->getCollectorNameDescription($name);
- $this->line("- {$name}: {$icon} {$description}");
- }
- $this->line("");
- // 显示用户日志表统计
- try {
- $totalUserLogs = \App\Module\Game\Models\UserLog::count();
- $this->line("📋 用户日志表统计:");
- $this->line(" 📝 总日志数: {$totalUserLogs}");
- if ($totalUserLogs > 0) {
- $latestLog = \App\Module\Game\Models\UserLog::orderBy('created_at', 'desc')->first();
- $oldestLog = \App\Module\Game\Models\UserLog::orderBy('created_at', 'asc')->first();
- $this->line(" 🕐 最新日志时间: " . $latestLog->created_at);
- $this->line(" 🕐 最旧日志时间: " . $oldestLog->created_at);
- // 按类型统计 - 动态获取所有收集器类型
- $this->line(" 📊 按类型统计:");
- foreach ($collectorsInfo as $name => $info) {
- $sourceType = $info['source_type'];
- $count = \App\Module\Game\Models\UserLog::where('source_type', $sourceType)->count();
- $icon = $this->getCollectorIcon($sourceType);
- $description = $this->getCollectorDescription($sourceType);
- $this->line(" {$icon} {$description}: {$count}");
- }
- }
- } catch (\Exception $e) {
- $this->line(" ⚠️ 无法获取用户日志统计: " . $e->getMessage());
- }
- $this->line("");
- return 0;
- }
- $manager = new UserLogCollectorManager();
- // 显示收集器信息
- if ($this->option('info')) {
- $this->showCollectorsInfo($manager);
- return 0;
- }
- // 执行日志收集
- return $this->executeCollection($manager);
- }
- /**
- * 执行日志收集
- *
- * @param UserLogCollectorManager $manager
- * @return int
- */
- private function executeCollection(UserLogCollectorManager $manager): int
- {
- $detail = $this->option('detail');
- $limit = (int)$this->option('limit');
- $force = $this->option('force');
- try {
- if ($force) {
- $this->info("🚀 强制执行用户日志收集...");
- $this->line("⚡ 忽略自动收集配置,强制执行收集任务");
- } else {
- $this->info("🚀 开始按时间线收集用户日志...");
- }
- if ($detail) {
- $this->showTimelineProgress();
- }
- $result = $this->executeTimelineCollection($manager, $limit, $detail);
- $this->displayTimelineResult($result);
- return 0;
- } catch (\Exception $e) {
- $this->error("❌ 日志收集失败: {$e->getMessage()}");
- return 1;
- }
- }
- /**
- * 执行单个收集器并显示进度
- *
- * @param UserLogCollectorManager $manager
- * @param string $collectorName
- * @param bool $detail
- * @param bool $showProgress
- * @return array
- */
- private function executeCollectorWithProgress(UserLogCollectorManager $manager, string $collectorName, bool $detail, bool $showProgress): array
- {
- if ($detail) {
- $this->line("📊 检查收集器状态...");
- $this->showCollectorProgress($manager, $collectorName);
- }
- $startTime = microtime(true);
- $result = $manager->collectByName($collectorName);
- $endTime = microtime(true);
- if ($detail) {
- $this->line("⏱️ 执行时间: " . round(($endTime - $startTime) * 1000, 2) . "ms");
- }
- return $result;
- }
- /**
- * 执行所有收集器并显示进度
- *
- * @param UserLogCollectorManager $manager
- * @param bool $detail
- * @param bool $showProgress
- * @return array
- */
- private function executeAllCollectorsWithProgress(UserLogCollectorManager $manager, bool $detail, bool $showProgress): array
- {
- $collectorsInfo = $manager->getCollectorsInfo();
- if ($detail) {
- $this->line("📋 收集器总数: " . count($collectorsInfo));
- $this->line("");
- }
- $results = $manager->collectAll();
- return $results;
- }
- /**
- * 显示收集器详细信息
- *
- * @param UserLogCollectorManager $manager
- * @param string $collectorName
- * @return void
- */
- private function showCollectorDetails(UserLogCollectorManager $manager, string $collectorName): void
- {
- $collectorsInfo = $manager->getCollectorsInfo();
- if (!isset($collectorsInfo[$collectorName])) {
- $this->error("收集器 {$collectorName} 不存在");
- return;
- }
- $info = $collectorsInfo[$collectorName];
- $this->line("📝 收集器详情:");
- $this->line(" 名称: <comment>{$info['name']}</comment>");
- $this->line(" 类名: {$info['class']}");
- $this->line(" 源表: <info>{$info['source_table']}</info>");
- $this->line(" 类型: <info>{$info['source_type']}</info>");
- $this->line("");
- }
- /**
- * 显示收集器进度信息
- *
- * @param UserLogCollectorManager $manager
- * @param string $collectorName
- * @return void
- */
- private function showCollectorProgress(UserLogCollectorManager $manager, string $collectorName): void
- {
- $collectorsInfo = $manager->getCollectorsInfo();
- $info = $collectorsInfo[$collectorName];
- // 从user_logs表获取最后处理的记录ID
- $lastProcessedId = $this->getLastProcessedIdFromUserLogs($info['source_table'], $info['source_type']);
- // 使用收集器获取源表的最大ID
- $maxId = $manager->getCollectorSourceTableMaxId($collectorName);
- // 计算待处理记录数
- $pendingCount = max(0, $maxId - $lastProcessedId);
- $this->line("📈 处理进度:");
- $this->line(" 最后处理ID: <comment>{$lastProcessedId}</comment>");
- $this->line(" 最后处理时间: <comment>" . ($lastProcessedTimestamp > 0 ? date('Y-m-d H:i:s', $lastProcessedTimestamp) : '未开始') . "</comment>");
- $this->line(" 源表最大ID: <comment>{$maxId}</comment>");
- $this->line(" 待处理记录: <comment>{$pendingCount}</comment>");
- $this->line("");
- }
- /**
- * 从user_logs表获取最后处理的ID
- *
- * @param string $sourceTable
- * @param string $sourceType
- * @return int
- */
- private function getLastProcessedIdFromUserLogs(string $sourceTable, string $sourceType): int
- {
- try {
- // 对于农场收集器,需要查询多个表
- if ($sourceType === 'farm') {
- $lastLog = \App\Module\Game\Models\UserLog::where('source_type', $sourceType)
- ->whereIn('source_table', ['farm_harvest_logs', 'farm_upgrade_logs'])
- ->orderBy('source_id', 'desc')
- ->first();
- } elseif ($sourceTable === 'fund_logs') {
- // 对于fund_logs表,使用动态source_type,只按source_table查询
- // 这与BaseLogCollector的getLastProcessedId方法保持一致
- $maxId = \App\Module\Game\Models\UserLog::where('source_table', $sourceTable)
- ->max('source_id');
- return $maxId ?: 0;
- } else {
- $lastLog = \App\Module\Game\Models\UserLog::where('source_table', $sourceTable)
- ->where('source_type', $sourceType)
- ->orderBy('source_id', 'desc')
- ->first();
- }
- return $lastLog ? $lastLog->source_id : 0;
- } catch (\Exception $e) {
- return 0;
- }
- }
- // 注意:getLastProcessedTimestampFromUserLogs方法已移除
- // 当前使用基于ID的进度追踪,不再需要时间戳追踪
- /**
- * 显示收集器信息
- *
- * @param UserLogCollectorManager $manager
- * @return void
- */
- private function showCollectorsInfo(UserLogCollectorManager $manager): void
- {
- $this->info("📋 注册的收集器信息:");
- $this->line("");
- $collectorsInfo = $manager->getCollectorsInfo();
- foreach ($collectorsInfo as $info) {
- $this->line("收集器: <comment>{$info['name']}</comment>");
- $this->line(" 类名: {$info['class']}");
- $this->line(" 源表: <info>{$info['source_table']}</info>");
- $this->line(" 类型: <info>{$info['source_type']}</info>");
- $this->line("");
- }
- }
- /**
- * 显示统计信息
- *
- * @param UserLogCollectorManager $manager
- * @return void
- */
- private function showStats(UserLogCollectorManager $manager): void
- {
- $collectorsInfo = $manager->getCollectorsInfo();
- $this->line("");
- $this->line("收集器数量: " . count($collectorsInfo));
- // 动态显示所有收集器
- foreach ($collectorsInfo as $name => $info) {
- $icon = $this->getCollectorIcon($info['source_type']);
- $description = $this->getCollectorNameDescription($name);
- $this->line("- {$name}: {$icon} {$description}");
- }
- $this->line("");
- }
- /**
- * 获取收集器图标
- *
- * @param string $sourceType
- * @return string
- */
- private function getCollectorIcon(string $sourceType): string
- {
- $icons = [
- 'fund' => '💰',
- 'item' => '📦',
- 'farm' => '🌾',
- 'point' => '⭐',
- 'pet' => '🐾',
- 'system' => '⚙️',
- ];
- return $icons[$sourceType] ?? '📄';
- }
- /**
- * 获取收集器描述
- *
- * @param string $sourceType
- * @return string
- */
- private function getCollectorDescription(string $sourceType): string
- {
- $descriptions = [
- 'fund' => '资金日志收集器',
- 'item' => '物品日志收集器',
- 'farm' => '农场日志收集器',
- 'point' => '积分日志收集器',
- 'pet' => '宠物日志收集器',
- 'system' => '系统日志收集器',
- ];
- return $descriptions[$sourceType] ?? '未知类型收集器';
- }
- /**
- * 获取收集器名称描述(基于收集器名称而非源类型)
- *
- * @param string $collectorName
- * @return string
- */
- private function getCollectorNameDescription(string $collectorName): string
- {
- $descriptions = [
- 'fund' => '资金日志收集器',
- 'item' => '物品日志收集器',
- 'farm_harvest' => '农场收获日志收集器',
- 'farm_upgrade' => '农场升级日志收集器',
- 'point' => '积分日志收集器',
- 'pet' => '宠物日志收集器',
- 'system' => '系统日志收集器',
- ];
- return $descriptions[$collectorName] ?? '未知收集器';
- }
- /**
- * 获取表的记录总数
- *
- * @param string $tableName
- * @return int
- */
- private function getTableRecordCount(string $tableName): int
- {
- try {
- // 根据表名使用对应的模型
- switch ($tableName) {
- case 'fund_logs':
- return \App\Module\Fund\Models\FundLogModel::count();
- case 'item_transaction_logs':
- return \App\Module\GameItems\Models\ItemTransactionLog::count();
- case 'farm_harvest_logs':
- return \App\Module\Farm\Models\FarmHarvestLog::count();
- case 'farm_upgrade_logs':
- return \App\Module\Farm\Models\FarmUpgradeLog::count();
- case 'point_logs':
- return \App\Module\Point\Models\PointLogModel::count();
- default:
- // 回退到直接查询
- return \Illuminate\Support\Facades\DB::table($tableName)->count();
- }
- } catch (\Exception $e) {
- return 0;
- }
- }
- /**
- * 显示进度条
- *
- * @param float $percent
- * @return void
- */
- private function displayProgressBar(float $percent): void
- {
- $barLength = 20;
- $filledLength = (int)round(($percent / 100) * $barLength);
- $emptyLength = $barLength - $filledLength;
- $bar = str_repeat('█', $filledLength) . str_repeat('░', $emptyLength);
- $this->line(" 📊 [{$bar}] {$percent}%");
- }
- /**
- * 执行时间线收集
- * 改为让每个收集器独立处理,避免全局时间戳的复杂性
- *
- * @param UserLogCollectorManager $manager
- * @param int $limit
- * @param bool $detail
- * @return array
- */
- private function executeTimelineCollection(UserLogCollectorManager $manager, int $limit, bool $detail): array
- {
- $startTime = microtime(true);
- if ($detail) {
- $this->line("📊 执行各收集器的日志收集...");
- }
- // 直接执行所有收集器,传递限制参数
- $results = $manager->collectAll($limit);
- if ($results['total_processed'] == 0) {
- return [
- 'processed_count' => 0,
- 'execution_time' => round((microtime(true) - $startTime) * 1000, 2),
- 'status' => 'success',
- 'message' => '没有新记录需要处理',
- 'timestamp' => now()->toDateTimeString()
- ];
- }
- if ($detail) {
- $this->line("📝 开始处理各收集器...");
- foreach ($results['collectors'] as $collectorName => $result) {
- $this->line(" {$collectorName}: 处理了 {$result['processed_count']} 条记录");
- }
- }
- $endTime = microtime(true);
- return [
- 'processed_count' => $results['total_processed'],
- 'execution_time' => round(($endTime - $startTime) * 1000, 2),
- 'status' => 'success',
- 'timestamp' => now()->toDateTimeString(),
- 'details' => $results['collectors']
- ];
- }
- // 注意:getAllRecordsByTimeline方法已移除
- // 当前使用基于ID的进度追踪,每个收集器独立处理,不再需要全局时间线排序
- /**
- * 显示收集器进度信息
- * 基于ID的进度追踪,显示各收集器的处理状态
- *
- * @return void
- */
- private function showTimelineProgress(): void
- {
- $this->line("📈 收集器进度状态:");
- try {
- $manager = new \App\Module\Game\Logics\UserLogCollectorManager();
- $collectorsInfo = $manager->getCollectorsInfo();
- foreach ($collectorsInfo as $name => $info) {
- $lastProcessedId = $this->getLastProcessedIdFromUserLogs($info['source_table'], $info['source_type']);
- $maxId = $manager->getCollectorSourceTableMaxId($name);
- $pendingCount = max(0, $maxId - $lastProcessedId);
- $this->line(" 🔧 {$name}: 最后处理ID <comment>{$lastProcessedId}</comment>, 待处理 <info>{$pendingCount}</info> 条");
- }
- } catch (\Exception $e) {
- $this->line(" ⚠️ 无法获取进度信息: " . $e->getMessage());
- }
- $this->line("");
- }
- /**
- * 显示时间线收集结果
- *
- * @param array $result
- * @return void
- */
- private function displayTimelineResult(array $result): void
- {
- $this->line("");
- if ($result['status'] === 'success') {
- $this->info("✅ 时间线收集完成!");
- $this->line("📊 <comment>处理统计</comment>:");
- $this->line(" 📝 处理记录数: <info>{$result['processed_count']}</info>");
- $this->line(" ⏱️ 执行时间: <info>{$result['execution_time']}ms</info>");
- $this->line(" 🕐 完成时间: <info>{$result['timestamp']}</info>");
- if (isset($result['last_timestamp'])) {
- $this->line(" 🎯 最新的处理时间: <info>" . date('Y-m-d H:i:s', $result['last_timestamp']) . "</info>");
- }
- if ($result['processed_count'] > 0) {
- $avgTime = round($result['execution_time'] / $result['processed_count'], 2);
- $this->line(" 📈 平均处理时间: <info>{$avgTime}ms/条</info>");
- }
- if (isset($result['message'])) {
- $this->line(" 💡 <comment>{$result['message']}</comment>");
- }
- } else {
- $this->error("❌ 时间线收集失败!");
- if (isset($result['error'])) {
- $this->line("🚨 <comment>错误信息</comment>:");
- $this->line(" {$result['error']}");
- }
- }
- $this->line("");
- }
- // 注意:以下时间戳相关方法已移除,因为当前使用基于ID的进度追踪:
- // - getGlobalLastProcessedTimestamp(): 全局时间戳追踪已废弃
- // - updateGlobalLastProcessedTimestamp(): 手动更新时间戳已废弃
- //
- // 当前进度追踪机制:
- // 1. 每个收集器通过getLastProcessedId()获取最后处理的记录ID
- // 2. 进度通过user_logs表中的source_id字段自动维护
- // 3. 无需手动重置或更新进度
- /**
- * 显示用户日志表统计
- *
- * @return void
- */
- private function showUserLogStats(): void
- {
- try {
- // 使用模型查询,避免表名前缀问题
- $totalUserLogs = \App\Module\Game\Models\UserLog::count();
- $todayUserLogs = \App\Module\Game\Models\UserLog::whereDate('created_at', now()->toDateString())->count();
- $this->line("📋 <comment>用户日志表统计</comment>:");
- $this->line(" 📝 总日志数: <info>{$totalUserLogs}</info>");
- $this->line(" 📅 今日新增: <info>{$todayUserLogs}</info>");
- // 按来源类型统计
- $sourceStats = \App\Module\Game\Models\UserLog::select('source_type', \Illuminate\Support\Facades\DB::raw('count(*) as count'))
- ->groupBy('source_type')
- ->get();
- if ($sourceStats->isNotEmpty()) {
- $this->line(" 📊 按来源类型统计:");
- foreach ($sourceStats as $stat) {
- $this->line(" {$stat->source_type}: <info>{$stat->count}</info>");
- }
- }
- } catch (\Exception $e) {
- $this->line(" ⚠️ 无法获取用户日志统计信息: " . $e->getMessage());
- }
- }
- /**
- * 显示单个收集器结果
- *
- * @param array $result
- * @return void
- */
- private function displaySingleResult(array $result): void
- {
- $this->line("");
- if ($result['status'] === 'success') {
- $this->info("✅ 收集完成!");
- $this->line("📊 <comment>处理统计</comment>:");
- $this->line(" 📝 处理记录数: <info>{$result['processed_count']}</info>");
- $this->line(" ⏱️ 执行时间: <info>{$result['execution_time']}ms</info>");
- $this->line(" 🕐 完成时间: <info>{$result['timestamp']}</info>");
- if ($result['processed_count'] > 0) {
- $avgTime = round($result['execution_time'] / $result['processed_count'], 2);
- $this->line(" 📈 平均处理时间: <info>{$avgTime}ms/条</info>");
- }
- } else {
- $this->error("❌ 收集失败!");
- $this->line("🚨 <comment>错误信息</comment>:");
- $this->line(" {$result['error']}");
- }
- $this->line("");
- }
- /**
- * 显示所有收集器结果
- *
- * @param array $results
- * @return void
- */
- private function displayAllResults(array $results): void
- {
- $this->line("");
- $this->info("🎉 所有收集器执行完成!");
- $this->line("");
- // 显示总体统计
- $this->line("📊 <comment>总体统计</comment>:");
- $this->line(" 📝 总处理记录数: <info>{$results['total_processed']}</info>");
- $this->line(" ⏱️ 总执行时间: <info>{$results['total_execution_time']}ms</info>");
- $this->line(" 🕐 完成时间: <info>{$results['timestamp']}</info>");
- if ($results['total_processed'] > 0) {
- $avgTime = round($results['total_execution_time'] / $results['total_processed'], 2);
- $this->line(" 📈 平均处理时间: <info>{$avgTime}ms/条</info>");
- }
- $this->line("");
- // 显示各收集器详情
- $this->line("📋 <comment>各收集器详情</comment>:");
- $successCount = 0;
- $failureCount = 0;
- foreach ($results['collectors'] as $name => $result) {
- if ($result['status'] === 'success') {
- $status = '<info>✅ 成功</info>';
- $successCount++;
- } else {
- $status = '<error>❌ 失败</error>';
- $failureCount++;
- }
- $this->line(" 🔧 <comment>{$name}</comment>: {$status}");
- $this->line(" 📝 处理记录: <info>{$result['processed_count']}</info> 条");
- $this->line(" ⏱️ 执行时间: <info>{$result['execution_time']}</info> ms");
- if ($result['status'] === 'error') {
- $this->line(" 🚨 错误信息: <error>{$result['error']}</error>");
- } elseif ($result['processed_count'] > 0) {
- $avgTime = round($result['execution_time'] / $result['processed_count'], 2);
- $this->line(" 📈 平均时间: <info>{$avgTime}</info> ms/条");
- }
- $this->line("");
- }
- // 显示执行摘要
- $totalCollectors = $successCount + $failureCount;
- $this->line("📈 <comment>执行摘要</comment>:");
- $this->line(" 🎯 成功收集器: <info>{$successCount}/{$totalCollectors}</info>");
- if ($failureCount > 0) {
- $this->line(" ⚠️ 失败收集器: <error>{$failureCount}/{$totalCollectors}</error>");
- }
- $this->line("");
- }
- /**
- * 检查是否允许自动收集日志
- *
- * @return bool
- */
- private function isAutoCollectEnabled(): bool
- {
- return GameConfigService::isAutoCollectEnabled();
- }
- }
|