CollectUserLogsCommand.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. <?php
  2. namespace App\Module\Game\Commands;
  3. use App\Module\Game\Logics\UserLogCollectorManager;
  4. use App\Module\Game\Services\GameConfigService;
  5. use UCore\Command\Command;
  6. /**
  7. * 用户日志收集命令
  8. *
  9. * 定时执行的计划任务,每2秒收集一次用户日志
  10. */
  11. class CollectUserLogsCommand extends Command
  12. {
  13. /**
  14. * 命令名称和参数
  15. *
  16. * @var string
  17. */
  18. protected $signature = 'game:collect-user-logs {--info : 显示收集器信息} {--statistics : 显示收集统计信息} {--detail : 显示详细的处理过程} {--limit=1000 : 单次处理最大记录数} {--force : 强制执行,忽略自动收集配置}';
  19. /**
  20. * 命令描述
  21. *
  22. * @var string
  23. */
  24. protected $description = '收集用户日志,将各模块的原始日志转换为用户友好的日志消息
  25. 选项说明:
  26. --info 显示收集器信息
  27. --statistics 显示收集统计信息
  28. --detail 显示详细的处理过程
  29. --limit 单次处理最大记录数(默认1000)
  30. --force 强制执行,忽略自动收集配置
  31. 进度追踪机制:
  32. - 基于原始日志ID进行进度追踪,确保不遗漏任何记录
  33. - 通过user_logs表中的source_id字段自动维护进度
  34. - 每个收集器独立追踪进度,无需手动重置
  35. - 系统会自动从上次处理的最大ID继续收集';
  36. /**
  37. * 执行命令
  38. */
  39. public function handleRun()
  40. {
  41. // 检查是否允许自动收集日志(除非强制执行)
  42. if (!$this->option('force') && !$this->isAutoCollectEnabled()) {
  43. $this->warn("⚠️ 自动收集日志功能已禁用");
  44. $this->line("💡 如需强制执行,请使用 --force 选项");
  45. return 0;
  46. }
  47. // 显示统计信息
  48. if ($this->option('statistics')) {
  49. $manager = new UserLogCollectorManager();
  50. $collectorsInfo = $manager->getCollectorsInfo();
  51. $this->info("📊 收集器统计信息:");
  52. $this->line("");
  53. $this->line("收集器数量: " . count($collectorsInfo));
  54. // 动态显示所有收集器
  55. foreach ($collectorsInfo as $name => $info) {
  56. $icon = $this->getCollectorIcon($info['source_type']);
  57. $description = $this->getCollectorNameDescription($name);
  58. $this->line("- {$name}: {$icon} {$description}");
  59. }
  60. $this->line("");
  61. // 显示用户日志表统计
  62. try {
  63. $totalUserLogs = \App\Module\Game\Models\UserLog::count();
  64. $this->line("📋 用户日志表统计:");
  65. $this->line(" 📝 总日志数: {$totalUserLogs}");
  66. if ($totalUserLogs > 0) {
  67. $latestLog = \App\Module\Game\Models\UserLog::orderBy('created_at', 'desc')->first();
  68. $oldestLog = \App\Module\Game\Models\UserLog::orderBy('created_at', 'asc')->first();
  69. $this->line(" 🕐 最新日志时间: " . $latestLog->created_at);
  70. $this->line(" 🕐 最旧日志时间: " . $oldestLog->created_at);
  71. // 按类型统计 - 动态获取所有收集器类型
  72. $this->line(" 📊 按类型统计:");
  73. foreach ($collectorsInfo as $name => $info) {
  74. $sourceType = $info['source_type'];
  75. $count = \App\Module\Game\Models\UserLog::where('source_type', $sourceType)->count();
  76. $icon = $this->getCollectorIcon($sourceType);
  77. $description = $this->getCollectorDescription($sourceType);
  78. $this->line(" {$icon} {$description}: {$count}");
  79. }
  80. }
  81. } catch (\Exception $e) {
  82. $this->line(" ⚠️ 无法获取用户日志统计: " . $e->getMessage());
  83. }
  84. $this->line("");
  85. return 0;
  86. }
  87. $manager = new UserLogCollectorManager();
  88. // 显示收集器信息
  89. if ($this->option('info')) {
  90. $this->showCollectorsInfo($manager);
  91. return 0;
  92. }
  93. // 执行日志收集
  94. return $this->executeCollection($manager);
  95. }
  96. /**
  97. * 执行日志收集
  98. *
  99. * @param UserLogCollectorManager $manager
  100. * @return int
  101. */
  102. private function executeCollection(UserLogCollectorManager $manager): int
  103. {
  104. $detail = $this->option('detail');
  105. $limit = (int)$this->option('limit');
  106. $force = $this->option('force');
  107. try {
  108. if ($force) {
  109. $this->info("🚀 强制执行用户日志收集...");
  110. $this->line("⚡ 忽略自动收集配置,强制执行收集任务");
  111. } else {
  112. $this->info("🚀 开始按时间线收集用户日志...");
  113. }
  114. if ($detail) {
  115. $this->showTimelineProgress();
  116. }
  117. $result = $this->executeTimelineCollection($manager, $limit, $detail);
  118. $this->displayTimelineResult($result);
  119. return 0;
  120. } catch (\Exception $e) {
  121. $this->error("❌ 日志收集失败: {$e->getMessage()}");
  122. return 1;
  123. }
  124. }
  125. /**
  126. * 执行单个收集器并显示进度
  127. *
  128. * @param UserLogCollectorManager $manager
  129. * @param string $collectorName
  130. * @param bool $detail
  131. * @param bool $showProgress
  132. * @return array
  133. */
  134. private function executeCollectorWithProgress(UserLogCollectorManager $manager, string $collectorName, bool $detail, bool $showProgress): array
  135. {
  136. if ($detail) {
  137. $this->line("📊 检查收集器状态...");
  138. $this->showCollectorProgress($manager, $collectorName);
  139. }
  140. $startTime = microtime(true);
  141. $result = $manager->collectByName($collectorName);
  142. $endTime = microtime(true);
  143. if ($detail) {
  144. $this->line("⏱️ 执行时间: " . round(($endTime - $startTime) * 1000, 2) . "ms");
  145. }
  146. return $result;
  147. }
  148. /**
  149. * 执行所有收集器并显示进度
  150. *
  151. * @param UserLogCollectorManager $manager
  152. * @param bool $detail
  153. * @param bool $showProgress
  154. * @return array
  155. */
  156. private function executeAllCollectorsWithProgress(UserLogCollectorManager $manager, bool $detail, bool $showProgress): array
  157. {
  158. $collectorsInfo = $manager->getCollectorsInfo();
  159. if ($detail) {
  160. $this->line("📋 收集器总数: " . count($collectorsInfo));
  161. $this->line("");
  162. }
  163. $results = $manager->collectAll();
  164. return $results;
  165. }
  166. /**
  167. * 显示收集器详细信息
  168. *
  169. * @param UserLogCollectorManager $manager
  170. * @param string $collectorName
  171. * @return void
  172. */
  173. private function showCollectorDetails(UserLogCollectorManager $manager, string $collectorName): void
  174. {
  175. $collectorsInfo = $manager->getCollectorsInfo();
  176. if (!isset($collectorsInfo[$collectorName])) {
  177. $this->error("收集器 {$collectorName} 不存在");
  178. return;
  179. }
  180. $info = $collectorsInfo[$collectorName];
  181. $enabledStatus = $info['enabled'] ? '<info>✅ 启用</info>' : '<comment>❌ 禁用</comment>';
  182. $this->line("📝 收集器详情:");
  183. $this->line(" 名称: <comment>{$info['name']}</comment>");
  184. $this->line(" 状态: {$enabledStatus}");
  185. $this->line(" 类名: {$info['class']}");
  186. $this->line(" 源表: <info>{$info['source_table']}</info>");
  187. $this->line(" 类型: <info>{$info['source_type']}</info>");
  188. $this->line("");
  189. }
  190. /**
  191. * 显示收集器进度信息
  192. *
  193. * @param UserLogCollectorManager $manager
  194. * @param string $collectorName
  195. * @return void
  196. */
  197. private function showCollectorProgress(UserLogCollectorManager $manager, string $collectorName): void
  198. {
  199. $collectorsInfo = $manager->getCollectorsInfo();
  200. $info = $collectorsInfo[$collectorName];
  201. // 从user_logs表获取最后处理的记录ID
  202. $lastProcessedId = $this->getLastProcessedIdFromUserLogs($info['source_table'], $info['source_type']);
  203. // 使用收集器获取源表的最大ID
  204. $maxId = $manager->getCollectorSourceTableMaxId($collectorName);
  205. // 计算待处理记录数
  206. $pendingCount = max(0, $maxId - $lastProcessedId);
  207. $this->line("📈 处理进度:");
  208. $this->line(" 最后处理ID: <comment>{$lastProcessedId}</comment>");
  209. $this->line(" 最后处理时间: <comment>" . ($lastProcessedTimestamp > 0 ? date('Y-m-d H:i:s', $lastProcessedTimestamp) : '未开始') . "</comment>");
  210. $this->line(" 源表最大ID: <comment>{$maxId}</comment>");
  211. $this->line(" 待处理记录: <comment>{$pendingCount}</comment>");
  212. $this->line("");
  213. }
  214. /**
  215. * 从user_logs表获取最后处理的ID
  216. *
  217. * @param string $sourceTable
  218. * @param string $sourceType
  219. * @return int
  220. */
  221. private function getLastProcessedIdFromUserLogs(string $sourceTable, string $sourceType): int
  222. {
  223. try {
  224. // 对于农场收集器,需要查询多个表
  225. if ($sourceType === 'farm') {
  226. $lastLog = \App\Module\Game\Models\UserLog::where('source_type', $sourceType)
  227. ->whereIn('source_table', ['farm_harvest_logs', 'farm_upgrade_logs'])
  228. ->orderBy('source_id', 'desc')
  229. ->first();
  230. } elseif ($sourceTable === 'fund_logs') {
  231. // 对于fund_logs表,使用动态source_type,只按source_table查询
  232. // 这与BaseLogCollector的getLastProcessedId方法保持一致
  233. $maxId = \App\Module\Game\Models\UserLog::where('source_table', $sourceTable)
  234. ->max('source_id');
  235. return $maxId ?: 0;
  236. } else {
  237. $lastLog = \App\Module\Game\Models\UserLog::where('source_table', $sourceTable)
  238. ->where('source_type', $sourceType)
  239. ->orderBy('source_id', 'desc')
  240. ->first();
  241. }
  242. return $lastLog ? $lastLog->source_id : 0;
  243. } catch (\Exception $e) {
  244. return 0;
  245. }
  246. }
  247. // 注意:getLastProcessedTimestampFromUserLogs方法已移除
  248. // 当前使用基于ID的进度追踪,不再需要时间戳追踪
  249. /**
  250. * 显示收集器信息
  251. *
  252. * @param UserLogCollectorManager $manager
  253. * @return void
  254. */
  255. private function showCollectorsInfo(UserLogCollectorManager $manager): void
  256. {
  257. $this->info("📋 注册的收集器信息:");
  258. $this->line("");
  259. $collectorsInfo = $manager->getCollectorsInfo();
  260. foreach ($collectorsInfo as $info) {
  261. $enabledStatus = $info['enabled'] ? '<info>✅ 启用</info>' : '<comment>❌ 禁用</comment>';
  262. $this->line("收集器: <comment>{$info['name']}</comment> ({$enabledStatus})");
  263. $this->line(" 类名: {$info['class']}");
  264. $this->line(" 源表: <info>{$info['source_table']}</info>");
  265. $this->line(" 类型: <info>{$info['source_type']}</info>");
  266. $this->line("");
  267. }
  268. }
  269. /**
  270. * 显示统计信息
  271. *
  272. * @param UserLogCollectorManager $manager
  273. * @return void
  274. */
  275. private function showStats(UserLogCollectorManager $manager): void
  276. {
  277. $collectorsInfo = $manager->getCollectorsInfo();
  278. $this->line("");
  279. $this->line("收集器数量: " . count($collectorsInfo));
  280. // 动态显示所有收集器
  281. foreach ($collectorsInfo as $name => $info) {
  282. $icon = $this->getCollectorIcon($info['source_type']);
  283. $description = $this->getCollectorNameDescription($name);
  284. $this->line("- {$name}: {$icon} {$description}");
  285. }
  286. $this->line("");
  287. }
  288. /**
  289. * 获取收集器图标
  290. *
  291. * @param string $sourceType
  292. * @return string
  293. */
  294. private function getCollectorIcon(string $sourceType): string
  295. {
  296. $icons = [
  297. 'fund' => '💰',
  298. 'item' => '📦',
  299. 'farm' => '🌾',
  300. 'point' => '⭐',
  301. 'pet' => '🐾',
  302. 'system' => '⚙️',
  303. ];
  304. return $icons[$sourceType] ?? '📄';
  305. }
  306. /**
  307. * 获取收集器描述
  308. *
  309. * @param string $sourceType
  310. * @return string
  311. */
  312. private function getCollectorDescription(string $sourceType): string
  313. {
  314. $descriptions = [
  315. 'fund' => '资金日志收集器',
  316. 'item' => '物品日志收集器',
  317. 'farm' => '农场日志收集器',
  318. 'point' => '积分日志收集器',
  319. 'pet' => '宠物日志收集器',
  320. 'system' => '系统日志收集器',
  321. ];
  322. return $descriptions[$sourceType] ?? '未知类型收集器';
  323. }
  324. /**
  325. * 获取收集器名称描述(基于收集器名称而非源类型)
  326. *
  327. * @param string $collectorName
  328. * @return string
  329. */
  330. private function getCollectorNameDescription(string $collectorName): string
  331. {
  332. $descriptions = [
  333. 'fund' => '资金日志收集器',
  334. 'item' => '物品日志收集器',
  335. 'farm_harvest' => '农场收获日志收集器',
  336. 'farm_upgrade' => '农场升级日志收集器',
  337. 'point' => '积分日志收集器',
  338. 'pet' => '宠物日志收集器',
  339. 'system' => '系统日志收集器',
  340. ];
  341. return $descriptions[$collectorName] ?? '未知收集器';
  342. }
  343. /**
  344. * 获取表的记录总数
  345. *
  346. * @param string $tableName
  347. * @return int
  348. */
  349. private function getTableRecordCount(string $tableName): int
  350. {
  351. try {
  352. // 根据表名使用对应的模型
  353. switch ($tableName) {
  354. case 'fund_logs':
  355. return \App\Module\Fund\Models\FundLogModel::count();
  356. case 'item_transaction_logs':
  357. return \App\Module\GameItems\Models\ItemTransactionLog::count();
  358. case 'farm_harvest_logs':
  359. return \App\Module\Farm\Models\FarmHarvestLog::count();
  360. case 'farm_upgrade_logs':
  361. return \App\Module\Farm\Models\FarmUpgradeLog::count();
  362. case 'point_logs':
  363. return \App\Module\Point\Models\PointLogModel::count();
  364. default:
  365. // 回退到直接查询
  366. return \Illuminate\Support\Facades\DB::table($tableName)->count();
  367. }
  368. } catch (\Exception $e) {
  369. return 0;
  370. }
  371. }
  372. /**
  373. * 显示进度条
  374. *
  375. * @param float $percent
  376. * @return void
  377. */
  378. private function displayProgressBar(float $percent): void
  379. {
  380. $barLength = 20;
  381. $filledLength = (int)round(($percent / 100) * $barLength);
  382. $emptyLength = $barLength - $filledLength;
  383. $bar = str_repeat('█', $filledLength) . str_repeat('░', $emptyLength);
  384. $this->line(" 📊 [{$bar}] {$percent}%");
  385. }
  386. /**
  387. * 执行时间线收集
  388. * 改为让每个收集器独立处理,避免全局时间戳的复杂性
  389. *
  390. * @param UserLogCollectorManager $manager
  391. * @param int $limit
  392. * @param bool $detail
  393. * @return array
  394. */
  395. private function executeTimelineCollection(UserLogCollectorManager $manager, int $limit, bool $detail): array
  396. {
  397. $startTime = microtime(true);
  398. if ($detail) {
  399. $this->line("📊 执行各收集器的日志收集...");
  400. }
  401. // 直接执行所有收集器,传递限制参数
  402. $results = $manager->collectAll($limit);
  403. if ($results['total_processed'] == 0) {
  404. return [
  405. 'processed_count' => 0,
  406. 'execution_time' => round((microtime(true) - $startTime) * 1000, 2),
  407. 'status' => 'success',
  408. 'message' => '没有新记录需要处理',
  409. 'timestamp' => now()->toDateTimeString()
  410. ];
  411. }
  412. if ($detail) {
  413. $this->line("📝 开始处理各收集器...");
  414. foreach ($results['collectors'] as $collectorName => $result) {
  415. $this->line(" {$collectorName}: 处理了 {$result['processed_count']} 条记录");
  416. }
  417. }
  418. $endTime = microtime(true);
  419. return [
  420. 'processed_count' => $results['total_processed'],
  421. 'execution_time' => round(($endTime - $startTime) * 1000, 2),
  422. 'status' => 'success',
  423. 'timestamp' => now()->toDateTimeString(),
  424. 'details' => $results['collectors']
  425. ];
  426. }
  427. // 注意:getAllRecordsByTimeline方法已移除
  428. // 当前使用基于ID的进度追踪,每个收集器独立处理,不再需要全局时间线排序
  429. /**
  430. * 显示收集器进度信息
  431. * 基于ID的进度追踪,显示各收集器的处理状态
  432. *
  433. * @return void
  434. */
  435. private function showTimelineProgress(): void
  436. {
  437. $this->line("📈 收集器进度状态:");
  438. try {
  439. $manager = new \App\Module\Game\Logics\UserLogCollectorManager();
  440. $collectorsInfo = $manager->getCollectorsInfo();
  441. foreach ($collectorsInfo as $name => $info) {
  442. $lastProcessedId = $this->getLastProcessedIdFromUserLogs($info['source_table'], $info['source_type']);
  443. $maxId = $manager->getCollectorSourceTableMaxId($name);
  444. $pendingCount = max(0, $maxId - $lastProcessedId);
  445. $this->line(" 🔧 {$name}: 最后处理ID <comment>{$lastProcessedId}</comment>, 待处理 <info>{$pendingCount}</info> 条");
  446. }
  447. } catch (\Exception $e) {
  448. $this->line(" ⚠️ 无法获取进度信息: " . $e->getMessage());
  449. }
  450. $this->line("");
  451. }
  452. /**
  453. * 显示时间线收集结果
  454. *
  455. * @param array $result
  456. * @return void
  457. */
  458. private function displayTimelineResult(array $result): void
  459. {
  460. $this->line("");
  461. if ($result['status'] === 'success') {
  462. $this->info("✅ 时间线收集完成!");
  463. $this->line("📊 <comment>处理统计</comment>:");
  464. $this->line(" 📝 处理记录数: <info>{$result['processed_count']}</info>");
  465. $this->line(" ⏱️ 执行时间: <info>{$result['execution_time']}ms</info>");
  466. $this->line(" 🕐 完成时间: <info>{$result['timestamp']}</info>");
  467. if (isset($result['last_timestamp'])) {
  468. $this->line(" 🎯 最新的处理时间: <info>" . date('Y-m-d H:i:s', $result['last_timestamp']) . "</info>");
  469. }
  470. if ($result['processed_count'] > 0) {
  471. $avgTime = round($result['execution_time'] / $result['processed_count'], 2);
  472. $this->line(" 📈 平均处理时间: <info>{$avgTime}ms/条</info>");
  473. }
  474. if (isset($result['message'])) {
  475. $this->line(" 💡 <comment>{$result['message']}</comment>");
  476. }
  477. } else {
  478. $this->error("❌ 时间线收集失败!");
  479. if (isset($result['error'])) {
  480. $this->line("🚨 <comment>错误信息</comment>:");
  481. $this->line(" {$result['error']}");
  482. }
  483. }
  484. $this->line("");
  485. }
  486. // 注意:以下时间戳相关方法已移除,因为当前使用基于ID的进度追踪:
  487. // - getGlobalLastProcessedTimestamp(): 全局时间戳追踪已废弃
  488. // - updateGlobalLastProcessedTimestamp(): 手动更新时间戳已废弃
  489. //
  490. // 当前进度追踪机制:
  491. // 1. 每个收集器通过getLastProcessedId()获取最后处理的记录ID
  492. // 2. 进度通过user_logs表中的source_id字段自动维护
  493. // 3. 无需手动重置或更新进度
  494. /**
  495. * 显示用户日志表统计
  496. *
  497. * @return void
  498. */
  499. private function showUserLogStats(): void
  500. {
  501. try {
  502. // 使用模型查询,避免表名前缀问题
  503. $totalUserLogs = \App\Module\Game\Models\UserLog::count();
  504. $todayUserLogs = \App\Module\Game\Models\UserLog::whereDate('created_at', now()->toDateString())->count();
  505. $this->line("📋 <comment>用户日志表统计</comment>:");
  506. $this->line(" 📝 总日志数: <info>{$totalUserLogs}</info>");
  507. $this->line(" 📅 今日新增: <info>{$todayUserLogs}</info>");
  508. // 按来源类型统计
  509. $sourceStats = \App\Module\Game\Models\UserLog::select('source_type', \Illuminate\Support\Facades\DB::raw('count(*) as count'))
  510. ->groupBy('source_type')
  511. ->get();
  512. if ($sourceStats->isNotEmpty()) {
  513. $this->line(" 📊 按来源类型统计:");
  514. foreach ($sourceStats as $stat) {
  515. $this->line(" {$stat->source_type}: <info>{$stat->count}</info>");
  516. }
  517. }
  518. } catch (\Exception $e) {
  519. $this->line(" ⚠️ 无法获取用户日志统计信息: " . $e->getMessage());
  520. }
  521. }
  522. /**
  523. * 显示单个收集器结果
  524. *
  525. * @param array $result
  526. * @return void
  527. */
  528. private function displaySingleResult(array $result): void
  529. {
  530. $this->line("");
  531. if ($result['status'] === 'success') {
  532. $this->info("✅ 收集完成!");
  533. $this->line("📊 <comment>处理统计</comment>:");
  534. $this->line(" 📝 处理记录数: <info>{$result['processed_count']}</info>");
  535. $this->line(" ⏱️ 执行时间: <info>{$result['execution_time']}ms</info>");
  536. $this->line(" 🕐 完成时间: <info>{$result['timestamp']}</info>");
  537. if ($result['processed_count'] > 0) {
  538. $avgTime = round($result['execution_time'] / $result['processed_count'], 2);
  539. $this->line(" 📈 平均处理时间: <info>{$avgTime}ms/条</info>");
  540. }
  541. } elseif ($result['status'] === 'disabled') {
  542. $this->comment("⏸️ 收集器已禁用!");
  543. $this->line("💡 <comment>提示</comment>:");
  544. $this->line(" 收集器已通过游戏配置禁用,如需启用请修改相关配置项");
  545. } else {
  546. $this->error("❌ 收集失败!");
  547. $this->line("🚨 <comment>错误信息</comment>:");
  548. $this->line(" {$result['error']}");
  549. }
  550. $this->line("");
  551. }
  552. /**
  553. * 显示所有收集器结果
  554. *
  555. * @param array $results
  556. * @return void
  557. */
  558. private function displayAllResults(array $results): void
  559. {
  560. $this->line("");
  561. $this->info("🎉 所有收集器执行完成!");
  562. $this->line("");
  563. // 显示总体统计
  564. $this->line("📊 <comment>总体统计</comment>:");
  565. $this->line(" 📝 总处理记录数: <info>{$results['total_processed']}</info>");
  566. $this->line(" ⏱️ 总执行时间: <info>{$results['total_execution_time']}ms</info>");
  567. $this->line(" 🕐 完成时间: <info>{$results['timestamp']}</info>");
  568. if ($results['total_processed'] > 0) {
  569. $avgTime = round($results['total_execution_time'] / $results['total_processed'], 2);
  570. $this->line(" 📈 平均处理时间: <info>{$avgTime}ms/条</info>");
  571. }
  572. $this->line("");
  573. // 显示各收集器详情
  574. $this->line("📋 <comment>各收集器详情</comment>:");
  575. $successCount = 0;
  576. $failureCount = 0;
  577. foreach ($results['collectors'] as $name => $result) {
  578. if ($result['status'] === 'success') {
  579. $status = '<info>✅ 成功</info>';
  580. $successCount++;
  581. } elseif ($result['status'] === 'disabled') {
  582. $status = '<comment>⏸️ 禁用</comment>';
  583. } else {
  584. $status = '<error>❌ 失败</error>';
  585. $failureCount++;
  586. }
  587. $this->line(" 🔧 <comment>{$name}</comment>: {$status}");
  588. $this->line(" 📝 处理记录: <info>{$result['processed_count']}</info> 条");
  589. $this->line(" ⏱️ 执行时间: <info>{$result['execution_time']}</info> ms");
  590. if ($result['status'] === 'error') {
  591. $this->line(" 🚨 错误信息: <error>{$result['error']}</error>");
  592. } elseif ($result['status'] === 'disabled') {
  593. $this->line(" 💡 收集器已禁用,可通过游戏配置启用");
  594. } elseif ($result['processed_count'] > 0) {
  595. $avgTime = round($result['execution_time'] / $result['processed_count'], 2);
  596. $this->line(" 📈 平均时间: <info>{$avgTime}</info> ms/条");
  597. }
  598. $this->line("");
  599. }
  600. // 显示执行摘要
  601. $totalCollectors = $successCount + $failureCount;
  602. $this->line("📈 <comment>执行摘要</comment>:");
  603. $this->line(" 🎯 成功收集器: <info>{$successCount}/{$totalCollectors}</info>");
  604. if ($failureCount > 0) {
  605. $this->line(" ⚠️ 失败收集器: <error>{$failureCount}/{$totalCollectors}</error>");
  606. }
  607. $this->line("");
  608. }
  609. /**
  610. * 检查是否允许自动收集日志
  611. *
  612. * @return bool
  613. */
  614. private function isAutoCollectEnabled(): bool
  615. {
  616. return GameConfigService::isAutoCollectEnabled();
  617. }
  618. }