CollectUserLogsCommand.php 26 KB

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