StatServer.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 StatServer extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'stat:server';
  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. $endAt = strtotime(date('Y-m-d'));
  38. $startAt = strtotime('-1 day', $endAt);
  39. $statistics = ServerLog::select([
  40. 'server_id',
  41. 'method as server_type',
  42. DB::raw("sum(u) as u"),
  43. DB::raw("sum(d) as d"),
  44. ])
  45. ->where('log_at', '>=', $startAt)
  46. ->where('log_at', '<', $endAt)
  47. ->groupBy('server_id', 'method')
  48. ->get()
  49. ->toArray();
  50. foreach ($statistics as $statistic) {
  51. $statistic['record_type'] = 'm';
  52. $statistic['record_at'] = $startAt;
  53. StatServerJob::dispatch($statistic);
  54. }
  55. }
  56. }