|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Created by PhpStorm.
|
|
|
|
* User: zhl
|
|
|
|
* Date: 2025/7/8
|
|
|
|
* Time: 14:11
|
|
|
|
*/
|
|
|
|
namespace App\Http\Controllers\Aside\Project;
|
|
|
|
|
|
|
|
use App\Enums\Common\Code;
|
|
|
|
use App\Http\Controllers\Aside\BaseController;
|
|
|
|
use App\Models\Project\Project;
|
|
|
|
use App\Models\Project\ProjectFlow;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 数据统计结果
|
|
|
|
* Class StatisticsController
|
|
|
|
* @package App\Http\Controllers\Aside\Project
|
|
|
|
*/
|
|
|
|
class StatisticsController extends BaseController
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 流量统计
|
|
|
|
* @param Request $request
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
|
|
|
public function flow(Request $request)
|
|
|
|
{
|
|
|
|
$project_id = intval($request->input('project_id', '0'));
|
|
|
|
$start_at = $request->input('start_at', '');
|
|
|
|
$end_at = $request->input('end_at', '');
|
|
|
|
|
|
|
|
$result = ProjectFlow::when($project_id, function ($query) use ($project_id) {
|
|
|
|
return $query->where(['project_id' => $project_id]);
|
|
|
|
})
|
|
|
|
->when($start_at, function ($query) use ($start_at) {
|
|
|
|
return $query->where('date', '>=', $start_at);
|
|
|
|
})
|
|
|
|
->when($end_at, function ($query) use ($end_at) {
|
|
|
|
return $query->where('date', '<=', $end_at);
|
|
|
|
})
|
|
|
|
->orderBy('date', 'desc')
|
|
|
|
->orderBy('id', 'desc')
|
|
|
|
->paginate($this->row);
|
|
|
|
|
|
|
|
if ($result->isEmpty())
|
|
|
|
return $this->response('success',Code::SUCCESS, []);
|
|
|
|
|
|
|
|
$result = $result->toArray();
|
|
|
|
$project_ids = array_column($result['list'], 'project_id');
|
|
|
|
$projects = Project::whereIn('id', $project_ids)->pluck('company', 'id')->toArray();
|
|
|
|
$total = 0;
|
|
|
|
foreach ($result['list'] as &$item) {
|
|
|
|
$item['company'] = $projects[$item['project_id']];
|
|
|
|
$item['use_flow'] = $this->formatBytes($item['cdn_flow']);
|
|
|
|
$item['use_flow_mb'] = round($item['cdn_flow'] / 1048576, 2);
|
|
|
|
$total += $item['cdn_flow'];
|
|
|
|
unset($item['id']);
|
|
|
|
unset($item['last_at']);
|
|
|
|
unset($item['updated_at']);
|
|
|
|
unset($item['created_at']);
|
|
|
|
}
|
|
|
|
$result['use_flow'] = $this->formatBytes($total);
|
|
|
|
return $this->response('success',Code::SUCCESS, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 字节单位转换
|
|
|
|
* @param $bytes
|
|
|
|
* @param int $precision
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function formatBytes($bytes, $precision = 2) {
|
|
|
|
$units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
|
|
|
|
|
|
|
|
$bytes = max($bytes, 0);
|
|
|
|
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
|
|
|
|
$pow = min($pow, count($units) - 1);
|
|
|
|
|
|
|
|
$bytes /= pow(1024, $pow);
|
|
|
|
|
|
|
|
return round($bytes, $precision) . ' ' . $units[$pow];
|
|
|
|
}
|
|
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|