作者 zhl

流量统计数据

  1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: zhl
  5 + * Date: 2025/7/8
  6 + * Time: 14:11
  7 + */
  8 +namespace App\Http\Controllers\Aside\Project;
  9 +
  10 +use App\Enums\Common\Code;
  11 +use App\Http\Controllers\Aside\BaseController;
  12 +use App\Models\Project\Project;
  13 +use App\Models\Project\ProjectFlow;
  14 +use Illuminate\Http\Request;
  15 +
  16 +/**
  17 + * 数据统计结果
  18 + * Class StatisticsController
  19 + * @package App\Http\Controllers\Aside\Project
  20 + */
  21 +class StatisticsController extends BaseController
  22 +{
  23 +
  24 + /**
  25 + * 流量统计
  26 + * @param Request $request
  27 + * @return \Illuminate\Http\JsonResponse
  28 + */
  29 + public function flow(Request $request)
  30 + {
  31 + $project_id = intval($request->input('project_id', '0'));
  32 + $start_at = $request->input('start_at', '');
  33 + $end_at = $request->input('end_at', '');
  34 +
  35 + $result = ProjectFlow::when($project_id, function ($query) use ($project_id) {
  36 + return $query->where(['project_id' => $project_id]);
  37 + })
  38 + ->when($start_at, function ($query) use ($start_at) {
  39 + return $query->where('date', '>=', $start_at);
  40 + })
  41 + ->when($end_at, function ($query) use ($end_at) {
  42 + return $query->where('date', '<=', $end_at);
  43 + })
  44 + ->orderBy('date', 'desc')
  45 + ->orderBy('id', 'desc')
  46 + ->paginate($this->row);
  47 +
  48 + if ($result->isEmpty())
  49 + return $this->response('success',Code::SUCCESS, []);
  50 +
  51 + $result = $result->toArray();
  52 + $project_ids = array_column($result['list'], 'project_id');
  53 + $projects = Project::whereIn('id', $project_ids)->pluck('company', 'id')->toArray();
  54 + $total = 0;
  55 + foreach ($result['list'] as &$item) {
  56 + $item['company'] = $projects[$item['project_id']];
  57 + $item['use_flow'] = $this->formatBytes($item['cdn_flow']);
  58 + $item['use_flow_mb'] = round($item['cdn_flow'] / 1048576, 2);
  59 + $total += $item['cdn_flow'];
  60 + unset($item['id']);
  61 + unset($item['last_at']);
  62 + unset($item['updated_at']);
  63 + unset($item['created_at']);
  64 + }
  65 + $result['use_flow'] = $this->formatBytes($total);
  66 + return $this->response('success',Code::SUCCESS, $result);
  67 + }
  68 +
  69 + /**
  70 + * 字节单位转换
  71 + * @param $bytes
  72 + * @param int $precision
  73 + * @return string
  74 + */
  75 + public function formatBytes($bytes, $precision = 2) {
  76 + $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
  77 +
  78 + $bytes = max($bytes, 0);
  79 + $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
  80 + $pow = min($pow, count($units) - 1);
  81 +
  82 + $bytes /= pow(1024, $pow);
  83 +
  84 + return round($bytes, $precision) . ' ' . $units[$pow];
  85 + }
  86 +}
@@ -609,6 +609,12 @@ Route::middleware(['aloginauth'])->group(function () { @@ -609,6 +609,12 @@ Route::middleware(['aloginauth'])->group(function () {
609 Route::any('/save', [Aside\Project\ProjectWhiteHatAffixController::class, 'save'])->name('admin.white_hat_save'); 609 Route::any('/save', [Aside\Project\ProjectWhiteHatAffixController::class, 'save'])->name('admin.white_hat_save');
610 Route::any('/del', [Aside\Project\ProjectWhiteHatAffixController::class, 'del'])->name('admin.white_hat_del'); 610 Route::any('/del', [Aside\Project\ProjectWhiteHatAffixController::class, 'del'])->name('admin.white_hat_del');
611 }); 611 });
  612 + // 统计数据
  613 + Route::prefix('statistics')->group(function () {
  614 + // 流量统计
  615 + Route::any('/flow', [Aside\Project\StatisticsController::class, 'flow'])->name('admin.statistics_flow');
  616 + });
  617 +
612 }); 618 });
613 619
614 //无需登录验证的路由组 620 //无需登录验证的路由组