Merge branch 'dev' of https://github.com/v2board/v2board into fix-surgeSurfboardRemainingTraffic

This commit is contained in:
coldice945 2023-06-08 18:54:01 +08:00
commit 75129efa28
7 changed files with 146 additions and 72 deletions

View File

@ -14,7 +14,7 @@ DB_USERNAME=root
DB_PASSWORD=123456 DB_PASSWORD=123456
BROADCAST_DRIVER=log BROADCAST_DRIVER=log
CACHE_DRIVER=file CACHE_DRIVER=redis
QUEUE_CONNECTION=redis QUEUE_CONNECTION=redis
SESSION_DRIVER=redis SESSION_DRIVER=redis
SESSION_LIFETIME=120 SESSION_LIFETIME=120

View File

@ -4,12 +4,9 @@ namespace App\Console\Commands;
use App\Models\StatServer; use App\Models\StatServer;
use App\Models\StatUser; use App\Models\StatUser;
use App\Models\User;
use App\Services\StatisticalService; use App\Services\StatisticalService;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use App\Models\Order;
use App\Models\Stat; use App\Models\Stat;
use App\Models\CommissionLog;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
class V2boardStatistics extends Command class V2boardStatistics extends Command
@ -50,82 +47,94 @@ class V2boardStatistics extends Command
$this->statUser(); $this->statUser();
$this->statServer(); $this->statServer();
$this->stat(); $this->stat();
$this->info('耗时' . (microtime(true) - $startAt)); info('统计任务执行完毕。耗时:' . (microtime(true) - $startAt) / 1000);
} }
private function statServer() private function statServer()
{ {
$createdAt = time(); try {
$recordAt = strtotime('-1 day', strtotime(date('Y-m-d'))); DB::beginTransaction();
$statService = new StatisticalService(); $createdAt = time();
$statService->setStartAt($recordAt); $recordAt = strtotime('-1 day', strtotime(date('Y-m-d')));
$statService->setServerStats(); $statService = new StatisticalService();
$stats = $statService->getStatServer(); $statService->setStartAt($recordAt);
DB::beginTransaction(); $statService->setServerStats();
foreach ($stats as $stat) { $stats = $statService->getStatServer();
if (!StatServer::insert([ foreach ($stats as $stat) {
'server_id' => $stat['server_id'], if (!StatServer::insert([
'server_type' => $stat['server_type'], 'server_id' => $stat['server_id'],
'u' => $stat['u'], 'server_type' => $stat['server_type'],
'd' => $stat['d'], 'u' => $stat['u'],
'created_at' => $createdAt, 'd' => $stat['d'],
'updated_at' => $createdAt, 'created_at' => $createdAt,
'record_type' => 'd', 'updated_at' => $createdAt,
'record_at' => $recordAt 'record_type' => 'd',
])) { 'record_at' => $recordAt
DB::rollback(); ])) {
throw new \Exception('stat server fail'); throw new \Exception('stat server fail');
}
} }
DB::commit();
$statService->clearStatServer();
} catch (\Exception $e) {
DB::rollback();
\Log::error($e->getMessage(), ['exception' => $e]);
} }
DB::commit();
$statService->clearStatServer();
} }
private function statUser() private function statUser()
{ {
$createdAt = time(); try {
$recordAt = strtotime('-1 day', strtotime(date('Y-m-d'))); DB::beginTransaction();
$statService = new StatisticalService(); $createdAt = time();
$statService->setStartAt($recordAt); $recordAt = strtotime('-1 day', strtotime(date('Y-m-d')));
$statService->setUserStats(); $statService = new StatisticalService();
$stats = $statService->getStatUser(); $statService->setStartAt($recordAt);
DB::beginTransaction(); $statService->setUserStats();
foreach ($stats as $stat) { $stats = $statService->getStatUser();
if (!StatUser::insert([ foreach ($stats as $stat) {
'user_id' => $stat['user_id'], if (!StatUser::insert([
'u' => $stat['u'], 'user_id' => $stat['user_id'],
'd' => $stat['d'], 'u' => $stat['u'],
'server_rate' => $stat['server_rate'], 'd' => $stat['d'],
'created_at' => $createdAt, 'server_rate' => $stat['server_rate'],
'updated_at' => $createdAt, 'created_at' => $createdAt,
'record_type' => 'd', 'updated_at' => $createdAt,
'record_at' => $recordAt 'record_type' => 'd',
])) { 'record_at' => $recordAt
DB::rollback(); ])) {
throw new \Exception('stat user fail'); throw new \Exception('stat user fail');
}
} }
DB::commit();
$statService->clearStatUser();
} catch (\Exception $e) {
DB::rollback();
\Log::error($e->getMessage(), ['exception' => $e]);
} }
DB::commit();
$statService->clearStatUser();
} }
private function stat() private function stat()
{ {
$endAt = strtotime(date('Y-m-d')); try {
$startAt = strtotime('-1 day', $endAt); $endAt = strtotime(date('Y-m-d'));
$statisticalService = new StatisticalService(); $startAt = strtotime('-1 day', $endAt);
$statisticalService->setStartAt($startAt); $statisticalService = new StatisticalService();
$statisticalService->setEndAt($endAt); $statisticalService->setStartAt($startAt);
$data = $statisticalService->generateStatData(); $statisticalService->setEndAt($endAt);
$data['record_at'] = $startAt; $data = $statisticalService->generateStatData();
$data['record_type'] = 'd'; $data['record_at'] = $startAt;
$statistic = Stat::where('record_at', $startAt) $data['record_type'] = 'd';
->where('record_type', 'd') $statistic = Stat::where('record_at', $startAt)
->first(); ->where('record_type', 'd')
if ($statistic) { ->first();
$statistic->update($data); if ($statistic) {
return; $statistic->update($data);
return;
}
Stat::create($data);
} catch (\Exception $e) {
\Log::error($e->getMessage(), ['exception' => $e]);
} }
Stat::create($data);
} }
} }

View File

@ -14,6 +14,7 @@ use Laravel\Horizon\Contracts\MetricsRepository;
use Laravel\Horizon\Contracts\SupervisorRepository; use Laravel\Horizon\Contracts\SupervisorRepository;
use Laravel\Horizon\Contracts\WorkloadRepository; use Laravel\Horizon\Contracts\WorkloadRepository;
use Laravel\Horizon\WaitTimeCalculator; use Laravel\Horizon\WaitTimeCalculator;
use App\Models\Log as LogModel;
class SystemController extends Controller class SystemController extends Controller
{ {
@ -101,5 +102,18 @@ class SystemController extends Controller
return $master->status === 'paused'; return $master->status === 'paused';
})->count(); })->count();
} }
}
public function getSystemLog(Request $request) {
$current = $request->input('current') ? $request->input('current') : 1;
$pageSize = $request->input('page_size') >= 10 ? $request->input('page_size') : 10;
$builder = LogModel::orderBy('created_at', 'DESC')
->setFilterAllowKeys('level');
$total = $builder->count();
$res = $builder->forPage($current, $pageSize)
->get();
return response([
'data' => $res,
'total' => $total
]);
}
}

View File

@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Model;
class Log extends Model class Log extends Model
{ {
use \App\Scope\FilterScope;
protected $table = 'v2_log'; protected $table = 'v2_log';
protected $dateFormat = 'U'; protected $dateFormat = 'U';
protected $guarded = ['id']; protected $guarded = ['id'];

50
app/Scope/FilterScope.php Normal file
View File

@ -0,0 +1,50 @@
<?php
namespace App\Scope;
use Illuminate\Database\Eloquent\Builder;
trait FilterScope
{
public function scopeSetFilterAllowKeys($builder, ...$allowKeys)
{
$allowKeys = implode(',', $allowKeys);
if (!$allowKeys) return $builder;
$request = request();
$request->validate([
'filter.*.key' => "required|in:{$allowKeys}",
'filter.*.condition' => 'required|in:in,is,not,like,lt,gt',
'filter.*.value' => 'required'
]);
$filters = $request->input('filter');
if ($filters) {
foreach ($filters as $k => $filter) {
if ($filter['condition'] === 'in') {
$builder->whereIn($filter['key'], $filter['value']);
continue;
}
if ($filter['condition'] === 'is') {
$builder->where($filter['key'], $filter['value']);
continue;
}
if ($filter['condition'] === 'not') {
$builder->where($filter['key'], '!=', $filter['value']);
continue;
}
if ($filter['condition'] === 'gt') {
$builder->where($filter['key'], '>', $filter['value']);
continue;
}
if ($filter['condition'] === 'lt') {
$builder->where($filter['key'], '<', $filter['value']);
continue;
}
if ($filter['condition'] === 'like') {
$builder->where($filter['key'], 'like', "%{$filter['value']}%");
continue;
}
}
}
return $builder;
}
}

View File

@ -30,7 +30,7 @@ class StatisticalService {
} }
public function setServerStats() { public function setServerStats() {
$this->serverStats = Cache::get("stat_server_{$this->startAt}"); $this->serverStats = Cache::store('file')->get("stat_server_{$this->startAt}");
$this->serverStats = json_decode($this->serverStats, true) ?? []; $this->serverStats = json_decode($this->serverStats, true) ?? [];
if (!is_array($this->serverStats)) { if (!is_array($this->serverStats)) {
$this->serverStats = []; $this->serverStats = [];
@ -38,7 +38,7 @@ class StatisticalService {
} }
public function setUserStats() { public function setUserStats() {
$this->userStats = Cache::get("stat_user_{$this->startAt}"); $this->userStats = Cache::store('file')->get("stat_user_{$this->startAt}");
$this->userStats = json_decode($this->userStats, true) ?? []; $this->userStats = json_decode($this->userStats, true) ?? [];
if (!is_array($this->userStats)) { if (!is_array($this->userStats)) {
$this->userStats = []; $this->userStats = [];
@ -95,7 +95,7 @@ class StatisticalService {
} else { } else {
$this->serverStats[$serverType][$serverId] = [$u, $d]; $this->serverStats[$serverType][$serverId] = [$u, $d];
} }
Cache::set("stat_server_{$this->startAt}", json_encode($this->serverStats)); Cache::store('file')->set("stat_server_{$this->startAt}", json_encode($this->serverStats));
} }
public function statUser($rate, $userId, $u, $d) public function statUser($rate, $userId, $u, $d)
@ -107,7 +107,7 @@ class StatisticalService {
} else { } else {
$this->userStats[$rate][$userId] = [$u, $d]; $this->userStats[$rate][$userId] = [$u, $d];
} }
Cache::set("stat_user_{$this->startAt}", json_encode($this->userStats)); Cache::store('file')->set("stat_user_{$this->startAt}", json_encode($this->userStats));
} }
public function getStatUserByUserID($userId): array public function getStatUserByUserID($userId): array
@ -165,12 +165,12 @@ class StatisticalService {
public function clearStatUser() public function clearStatUser()
{ {
Cache::forget("stat_user_{$this->startAt}"); Cache::store('file')->forget("stat_user_{$this->startAt}");
} }
public function clearStatServer() public function clearStatServer()
{ {
Cache::forget("stat_server_{$this->startAt}"); Cache::store('file')->forget("stat_server_{$this->startAt}");
} }
public function getStatRecord($type) public function getStatRecord($type)

View File

@ -237,5 +237,5 @@ return [
| The only modification by laravel config | The only modification by laravel config
| |
*/ */
'version' => '1.7.4.1681103823832' 'version' => '1.7.5.1685907718051'
]; ];