作者 周海龙

合并分支 'zhl' 到 'master'

流量统计



查看合并请求 !2273
<?php
/**
* Created by PhpStorm.
* User: zhl
* Date: 2025/7/4
* Time: 11:14
*/
namespace App\Console\Commands\Statistics;
use App\Models\Project\ProjectFlow;
use App\Services\UpyunService;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
class Flow extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'project_flow_statistics';
/**
* The console command description.
*
* @var string
*/
protected $description = '项目流量统计';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct(); // 确保调用父类构造函数
}
public function handle()
{
$this->cdnStatistics();
}
public function cdnStatistics()
{
$class = new UpyunService();
list($start_at, $end_at) = $this->getTime();
$date = date('Y-m-d', strtotime($end_at));
echo 'start_at: ' . $start_at . PHP_EOL;
echo 'end_at: ' . $end_at . PHP_EOL;
dd($start_at, $end_at);
$result = $class->realTimeStatistics($start_at, $end_at);
file_put_contents(storage_path('logs/flow/' . date('YmdHis', strtotime($start_at)) . '.json'), $result);
// $result = file_get_contents(storage_path('logs/flow/' . date('YmdHis', strtotime($start_at)) . '.json'));
$result = json_decode($result, true);
$flow = [];
if (FALSE == empty($result['data']) && is_array($result['data'])) {
// 结算 所有项目 流量
foreach ($result['data'] as $item) {
if (Str::startsWith($item['key'], '/upload/p/')) {
$tmp = explode('/', $item['key']);
$flow[$tmp[3]] = FALSE == empty($flow[$tmp[3]]) ? $flow[$tmp[3]] + $item['val'] : $item['val'];
}
}
}
ksort($flow);
$total_flow = 0;
foreach ($flow as $project_id=>$val) {
ProjectFlow::flowInsert($project_id, $date, $val, $end_at);
$total_flow += $val;
}
echo 'total project: ' . count($flow) . PHP_EOL;
echo 'total flow: ' . $total_flow . PHP_EOL;
return true;
}
/**
* @return array
*/
public function getTime()
{
$last_at_log = ProjectFlow::orderBy('last_at', 'desc')->first();
$today = date('Y-m-d 00:00:00');
$start_at = $last_at_log ? $last_at_log->last_at : $today;
if (strtotime($start_at) < strtotime($today))
$start_at = $today;
$end_at = date('Y-m-d H:i:s', time() - 1);
return [$start_at, $end_at];
}
}
\ No newline at end of file
... ...
<?php
/**
* Created by PhpStorm.
* User: zhl
* Date: 2025/7/4
* Time: 15:05
*/
namespace App\Models\Project;
use App\Models\Base;
/**
* 流量统计
* Class ProjectFlow
* @package App\Models\Project
*/
class ProjectFlow extends Base
{
/**
* 表名
* @var string
*/
protected $table = 'gl_project_flow';
/**
* 流量日志记录
* @param $project_id
* @param $date
* @param $cdn_flow
* @param $last_at
* @return ProjectFlow
*/
public static function flowInsert($project_id, $date, $cdn_flow, $last_at)
{
$flow = self::where(['project_id' => $project_id, 'date' => $date])->first();
if (empty($flow)) {
$flow = new self();
$flow->project_id = $project_id;
$flow->date = $date;
$flow->cdn_flow = $cdn_flow;
} else {
$flow->cdn_flow = $flow->cdn_flow + $cdn_flow;
}
$flow->last_at = $last_at;
$flow->save();
return $flow;
}
}
\ No newline at end of file
... ...
... ... @@ -67,6 +67,47 @@ class UpyunService
}
/**
* 统计流量
* @param $start_time
* @param $end_time
* @param string $domain
* @return mixed
*/
public function commonData($start_time, $end_time, $domain = 'ecdn6.globalso.com')
{
$action = '/flow/common_data';
$param = [
'start_time' => $start_time,
'end_time' => $end_time,
'domain' => $domain,
];
// dd($param);
list($status, $result) = $this->curlRequest($action, $param, 'GET', $this->getHeader());
return $result;
}
/**
* 统计流量
* @param $start_time
* @param $end_time
* @param string $domain
* @return mixed
*/
public function realTimeStatistics($start_time, $end_time, $domain = 'ecdn6.globalso.com')
{
$action = '/flow/real_time_statistics';
$param = [
'start_time' => $start_time,
'end_time' => $end_time,
'query_domain' => $domain,
'query_type' => 'uri_flux'
];
// dd($param);
list($status, $result) = $this->curlRequest($action, $param, 'GET', $this->getHeader());
return $result;
}
/**
* 头信息需要携带授权token
* @return array
*/
... ... @@ -88,6 +129,9 @@ class UpyunService
public function curlRequest($url, $data, $method = 'POST', $header = [], $time_out = 60)
{
$url = config('custom.upyun.api_url') . $url;
if ($method == 'GET') {
$url .= '?' . http_build_query($data);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, $time_out);
curl_setopt($ch, CURLOPT_URL, $url);
... ...