mirror of
https://github.com/v2board/v2board.git
synced 2024-11-10 09:39:10 +08:00
update: statistics service
This commit is contained in:
parent
8b3ea1f8ea
commit
eee5152f52
@ -2,11 +2,13 @@
|
|||||||
|
|
||||||
namespace App\Console\Commands;
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
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\Order;
|
||||||
use App\Models\StatOrder;
|
use App\Models\Stat;
|
||||||
use App\Models\CommissionLog;
|
use App\Models\CommissionLog;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
@ -43,16 +45,47 @@ class V2boardStatistics extends Command
|
|||||||
*/
|
*/
|
||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
|
$startAt = microtime(true);
|
||||||
ini_set('memory_limit', -1);
|
ini_set('memory_limit', -1);
|
||||||
$this->statOrder();
|
|
||||||
$this->statUser();
|
$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()
|
private function statUser()
|
||||||
{
|
{
|
||||||
$createdAt = time();
|
$createdAt = time();
|
||||||
$recordAt = strtotime('-1 day', strtotime(date('Y-m-d')));
|
$recordAt = strtotime('-1 day', strtotime(date('Y-m-d')));
|
||||||
$statService = new StatisticalService($recordAt);
|
$statService = new StatisticalService();
|
||||||
|
$statService->setStartAt($recordAt);
|
||||||
$stats = $statService->getStatUser();
|
$stats = $statService->getStatUser();
|
||||||
DB::beginTransaction();
|
DB::beginTransaction();
|
||||||
foreach ($stats as $stat) {
|
foreach ($stats as $stat) {
|
||||||
@ -74,34 +107,19 @@ class V2boardStatistics extends Command
|
|||||||
$statService->clearStatUser();
|
$statService->clearStatUser();
|
||||||
}
|
}
|
||||||
|
|
||||||
private function statOrder()
|
private function stat()
|
||||||
{
|
{
|
||||||
$endAt = strtotime(date('Y-m-d'));
|
$startAt = strtotime('-1 day', strtotime(date('Y-m-d')));
|
||||||
$startAt = strtotime('-1 day', $endAt);
|
$statisticalService = new StatisticalService();
|
||||||
$orderBuilder = Order::where('paid_at', '>=', $startAt)
|
$statisticalService->setRecordAt($startAt);
|
||||||
->where('paid_at', '<', $endAt)
|
$data = $statisticalService->generateStatData();
|
||||||
->whereNotIn('status', [0, 2]);
|
$statistic = Stat::where('record_at', $startAt)
|
||||||
$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)
|
|
||||||
->where('record_type', 'd')
|
->where('record_type', 'd')
|
||||||
->first();
|
->first();
|
||||||
if ($statistic) {
|
if ($statistic) {
|
||||||
$statistic->update($data);
|
$statistic->update($data);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
StatOrder::create($data);
|
Stat::create($data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ use App\Models\ServerShadowsocks;
|
|||||||
use App\Models\ServerTrojan;
|
use App\Models\ServerTrojan;
|
||||||
use App\Models\StatUser;
|
use App\Models\StatUser;
|
||||||
use App\Services\ServerService;
|
use App\Services\ServerService;
|
||||||
|
use App\Services\StatisticalService;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Models\ServerGroup;
|
use App\Models\ServerGroup;
|
||||||
@ -15,16 +16,50 @@ use App\Models\Plan;
|
|||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use App\Models\Ticket;
|
use App\Models\Ticket;
|
||||||
use App\Models\Order;
|
use App\Models\Order;
|
||||||
use App\Models\StatOrder;
|
use App\Models\Stat;
|
||||||
use App\Models\StatServer;
|
use App\Models\StatServer;
|
||||||
use Illuminate\Support\Facades\Cache;
|
use Illuminate\Support\Facades\Cache;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
class StatController extends Controller
|
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)
|
public function getOverride(Request $request)
|
||||||
{
|
{
|
||||||
return response([
|
return [
|
||||||
'data' => [
|
'data' => [
|
||||||
'month_income' => Order::where('created_at', '>=', strtotime(date('Y-m-1')))
|
'month_income' => Order::where('created_at', '>=', strtotime(date('Y-m-1')))
|
||||||
->where('created_at', '<', time())
|
->where('created_at', '<', time())
|
||||||
@ -55,12 +90,12 @@ class StatController extends Controller
|
|||||||
->where('created_at', '<', strtotime(date('Y-m-1')))
|
->where('created_at', '<', strtotime(date('Y-m-1')))
|
||||||
->sum('get_amount'),
|
->sum('get_amount'),
|
||||||
]
|
]
|
||||||
]);
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getOrder(Request $request)
|
public function getOrder(Request $request)
|
||||||
{
|
{
|
||||||
$statistics = StatOrder::where('record_type', 'd')
|
$statistics = Stat::where('record_type', 'd')
|
||||||
->limit(31)
|
->limit(31)
|
||||||
->orderBy('record_at', 'DESC')
|
->orderBy('record_at', 'DESC')
|
||||||
->get()
|
->get()
|
||||||
@ -68,31 +103,31 @@ class StatController extends Controller
|
|||||||
$result = [];
|
$result = [];
|
||||||
foreach ($statistics as $statistic) {
|
foreach ($statistics as $statistic) {
|
||||||
$date = date('m-d', $statistic['record_at']);
|
$date = date('m-d', $statistic['record_at']);
|
||||||
array_push($result, [
|
$result[] = [
|
||||||
'type' => '收款金额',
|
'type' => '收款金额',
|
||||||
'date' => $date,
|
'date' => $date,
|
||||||
'value' => $statistic['order_amount'] / 100
|
'value' => $statistic['order_total'] / 100
|
||||||
]);
|
];
|
||||||
array_push($result, [
|
$result[] = [
|
||||||
'type' => '收款笔数',
|
'type' => '收款笔数',
|
||||||
'date' => $date,
|
'date' => $date,
|
||||||
'value' => $statistic['order_count']
|
'value' => $statistic['order_count']
|
||||||
]);
|
];
|
||||||
array_push($result, [
|
$result[] = [
|
||||||
'type' => '佣金金额(已发放)',
|
'type' => '佣金金额(已发放)',
|
||||||
'date' => $date,
|
'date' => $date,
|
||||||
'value' => $statistic['commission_amount'] / 100
|
'value' => $statistic['commission_total'] / 100
|
||||||
]);
|
];
|
||||||
array_push($result, [
|
$result[] = [
|
||||||
'type' => '佣金笔数(已发放)',
|
'type' => '佣金笔数(已发放)',
|
||||||
'date' => $date,
|
'date' => $date,
|
||||||
'value' => $statistic['commission_count']
|
'value' => $statistic['commission_count']
|
||||||
]);
|
];
|
||||||
}
|
}
|
||||||
$result = array_reverse($result);
|
$result = array_reverse($result);
|
||||||
return response([
|
return [
|
||||||
'data' => $result
|
'data' => $result
|
||||||
]);
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getServerLastRank()
|
public function getServerLastRank()
|
||||||
@ -128,9 +163,9 @@ class StatController extends Controller
|
|||||||
$statistics[$k]['total'] = $statistics[$k]['total'] / 1073741824;
|
$statistics[$k]['total'] = $statistics[$k]['total'] / 1073741824;
|
||||||
}
|
}
|
||||||
array_multisort(array_column($statistics, 'total'), SORT_DESC, $statistics);
|
array_multisort(array_column($statistics, 'total'), SORT_DESC, $statistics);
|
||||||
return response([
|
return [
|
||||||
'data' => $statistics
|
'data' => $statistics
|
||||||
]);
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getStatUser(Request $request)
|
public function getStatUser(Request $request)
|
||||||
@ -150,5 +185,6 @@ class StatController extends Controller
|
|||||||
'total' => $total
|
'total' => $total
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,20 +2,9 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers\Admin;
|
namespace App\Http\Controllers\Admin;
|
||||||
|
|
||||||
use App\Models\ServerShadowsocks;
|
|
||||||
use App\Models\ServerTrojan;
|
|
||||||
use App\Services\ServerService;
|
|
||||||
use App\Utils\CacheKey;
|
use App\Utils\CacheKey;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use App\Http\Controllers\Controller;
|
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\Cache;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Http;
|
use Illuminate\Support\Facades\Http;
|
||||||
|
@ -91,6 +91,8 @@ class DeepbworkController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
$statService = new StatisticalService();
|
$statService = new StatisticalService();
|
||||||
|
$statService->setStartAt(strtotime(date('Y-m-d')));
|
||||||
|
$statService->setUserStats();
|
||||||
$statService->statUser($server['rate'], $statData);
|
$statService->statUser($server['rate'], $statData);
|
||||||
|
|
||||||
return response([
|
return response([
|
||||||
|
@ -83,6 +83,8 @@ class ShadowsocksTidalabController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
$statService = new StatisticalService();
|
$statService = new StatisticalService();
|
||||||
|
$statService->setStartAt(strtotime(date('Y-m-d')));
|
||||||
|
$statService->setUserStats();
|
||||||
$statService->statUser($server['rate'], $statData);
|
$statService->statUser($server['rate'], $statData);
|
||||||
|
|
||||||
return response([
|
return response([
|
||||||
|
@ -88,6 +88,8 @@ class TrojanTidalabController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
$statService = new StatisticalService();
|
$statService = new StatisticalService();
|
||||||
|
$statService->setStartAt(strtotime(date('Y-m-d')));
|
||||||
|
$statService->setUserStats();
|
||||||
$statService->statUser($server['rate'], $statData);
|
$statService->statUser($server['rate'], $statData);
|
||||||
|
|
||||||
return response([
|
return response([
|
||||||
|
@ -71,7 +71,11 @@ class UniProxyController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
$statService = new StatisticalService();
|
$statService = new StatisticalService();
|
||||||
|
$statService->setStartAt(strtotime(date('Y-m-d')));
|
||||||
|
$statService->setUserStats();
|
||||||
$statService->statUser($this->nodeInfo->rate, $data);
|
$statService->statUser($this->nodeInfo->rate, $data);
|
||||||
|
$statService->setServerStats();
|
||||||
|
$statService->statServer($this->nodeId, $this->nodeType, $data);
|
||||||
|
|
||||||
return response([
|
return response([
|
||||||
'data' => true
|
'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;
|
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 $dateFormat = 'U';
|
||||||
protected $guarded = ['id'];
|
protected $guarded = ['id'];
|
||||||
protected $casts = [
|
protected $casts = [
|
@ -1,23 +1,108 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace App\Services;
|
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\Cache;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
class StatisticalService {
|
class StatisticalService {
|
||||||
protected $userStats;
|
protected $userStats;
|
||||||
protected $recordAt;
|
protected $startAt;
|
||||||
|
protected $endAt;
|
||||||
|
protected $serverStats;
|
||||||
|
|
||||||
public function __construct($recordAt = NULL)
|
public function __construct()
|
||||||
{
|
{
|
||||||
ini_set('memory_limit', -1);
|
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) ?? [];
|
$this->userStats = json_decode($this->userStats, true) ?? [];
|
||||||
if (!is_array($this->userStats)) {
|
if (!is_array($this->userStats)) {
|
||||||
$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)
|
public function statUser($rate, $data)
|
||||||
{
|
{
|
||||||
foreach (array_keys($data) as $key) {
|
foreach (array_keys($data) as $key) {
|
||||||
@ -29,9 +114,24 @@ class StatisticalService {
|
|||||||
$this->userStats[$rate][$key] = $data[$key];
|
$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()
|
public function getStatUser()
|
||||||
{
|
{
|
||||||
@ -51,8 +151,32 @@ class StatisticalService {
|
|||||||
return $stats;
|
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()
|
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)
|
public function trafficFetch(int $u, int $d, int $userId, array $server, string $protocol)
|
||||||
{
|
{
|
||||||
TrafficFetchJob::dispatch($u, $d, $userId, $server, $protocol);
|
TrafficFetchJob::dispatch($u, $d, $userId, $server, $protocol);
|
||||||
StatServerJob::dispatch($u, $d, $server, $protocol, 'd');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -175,7 +175,6 @@ return [
|
|||||||
'queue' => [
|
'queue' => [
|
||||||
'order_handle',
|
'order_handle',
|
||||||
'traffic_fetch',
|
'traffic_fetch',
|
||||||
'stat',
|
|
||||||
'send_email',
|
'send_email',
|
||||||
'send_email_mass',
|
'send_email_mass',
|
||||||
'send_telegram',
|
'send_telegram',
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
-- Adminer 4.8.1 MySQL 5.7.29 dump
|
-- Adminer 4.7.7 MySQL dump
|
||||||
|
|
||||||
SET NAMES utf8;
|
SET NAMES utf8;
|
||||||
SET time_zone = '+00:00';
|
SET time_zone = '+00:00';
|
||||||
@ -304,19 +304,24 @@ CREATE TABLE `v2_server_vmess` (
|
|||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||||
|
|
||||||
|
|
||||||
DROP TABLE IF EXISTS `v2_stat_order`;
|
DROP TABLE IF EXISTS `v2_stat`;
|
||||||
CREATE TABLE `v2_stat_order` (
|
CREATE TABLE `v2_stat` (
|
||||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||||
`order_count` int(11) NOT NULL COMMENT '订单数量',
|
`record_at` int(11) NOT NULL,
|
||||||
`order_amount` int(11) NOT NULL COMMENT '订单合计',
|
`record_type` char(1) NOT NULL,
|
||||||
`commission_count` int(11) NOT NULL,
|
`order_count` int(11) NOT NULL COMMENT '订单数量',
|
||||||
`commission_amount` int(11) NOT NULL COMMENT '佣金合计',
|
`order_total` int(11) NOT NULL COMMENT '订单合计',
|
||||||
`record_type` char(1) NOT NULL,
|
`commission_count` int(11) NOT NULL,
|
||||||
`record_at` int(11) NOT NULL,
|
`commission_total` int(11) NOT NULL COMMENT '佣金合计',
|
||||||
`created_at` int(11) NOT NULL,
|
`paid_count` int(11) NOT NULL,
|
||||||
`updated_at` int(11) NOT NULL,
|
`paid_total` int(11) NOT NULL,
|
||||||
PRIMARY KEY (`id`),
|
`register_count` int(11) NOT NULL,
|
||||||
UNIQUE KEY `record_at` (`record_at`)
|
`invite_count` int(11) NOT NULL,
|
||||||
|
`transfer_used_total` varchar(32) NOT NULL,
|
||||||
|
`created_at` int(11) NOT NULL,
|
||||||
|
`updated_at` int(11) NOT NULL,
|
||||||
|
PRIMARY KEY (`id`),
|
||||||
|
UNIQUE KEY `record_at` (`record_at`)
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='订单统计';
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='订单统计';
|
||||||
|
|
||||||
|
|
||||||
@ -422,4 +427,4 @@ CREATE TABLE `v2_user` (
|
|||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||||
|
|
||||||
|
|
||||||
-- 2023-03-08 06:17:49
|
-- 2023-05-03 07:27:22
|
||||||
|
@ -683,3 +683,17 @@ CREATE TABLE `v2_server_hysteria` (
|
|||||||
|
|
||||||
ALTER TABLE `v2_plan`
|
ALTER TABLE `v2_plan`
|
||||||
ADD `capacity_limit` int(11) NULL AFTER `reset_traffic_method`;
|
ADD `capacity_limit` int(11) NULL AFTER `reset_traffic_method`;
|
||||||
|
|
||||||
|
ALTER TABLE `v2_stat_order`
|
||||||
|
CHANGE `record_at` `record_at` int(11) NOT NULL AFTER `id`,
|
||||||
|
CHANGE `record_type` `record_type` char(1) COLLATE 'utf8_general_ci' NOT NULL AFTER `record_at`,
|
||||||
|
CHANGE `order_count` `order_count` int(11) NOT NULL COMMENT '订单数量' AFTER `record_type`,
|
||||||
|
CHANGE `order_amount` `order_total` int(11) NOT NULL COMMENT '订单合计' AFTER `order_count`,
|
||||||
|
CHANGE `commission_count` `commission_count` int(11) NOT NULL AFTER `order_total`,
|
||||||
|
CHANGE `commission_amount` `commission_total` int(11) NOT NULL COMMENT '佣金合计' AFTER `commission_count`,
|
||||||
|
ADD `paid_count` int(11) NOT NULL AFTER `commission_total`,
|
||||||
|
ADD `paid_total` int(11) NOT NULL AFTER `paid_count`,
|
||||||
|
ADD `register_count` int(11) NOT NULL AFTER `paid_total`,
|
||||||
|
ADD `invite_count` int(11) NOT NULL AFTER `register_count`,
|
||||||
|
ADD `transfer_used_total` varchar(32) NOT NULL AFTER `invite_count`,
|
||||||
|
RENAME TO `v2_stat`;
|
||||||
|
Loading…
Reference in New Issue
Block a user