update: more api

This commit is contained in:
v2board 2023-05-20 20:50:53 +08:00
parent 2f04505562
commit b20504a431
5 changed files with 277 additions and 27 deletions

View File

@ -22,7 +22,8 @@ class SystemController extends Controller
return response([ return response([
'data' => [ 'data' => [
'schedule' => $this->getScheduleStatus(), 'schedule' => $this->getScheduleStatus(),
'horizon' => $this->getHorizonStatus() 'horizon' => $this->getHorizonStatus(),
'schedule_last_runtime' => Cache::get(CacheKey::get('SCHEDULE_LAST_CHECK_AT', null))
] ]
]); ]);
} }

View File

@ -1,40 +1,225 @@
<?php <?php
namespace App\Http\Controllers\User; namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller; use App\Models\CommissionLog;
use App\Models\ServerShadowsocks;
use App\Models\ServerTrojan;
use App\Models\StatUser; use App\Models\StatUser;
use App\Services\ServerService;
use App\Services\StatisticalService; use App\Services\StatisticalService;
use Illuminate\Http\Request; 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\Stat;
use App\Models\StatServer;
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 getTrafficLog(Request $request) public function getStat(Request $request)
{ {
$statService = new StatisticalService(); $params = $request->validate([
$statService->setStartAt(strtotime(date('Y-m-d'))); 'start_at' => '',
$statService->setUserStats(); 'end_at' => ''
$stats = StatUser::select([ ]);
'u',
'd', if (isset($params['start_at']) && isset($params['end_at'])) {
'record_at', $stats = Stat::where('record_at', '>=', $params['start_at'])
'user_id', ->where('record_at', '<', $params['end_at'])
'server_rate' ->get()
]) ->makeHidden(['record_at', 'created_at', 'updated_at', 'id', 'record_type'])
->where('user_id', $request->user['id']) ->toArray();
->where('record_at', '>=', strtotime(date('Y-m-1'))) } else {
$statisticalService = new StatisticalService();
return [
'data' => $statisticalService->generateStatData()
];
}
$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
];
}
public function getStatRecord(Request $request)
{
$request->validate([
'type' => 'required|in:paid_total,commission_total,register_count',
'start_at' => '',
'end_at' => ''
]);
$statisticalService = new StatisticalService();
$statisticalService->setStartAt($request->input('start_at'));
$statisticalService->setEndAt($request->input('end_at'));
return [
'data' => $statisticalService->getStatRecord($request->input('type'))
];
}
public function getRanking(Request $request)
{
$request->validate([
'type' => 'required|in:server_traffic_rank,user_consumption_rank',
'start_at' => '',
'end_at' => '',
'limit' => 'nullable|integer'
]);
$statisticalService = new StatisticalService();
$statisticalService->setStartAt($request->input('start_at'));
$statisticalService->setEndAt($request->input('end_at'));
return [
'data' => $statisticalService->getRanking($request->input('type'), $request->input('limit') ?? 20)
];
}
public function getOverride(Request $request)
{
return [
'data' => [
'month_income' => Order::where('created_at', '>=', strtotime(date('Y-m-1')))
->where('created_at', '<', time())
->whereNotIn('status', [0, 2])
->sum('total_amount'),
'month_register_total' => User::where('created_at', '>=', strtotime(date('Y-m-1')))
->where('created_at', '<', time())
->count(),
'ticket_pending_total' => Ticket::where('status', 0)
->count(),
'commission_pending_total' => Order::where('commission_status', 0)
->where('invite_user_id', '!=', NULL)
->whereNotIn('status', [0, 2])
->where('commission_balance', '>', 0)
->count(),
'day_income' => Order::where('created_at', '>=', strtotime(date('Y-m-d')))
->where('created_at', '<', time())
->whereNotIn('status', [0, 2])
->sum('total_amount'),
'last_month_income' => Order::where('created_at', '>=', strtotime('-1 month', strtotime(date('Y-m-1'))))
->where('created_at', '<', strtotime(date('Y-m-1')))
->whereNotIn('status', [0, 2])
->sum('total_amount'),
'commission_month_payout' => CommissionLog::where('created_at', '>=', strtotime(date('Y-m-1')))
->where('created_at', '<', time())
->sum('get_amount'),
'commission_last_month_payout' => CommissionLog::where('created_at', '>=', strtotime('-1 month', strtotime(date('Y-m-1'))))
->where('created_at', '<', strtotime(date('Y-m-1')))
->sum('get_amount'),
]
];
}
public function getOrder(Request $request)
{
$statistics = Stat::where('record_type', 'd')
->limit(31)
->orderBy('record_at', 'DESC') ->orderBy('record_at', 'DESC')
->get() ->get()
->toArray(); ->toArray();
$result = [];
$stats = array_merge($stats, $statService->getStatUserByUserID($request->user['id'])); foreach ($statistics as $statistic) {
usort($stats, function ($a, $b) { $date = date('m-d', $statistic['record_at']);
return $b['record_at'] - $a['record_at']; $result[] = [
}); 'type' => '收款金额',
'date' => $date,
return response([ 'value' => $statistic['paid_total'] / 100
'data' => $stats ];
]); $result[] = [
'type' => '收款笔数',
'date' => $date,
'value' => $statistic['paid_count']
];
$result[] = [
'type' => '佣金金额(已发放)',
'date' => $date,
'value' => $statistic['commission_total'] / 100
];
$result[] = [
'type' => '佣金笔数(已发放)',
'date' => $date,
'value' => $statistic['commission_count']
];
}
$result = array_reverse($result);
return [
'data' => $result
];
} }
public function getServerLastRank()
{
$servers = [
'shadowsocks' => ServerShadowsocks::where('parent_id', null)->get()->toArray(),
'v2ray' => ServerVmess::where('parent_id', null)->get()->toArray(),
'trojan' => ServerTrojan::where('parent_id', null)->get()->toArray(),
'vmess' => ServerVmess::where('parent_id', null)->get()->toArray()
];
$startAt = strtotime('-1 day', strtotime(date('Y-m-d')));
$endAt = strtotime(date('Y-m-d'));
$statistics = StatServer::select([
'server_id',
'server_type',
'u',
'd',
DB::raw('(u+d) as total')
])
->where('record_at', '>=', $startAt)
->where('record_at', '<', $endAt)
->where('record_type', 'd')
->limit(10)
->orderBy('total', 'DESC')
->get()
->toArray();
foreach ($statistics as $k => $v) {
foreach ($servers[$v['server_type']] as $server) {
if ($server['id'] === $v['server_id']) {
$statistics[$k]['server_name'] = $server['name'];
}
}
$statistics[$k]['total'] = $statistics[$k]['total'] / 1073741824;
}
array_multisort(array_column($statistics, 'total'), SORT_DESC, $statistics);
return [
'data' => $statistics
];
}
public function getStatUser(Request $request)
{
$request->validate([
'user_id' => 'required|integer'
]);
$current = $request->input('current') ? $request->input('current') : 1;
$pageSize = $request->input('pageSize') >= 10 ? $request->input('pageSize') : 10;
$builder = StatUser::orderBy('record_at', 'DESC')->where('user_id', $request->input('user_id'));
$total = $builder->count();
$records = $builder->forPage($current, $pageSize)
->get();
return [
'data' => $records,
'total' => $total
];
}
} }

View File

@ -5,6 +5,7 @@ use App\Models\CommissionLog;
use App\Models\Order; use App\Models\Order;
use App\Models\Stat; use App\Models\Stat;
use App\Models\StatServer; use App\Models\StatServer;
use App\Models\StatUser;
use App\Models\User; use App\Models\User;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
@ -175,10 +176,10 @@ class StatisticalService {
public function getStatRecord($type) public function getStatRecord($type)
{ {
switch ($type) { switch ($type) {
case "order_total": { case "paid_total": {
return Stat::select([ return Stat::select([
'*', '*',
DB::raw('order_total / 100 as order_total') DB::raw('paid_total / 100 as paid_total')
]) ])
->where('record_at', '>=', $this->startAt) ->where('record_at', '>=', $this->startAt)
->where('record_at', '<', $this->endAt) ->where('record_at', '<', $this->endAt)
@ -203,4 +204,45 @@ class StatisticalService {
} }
} }
} }
public function getRanking($type, $limit = 20)
{
switch ($type) {
case 'server_traffic_rank': {
return StatServer::select([
'server_id',
'server_type',
DB::raw('sum(u) as u'),
DB::raw('sum(d) as d'),
DB::raw('sum(u) + sum(d) as total')
])
->where('record_at', '>=', $this->startAt)
->where('record_at', '<', $this->endAt)
->groupBy('server_id', 'server_type')
->orderBy('total', 'DESC')
->limit($limit)
->get();
}
case 'user_consumption_rank': {
$stats = StatUser::select([
'user_id',
DB::raw('sum(u) as u'),
DB::raw('sum(d) as d'),
DB::raw('sum(u) + sum(d) as total')
])
->where('record_at', '>=', $this->startAt)
->where('record_at', '<', $this->endAt)
->groupBy('user_id')
->orderBy('total', 'DESC')
->limit($limit)
->get();
$users = User::whereIn('id', $stats->pluck('user_id')->toArray())->get()->keyBy('id');
foreach ($stats as $k => $v) {
if (!isset($users[$v['user_id']])) continue;
$stats[$k]['email'] = $users[$v['user_id']]['email'];
}
return $stats;
}
}
}
} }

View File

@ -1,5 +1,16 @@
#!/bin/bash #!/bin/bash
if [ ! -d ".git" ]; then
echo "Please deploy using Git."
exit 1
fi
if ! command -v git &> /dev/null; then
echo "Git is not installed! Please install git and try again."
exit 1
fi
git config --global --add safe.directory $(pwd)
git fetch --all && git reset --hard origin/master && git pull origin master git fetch --all && git reset --hard origin/master && git pull origin master
rm -rf composer.lock composer.phar rm -rf composer.lock composer.phar
wget https://github.com/composer/composer/releases/latest/download/composer.phar -O composer.phar wget https://github.com/composer/composer/releases/latest/download/composer.phar -O composer.phar

View File

@ -1,5 +1,16 @@
#!/bin/bash #!/bin/bash
if [ ! -d ".git" ]; then
echo "Please deploy using Git."
exit 1
fi
if ! command -v git &> /dev/null; then
echo "Git is not installed! Please install git and try again."
exit 1
fi
git config --global --add safe.directory $(pwd)
git fetch --all && git reset --hard origin/dev && git pull origin dev git fetch --all && git reset --hard origin/dev && git pull origin dev
git checkout dev git checkout dev
rm -rf composer.lock composer.phar rm -rf composer.lock composer.phar