CollectUserLogsCommand.php 26 KB

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