mirror of
				https://github.com/v2board/v2board.git
				synced 2025-10-31 17:31:49 +08:00 
			
		
		
		
	update: statistics service
This commit is contained in:
		| @@ -2,11 +2,13 @@ | ||||
|  | ||||
| namespace App\Console\Commands; | ||||
|  | ||||
| use App\Models\StatServer; | ||||
| use App\Models\StatUser; | ||||
| use App\Models\User; | ||||
| use App\Services\StatisticalService; | ||||
| use Illuminate\Console\Command; | ||||
| use App\Models\Order; | ||||
| use App\Models\StatOrder; | ||||
| use App\Models\Stat; | ||||
| use App\Models\CommissionLog; | ||||
| use Illuminate\Support\Facades\DB; | ||||
|  | ||||
| @@ -43,16 +45,47 @@ class V2boardStatistics extends Command | ||||
|      */ | ||||
|     public function handle() | ||||
|     { | ||||
|         $startAt = microtime(true); | ||||
|         ini_set('memory_limit', -1); | ||||
|         $this->statOrder(); | ||||
|         $this->statUser(); | ||||
|         $this->statServer(); | ||||
|         $this->stat(); | ||||
|         $this->info('耗时' . (microtime(true) - $startAt)); | ||||
|     } | ||||
|  | ||||
|     private function statServer() | ||||
|     { | ||||
|         $createdAt = time(); | ||||
|         $recordAt = strtotime('-1 day', strtotime(date('Y-m-d'))); | ||||
|         $statService = new StatisticalService(); | ||||
|         $statService->setStartAt($recordAt); | ||||
|         $stats = $statService->getStatUser(); | ||||
|         DB::beginTransaction(); | ||||
|         foreach ($stats as $stat) { | ||||
|             if (!StatServer::insert([ | ||||
|                 'server_id' => $stat['server_id'], | ||||
|                 'server_type' => $stat['server_type'], | ||||
|                 'u' => $stat['u'], | ||||
|                 'd' => $stat['d'], | ||||
|                 'created_at' => $createdAt, | ||||
|                 'updated_at' => $createdAt, | ||||
|                 'record_type' => 'd', | ||||
|                 'record_at' => $recordAt | ||||
|             ])) { | ||||
|                 DB::rollback(); | ||||
|                 throw new \Exception('stat server fail'); | ||||
|             } | ||||
|         } | ||||
|         DB::commit(); | ||||
|         $statService->clearStatServer(); | ||||
|     } | ||||
|  | ||||
|     private function statUser() | ||||
|     { | ||||
|         $createdAt = time(); | ||||
|         $recordAt = strtotime('-1 day', strtotime(date('Y-m-d'))); | ||||
|         $statService = new StatisticalService($recordAt); | ||||
|         $statService = new StatisticalService(); | ||||
|         $statService->setStartAt($recordAt); | ||||
|         $stats = $statService->getStatUser(); | ||||
|         DB::beginTransaction(); | ||||
|         foreach ($stats as $stat) { | ||||
| @@ -74,34 +107,19 @@ class V2boardStatistics extends Command | ||||
|         $statService->clearStatUser(); | ||||
|     } | ||||
|  | ||||
|     private function statOrder() | ||||
|     private function stat() | ||||
|     { | ||||
|         $endAt = strtotime(date('Y-m-d')); | ||||
|         $startAt = strtotime('-1 day', $endAt); | ||||
|         $orderBuilder = Order::where('paid_at', '>=', $startAt) | ||||
|             ->where('paid_at', '<', $endAt) | ||||
|             ->whereNotIn('status', [0, 2]); | ||||
|         $orderCount = $orderBuilder->count(); | ||||
|         $orderAmount = $orderBuilder->sum('total_amount'); | ||||
|         $commissionLogBuilder = CommissionLog::where('created_at', '>=', $startAt) | ||||
|             ->where('created_at', '<', $endAt); | ||||
|         $commissionCount = $commissionLogBuilder->count(); | ||||
|         $commissionAmount = $commissionLogBuilder->sum('get_amount'); | ||||
|         $data = [ | ||||
|             'order_count' => $orderCount, | ||||
|             'order_amount' => $orderAmount, | ||||
|             'commission_count' => $commissionCount, | ||||
|             'commission_amount' => $commissionAmount, | ||||
|             'record_type' => 'd', | ||||
|             'record_at' => $startAt | ||||
|         ]; | ||||
|         $statistic = StatOrder::where('record_at', $startAt) | ||||
|         $startAt = strtotime('-1 day', strtotime(date('Y-m-d'))); | ||||
|         $statisticalService = new StatisticalService(); | ||||
|         $statisticalService->setRecordAt($startAt); | ||||
|         $data = $statisticalService->generateStatData(); | ||||
|         $statistic = Stat::where('record_at', $startAt) | ||||
|             ->where('record_type', 'd') | ||||
|             ->first(); | ||||
|         if ($statistic) { | ||||
|             $statistic->update($data); | ||||
|             return; | ||||
|         } | ||||
|         StatOrder::create($data); | ||||
|         Stat::create($data); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -7,6 +7,7 @@ use App\Models\ServerShadowsocks; | ||||
| use App\Models\ServerTrojan; | ||||
| use App\Models\StatUser; | ||||
| use App\Services\ServerService; | ||||
| use App\Services\StatisticalService; | ||||
| use Illuminate\Http\Request; | ||||
| use App\Http\Controllers\Controller; | ||||
| use App\Models\ServerGroup; | ||||
| @@ -15,16 +16,50 @@ use App\Models\Plan; | ||||
| use App\Models\User; | ||||
| use App\Models\Ticket; | ||||
| use App\Models\Order; | ||||
| use App\Models\StatOrder; | ||||
| use App\Models\Stat; | ||||
| use App\Models\StatServer; | ||||
| use Illuminate\Support\Facades\Cache; | ||||
| use Illuminate\Support\Facades\DB; | ||||
|  | ||||
| class StatController extends Controller | ||||
| { | ||||
|     public function getStat(Request $request) | ||||
|     { | ||||
|         $params = $request->validate([ | ||||
|             'start_at' => '', | ||||
|             'end_at' => '' | ||||
|         ]); | ||||
|  | ||||
|         if (isset($params['start_at']) && isset($params['end_at'])) { | ||||
|             $stats = Stat::where('record_at', '>=', $params['start_at']) | ||||
|                 ->where('record_at', '<', $params['end_at']) | ||||
|                 ->get() | ||||
|                 ->makeHidden(['record_at', 'created_at', 'updated_at', 'id', 'record_type']) | ||||
|                 ->toArray(); | ||||
|             $stats = array_reduce($stats, function($carry, $item) { | ||||
|                 foreach($item as $key => $value) { | ||||
|                     if(isset($carry[$key]) && $carry[$key]) { | ||||
|                         $carry[$key] += $value; | ||||
|                     } else { | ||||
|                         $carry[$key] = $value; | ||||
|                     } | ||||
|                 } | ||||
|                 return $carry; | ||||
|             }, []); | ||||
|             return [ | ||||
|                 'data' => $stats | ||||
|             ]; | ||||
|         } | ||||
|  | ||||
|         $statisticalService = new StatisticalService(); | ||||
|         return [ | ||||
|             'data' => $statisticalService->generateStatData() | ||||
|         ]; | ||||
|     } | ||||
|  | ||||
|     public function getOverride(Request $request) | ||||
|     { | ||||
|         return response([ | ||||
|         return [ | ||||
|             'data' => [ | ||||
|                 'month_income' => Order::where('created_at', '>=', strtotime(date('Y-m-1'))) | ||||
|                     ->where('created_at', '<', time()) | ||||
| @@ -55,12 +90,12 @@ class StatController extends Controller | ||||
|                     ->where('created_at', '<', strtotime(date('Y-m-1'))) | ||||
|                     ->sum('get_amount'), | ||||
|             ] | ||||
|         ]); | ||||
|         ]; | ||||
|     } | ||||
|  | ||||
|     public function getOrder(Request $request) | ||||
|     { | ||||
|         $statistics = StatOrder::where('record_type', 'd') | ||||
|         $statistics = Stat::where('record_type', 'd') | ||||
|             ->limit(31) | ||||
|             ->orderBy('record_at', 'DESC') | ||||
|             ->get() | ||||
| @@ -68,31 +103,31 @@ class StatController extends Controller | ||||
|         $result = []; | ||||
|         foreach ($statistics as $statistic) { | ||||
|             $date = date('m-d', $statistic['record_at']); | ||||
|             array_push($result, [ | ||||
|             $result[] = [ | ||||
|                 'type' => '收款金额', | ||||
|                 'date' => $date, | ||||
|                 'value' => $statistic['order_amount'] / 100 | ||||
|             ]); | ||||
|             array_push($result, [ | ||||
|                 'value' => $statistic['order_total'] / 100 | ||||
|             ]; | ||||
|             $result[] = [ | ||||
|                 'type' => '收款笔数', | ||||
|                 'date' => $date, | ||||
|                 'value' => $statistic['order_count'] | ||||
|             ]); | ||||
|             array_push($result, [ | ||||
|             ]; | ||||
|             $result[] = [ | ||||
|                 'type' => '佣金金额(已发放)', | ||||
|                 'date' => $date, | ||||
|                 'value' => $statistic['commission_amount'] / 100 | ||||
|             ]); | ||||
|             array_push($result, [ | ||||
|                 'value' => $statistic['commission_total'] / 100 | ||||
|             ]; | ||||
|             $result[] = [ | ||||
|                 'type' => '佣金笔数(已发放)', | ||||
|                 'date' => $date, | ||||
|                 'value' => $statistic['commission_count'] | ||||
|             ]); | ||||
|             ]; | ||||
|         } | ||||
|         $result = array_reverse($result); | ||||
|         return response([ | ||||
|         return [ | ||||
|             'data' => $result | ||||
|         ]); | ||||
|         ]; | ||||
|     } | ||||
|  | ||||
|     public function getServerLastRank() | ||||
| @@ -128,9 +163,9 @@ class StatController extends Controller | ||||
|             $statistics[$k]['total'] = $statistics[$k]['total'] / 1073741824; | ||||
|         } | ||||
|         array_multisort(array_column($statistics, 'total'), SORT_DESC, $statistics); | ||||
|         return response([ | ||||
|         return [ | ||||
|             'data' => $statistics | ||||
|         ]); | ||||
|         ]; | ||||
|     } | ||||
|  | ||||
|     public function getStatUser(Request $request) | ||||
| @@ -150,5 +185,6 @@ class StatController extends Controller | ||||
|             'total' => $total | ||||
|         ]; | ||||
|     } | ||||
|  | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -2,20 +2,9 @@ | ||||
|  | ||||
| namespace App\Http\Controllers\Admin; | ||||
|  | ||||
| use App\Models\ServerShadowsocks; | ||||
| use App\Models\ServerTrojan; | ||||
| use App\Services\ServerService; | ||||
| use App\Utils\CacheKey; | ||||
| use Illuminate\Http\Request; | ||||
| use App\Http\Controllers\Controller; | ||||
| use App\Models\ServerGroup; | ||||
| use App\Models\ServerVmess; | ||||
| use App\Models\Plan; | ||||
| use App\Models\User; | ||||
| use App\Models\Ticket; | ||||
| use App\Models\Order; | ||||
| use App\Models\StatOrder; | ||||
| use App\Models\StatServer; | ||||
| use Illuminate\Support\Facades\Cache; | ||||
| use Illuminate\Support\Facades\DB; | ||||
| use Illuminate\Support\Facades\Http; | ||||
|   | ||||
| @@ -91,6 +91,8 @@ class DeepbworkController extends Controller | ||||
|         } | ||||
|  | ||||
|         $statService = new StatisticalService(); | ||||
|         $statService->setStartAt(strtotime(date('Y-m-d'))); | ||||
|         $statService->setUserStats(); | ||||
|         $statService->statUser($server['rate'], $statData); | ||||
|  | ||||
|         return response([ | ||||
|   | ||||
| @@ -83,6 +83,8 @@ class ShadowsocksTidalabController extends Controller | ||||
|         } | ||||
|  | ||||
|         $statService = new StatisticalService(); | ||||
|         $statService->setStartAt(strtotime(date('Y-m-d'))); | ||||
|         $statService->setUserStats(); | ||||
|         $statService->statUser($server['rate'], $statData); | ||||
|  | ||||
|         return response([ | ||||
|   | ||||
| @@ -88,6 +88,8 @@ class TrojanTidalabController extends Controller | ||||
|         } | ||||
|  | ||||
|         $statService = new StatisticalService(); | ||||
|         $statService->setStartAt(strtotime(date('Y-m-d'))); | ||||
|         $statService->setUserStats(); | ||||
|         $statService->statUser($server['rate'], $statData); | ||||
|  | ||||
|         return response([ | ||||
|   | ||||
| @@ -71,7 +71,11 @@ class UniProxyController extends Controller | ||||
|         } | ||||
|  | ||||
|         $statService = new StatisticalService(); | ||||
|         $statService->setStartAt(strtotime(date('Y-m-d'))); | ||||
|         $statService->setUserStats(); | ||||
|         $statService->statUser($this->nodeInfo->rate, $data); | ||||
|         $statService->setServerStats(); | ||||
|         $statService->statServer($this->nodeId, $this->nodeType, $data); | ||||
|  | ||||
|         return response([ | ||||
|             'data' => true | ||||
|   | ||||
| @@ -1,80 +0,0 @@ | ||||
| <?php | ||||
|  | ||||
| namespace App\Jobs; | ||||
|  | ||||
| use App\Models\StatServer; | ||||
| use Illuminate\Bus\Queueable; | ||||
| use Illuminate\Contracts\Queue\ShouldQueue; | ||||
| use Illuminate\Foundation\Bus\Dispatchable; | ||||
| use Illuminate\Queue\InteractsWithQueue; | ||||
| use Illuminate\Queue\SerializesModels; | ||||
|  | ||||
| class StatServerJob implements ShouldQueue | ||||
| { | ||||
|     use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||||
|     protected $u; | ||||
|     protected $d; | ||||
|     protected $server; | ||||
|     protected $protocol; | ||||
|     protected $recordType; | ||||
|  | ||||
|     public $tries = 3; | ||||
|     public $timeout = 10; | ||||
|  | ||||
|     /** | ||||
|      * Create a new job instance. | ||||
|      * | ||||
|      * @return void | ||||
|      */ | ||||
|     public function __construct($u, $d, $server, $protocol, $recordType = 'd') | ||||
|     { | ||||
|         $this->onQueue('stat'); | ||||
|         $this->u = $u; | ||||
|         $this->d = $d; | ||||
|         $this->server = $server; | ||||
|         $this->protocol = $protocol; | ||||
|         $this->recordType = $recordType; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Execute the job. | ||||
|      * | ||||
|      * @return void | ||||
|      */ | ||||
|     public function handle() | ||||
|     { | ||||
|         $recordAt = strtotime(date('Y-m-d')); | ||||
|         if ($this->recordType === 'm') { | ||||
|             // | ||||
|         } | ||||
|  | ||||
|         $data = StatServer::lockForUpdate() | ||||
|             ->where('record_at', $recordAt) | ||||
|             ->where('server_id', $this->server['id']) | ||||
|             ->where('server_type', $this->protocol) | ||||
|             ->first(); | ||||
|  | ||||
|         if (!$data) { | ||||
|             if (!StatServer::create([ | ||||
|                 'server_id' => $this->server['id'], | ||||
|                 'server_type' => $this->protocol, | ||||
|                 'u' => $this->u, | ||||
|                 'd' => $this->d, | ||||
|                 'record_type' => $this->recordType, | ||||
|                 'record_at' => $recordAt | ||||
|             ])) { | ||||
|                 abort(500, '节点统计数据创建失败'); | ||||
|             } | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         try { | ||||
|             $data->update([ | ||||
|                 'u' => $data['u'] + $this->u, | ||||
|                 'd' => $data['d'] + $this->d | ||||
|             ]); | ||||
|         } catch (\Exception $e) { | ||||
|             abort(500, '节点统计数据更新失败'); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -1,80 +0,0 @@ | ||||
| <?php | ||||
|  | ||||
| namespace App\Jobs; | ||||
|  | ||||
| use App\Models\StatServer; | ||||
| use App\Models\StatUser; | ||||
| use Illuminate\Bus\Queueable; | ||||
| use Illuminate\Contracts\Queue\ShouldQueue; | ||||
| use Illuminate\Foundation\Bus\Dispatchable; | ||||
| use Illuminate\Queue\InteractsWithQueue; | ||||
| use Illuminate\Queue\SerializesModels; | ||||
|  | ||||
| class StatUserJob implements ShouldQueue | ||||
| { | ||||
|     use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||||
|     protected $u; | ||||
|     protected $d; | ||||
|     protected $userId; | ||||
|     protected $server; | ||||
|     protected $protocol; | ||||
|     protected $recordType; | ||||
|  | ||||
|     public $tries = 3; | ||||
|     public $timeout = 10; | ||||
|  | ||||
|     /** | ||||
|      * Create a new job instance. | ||||
|      * | ||||
|      * @return void | ||||
|      */ | ||||
|     public function __construct($u, $d, $userId, array $server, $protocol, $recordType = 'd') | ||||
|     { | ||||
|         $this->onQueue('stat'); | ||||
|         $this->u = $u; | ||||
|         $this->d = $d; | ||||
|         $this->userId = $userId; | ||||
|         $this->server = $server; | ||||
|         $this->protocol = $protocol; | ||||
|         $this->recordType = $recordType; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Execute the job. | ||||
|      * | ||||
|      * @return void | ||||
|      */ | ||||
|     public function handle() | ||||
|     { | ||||
|         $recordAt = strtotime(date('Y-m-d')); | ||||
|         if ($this->recordType === 'm') { | ||||
|             // | ||||
|         } | ||||
|  | ||||
|         $data = StatUser::where('record_at', $recordAt) | ||||
|             ->where('server_rate', $this->server['rate']) | ||||
|             ->where('user_id', $this->userId) | ||||
|             ->first(); | ||||
|         if ($data) { | ||||
|             try { | ||||
|                 $data->update([ | ||||
|                     'u' => $data['u'] + ($this->u * $this->server['rate']), | ||||
|                     'd' => $data['d'] + ($this->d * $this->server['rate']) | ||||
|                 ]); | ||||
|             } catch (\Exception $e) { | ||||
|                 abort(500, '用户统计数据更新失败'); | ||||
|             } | ||||
|         } else { | ||||
|             if (!StatUser::create([ | ||||
|                 'user_id' => $this->userId, | ||||
|                 'server_rate' => $this->server['rate'], | ||||
|                 'u' => $this->u, | ||||
|                 'd' => $this->d, | ||||
|                 'record_type' => $this->recordType, | ||||
|                 'record_at' => $recordAt | ||||
|             ])) { | ||||
|                 abort(500, '用户统计数据创建失败'); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -4,9 +4,9 @@ namespace App\Models; | ||||
| 
 | ||||
| use Illuminate\Database\Eloquent\Model; | ||||
| 
 | ||||
| class StatOrder extends Model | ||||
| class Stat extends Model | ||||
| { | ||||
|     protected $table = 'v2_stat_order'; | ||||
|     protected $table = 'v2_stat'; | ||||
|     protected $dateFormat = 'U'; | ||||
|     protected $guarded = ['id']; | ||||
|     protected $casts = [ | ||||
| @@ -1,23 +1,108 @@ | ||||
| <?php | ||||
| namespace App\Services; | ||||
|  | ||||
| use App\Models\CommissionLog; | ||||
| use App\Models\Order; | ||||
| use App\Models\Stat; | ||||
| use App\Models\StatServer; | ||||
| use App\Models\User; | ||||
| use Illuminate\Support\Facades\Cache; | ||||
| use Illuminate\Support\Facades\DB; | ||||
|  | ||||
| class StatisticalService { | ||||
|     protected $userStats; | ||||
|     protected $recordAt; | ||||
|     protected $startAt; | ||||
|     protected $endAt; | ||||
|     protected $serverStats; | ||||
|  | ||||
|     public function __construct($recordAt = NULL) | ||||
|     public function __construct() | ||||
|     { | ||||
|         ini_set('memory_limit', -1); | ||||
|         $this->recordAt = $recordAt ?? strtotime(date('Y-m-d')); | ||||
|         $this->userStats = Cache::get("stat_user_{$this->recordAt}"); | ||||
|     } | ||||
|  | ||||
|     public function setStartAt($timestamp) { | ||||
|         $this->startAt = $timestamp; | ||||
|     } | ||||
|  | ||||
|     public function setEndAt($timestamp) { | ||||
|         $this->endAt = $timestamp; | ||||
|     } | ||||
|  | ||||
|     public function setServerStats() { | ||||
|         $this->serverStats = Cache::get("stat_server_{$this->startAt}"); | ||||
|         $this->serverStats = json_decode($this->serverStats, true) ?? []; | ||||
|         if (!is_array($this->serverStats)) { | ||||
|             $this->serverStats = []; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public function setUserStats() { | ||||
|         $this->userStats = Cache::get("stat_user_{$this->startAt}"); | ||||
|         $this->userStats = json_decode($this->userStats, true) ?? []; | ||||
|         if (!is_array($this->userStats)) { | ||||
|             $this->userStats = []; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public function generateStatData(): array | ||||
|     { | ||||
|         $startAt = $this->startAt; | ||||
|         $endAt = $this->endAt; | ||||
|         if (!$startAt || !$endAt) { | ||||
|             $startAt = strtotime(date('Y-m-d')); | ||||
|             $endAt = strtotime('+1 day', $startAt); | ||||
|         } | ||||
|         $data = [ | ||||
|             'record_type' => 'd', | ||||
|             'paid_rate' => 0, | ||||
|             'record_at' => $startAt | ||||
|         ]; | ||||
|         $data['order_count'] = Order::where('created_at', '>=', $startAt) | ||||
|             ->where('created_at', '<', $endAt) | ||||
|             ->count(); | ||||
|         $data['order_total'] = Order::where('created_at', '>=', $startAt) | ||||
|             ->where('created_at', '<', $endAt) | ||||
|             ->sum('total_amount'); | ||||
|         $data['paid_count'] = Order::where('paid_at', '>=', $startAt) | ||||
|             ->where('paid_at', '<', $endAt) | ||||
|             ->whereNotIn('status', [0, 2]) | ||||
|             ->count(); | ||||
|         $data['paid_total'] = Order::where('paid_at', '>=', $startAt) | ||||
|             ->where('paid_at', '<', $endAt) | ||||
|             ->whereNotIn('status', [0, 2]) | ||||
|             ->sum('total_amount'); | ||||
|         $commissionLogBuilder = CommissionLog::where('created_at', '>=', $startAt) | ||||
|             ->where('created_at', '<', $endAt); | ||||
|         $data['commission_count'] = $commissionLogBuilder->count(); | ||||
|         $data['commission_amount'] = $commissionLogBuilder->sum('get_amount'); | ||||
|         $data['register_count'] = User::where('created_at', '>=', $startAt) | ||||
|             ->where('created_at', '<', $endAt) | ||||
|             ->count(); | ||||
|         $data['invite_count'] = User::where('created_at', '>=', $startAt) | ||||
|             ->where('created_at', '<', $endAt) | ||||
|             ->whereNotNull('invite_user_id') | ||||
|             ->count(); | ||||
|         $data['transfer_used_total'] = StatServer::where('created_at', '>=', $startAt) | ||||
|                 ->where('created_at', '<', $endAt) | ||||
|                 ->select(DB::raw('SUM(u) + SUM(d) as total')) | ||||
|                 ->value('total') ?? 0; | ||||
|         return $data; | ||||
|     } | ||||
|  | ||||
|     public function statServer($serverId, $serverType, $data) | ||||
|     { | ||||
|         foreach (array_keys($data) as $key) { | ||||
|             $this->serverStats[$serverType] = $this->serverStats[$serverType] ?? []; | ||||
|             if (isset($this->serverStats[$serverType][$serverId])) { | ||||
|                 $this->serverStats[$serverType][$serverId][0] += $data[$key][0]; | ||||
|                 $this->serverStats[$serverType][$serverId][1] += $data[$key][1]; | ||||
|             } else { | ||||
|                 $this->serverStats[$serverType][$serverId] = $data[$key]; | ||||
|             } | ||||
|         } | ||||
|         Cache::set("stat_server_{$this->startAt}", json_encode($this->serverStats)); | ||||
|     } | ||||
|  | ||||
|     public function statUser($rate, $data) | ||||
|     { | ||||
|         foreach (array_keys($data) as $key) { | ||||
| @@ -29,9 +114,24 @@ class StatisticalService { | ||||
|                 $this->userStats[$rate][$key] = $data[$key]; | ||||
|             } | ||||
|         } | ||||
|         Cache::set("stat_user_{$this->recordAt}", json_encode($this->userStats)); | ||||
|         Cache::set("stat_user_{$this->startAt}", json_encode($this->userStats)); | ||||
|     } | ||||
|  | ||||
|     public function getStatUserByUserID($userId): array | ||||
|     { | ||||
|         $stats = []; | ||||
|         foreach (array_keys($this->userStats) as $rate) { | ||||
|             if (!isset($this->userStats[$rate][$userId])) continue; | ||||
|             $stats[] = [ | ||||
|                 'record_at' => $this->startAt, | ||||
|                 'server_rate' => $rate, | ||||
|                 'u' => $this->userStats[$rate][$userId][0], | ||||
|                 'd' => $this->userStats[$rate][$userId][1], | ||||
|                 'user_id' => $userId | ||||
|             ]; | ||||
|         } | ||||
|         return $stats; | ||||
|     } | ||||
|  | ||||
|     public function getStatUser() | ||||
|     { | ||||
| @@ -51,8 +151,32 @@ class StatisticalService { | ||||
|         return $stats; | ||||
|     } | ||||
|  | ||||
|  | ||||
|     public function getStatServer() | ||||
|     { | ||||
|         $stats = []; | ||||
|         foreach ($this->serverStats as $serverType => $v) { | ||||
|             foreach (array_keys($v) as $serverId) { | ||||
|                 if (isset($v[$serverId])) { | ||||
|                     $stats[] = [ | ||||
|                         'server_id' => $serverId, | ||||
|                         'server_type' => $serverType, | ||||
|                         'u' => $v[$serverId][0], | ||||
|                         'd' => $v[$serverId][1], | ||||
|                     ]; | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|         return $stats; | ||||
|     } | ||||
|  | ||||
|     public function clearStatUser() | ||||
|     { | ||||
|         Cache::forget("stat_user_{$this->recordAt}"); | ||||
|         Cache::forget("stat_user_{$this->startAt}"); | ||||
|     } | ||||
|  | ||||
|     public function clearStatServer() | ||||
|     { | ||||
|         Cache::forget("stat_server_{$this->startAt}"); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -171,6 +171,5 @@ class UserService | ||||
|     public function trafficFetch(int $u, int $d, int $userId, array $server, string $protocol) | ||||
|     { | ||||
|         TrafficFetchJob::dispatch($u, $d, $userId, $server, $protocol); | ||||
|         StatServerJob::dispatch($u, $d, $server, $protocol, 'd'); | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user