Speed.php 1.6 KB
<?php

namespace App\Console\Commands\RankData;

use App\Helper\Arr;
use App\Helper\GoogleSpeedApi;
use App\Models\Project\DeployOptimize;
use App\Models\RankData\Speed as GoogleSpeedModel;

/**
 * Class GoogleSpeed
 * @package App\Console\Commands
 * @author zbj
 * @date 2023/5/10
 */
class Speed extends BaseCommands
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'rank_data_speed';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '排名数据-测速数据';


    /**
     * @author zbj
     * @date 2023/5/10
     */
    public function do()
    {
        $error = 0;

        $googleSpeedApi = new GoogleSpeedApi();

        //有排名api编号的项目
        $list = DeployOptimize::where('api_no', '>', 0)->pluck('domain', 'project_id')->toArray();

        foreach ($list as $project_id => $domain) {
            $model = GoogleSpeedModel::where('project_id', $project_id)->first();
            if ($model && $model->updated_date == getThisWeekStarDate()) {
                //今周已更新 跳过
                continue;
            }

            $res = $googleSpeedApi->run($domain);
            if (!$res) {
                $error++;
            }
            if (!$model) {
                $model = new GoogleSpeedModel;
            }
            $model->project_id = $project_id;
            $model->data = $res;
            $model->updated_date = date('Y-m-d');
            $model->save();
        }

        return !$error;
    }
}