Num.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Http\Models\Site;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Log;
  7. class Num extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'num:count';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Command 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. $rankCon = DB::connection('rank');
  38. $allSites = Site::query()->with('server')->whereIn('status', [2, 3, 5, 8, 9])->get();
  39. DB::table('num_log')->insert([
  40. 'title' => 'count',
  41. 'content' => $allSites->count(),
  42. 'created_at' => date('Y-m-d H:i:s')
  43. ]);
  44. $errorCount = 0;
  45. foreach ($allSites as $site) {
  46. try {
  47. if (empty($site->server)) {
  48. continue;
  49. }
  50. $top10 = $rankCon->table('project_history')->selectRaw('top10')
  51. ->where(['project_id' => $site->old_id])->orderByDesc('create_time')->first();
  52. //流程程序开发流程 部署
  53. $config = [
  54. 'connection_name' => sprintf('connection_name_%s', $site->id),
  55. 'host' => $site->server->server_ip,
  56. 'port' => '3306',
  57. 'database' => $site->database,
  58. 'username' => $site->server->mysql_user_name,
  59. 'password' => $site->server->mysql_passwd,
  60. ];
  61. config_connection($config);
  62. $inquire = DB::connection($config['connection_name'])->table('user_msg')->count();
  63. $traffic = DB::connection($config['connection_name'])->table('traffic_report_hourly')->selectRaw('SUM(pv) as pvTotal')->first();
  64. DB::table('num')->insert([
  65. 'site_id' => $site->id,
  66. 'cn_title' => $site->cn_title,
  67. 'domain' => $site->domain,
  68. 'ymd' => date('Ymd'),
  69. 'traffic' => $traffic->pvTotal ?? 0,
  70. 'inquire' => $inquire,
  71. 'top10' => $top10->top10 ?? 0,
  72. 'created_at' => date('Y-m-d H:i:s')
  73. ]);
  74. } catch (\Throwable $throwable) {
  75. $errorCount++;
  76. Log::error(substr(var_export($throwable->getMessage(), 1), 0, 200));
  77. echo $site->cn_title . PHP_EOL;
  78. }
  79. }
  80. DB::table('num_log')->insert([
  81. 'title' => 'error',
  82. 'content' => $errorCount,
  83. 'created_at' => date('Y-m-d H:i:s')
  84. ]);
  85. $this->info('success');
  86. }
  87. }