123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- <?php
- namespace App\Console\Commands;
- use App\Http\Models\Site;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- class Num extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'num:count';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'Command description';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- * 每日数据统计
- * @return mixed
- */
- public function handle()
- {
- $rankCon = DB::connection('rank');
- $allSites = Site::query()->with('server')->whereIn('status', [2, 3, 5, 8, 9])->get();
- DB::table('num_log')->insert([
- 'title' => 'count',
- 'content' => $allSites->count(),
- 'created_at' => date('Y-m-d H:i:s')
- ]);
- $errorCount = 0;
- foreach ($allSites as $site) {
- try {
- if (empty($site->server)) {
- continue;
- }
- $top10 = $rankCon->table('project_history')->selectRaw('top10')
- ->where(['project_id' => $site->old_id])->orderByDesc('create_time')->first();
- //流程程序开发流程 部署
- $config = [
- 'connection_name' => sprintf('connection_name_%s', $site->id),
- 'host' => $site->server->server_ip,
- 'port' => '3306',
- 'database' => $site->database,
- 'username' => $site->server->mysql_user_name,
- 'password' => $site->server->mysql_passwd,
- ];
- config_connection($config);
- $inquire = DB::connection($config['connection_name'])->table('user_msg')->count();
- $traffic = DB::connection($config['connection_name'])->table('traffic_report_hourly')->selectRaw('SUM(pv) as pvTotal')->first();
- DB::table('num')->insert([
- 'site_id' => $site->id,
- 'cn_title' => $site->cn_title,
- 'domain' => $site->domain,
- 'ymd' => date('Ymd'),
- 'traffic' => $traffic->pvTotal ?? 0,
- 'inquire' => $inquire,
- 'top10' => $top10->top10 ?? 0,
- 'created_at' => date('Y-m-d H:i:s')
- ]);
- } catch (\Throwable $throwable) {
- $errorCount++;
- Log::error(substr(var_export($throwable->getMessage(), 1), 0, 200));
- echo $site->cn_title . PHP_EOL;
- }
- }
- DB::table('num_log')->insert([
- 'title' => 'error',
- 'content' => $errorCount,
- 'created_at' => date('Y-m-d H:i:s')
- ]);
- $this->info('success');
- }
- }
|