Guest User

Untitled

a guest
Mar 18th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Core\Console\Commands;
  4.  
  5. use Illuminate\Console\Command;
  6. use Carbon\Carbon;
  7. use DB;
  8.  
  9. class QueueStats extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'queue:stats';
  17.  
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = 'Queue jobs statistics';
  24.  
  25. /**
  26. * Execute the console command.
  27. *
  28. * @return mixed
  29. */
  30. public function handle()
  31. {
  32. $active = DB::table('queue_monitor')->whereNull('finished_at')->count();
  33. $active_hour = DB::table('queue_monitor')
  34. ->whereNull('finished_at')
  35. ->where('created_at', Carbon::now()->subHours(1))
  36. ->count();
  37.  
  38. $failed = DB::table('queue_monitor')->where('failed', true)->count();
  39. $failed_hour = DB::table('queue_monitor')
  40. ->where('failed', true)
  41. ->where('created_at', Carbon::now()->subHours(1))
  42. ->count();
  43.  
  44. $done = DB::table('queue_monitor')->whereNotNull('finished_at')->where('failed', false)->count();
  45. $done_hour = DB::table('queue_monitor')
  46. ->whereNotNull('finished_at')
  47. ->where('failed', false)
  48. ->where('created_at', Carbon::now()->subHours(1))
  49. ->count();
  50.  
  51. $delay = DB::table('queue_monitor')->selectRaw('avg(started_at-created_at) as avg')->whereNotNull('started_at')->first();
  52. $delay_hour = DB::table('queue_monitor')
  53. ->selectRaw('avg(started_at-created_at) as avg')
  54. ->whereNotNull('started_at')
  55. ->where('created_at', Carbon::now()->subHours(1))
  56. ->first();
  57.  
  58. $worktime = DB::table('queue_monitor')->selectRaw('avg(finished_at-started_at) as avg')->whereNotNull('finished_at')->first();
  59. $worktime_hour = DB::table('queue_monitor')
  60. ->selectRaw('avg(finished_at-started_at) as avg')
  61. ->whereNotNull('finished_at')
  62. ->where('created_at', Carbon::now()->subHours(1))
  63. ->first();
  64.  
  65. $data[] = ['Queued', $active, $active_hour];
  66. $data[] = ['Failed', $failed, $failed_hour];
  67. $data[] = ['Done', $done, $done_hour];
  68. $data[] = ['Avg waiting time', $delay->avg, ($delay_hour->avg) ? $delay_hour->avg:'-'];
  69. $data[] = ['Avg work time', $worktime->avg, ($worktime->avg) ? $worktime->avg:'-'];
  70. $this->table(['Metric', 'All time', 'Last hour'], $data);
  71.  
  72. }
  73. }
Add Comment
Please, Sign In to add comment