正在显示
3 个修改的文件
包含
187 行增加
和
0 行删除
app/Console/Commands/Statistics/Flow.php
0 → 100644
| 1 | +<?php | ||
| 2 | +/** | ||
| 3 | + * Created by PhpStorm. | ||
| 4 | + * User: zhl | ||
| 5 | + * Date: 2025/7/4 | ||
| 6 | + * Time: 11:14 | ||
| 7 | + */ | ||
| 8 | +namespace App\Console\Commands\Statistics; | ||
| 9 | + | ||
| 10 | +use App\Models\Project\ProjectFlow; | ||
| 11 | +use App\Services\UpyunService; | ||
| 12 | +use Illuminate\Console\Command; | ||
| 13 | +use Illuminate\Support\Str; | ||
| 14 | + | ||
| 15 | +class Flow extends Command | ||
| 16 | +{ | ||
| 17 | + /** | ||
| 18 | + * The name and signature of the console command. | ||
| 19 | + * | ||
| 20 | + * @var string | ||
| 21 | + */ | ||
| 22 | + protected $signature = 'project_flow_statistics'; | ||
| 23 | + | ||
| 24 | + /** | ||
| 25 | + * The console command description. | ||
| 26 | + * | ||
| 27 | + * @var string | ||
| 28 | + */ | ||
| 29 | + protected $description = '项目流量统计'; | ||
| 30 | + | ||
| 31 | + /** | ||
| 32 | + * Create a new command instance. | ||
| 33 | + * | ||
| 34 | + * @return void | ||
| 35 | + */ | ||
| 36 | + public function __construct() | ||
| 37 | + { | ||
| 38 | + parent::__construct(); // 确保调用父类构造函数 | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + public function handle() | ||
| 42 | + { | ||
| 43 | + $this->cdnStatistics(); | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + public function cdnStatistics() | ||
| 47 | + { | ||
| 48 | + $class = new UpyunService(); | ||
| 49 | + | ||
| 50 | + list($start_at, $end_at) = $this->getTime(); | ||
| 51 | + $date = date('Y-m-d', strtotime($end_at)); | ||
| 52 | + echo 'start_at: ' . $start_at . PHP_EOL; | ||
| 53 | + echo 'end_at: ' . $end_at . PHP_EOL; | ||
| 54 | + dd($start_at, $end_at); | ||
| 55 | + $result = $class->realTimeStatistics($start_at, $end_at); | ||
| 56 | + | ||
| 57 | + file_put_contents(storage_path('logs/flow/' . date('YmdHis', strtotime($start_at)) . '.json'), $result); | ||
| 58 | +// $result = file_get_contents(storage_path('logs/flow/' . date('YmdHis', strtotime($start_at)) . '.json')); | ||
| 59 | + $result = json_decode($result, true); | ||
| 60 | + $flow = []; | ||
| 61 | + if (FALSE == empty($result['data']) && is_array($result['data'])) { | ||
| 62 | + // 结算 所有项目 流量 | ||
| 63 | + foreach ($result['data'] as $item) { | ||
| 64 | + if (Str::startsWith($item['key'], '/upload/p/')) { | ||
| 65 | + $tmp = explode('/', $item['key']); | ||
| 66 | + $flow[$tmp[3]] = FALSE == empty($flow[$tmp[3]]) ? $flow[$tmp[3]] + $item['val'] : $item['val']; | ||
| 67 | + } | ||
| 68 | + } | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + ksort($flow); | ||
| 72 | + $total_flow = 0; | ||
| 73 | + foreach ($flow as $project_id=>$val) { | ||
| 74 | + ProjectFlow::flowInsert($project_id, $date, $val, $end_at); | ||
| 75 | + $total_flow += $val; | ||
| 76 | + } | ||
| 77 | + echo 'total project: ' . count($flow) . PHP_EOL; | ||
| 78 | + echo 'total flow: ' . $total_flow . PHP_EOL; | ||
| 79 | + return true; | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + /** | ||
| 83 | + * @return array | ||
| 84 | + */ | ||
| 85 | + public function getTime() | ||
| 86 | + { | ||
| 87 | + $last_at_log = ProjectFlow::orderBy('last_at', 'desc')->first(); | ||
| 88 | + $today = date('Y-m-d 00:00:00'); | ||
| 89 | + $start_at = $last_at_log ? $last_at_log->last_at : $today; | ||
| 90 | + if (strtotime($start_at) < strtotime($today)) | ||
| 91 | + $start_at = $today; | ||
| 92 | + $end_at = date('Y-m-d H:i:s', time() - 1); | ||
| 93 | + return [$start_at, $end_at]; | ||
| 94 | + } | ||
| 95 | +} |
app/Models/Project/ProjectFlow.php
0 → 100644
| 1 | +<?php | ||
| 2 | +/** | ||
| 3 | + * Created by PhpStorm. | ||
| 4 | + * User: zhl | ||
| 5 | + * Date: 2025/7/4 | ||
| 6 | + * Time: 15:05 | ||
| 7 | + */ | ||
| 8 | +namespace App\Models\Project; | ||
| 9 | + | ||
| 10 | +use App\Models\Base; | ||
| 11 | + | ||
| 12 | +/** | ||
| 13 | + * 流量统计 | ||
| 14 | + * Class ProjectFlow | ||
| 15 | + * @package App\Models\Project | ||
| 16 | + */ | ||
| 17 | +class ProjectFlow extends Base | ||
| 18 | +{ | ||
| 19 | + /** | ||
| 20 | + * 表名 | ||
| 21 | + * @var string | ||
| 22 | + */ | ||
| 23 | + protected $table = 'gl_project_flow'; | ||
| 24 | + | ||
| 25 | + /** | ||
| 26 | + * 流量日志记录 | ||
| 27 | + * @param $project_id | ||
| 28 | + * @param $date | ||
| 29 | + * @param $cdn_flow | ||
| 30 | + * @param $last_at | ||
| 31 | + * @return ProjectFlow | ||
| 32 | + */ | ||
| 33 | + public static function flowInsert($project_id, $date, $cdn_flow, $last_at) | ||
| 34 | + { | ||
| 35 | + $flow = self::where(['project_id' => $project_id, 'date' => $date])->first(); | ||
| 36 | + if (empty($flow)) { | ||
| 37 | + $flow = new self(); | ||
| 38 | + $flow->project_id = $project_id; | ||
| 39 | + $flow->date = $date; | ||
| 40 | + $flow->cdn_flow = $cdn_flow; | ||
| 41 | + } else { | ||
| 42 | + $flow->cdn_flow = $flow->cdn_flow + $cdn_flow; | ||
| 43 | + } | ||
| 44 | + $flow->last_at = $last_at; | ||
| 45 | + $flow->save(); | ||
| 46 | + return $flow; | ||
| 47 | + } | ||
| 48 | +} |
| @@ -67,6 +67,47 @@ class UpyunService | @@ -67,6 +67,47 @@ class UpyunService | ||
| 67 | } | 67 | } |
| 68 | 68 | ||
| 69 | /** | 69 | /** |
| 70 | + * 统计流量 | ||
| 71 | + * @param $start_time | ||
| 72 | + * @param $end_time | ||
| 73 | + * @param string $domain | ||
| 74 | + * @return mixed | ||
| 75 | + */ | ||
| 76 | + public function commonData($start_time, $end_time, $domain = 'ecdn6.globalso.com') | ||
| 77 | + { | ||
| 78 | + $action = '/flow/common_data'; | ||
| 79 | + $param = [ | ||
| 80 | + 'start_time' => $start_time, | ||
| 81 | + 'end_time' => $end_time, | ||
| 82 | + 'domain' => $domain, | ||
| 83 | + ]; | ||
| 84 | +// dd($param); | ||
| 85 | + list($status, $result) = $this->curlRequest($action, $param, 'GET', $this->getHeader()); | ||
| 86 | + return $result; | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + /** | ||
| 90 | + * 统计流量 | ||
| 91 | + * @param $start_time | ||
| 92 | + * @param $end_time | ||
| 93 | + * @param string $domain | ||
| 94 | + * @return mixed | ||
| 95 | + */ | ||
| 96 | + public function realTimeStatistics($start_time, $end_time, $domain = 'ecdn6.globalso.com') | ||
| 97 | + { | ||
| 98 | + $action = '/flow/real_time_statistics'; | ||
| 99 | + $param = [ | ||
| 100 | + 'start_time' => $start_time, | ||
| 101 | + 'end_time' => $end_time, | ||
| 102 | + 'query_domain' => $domain, | ||
| 103 | + 'query_type' => 'uri_flux' | ||
| 104 | + ]; | ||
| 105 | +// dd($param); | ||
| 106 | + list($status, $result) = $this->curlRequest($action, $param, 'GET', $this->getHeader()); | ||
| 107 | + return $result; | ||
| 108 | + } | ||
| 109 | + | ||
| 110 | + /** | ||
| 70 | * 头信息需要携带授权token | 111 | * 头信息需要携带授权token |
| 71 | * @return array | 112 | * @return array |
| 72 | */ | 113 | */ |
| @@ -88,6 +129,9 @@ class UpyunService | @@ -88,6 +129,9 @@ class UpyunService | ||
| 88 | public function curlRequest($url, $data, $method = 'POST', $header = [], $time_out = 60) | 129 | public function curlRequest($url, $data, $method = 'POST', $header = [], $time_out = 60) |
| 89 | { | 130 | { |
| 90 | $url = config('custom.upyun.api_url') . $url; | 131 | $url = config('custom.upyun.api_url') . $url; |
| 132 | + if ($method == 'GET') { | ||
| 133 | + $url .= '?' . http_build_query($data); | ||
| 134 | + } | ||
| 91 | $ch = curl_init(); | 135 | $ch = curl_init(); |
| 92 | curl_setopt($ch, CURLOPT_TIMEOUT, $time_out); | 136 | curl_setopt($ch, CURLOPT_TIMEOUT, $time_out); |
| 93 | curl_setopt($ch, CURLOPT_URL, $url); | 137 | curl_setopt($ch, CURLOPT_URL, $url); |
-
请 注册 或 登录 后发表评论