CollectUserLogsCommand.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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. $this->line("📝 收集器详情:");
  182. $this->line(" 名称: <comment>{$info['name']}</comment>");
  183. $this->line(" 类名: {$info['class']}");
  184. $this->line(" 源表: <info>{$info['source_table']}</info>");
  185. $this->line(" 类型: <info>{$info['source_type']}</info>");
  186. $this->line("");
  187. }
  188. /**
  189. * 显示收集器进度信息
  190. *
  191. * @param UserLogCollectorManager $manager
  192. * @param string $collectorName
  193. * @return void
  194. */
  195. private function showCollectorProgress(UserLogCollectorManager $manager, string $collectorName): void
  196. {
  197. $collectorsInfo = $manager->getCollectorsInfo();
  198. $info = $collectorsInfo[$collectorName];
  199. // 从user_logs表获取最后处理的记录ID
  200. $lastProcessedId = $this->getLastProcessedIdFromUserLogs($info['source_table'], $info['source_type']);
  201. // 获取源表的最大ID
  202. $maxId = $this->getTableMaxId($info['source_table']);
  203. // 计算待处理记录数
  204. $pendingCount = max(0, $maxId - $lastProcessedId);
  205. $this->line("📈 处理进度:");
  206. $this->line(" 最后处理ID: <comment>{$lastProcessedId}</comment>");
  207. $this->line(" 最后处理时间: <comment>" . ($lastProcessedTimestamp > 0 ? date('Y-m-d H:i:s', $lastProcessedTimestamp) : '未开始') . "</comment>");
  208. $this->line(" 源表最大ID: <comment>{$maxId}</comment>");
  209. $this->line(" 待处理记录: <comment>{$pendingCount}</comment>");
  210. $this->line("");
  211. }
  212. /**
  213. * 获取表的最大ID
  214. *
  215. * @param string $tableName
  216. * @return int
  217. */
  218. private function getTableMaxId(string $tableName): int
  219. {
  220. try {
  221. // 根据表名使用对应的模型
  222. switch ($tableName) {
  223. case 'fund_logs':
  224. return \App\Module\Fund\Models\FundLogModel::max('id') ?: 0;
  225. case 'item_transaction_logs':
  226. return \App\Module\GameItems\Models\ItemTransactionLog::max('id') ?: 0;
  227. case 'farm_harvest_logs':
  228. return \App\Module\Farm\Models\FarmHarvestLog::max('id') ?: 0;
  229. case 'farm_upgrade_logs':
  230. return \App\Module\Farm\Models\FarmUpgradeLog::max('id') ?: 0;
  231. case 'point_logs':
  232. return \App\Module\Point\Models\PointLogModel::max('id') ?: 0;
  233. default:
  234. // 回退到直接查询
  235. $result = \Illuminate\Support\Facades\DB::table($tableName)->max('id');
  236. return $result ? (int)$result : 0;
  237. }
  238. } catch (\Exception $e) {
  239. return 0;
  240. }
  241. }
  242. /**
  243. * 从user_logs表获取最后处理的ID
  244. *
  245. * @param string $sourceTable
  246. * @param string $sourceType
  247. * @return int
  248. */
  249. private function getLastProcessedIdFromUserLogs(string $sourceTable, string $sourceType): int
  250. {
  251. try {
  252. // 对于农场收集器,需要查询多个表
  253. if ($sourceType === 'farm') {
  254. $lastLog = \App\Module\Game\Models\UserLog::where('source_type', $sourceType)
  255. ->whereIn('source_table', ['farm_harvest_logs', 'farm_upgrade_logs'])
  256. ->orderBy('source_id', 'desc')
  257. ->first();
  258. } else {
  259. $lastLog = \App\Module\Game\Models\UserLog::where('source_table', $sourceTable)
  260. ->where('source_type', $sourceType)
  261. ->orderBy('source_id', 'desc')
  262. ->first();
  263. }
  264. return $lastLog ? $lastLog->source_id : 0;
  265. } catch (\Exception $e) {
  266. return 0;
  267. }
  268. }
  269. // 注意:getLastProcessedTimestampFromUserLogs方法已移除
  270. // 当前使用基于ID的进度追踪,不再需要时间戳追踪
  271. /**
  272. * 显示收集器信息
  273. *
  274. * @param UserLogCollectorManager $manager
  275. * @return void
  276. */
  277. private function showCollectorsInfo(UserLogCollectorManager $manager): void
  278. {
  279. $this->info("📋 注册的收集器信息:");
  280. $this->line("");
  281. $collectorsInfo = $manager->getCollectorsInfo();
  282. foreach ($collectorsInfo as $info) {
  283. $this->line("收集器: <comment>{$info['name']}</comment>");
  284. $this->line(" 类名: {$info['class']}");
  285. $this->line(" 源表: <info>{$info['source_table']}</info>");
  286. $this->line(" 类型: <info>{$info['source_type']}</info>");
  287. $this->line("");
  288. }
  289. }
  290. /**
  291. * 显示统计信息
  292. *
  293. * @param UserLogCollectorManager $manager
  294. * @return void
  295. */
  296. private function showStats(UserLogCollectorManager $manager): void
  297. {
  298. $collectorsInfo = $manager->getCollectorsInfo();
  299. $this->line("");
  300. $this->line("收集器数量: " . count($collectorsInfo));
  301. // 动态显示所有收集器
  302. foreach ($collectorsInfo as $name => $info) {
  303. $icon = $this->getCollectorIcon($info['source_type']);
  304. $description = $this->getCollectorNameDescription($name);
  305. $this->line("- {$name}: {$icon} {$description}");
  306. }
  307. $this->line("");
  308. }
  309. /**
  310. * 获取收集器图标
  311. *
  312. * @param string $sourceType
  313. * @return string
  314. */
  315. private function getCollectorIcon(string $sourceType): string
  316. {
  317. $icons = [
  318. 'fund' => '💰',
  319. 'item' => '📦',
  320. 'farm' => '🌾',
  321. 'point' => '⭐',
  322. 'pet' => '🐾',
  323. 'system' => '⚙️',
  324. ];
  325. return $icons[$sourceType] ?? '📄';
  326. }
  327. /**
  328. * 获取收集器描述
  329. *
  330. * @param string $sourceType
  331. * @return string
  332. */
  333. private function getCollectorDescription(string $sourceType): string
  334. {
  335. $descriptions = [
  336. 'fund' => '资金日志收集器',
  337. 'item' => '物品日志收集器',
  338. 'farm' => '农场日志收集器',
  339. 'point' => '积分日志收集器',
  340. 'pet' => '宠物日志收集器',
  341. 'system' => '系统日志收集器',
  342. ];
  343. return $descriptions[$sourceType] ?? '未知类型收集器';
  344. }
  345. /**
  346. * 获取收集器名称描述(基于收集器名称而非源类型)
  347. *
  348. * @param string $collectorName
  349. * @return string
  350. */
  351. private function getCollectorNameDescription(string $collectorName): string
  352. {
  353. $descriptions = [
  354. 'fund' => '资金日志收集器',
  355. 'item' => '物品日志收集器',
  356. 'farm_harvest' => '农场收获日志收集器',
  357. 'farm_upgrade' => '农场升级日志收集器',
  358. 'point' => '积分日志收集器',
  359. 'pet' => '宠物日志收集器',
  360. 'system' => '系统日志收集器',
  361. ];
  362. return $descriptions[$collectorName] ?? '未知收集器';
  363. }
  364. /**
  365. * 获取表的记录总数
  366. *
  367. * @param string $tableName
  368. * @return int
  369. */
  370. private function getTableRecordCount(string $tableName): int
  371. {
  372. try {
  373. // 根据表名使用对应的模型
  374. switch ($tableName) {
  375. case 'fund_logs':
  376. return \App\Module\Fund\Models\FundLogModel::count();
  377. case 'item_transaction_logs':
  378. return \App\Module\GameItems\Models\ItemTransactionLog::count();
  379. case 'farm_harvest_logs':
  380. return \App\Module\Farm\Models\FarmHarvestLog::count();
  381. case 'farm_upgrade_logs':
  382. return \App\Module\Farm\Models\FarmUpgradeLog::count();
  383. case 'point_logs':
  384. return \App\Module\Point\Models\PointLogModel::count();
  385. default:
  386. // 回退到直接查询
  387. return \Illuminate\Support\Facades\DB::table($tableName)->count();
  388. }
  389. } catch (\Exception $e) {
  390. return 0;
  391. }
  392. }
  393. /**
  394. * 显示进度条
  395. *
  396. * @param float $percent
  397. * @return void
  398. */
  399. private function displayProgressBar(float $percent): void
  400. {
  401. $barLength = 20;
  402. $filledLength = (int)round(($percent / 100) * $barLength);
  403. $emptyLength = $barLength - $filledLength;
  404. $bar = str_repeat('█', $filledLength) . str_repeat('░', $emptyLength);
  405. $this->line(" 📊 [{$bar}] {$percent}%");
  406. }
  407. /**
  408. * 执行时间线收集
  409. * 改为让每个收集器独立处理,避免全局时间戳的复杂性
  410. *
  411. * @param UserLogCollectorManager $manager
  412. * @param int $limit
  413. * @param bool $detail
  414. * @return array
  415. */
  416. private function executeTimelineCollection(UserLogCollectorManager $manager, int $limit, bool $detail): array
  417. {
  418. $startTime = microtime(true);
  419. if ($detail) {
  420. $this->line("📊 执行各收集器的日志收集...");
  421. }
  422. // 直接执行所有收集器,传递限制参数
  423. $results = $manager->collectAll($limit);
  424. if ($results['total_processed'] == 0) {
  425. return [
  426. 'processed_count' => 0,
  427. 'execution_time' => round((microtime(true) - $startTime) * 1000, 2),
  428. 'status' => 'success',
  429. 'message' => '没有新记录需要处理',
  430. 'timestamp' => now()->toDateTimeString()
  431. ];
  432. }
  433. if ($detail) {
  434. $this->line("📝 开始处理各收集器...");
  435. foreach ($results['collectors'] as $collectorName => $result) {
  436. $this->line(" {$collectorName}: 处理了 {$result['processed_count']} 条记录");
  437. }
  438. }
  439. $endTime = microtime(true);
  440. return [
  441. 'processed_count' => $results['total_processed'],
  442. 'execution_time' => round(($endTime - $startTime) * 1000, 2),
  443. 'status' => 'success',
  444. 'timestamp' => now()->toDateTimeString(),
  445. 'details' => $results['collectors']
  446. ];
  447. }
  448. // 注意:getAllRecordsByTimeline方法已移除
  449. // 当前使用基于ID的进度追踪,每个收集器独立处理,不再需要全局时间线排序
  450. /**
  451. * 显示收集器进度信息
  452. * 基于ID的进度追踪,显示各收集器的处理状态
  453. *
  454. * @return void
  455. */
  456. private function showTimelineProgress(): void
  457. {
  458. $this->line("📈 收集器进度状态:");
  459. try {
  460. $manager = new \App\Module\Game\Logics\UserLogCollectorManager();
  461. $collectorsInfo = $manager->getCollectorsInfo();
  462. foreach ($collectorsInfo as $name => $info) {
  463. $lastProcessedId = $this->getLastProcessedIdFromUserLogs($info['source_table'], $info['source_type']);
  464. $maxId = $this->getTableMaxId($info['source_table']);
  465. $pendingCount = max(0, $maxId - $lastProcessedId);
  466. $this->line(" 🔧 {$name}: 最后处理ID <comment>{$lastProcessedId}</comment>, 待处理 <info>{$pendingCount}</info> 条");
  467. }
  468. } catch (\Exception $e) {
  469. $this->line(" ⚠️ 无法获取进度信息: " . $e->getMessage());
  470. }
  471. $this->line("");
  472. }
  473. /**
  474. * 显示时间线收集结果
  475. *
  476. * @param array $result
  477. * @return void
  478. */
  479. private function displayTimelineResult(array $result): void
  480. {
  481. $this->line("");
  482. if ($result['status'] === 'success') {
  483. $this->info("✅ 时间线收集完成!");
  484. $this->line("📊 <comment>处理统计</comment>:");
  485. $this->line(" 📝 处理记录数: <info>{$result['processed_count']}</info>");
  486. $this->line(" ⏱️ 执行时间: <info>{$result['execution_time']}ms</info>");
  487. $this->line(" 🕐 完成时间: <info>{$result['timestamp']}</info>");
  488. if (isset($result['last_timestamp'])) {
  489. $this->line(" 🎯 最新的处理时间: <info>" . date('Y-m-d H:i:s', $result['last_timestamp']) . "</info>");
  490. }
  491. if ($result['processed_count'] > 0) {
  492. $avgTime = round($result['execution_time'] / $result['processed_count'], 2);
  493. $this->line(" 📈 平均处理时间: <info>{$avgTime}ms/条</info>");
  494. }
  495. if (isset($result['message'])) {
  496. $this->line(" 💡 <comment>{$result['message']}</comment>");
  497. }
  498. } else {
  499. $this->error("❌ 时间线收集失败!");
  500. if (isset($result['error'])) {
  501. $this->line("🚨 <comment>错误信息</comment>:");
  502. $this->line(" {$result['error']}");
  503. }
  504. }
  505. $this->line("");
  506. }
  507. // 注意:以下时间戳相关方法已移除,因为当前使用基于ID的进度追踪:
  508. // - getGlobalLastProcessedTimestamp(): 全局时间戳追踪已废弃
  509. // - updateGlobalLastProcessedTimestamp(): 手动更新时间戳已废弃
  510. //
  511. // 当前进度追踪机制:
  512. // 1. 每个收集器通过getLastProcessedId()获取最后处理的记录ID
  513. // 2. 进度通过user_logs表中的source_id字段自动维护
  514. // 3. 无需手动重置或更新进度
  515. /**
  516. * 显示用户日志表统计
  517. *
  518. * @return void
  519. */
  520. private function showUserLogStats(): void
  521. {
  522. try {
  523. // 使用模型查询,避免表名前缀问题
  524. $totalUserLogs = \App\Module\Game\Models\UserLog::count();
  525. $todayUserLogs = \App\Module\Game\Models\UserLog::whereDate('created_at', now()->toDateString())->count();
  526. $this->line("📋 <comment>用户日志表统计</comment>:");
  527. $this->line(" 📝 总日志数: <info>{$totalUserLogs}</info>");
  528. $this->line(" 📅 今日新增: <info>{$todayUserLogs}</info>");
  529. // 按来源类型统计
  530. $sourceStats = \App\Module\Game\Models\UserLog::select('source_type', \Illuminate\Support\Facades\DB::raw('count(*) as count'))
  531. ->groupBy('source_type')
  532. ->get();
  533. if ($sourceStats->isNotEmpty()) {
  534. $this->line(" 📊 按来源类型统计:");
  535. foreach ($sourceStats as $stat) {
  536. $this->line(" {$stat->source_type}: <info>{$stat->count}</info>");
  537. }
  538. }
  539. } catch (\Exception $e) {
  540. $this->line(" ⚠️ 无法获取用户日志统计信息: " . $e->getMessage());
  541. }
  542. }
  543. /**
  544. * 显示单个收集器结果
  545. *
  546. * @param array $result
  547. * @return void
  548. */
  549. private function displaySingleResult(array $result): void
  550. {
  551. $this->line("");
  552. if ($result['status'] === 'success') {
  553. $this->info("✅ 收集完成!");
  554. $this->line("📊 <comment>处理统计</comment>:");
  555. $this->line(" 📝 处理记录数: <info>{$result['processed_count']}</info>");
  556. $this->line(" ⏱️ 执行时间: <info>{$result['execution_time']}ms</info>");
  557. $this->line(" 🕐 完成时间: <info>{$result['timestamp']}</info>");
  558. if ($result['processed_count'] > 0) {
  559. $avgTime = round($result['execution_time'] / $result['processed_count'], 2);
  560. $this->line(" 📈 平均处理时间: <info>{$avgTime}ms/条</info>");
  561. }
  562. } else {
  563. $this->error("❌ 收集失败!");
  564. $this->line("🚨 <comment>错误信息</comment>:");
  565. $this->line(" {$result['error']}");
  566. }
  567. $this->line("");
  568. }
  569. /**
  570. * 显示所有收集器结果
  571. *
  572. * @param array $results
  573. * @return void
  574. */
  575. private function displayAllResults(array $results): void
  576. {
  577. $this->line("");
  578. $this->info("🎉 所有收集器执行完成!");
  579. $this->line("");
  580. // 显示总体统计
  581. $this->line("📊 <comment>总体统计</comment>:");
  582. $this->line(" 📝 总处理记录数: <info>{$results['total_processed']}</info>");
  583. $this->line(" ⏱️ 总执行时间: <info>{$results['total_execution_time']}ms</info>");
  584. $this->line(" 🕐 完成时间: <info>{$results['timestamp']}</info>");
  585. if ($results['total_processed'] > 0) {
  586. $avgTime = round($results['total_execution_time'] / $results['total_processed'], 2);
  587. $this->line(" 📈 平均处理时间: <info>{$avgTime}ms/条</info>");
  588. }
  589. $this->line("");
  590. // 显示各收集器详情
  591. $this->line("📋 <comment>各收集器详情</comment>:");
  592. $successCount = 0;
  593. $failureCount = 0;
  594. foreach ($results['collectors'] as $name => $result) {
  595. if ($result['status'] === 'success') {
  596. $status = '<info>✅ 成功</info>';
  597. $successCount++;
  598. } else {
  599. $status = '<error>❌ 失败</error>';
  600. $failureCount++;
  601. }
  602. $this->line(" 🔧 <comment>{$name}</comment>: {$status}");
  603. $this->line(" 📝 处理记录: <info>{$result['processed_count']}</info> 条");
  604. $this->line(" ⏱️ 执行时间: <info>{$result['execution_time']}</info> ms");
  605. if ($result['status'] === 'error') {
  606. $this->line(" 🚨 错误信息: <error>{$result['error']}</error>");
  607. } elseif ($result['processed_count'] > 0) {
  608. $avgTime = round($result['execution_time'] / $result['processed_count'], 2);
  609. $this->line(" 📈 平均时间: <info>{$avgTime}</info> ms/条");
  610. }
  611. $this->line("");
  612. }
  613. // 显示执行摘要
  614. $totalCollectors = $successCount + $failureCount;
  615. $this->line("📈 <comment>执行摘要</comment>:");
  616. $this->line(" 🎯 成功收集器: <info>{$successCount}/{$totalCollectors}</info>");
  617. if ($failureCount > 0) {
  618. $this->line(" ⚠️ 失败收集器: <error>{$failureCount}/{$totalCollectors}</error>");
  619. }
  620. $this->line("");
  621. }
  622. /**
  623. * 检查是否允许自动收集日志
  624. *
  625. * @return bool
  626. */
  627. private function isAutoCollectEnabled(): bool
  628. {
  629. return GameConfigService::isAutoCollectEnabled();
  630. }
  631. }