CollectUserLogsCommand.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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 {--info : 显示收集器信息} {--statistics : 显示收集统计信息} {--detail : 显示详细的处理过程} {--limit=1000 : 单次处理最大记录数}';
  18. /**
  19. * 命令描述
  20. *
  21. * @var string
  22. */
  23. protected $description = '收集用户日志,将各模块的原始日志转换为用户友好的日志消息
  24. 选项说明:
  25. --info 显示收集器信息
  26. --statistics 显示收集统计信息
  27. --detail 显示详细的处理过程
  28. --limit 单次处理最大记录数(默认1000)
  29. 注意:进度追踪基于user_logs表中的已处理记录,系统会自动从上次处理位置继续收集';
  30. /**
  31. * 执行命令
  32. */
  33. public function handleRun()
  34. {
  35. // 显示统计信息
  36. if ($this->option('statistics')) {
  37. $manager = new UserLogCollectorManager();
  38. $collectorsInfo = $manager->getCollectorsInfo();
  39. $this->info("📊 收集器统计信息:");
  40. $this->line("");
  41. $this->line("收集器数量: " . count($collectorsInfo));
  42. // 动态显示所有收集器
  43. foreach ($collectorsInfo as $name => $info) {
  44. $icon = $this->getCollectorIcon($info['source_type']);
  45. $this->line("- {$name}: {$icon} {$this->getCollectorDescription($info['source_type'])}");
  46. }
  47. $this->line("");
  48. // 显示用户日志表统计
  49. try {
  50. $totalUserLogs = \App\Module\Game\Models\UserLog::count();
  51. $this->line("📋 用户日志表统计:");
  52. $this->line(" 📝 总日志数: {$totalUserLogs}");
  53. if ($totalUserLogs > 0) {
  54. $latestLog = \App\Module\Game\Models\UserLog::orderBy('created_at', 'desc')->first();
  55. $oldestLog = \App\Module\Game\Models\UserLog::orderBy('created_at', 'asc')->first();
  56. $this->line(" 🕐 最新日志时间: " . $latestLog->created_at);
  57. $this->line(" 🕐 最旧日志时间: " . $oldestLog->created_at);
  58. // 按类型统计 - 动态获取所有收集器类型
  59. $this->line(" 📊 按类型统计:");
  60. foreach ($collectorsInfo as $name => $info) {
  61. $sourceType = $info['source_type'];
  62. $count = \App\Module\Game\Models\UserLog::where('source_type', $sourceType)->count();
  63. $icon = $this->getCollectorIcon($sourceType);
  64. $description = $this->getCollectorDescription($sourceType);
  65. $this->line(" {$icon} {$description}: {$count}");
  66. }
  67. }
  68. } catch (\Exception $e) {
  69. $this->line(" ⚠️ 无法获取用户日志统计: " . $e->getMessage());
  70. }
  71. $this->line("");
  72. return 0;
  73. }
  74. $manager = new UserLogCollectorManager();
  75. // 显示收集器信息
  76. if ($this->option('info')) {
  77. $this->showCollectorsInfo($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. case 'point_logs':
  214. return \App\Module\Point\Models\PointLogModel::max('id') ?: 0;
  215. default:
  216. // 回退到直接查询
  217. $result = \Illuminate\Support\Facades\DB::table($tableName)->max('id');
  218. return $result ? (int)$result : 0;
  219. }
  220. } catch (\Exception $e) {
  221. return 0;
  222. }
  223. }
  224. /**
  225. * 从user_logs表获取最后处理的ID
  226. *
  227. * @param string $sourceTable
  228. * @param string $sourceType
  229. * @return int
  230. */
  231. private function getLastProcessedIdFromUserLogs(string $sourceTable, string $sourceType): int
  232. {
  233. try {
  234. // 对于农场收集器,需要查询多个表
  235. if ($sourceType === 'farm') {
  236. $lastLog = \App\Module\Game\Models\UserLog::where('source_type', $sourceType)
  237. ->whereIn('source_table', ['farm_harvest_logs', 'farm_upgrade_logs'])
  238. ->orderBy('source_id', 'desc')
  239. ->first();
  240. } else {
  241. $lastLog = \App\Module\Game\Models\UserLog::where('source_table', $sourceTable)
  242. ->where('source_type', $sourceType)
  243. ->orderBy('source_id', 'desc')
  244. ->first();
  245. }
  246. return $lastLog ? $lastLog->source_id : 0;
  247. } catch (\Exception $e) {
  248. return 0;
  249. }
  250. }
  251. /**
  252. * 从user_logs表获取最后处理的时间戳
  253. *
  254. * @param string $sourceTable
  255. * @param string $sourceType
  256. * @return int
  257. */
  258. private function getLastProcessedTimestampFromUserLogs(string $sourceTable, string $sourceType): int
  259. {
  260. try {
  261. $lastLog = \App\Module\Game\Models\UserLog::where('source_table', $sourceTable)
  262. ->where('source_type', $sourceType)
  263. ->orderBy('created_at', 'desc')
  264. ->first();
  265. if (!$lastLog) {
  266. return 0;
  267. }
  268. // 返回user_logs记录的创建时间戳
  269. return strtotime($lastLog->created_at);
  270. } catch (\Exception $e) {
  271. return 0;
  272. }
  273. }
  274. /**
  275. * 显示收集器信息
  276. *
  277. * @param UserLogCollectorManager $manager
  278. * @return void
  279. */
  280. private function showCollectorsInfo(UserLogCollectorManager $manager): void
  281. {
  282. $this->info("📋 注册的收集器信息:");
  283. $this->line("");
  284. $collectorsInfo = $manager->getCollectorsInfo();
  285. foreach ($collectorsInfo as $info) {
  286. $this->line("收集器: <comment>{$info['name']}</comment>");
  287. $this->line(" 类名: {$info['class']}");
  288. $this->line(" 源表: <info>{$info['source_table']}</info>");
  289. $this->line(" 类型: <info>{$info['source_type']}</info>");
  290. $this->line("");
  291. }
  292. }
  293. /**
  294. * 显示统计信息
  295. *
  296. * @param UserLogCollectorManager $manager
  297. * @return void
  298. */
  299. private function showStats(UserLogCollectorManager $manager): void
  300. {
  301. $collectorsInfo = $manager->getCollectorsInfo();
  302. $this->line("");
  303. $this->line("收集器数量: " . count($collectorsInfo));
  304. // 动态显示所有收集器
  305. foreach ($collectorsInfo as $name => $info) {
  306. $icon = $this->getCollectorIcon($info['source_type']);
  307. $this->line("- {$name}: {$icon} {$this->getCollectorDescription($info['source_type'])}");
  308. }
  309. $this->line("");
  310. }
  311. /**
  312. * 获取收集器图标
  313. *
  314. * @param string $sourceType
  315. * @return string
  316. */
  317. private function getCollectorIcon(string $sourceType): string
  318. {
  319. $icons = [
  320. 'fund' => '💰',
  321. 'item' => '📦',
  322. 'farm' => '🌾',
  323. 'point' => '⭐',
  324. 'pet' => '🐾',
  325. 'system' => '⚙️',
  326. ];
  327. return $icons[$sourceType] ?? '📄';
  328. }
  329. /**
  330. * 获取收集器描述
  331. *
  332. * @param string $sourceType
  333. * @return string
  334. */
  335. private function getCollectorDescription(string $sourceType): string
  336. {
  337. $descriptions = [
  338. 'fund' => '资金日志收集器',
  339. 'item' => '物品日志收集器',
  340. 'farm' => '农场日志收集器',
  341. 'point' => '积分日志收集器',
  342. 'pet' => '宠物日志收集器',
  343. 'system' => '系统日志收集器',
  344. ];
  345. return $descriptions[$sourceType] ?? '未知类型收集器';
  346. }
  347. /**
  348. * 获取表的记录总数
  349. *
  350. * @param string $tableName
  351. * @return int
  352. */
  353. private function getTableRecordCount(string $tableName): int
  354. {
  355. try {
  356. // 根据表名使用对应的模型
  357. switch ($tableName) {
  358. case 'fund_logs':
  359. return \App\Module\Fund\Models\FundLogModel::count();
  360. case 'item_transaction_logs':
  361. return \App\Module\GameItems\Models\ItemTransactionLog::count();
  362. case 'farm_harvest_logs':
  363. return \App\Module\Farm\Models\FarmHarvestLog::count();
  364. case 'farm_upgrade_logs':
  365. return \App\Module\Farm\Models\FarmUpgradeLog::count();
  366. case 'point_logs':
  367. return \App\Module\Point\Models\PointLogModel::count();
  368. default:
  369. // 回退到直接查询
  370. return \Illuminate\Support\Facades\DB::table($tableName)->count();
  371. }
  372. } catch (\Exception $e) {
  373. return 0;
  374. }
  375. }
  376. /**
  377. * 显示进度条
  378. *
  379. * @param float $percent
  380. * @return void
  381. */
  382. private function displayProgressBar(float $percent): void
  383. {
  384. $barLength = 20;
  385. $filledLength = (int)round(($percent / 100) * $barLength);
  386. $emptyLength = $barLength - $filledLength;
  387. $bar = str_repeat('█', $filledLength) . str_repeat('░', $emptyLength);
  388. $this->line(" 📊 [{$bar}] {$percent}%");
  389. }
  390. /**
  391. * 执行时间线收集
  392. * 改为让每个收集器独立处理,避免全局时间戳的复杂性
  393. *
  394. * @param UserLogCollectorManager $manager
  395. * @param int $limit
  396. * @param bool $detail
  397. * @return array
  398. */
  399. private function executeTimelineCollection(UserLogCollectorManager $manager, int $limit, bool $detail): array
  400. {
  401. $startTime = microtime(true);
  402. if ($detail) {
  403. $this->line("📊 执行各收集器的日志收集...");
  404. }
  405. // 直接执行所有收集器
  406. $results = $manager->collectAll();
  407. if ($results['total_processed'] == 0) {
  408. return [
  409. 'processed_count' => 0,
  410. 'execution_time' => round((microtime(true) - $startTime) * 1000, 2),
  411. 'status' => 'success',
  412. 'message' => '没有新记录需要处理',
  413. 'timestamp' => now()->toDateTimeString()
  414. ];
  415. }
  416. if ($detail) {
  417. $this->line("📝 开始处理各收集器...");
  418. foreach ($results['collectors'] as $collectorName => $result) {
  419. $this->line(" {$collectorName}: 处理了 {$result['processed_count']} 条记录");
  420. }
  421. }
  422. $endTime = microtime(true);
  423. return [
  424. 'processed_count' => $results['total_processed'],
  425. 'execution_time' => round(($endTime - $startTime) * 1000, 2),
  426. 'status' => 'success',
  427. 'timestamp' => now()->toDateTimeString(),
  428. 'details' => $results['collectors']
  429. ];
  430. }
  431. /**
  432. * 从所有收集器获取按时间线排序的记录
  433. *
  434. * @param UserLogCollectorManager $manager
  435. * @param int $lastTimestamp
  436. * @param int $limit
  437. * @return array
  438. */
  439. private function getAllRecordsByTimeline(UserLogCollectorManager $manager, int $lastTimestamp, int $limit): array
  440. {
  441. $allRecords = [];
  442. $collectorsInfo = $manager->getCollectorsInfo();
  443. foreach ($collectorsInfo as $name => $info) {
  444. $collector = $manager->getCollector($name);
  445. if (!$collector) continue;
  446. try {
  447. $records = $collector->getNewRecordsByTimePublic($lastTimestamp);
  448. foreach ($records as $record) {
  449. $timestamp = $collector->getRecordTimestampPublic($record);
  450. $allRecords[] = [
  451. 'id' => $record->id,
  452. 'timestamp' => $timestamp,
  453. 'collector' => $collector,
  454. 'record' => $record,
  455. 'source_type' => $info['source_type']
  456. ];
  457. }
  458. } catch (\Exception $e) {
  459. \Illuminate\Support\Facades\Log::error("获取收集器记录失败", [
  460. 'collector' => $name,
  461. 'error' => $e->getMessage()
  462. ]);
  463. }
  464. }
  465. // 限制记录数量
  466. if (count($allRecords) > $limit) {
  467. // 先按时间排序,然后取前N条
  468. usort($allRecords, function($a, $b) {
  469. $timeA = $a['timestamp'];
  470. $timeB = $b['timestamp'];
  471. if ($timeA == $timeB) {
  472. return $a['id'] <=> $b['id'];
  473. }
  474. return $timeA <=> $timeB;
  475. });
  476. $allRecords = array_slice($allRecords, 0, $limit);
  477. }
  478. return $allRecords;
  479. }
  480. /**
  481. * 显示时间线进度
  482. *
  483. * @return void
  484. */
  485. private function showTimelineProgress(): void
  486. {
  487. $lastTimestamp = $this->getGlobalLastProcessedTimestamp();
  488. $this->line("📈 时间线处理进度:");
  489. $this->line(" 🕐 最后处理时间: <comment>" . ($lastTimestamp > 0 ? date('Y-m-d H:i:s', $lastTimestamp) : '未开始') . "</comment>");
  490. $this->line("");
  491. }
  492. /**
  493. * 显示时间线收集结果
  494. *
  495. * @param array $result
  496. * @return void
  497. */
  498. private function displayTimelineResult(array $result): void
  499. {
  500. $this->line("");
  501. if ($result['status'] === 'success') {
  502. $this->info("✅ 时间线收集完成!");
  503. $this->line("📊 <comment>处理统计</comment>:");
  504. $this->line(" 📝 处理记录数: <info>{$result['processed_count']}</info>");
  505. $this->line(" ⏱️ 执行时间: <info>{$result['execution_time']}ms</info>");
  506. $this->line(" 🕐 完成时间: <info>{$result['timestamp']}</info>");
  507. if (isset($result['last_timestamp'])) {
  508. $this->line(" 🎯 最新的处理时间: <info>" . date('Y-m-d H:i:s', $result['last_timestamp']) . "</info>");
  509. }
  510. if ($result['processed_count'] > 0) {
  511. $avgTime = round($result['execution_time'] / $result['processed_count'], 2);
  512. $this->line(" 📈 平均处理时间: <info>{$avgTime}ms/条</info>");
  513. }
  514. if (isset($result['message'])) {
  515. $this->line(" 💡 <comment>{$result['message']}</comment>");
  516. }
  517. } else {
  518. $this->error("❌ 时间线收集失败!");
  519. if (isset($result['error'])) {
  520. $this->line("🚨 <comment>错误信息</comment>:");
  521. $this->line(" {$result['error']}");
  522. }
  523. }
  524. $this->line("");
  525. }
  526. /**
  527. * 获取全局最后处理时间戳
  528. * 从各个收集器获取最后处理的原始记录时间戳,取最小值
  529. *
  530. * @return int
  531. */
  532. private function getGlobalLastProcessedTimestamp(): int
  533. {
  534. try {
  535. $manager = new \App\Module\Game\Logics\UserLogCollectorManager();
  536. $collectorsInfo = $manager->getCollectorsInfo();
  537. $minTimestamp = PHP_INT_MAX;
  538. $hasValidTimestamp = false;
  539. foreach ($collectorsInfo as $name => $info) {
  540. $collector = $manager->getCollector($name);
  541. if (!$collector) continue;
  542. // 获取该收集器最后处理的时间戳
  543. $lastProcessedTimestamp = $this->getLastProcessedTimestampFromUserLogs($info['source_table'], $info['source_type']);
  544. if ($lastProcessedTimestamp > 0) {
  545. $minTimestamp = min($minTimestamp, $lastProcessedTimestamp);
  546. $hasValidTimestamp = true;
  547. }
  548. }
  549. return $hasValidTimestamp ? $minTimestamp : 0;
  550. } catch (\Exception $e) {
  551. return 0;
  552. }
  553. }
  554. /**
  555. * 更新全局最后处理时间戳
  556. * 不再需要手动更新,因为进度通过user_logs表自动追踪
  557. *
  558. * @param int $timestamp
  559. * @return void
  560. */
  561. private function updateGlobalLastProcessedTimestamp(int $timestamp): void
  562. {
  563. // 不再需要手动更新,进度通过user_logs表自动追踪
  564. // 这个方法保留是为了兼容性
  565. }
  566. /**
  567. * 显示用户日志表统计
  568. *
  569. * @return void
  570. */
  571. private function showUserLogStats(): void
  572. {
  573. try {
  574. // 使用模型查询,避免表名前缀问题
  575. $totalUserLogs = \App\Module\Game\Models\UserLog::count();
  576. $todayUserLogs = \App\Module\Game\Models\UserLog::whereDate('created_at', now()->toDateString())->count();
  577. $this->line("📋 <comment>用户日志表统计</comment>:");
  578. $this->line(" 📝 总日志数: <info>{$totalUserLogs}</info>");
  579. $this->line(" 📅 今日新增: <info>{$todayUserLogs}</info>");
  580. // 按来源类型统计
  581. $sourceStats = \App\Module\Game\Models\UserLog::select('source_type', \Illuminate\Support\Facades\DB::raw('count(*) as count'))
  582. ->groupBy('source_type')
  583. ->get();
  584. if ($sourceStats->isNotEmpty()) {
  585. $this->line(" 📊 按来源类型统计:");
  586. foreach ($sourceStats as $stat) {
  587. $this->line(" {$stat->source_type}: <info>{$stat->count}</info>");
  588. }
  589. }
  590. } catch (\Exception $e) {
  591. $this->line(" ⚠️ 无法获取用户日志统计信息: " . $e->getMessage());
  592. }
  593. }
  594. /**
  595. * 显示单个收集器结果
  596. *
  597. * @param array $result
  598. * @return void
  599. */
  600. private function displaySingleResult(array $result): void
  601. {
  602. $this->line("");
  603. if ($result['status'] === 'success') {
  604. $this->info("✅ 收集完成!");
  605. $this->line("📊 <comment>处理统计</comment>:");
  606. $this->line(" 📝 处理记录数: <info>{$result['processed_count']}</info>");
  607. $this->line(" ⏱️ 执行时间: <info>{$result['execution_time']}ms</info>");
  608. $this->line(" 🕐 完成时间: <info>{$result['timestamp']}</info>");
  609. if ($result['processed_count'] > 0) {
  610. $avgTime = round($result['execution_time'] / $result['processed_count'], 2);
  611. $this->line(" 📈 平均处理时间: <info>{$avgTime}ms/条</info>");
  612. }
  613. } else {
  614. $this->error("❌ 收集失败!");
  615. $this->line("🚨 <comment>错误信息</comment>:");
  616. $this->line(" {$result['error']}");
  617. }
  618. $this->line("");
  619. }
  620. /**
  621. * 显示所有收集器结果
  622. *
  623. * @param array $results
  624. * @return void
  625. */
  626. private function displayAllResults(array $results): void
  627. {
  628. $this->line("");
  629. $this->info("🎉 所有收集器执行完成!");
  630. $this->line("");
  631. // 显示总体统计
  632. $this->line("📊 <comment>总体统计</comment>:");
  633. $this->line(" 📝 总处理记录数: <info>{$results['total_processed']}</info>");
  634. $this->line(" ⏱️ 总执行时间: <info>{$results['total_execution_time']}ms</info>");
  635. $this->line(" 🕐 完成时间: <info>{$results['timestamp']}</info>");
  636. if ($results['total_processed'] > 0) {
  637. $avgTime = round($results['total_execution_time'] / $results['total_processed'], 2);
  638. $this->line(" 📈 平均处理时间: <info>{$avgTime}ms/条</info>");
  639. }
  640. $this->line("");
  641. // 显示各收集器详情
  642. $this->line("📋 <comment>各收集器详情</comment>:");
  643. $successCount = 0;
  644. $failureCount = 0;
  645. foreach ($results['collectors'] as $name => $result) {
  646. if ($result['status'] === 'success') {
  647. $status = '<info>✅ 成功</info>';
  648. $successCount++;
  649. } else {
  650. $status = '<error>❌ 失败</error>';
  651. $failureCount++;
  652. }
  653. $this->line(" 🔧 <comment>{$name}</comment>: {$status}");
  654. $this->line(" 📝 处理记录: <info>{$result['processed_count']}</info> 条");
  655. $this->line(" ⏱️ 执行时间: <info>{$result['execution_time']}</info> ms");
  656. if ($result['status'] === 'error') {
  657. $this->line(" 🚨 错误信息: <error>{$result['error']}</error>");
  658. } elseif ($result['processed_count'] > 0) {
  659. $avgTime = round($result['execution_time'] / $result['processed_count'], 2);
  660. $this->line(" 📈 平均时间: <info>{$avgTime}</info> ms/条");
  661. }
  662. $this->line("");
  663. }
  664. // 显示执行摘要
  665. $totalCollectors = $successCount + $failureCount;
  666. $this->line("📈 <comment>执行摘要</comment>:");
  667. $this->line(" 🎯 成功收集器: <info>{$successCount}/{$totalCollectors}</info>");
  668. if ($failureCount > 0) {
  669. $this->line(" ⚠️ 失败收集器: <error>{$failureCount}/{$totalCollectors}</error>");
  670. }
  671. $this->line("");
  672. }
  673. }