V2boardStatistics.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Jobs\StatServerJob;
  4. use Illuminate\Console\Command;
  5. use App\Models\ServerLog;
  6. use Illuminate\Support\Facades\DB;
  7. class V2BoardStatistics extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'v2board:statistics';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = '统计任务';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return mixed
  34. */
  35. public function handle()
  36. {
  37. $this->statServer();
  38. }
  39. private function statServer()
  40. {
  41. $endAt = strtotime(date('Y-m-d'));
  42. $startAt = strtotime('-1 day', $endAt);
  43. $statistics = ServerLog::select([
  44. 'server_id',
  45. 'method as server_type',
  46. DB::raw("sum(u) as u"),
  47. DB::raw("sum(d) as d"),
  48. ])
  49. ->where('log_at', '>=', $startAt)
  50. ->where('log_at', '<', $endAt)
  51. ->groupBy('server_id', 'method')
  52. ->get()
  53. ->toArray();
  54. foreach ($statistics as $statistic) {
  55. $statistic['record_type'] = 'm';
  56. $statistic['record_at'] = $startAt;
  57. StatServerJob::dispatch($statistic);
  58. }
  59. }
  60. }