V2boardStatistics.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\StatServer;
  4. use App\Models\StatUser;
  5. use App\Services\StatisticalService;
  6. use Illuminate\Console\Command;
  7. use App\Models\Stat;
  8. use Illuminate\Support\Facades\DB;
  9. class V2boardStatistics extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'v2board:statistics';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = '统计任务';
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. }
  32. /**
  33. * Execute the console command.
  34. *
  35. * @return mixed
  36. */
  37. public function handle()
  38. {
  39. $startAt = microtime(true);
  40. ini_set('memory_limit', -1);
  41. $this->statUser();
  42. $this->statServer();
  43. $this->stat();
  44. info('统计任务执行完毕。耗时:' . (microtime(true) - $startAt) / 1000);
  45. }
  46. private function statServer()
  47. {
  48. try {
  49. DB::beginTransaction();
  50. $createdAt = time();
  51. $recordAt = strtotime('-1 day', strtotime(date('Y-m-d')));
  52. $statService = new StatisticalService();
  53. $statService->setStartAt($recordAt);
  54. $statService->setServerStats();
  55. $stats = $statService->getStatServer();
  56. foreach ($stats as $stat) {
  57. if (!StatServer::insert([
  58. 'server_id' => $stat['server_id'],
  59. 'server_type' => $stat['server_type'],
  60. 'u' => $stat['u'],
  61. 'd' => $stat['d'],
  62. 'created_at' => $createdAt,
  63. 'updated_at' => $createdAt,
  64. 'record_type' => 'd',
  65. 'record_at' => $recordAt
  66. ])) {
  67. throw new \Exception('stat server fail');
  68. }
  69. }
  70. DB::commit();
  71. $statService->clearStatServer();
  72. } catch (\Exception $e) {
  73. DB::rollback();
  74. \Log::error($e->getMessage(), ['exception' => $e]);
  75. }
  76. }
  77. private function statUser()
  78. {
  79. try {
  80. DB::beginTransaction();
  81. $createdAt = time();
  82. $recordAt = strtotime('-1 day', strtotime(date('Y-m-d')));
  83. $statService = new StatisticalService();
  84. $statService->setStartAt($recordAt);
  85. $statService->setUserStats();
  86. $stats = $statService->getStatUser();
  87. foreach ($stats as $stat) {
  88. if (!StatUser::insert([
  89. 'user_id' => $stat['user_id'],
  90. 'u' => $stat['u'],
  91. 'd' => $stat['d'],
  92. 'server_rate' => $stat['server_rate'],
  93. 'created_at' => $createdAt,
  94. 'updated_at' => $createdAt,
  95. 'record_type' => 'd',
  96. 'record_at' => $recordAt
  97. ])) {
  98. throw new \Exception('stat user fail');
  99. }
  100. }
  101. DB::commit();
  102. $statService->clearStatUser();
  103. } catch (\Exception $e) {
  104. DB::rollback();
  105. \Log::error($e->getMessage(), ['exception' => $e]);
  106. }
  107. }
  108. private function stat()
  109. {
  110. try {
  111. $endAt = strtotime(date('Y-m-d'));
  112. $startAt = strtotime('-1 day', $endAt);
  113. $statisticalService = new StatisticalService();
  114. $statisticalService->setStartAt($startAt);
  115. $statisticalService->setEndAt($endAt);
  116. $data = $statisticalService->generateStatData();
  117. $data['record_at'] = $startAt;
  118. $data['record_type'] = 'd';
  119. $statistic = Stat::where('record_at', $startAt)
  120. ->where('record_type', 'd')
  121. ->first();
  122. if ($statistic) {
  123. $statistic->update($data);
  124. return;
  125. }
  126. Stat::create($data);
  127. } catch (\Exception $e) {
  128. \Log::error($e->getMessage(), ['exception' => $e]);
  129. }
  130. }
  131. }