update: statistical service

This commit is contained in:
v2board 2023-04-15 19:34:54 +08:00
parent 6dd509d73e
commit 7be6553396
10 changed files with 112 additions and 5 deletions

View File

@ -45,6 +45,7 @@ class SendRemindMail extends Command
$mailService = new MailService();
foreach ($users as $user) {
if ($user->remind_expire) $mailService->remindExpire($user);
if ($user->remind_traffic) $mailService->remindTraffic($user);
}
}
}

View File

@ -2,10 +2,13 @@
namespace App\Console\Commands;
use App\Models\StatUser;
use App\Services\StatisticalService;
use Illuminate\Console\Command;
use App\Models\Order;
use App\Models\StatOrder;
use App\Models\CommissionLog;
use Illuminate\Support\Facades\DB;
class V2boardStatistics extends Command
{
@ -44,6 +47,31 @@ class V2boardStatistics extends Command
$this->statOrder();
}
private function statUser()
{
$createdAt = time();
$recordAt = strtotime('-1 day', strtotime(date('Y-m-d')));
$statService = new StatisticalService($recordAt);
$stats = $statService->getStatUser();
DB::beginTransaction();
foreach ($stats as $stat) {
if (!StatUser::insert([
'user_id' => $stat['user_Id'],
'u' => $stat['u'],
'd' => $stat['d'],
'server_rate' => $stat['server_rate'],
'created_at' => $createdAt,
'updated_at' => $createdAt,
'record_type' => 'd',
'record_at' => $recordAt
])) {
DB::rollback();
break;
}
}
DB::commit();
}
private function statOrder()
{
$endAt = strtotime(date('Y-m-d'));

View File

@ -3,6 +3,7 @@
namespace App\Http\Controllers\Server;
use App\Services\ServerService;
use App\Services\StatisticalService;
use App\Services\UserService;
use App\Utils\CacheKey;
use Illuminate\Http\Request;
@ -81,12 +82,17 @@ class DeepbworkController extends Controller
Cache::put(CacheKey::get('SERVER_VMESS_ONLINE_USER', $server->id), count($data), 3600);
Cache::put(CacheKey::get('SERVER_VMESS_LAST_PUSH_AT', $server->id), time(), 3600);
$userService = new UserService();
$statData = [];
foreach ($data as $item) {
$u = $item['u'];
$d = $item['d'];
$userService->trafficFetch($u, $d, $item['user_id'], $server->toArray(), 'vmess');
$statData[$item['user_id']] = [$u, $d];
}
$statService = new StatisticalService();
$statService->statUser($server['rate'], $statData);
return response([
'ret' => 1,
'msg' => 'ok'

View File

@ -4,6 +4,7 @@ namespace App\Http\Controllers\Server;
use App\Models\ServerShadowsocks;
use App\Services\ServerService;
use App\Services\StatisticalService;
use App\Services\UserService;
use App\Utils\CacheKey;
use Illuminate\Http\Request;
@ -73,12 +74,17 @@ class ShadowsocksTidalabController extends Controller
Cache::put(CacheKey::get('SERVER_SHADOWSOCKS_ONLINE_USER', $server->id), count($data), 3600);
Cache::put(CacheKey::get('SERVER_SHADOWSOCKS_LAST_PUSH_AT', $server->id), time(), 3600);
$userService = new UserService();
$statData = [];
foreach ($data as $item) {
$u = $item['u'];
$d = $item['d'];
$userService->trafficFetch($u, $d, $item['user_id'], $server->toArray(), 'shadowsocks');
$statData[$item['user_id']] = [$u, $d];
}
$statService = new StatisticalService();
$statService->statUser($server['rate'], $statData);
return response([
'ret' => 1,
'msg' => 'ok'

View File

@ -3,6 +3,7 @@
namespace App\Http\Controllers\Server;
use App\Services\ServerService;
use App\Services\StatisticalService;
use App\Services\UserService;
use App\Utils\CacheKey;
use Illuminate\Http\Request;
@ -78,12 +79,17 @@ class TrojanTidalabController extends Controller
Cache::put(CacheKey::get('SERVER_TROJAN_ONLINE_USER', $server->id), count($data), 3600);
Cache::put(CacheKey::get('SERVER_TROJAN_LAST_PUSH_AT', $server->id), time(), 3600);
$userService = new UserService();
$statData = [];
foreach ($data as $item) {
$u = $item['u'];
$d = $item['d'];
$userService->trafficFetch($u, $d, $item['user_id'], $server->toArray(), 'trojan');
$statData[$item['user_id']] = [$u, $d];
}
$statService = new StatisticalService();
$statService->statUser($server['rate'], $statData);
return response([
'ret' => 1,
'msg' => 'ok'

View File

@ -3,6 +3,7 @@
namespace App\Http\Controllers\Server;
use App\Services\ServerService;
use App\Services\StatisticalService;
use App\Services\UserService;
use App\Utils\CacheKey;
use App\Utils\Helper;
@ -69,6 +70,9 @@ class UniProxyController extends Controller
$userService->trafficFetch($u, $d, $k, $this->nodeInfo->toArray(), $this->nodeType);
}
$statService = new StatisticalService();
$statService->statUser($this->nodeInfo->rate, $data);
return response([
'data' => true
]);

View File

@ -50,8 +50,8 @@ class TrafficFetchJob implements ShouldQueue
$user->t = time();
$user->u = $user->u + ($this->u * $this->server['rate']);
$user->d = $user->d + ($this->d * $this->server['rate']);
if (!$user->save()) throw new \Exception('流量更新失败');
$mailService = new MailService();
$mailService->remindTraffic($user);
if (!$user->save()) {
info("流量更新失败\n未记录用户ID:{$this->userId}\n未记录上行:{$user->u}\n未记录下行:{$user->d}");
}
}
}

View File

@ -223,7 +223,11 @@ class OrderService
$order->paid_at = time();
$order->callback_no = $callbackNo;
if (!$order->save()) return false;
OrderHandleJob::dispatch($order->trade_no);
try {
OrderHandleJob::dispatchNow($order->trade_no);
} catch (\Exception $e) {
return false;
}
return true;
}

View File

@ -0,0 +1,53 @@
<?php
namespace App\Services;
use Illuminate\Support\Facades\Cache;
class StatisticalService {
protected $userStats;
protected $recordAt;
public function __construct($recordAt = '')
{
ini_set('memory_limit', -1);
$this->recordAt = $recordAt ?? strtotime(date('Y-m-d'));
$this->userStats = Cache::get("stat_user_{$this->recordAt}");
$this->userStats = json_decode($this->userStats, true) ?? [];
if (!is_array($this->userStats)) {
$this->userStats = [];
}
}
public function statUser($rate, $data)
{
foreach (array_keys($data) as $key) {
$this->userStats[$rate] = $this->userStats[$rate] ?? [];
if (isset($this->userStats[$rate][$key])) {
$this->userStats[$rate][$key][0] += $data[$key][0];
$this->userStats[$rate][$key][1] += $data[$key][1];
} else {
$this->userStats[$rate][$key] = $data[$key];
}
}
Cache::set("stat_user_{$this->recordAt}", json_encode($this->userStats));
}
public function getStatUser()
{
$stats = [];
foreach ($this->userStats as $k => $v) {
foreach (array_keys($v) as $userId) {
if (isset($v[$userId])) {
$stats[] = [
'server_rate' => $k,
'u' => $v[$userId][0],
'd' => $v[$userId][1],
'user_id' => $userId
];
}
}
}
return $stats;
}
}

View File

@ -172,6 +172,5 @@ class UserService
{
TrafficFetchJob::dispatch($u, $d, $userId, $server, $protocol);
StatServerJob::dispatch($u, $d, $server, $protocol, 'd');
StatUserJob::dispatch($u, $d, $userId, $server, $protocol, 'd');
}
}