TestPetSkillProcessLog.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Str;
  5. use App\Module\Pet\Models\PetActiveSkill;
  6. use App\Module\Pet\Models\PetSkillProcessLog;
  7. use App\Module\Pet\Jobs\ProcessActiveSkillsJob;
  8. /**
  9. * 测试宠物技能处理日志功能
  10. */
  11. class TestPetSkillProcessLog extends Command
  12. {
  13. /**
  14. * 命令签名
  15. *
  16. * @var string
  17. */
  18. protected $signature = 'pet:test-skill-process-log {--user-id= : 指定用户ID进行测试}';
  19. /**
  20. * 命令描述
  21. *
  22. * @var string
  23. */
  24. protected $description = '测试宠物技能处理日志功能';
  25. /**
  26. * 执行命令
  27. *
  28. * @return int
  29. */
  30. public function handle()
  31. {
  32. $userId = $this->option('user-id');
  33. $this->info('开始测试宠物技能处理日志功能...');
  34. // 1. 查看当前激活的技能
  35. $this->info('1. 查看当前激活的技能');
  36. $activeSkills = PetActiveSkill::active();
  37. if ($userId) {
  38. $activeSkills = $activeSkills->whereHas('pet', function ($query) use ($userId) {
  39. $query->where('user_id', $userId);
  40. });
  41. }
  42. $activeSkills = $activeSkills->get();
  43. if ($activeSkills->isEmpty()) {
  44. $this->warn('没有找到激活的技能');
  45. return 0;
  46. }
  47. $this->table(
  48. ['ID', '宠物ID', '用户ID', '技能名称', '状态', '开始时间', '结束时间'],
  49. $activeSkills->map(function ($skill) {
  50. return [
  51. $skill->id,
  52. $skill->pet_id,
  53. $skill->pet->user_id,
  54. $skill->skill_name,
  55. $skill->status,
  56. $skill->start_time->format('Y-m-d H:i:s'),
  57. $skill->end_time->format('Y-m-d H:i:s'),
  58. ];
  59. })->toArray()
  60. );
  61. // 2. 执行技能处理任务
  62. $this->info('2. 执行技能处理任务');
  63. $job = new ProcessActiveSkillsJob();
  64. $result = $job->run();
  65. if ($result) {
  66. $this->info('技能处理任务执行成功');
  67. } else {
  68. $this->error('技能处理任务执行失败');
  69. return 1;
  70. }
  71. // 3. 查看生成的处理日志
  72. $this->info('3. 查看生成的处理日志(最近10条)');
  73. $logs = PetSkillProcessLog::with(['activeSkill', 'pet'])
  74. ->orderBy('processed_at', 'desc')
  75. ->limit(10);
  76. if ($userId) {
  77. $logs = $logs->where('user_id', $userId);
  78. }
  79. $logs = $logs->get();
  80. if ($logs->isEmpty()) {
  81. $this->warn('没有找到处理日志');
  82. } else {
  83. $this->table(
  84. ['ID', '技能ID', '用户ID', '技能名称', '状态', '原因', '执行时间(秒)', '处理时间'],
  85. $logs->map(function ($log) {
  86. return [
  87. $log->id,
  88. $log->active_skill_id,
  89. $log->user_id,
  90. $log->skill_name,
  91. $log->process_status,
  92. Str::limit($log->process_reason, 30),
  93. $log->execution_time,
  94. $log->processed_at->format('Y-m-d H:i:s'),
  95. ];
  96. })->toArray()
  97. );
  98. }
  99. // 4. 显示处理数据详情
  100. if ($logs->isNotEmpty()) {
  101. $this->info('4. 显示最新日志的处理数据详情');
  102. $latestLog = $logs->first();
  103. $this->line("日志ID: {$latestLog->id}");
  104. $this->line("技能名称: {$latestLog->skill_name}");
  105. $this->line("处理状态: {$latestLog->process_status}");
  106. $this->line("处理原因: {$latestLog->process_reason}");
  107. $this->line("执行时间: {$latestLog->execution_time}秒");
  108. if ($latestLog->process_data) {
  109. $this->line("处理数据:");
  110. $this->line(json_encode($latestLog->process_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
  111. }
  112. }
  113. // 5. 统计信息
  114. $this->info('5. 处理日志统计信息');
  115. $stats = PetSkillProcessLog::selectRaw('
  116. process_status,
  117. skill_name,
  118. COUNT(*) as count,
  119. AVG(execution_time) as avg_execution_time,
  120. MAX(execution_time) as max_execution_time
  121. ')
  122. ->where('processed_at', '>=', now()->subHour())
  123. ->groupBy('process_status', 'skill_name')
  124. ->get();
  125. if ($stats->isNotEmpty()) {
  126. $this->table(
  127. ['状态', '技能名称', '次数', '平均执行时间(秒)', '最大执行时间(秒)'],
  128. $stats->map(function ($stat) {
  129. return [
  130. $stat->process_status,
  131. $stat->skill_name,
  132. $stat->count,
  133. round($stat->avg_execution_time, 3),
  134. round($stat->max_execution_time, 3),
  135. ];
  136. })->toArray()
  137. );
  138. } else {
  139. $this->warn('最近1小时内没有处理日志');
  140. }
  141. $this->info('测试完成!');
  142. return 0;
  143. }
  144. }