update: stat user

This commit is contained in:
tokumeikoi 2022-02-20 01:06:02 +08:00
parent 23f98d7abc
commit c270f3ab5a
8 changed files with 168 additions and 35 deletions

View File

@ -44,7 +44,6 @@ class V2boardStatistics extends Command
{ {
ini_set('memory_limit', -1); ini_set('memory_limit', -1);
$this->statOrder(); $this->statOrder();
$this->statServer();
} }
private function statOrder() private function statOrder()
@ -76,26 +75,4 @@ class V2boardStatistics extends Command
} }
StatOrder::create($data); StatOrder::create($data);
} }
private function statServer()
{
$endAt = strtotime(date('Y-m-d'));
$startAt = strtotime('-1 day', $endAt);
$statistics = ServerLog::select([
'server_id',
'method as server_type',
DB::raw("sum(u) as u"),
DB::raw("sum(d) as d"),
])
->where('log_at', '>=', $startAt)
->where('log_at', '<', $endAt)
->groupBy('server_id', 'method')
->get()
->toArray();
foreach ($statistics as $statistic) {
$statistic['record_type'] = 'd';
$statistic['record_at'] = $startAt;
StatServerJob::dispatch($statistic);
}
}
} }

View File

@ -12,7 +12,11 @@ use Illuminate\Queue\SerializesModels;
class StatServerJob implements ShouldQueue class StatServerJob implements ShouldQueue
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $statistic; protected $u;
protected $d;
protected $server;
protected $protocol;
protected $recordType;
public $tries = 3; public $tries = 3;
public $timeout = 60; public $timeout = 60;
@ -22,10 +26,14 @@ class StatServerJob implements ShouldQueue
* *
* @return void * @return void
*/ */
public function __construct(array $statistic) public function __construct($u, $d, $server, $protocol, $recordType = 'd')
{ {
$this->onQueue('stat_server'); $this->onQueue('stat');
$this->statistic = $statistic; $this->u = $u;
$this->d = $d;
$this->server = $server;
$this->protocol = $protocol;
$this->recordType = $recordType;
} }
/** /**
@ -35,18 +43,32 @@ class StatServerJob implements ShouldQueue
*/ */
public function handle() public function handle()
{ {
$statistic = $this->statistic; $recordAt = strtotime(date('Y-m-d'));
$data = StatServer::where('record_at', $statistic['record_at']) if ($this->recordType === 'm') {
->where('server_id', $statistic['server_id']) //
}
$data = StatServer::where('record_at', $recordAt)
->where('server_id', $this->server->id)
->first(); ->first();
if ($data) { if ($data) {
try { try {
$data->update($statistic); $data->update([
'u' => $data['u'] + $this->u,
'd' => $data['d'] + $this->d
]);
} catch (\Exception $e) { } catch (\Exception $e) {
abort(500, '节点统计数据更新失败'); abort(500, '节点统计数据更新失败');
} }
} else { } else {
if (!StatServer::create($statistic)) { 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, '节点统计数据创建失败'); abort(500, '节点统计数据创建失败');
} }
} }

82
app/Jobs/StatUserJob.php Normal file
View File

@ -0,0 +1,82 @@
<?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 = 60;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($u, $d, $userId, $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_id', $this->server->id)
->where('user_id', $this->userId)
->first();
if ($data) {
try {
$data->update([
'u' => $data['u'] + $this->u,
'd' => $data['d'] + $this->d
]);
} catch (\Exception $e) {
abort(500, '用户统计数据更新失败');
}
} else {
if (!StatUser::create([
'user_id' => $this->userId,
'server_id' => $this->server->id,
'server_type' => $this->protocol,
'server_rate' => $this->server->rate,
'u' => $this->u,
'd' => $this->d,
'record_type' => $this->recordType,
'record_at' => $recordAt
])) {
abort(500, '用户统计数据创建失败');
}
}
}
}

16
app/Models/StatUser.php Normal file
View File

@ -0,0 +1,16 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class StatUser extends Model
{
protected $table = 'v2_stat_user';
protected $dateFormat = 'U';
protected $guarded = ['id'];
protected $casts = [
'created_at' => 'timestamp',
'updated_at' => 'timestamp'
];
}

View File

@ -3,6 +3,8 @@
namespace App\Services; namespace App\Services;
use App\Jobs\ServerLogJob; use App\Jobs\ServerLogJob;
use App\Jobs\StatServerJob;
use App\Jobs\StatUserJob;
use App\Jobs\TrafficFetchJob; use App\Jobs\TrafficFetchJob;
use App\Models\InviteCode; use App\Models\InviteCode;
use App\Models\Order; use App\Models\Order;
@ -86,5 +88,7 @@ class UserService
{ {
TrafficFetchJob::dispatch($u, $d, $userId, $server, $protocol); TrafficFetchJob::dispatch($u, $d, $userId, $server, $protocol);
ServerLogJob::dispatch($u, $d, $userId, $server, $protocol); ServerLogJob::dispatch($u, $d, $userId, $server, $protocol);
StatServerJob::dispatch($u, $d, $server, $protocol, 'd');
StatUserJob::dispatch($u, $d, $userId, $server, $protocol, 'd');
} }
} }

View File

@ -173,13 +173,13 @@ return [
'V2board' => [ 'V2board' => [
'connection' => 'redis', 'connection' => 'redis',
'queue' => [ 'queue' => [
'order_handle',
'traffic_fetch', 'traffic_fetch',
'stat',
'server_log', 'server_log',
'send_email', 'send_email',
'send_email_mass', 'send_email_mass',
'send_telegram', 'send_telegram',
'stat_server',
'order_handle',
], ],
'balance' => 'auto', 'balance' => 'auto',
'minProcesses' => 1, 'minProcesses' => 1,

View File

@ -308,6 +308,23 @@ CREATE TABLE `v2_stat_server` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='节点数据统计'; ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='节点数据统计';
DROP TABLE IF EXISTS `v2_stat_user`;
CREATE TABLE `v2_stat_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`server_id` int(11) NOT NULL,
`server_type` char(11) NOT NULL,
`server_rate` decimal(10,2) NOT NULL,
`u` bigint(20) NOT NULL,
`d` bigint(20) NOT NULL,
`record_type` char(2) NOT NULL,
`record_at` int(11) NOT NULL,
`created_at` int(11) NOT NULL,
`updated_at` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
DROP TABLE IF EXISTS `v2_ticket`; DROP TABLE IF EXISTS `v2_ticket`;
CREATE TABLE `v2_ticket` ( CREATE TABLE `v2_ticket` (
`id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
@ -372,4 +389,4 @@ CREATE TABLE `v2_user` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8; ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- 2022-02-16 19:16:57 -- 2022-02-19 16:59:30

View File

@ -489,3 +489,18 @@ ALTER TABLE `v2_order`
ALTER TABLE `v2_server_v2ray` ALTER TABLE `v2_server_v2ray`
CHANGE `port` `port` char(11) NOT NULL AFTER `host`; CHANGE `port` `port` char(11) NOT NULL AFTER `host`;
CREATE TABLE `v2_stat_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`server_id` int(11) NOT NULL,
`server_type` char(11) NOT NULL,
`server_rate` decimal(10,2) NOT NULL,
`u` bigint(20) NOT NULL,
`d` bigint(20) NOT NULL,
`record_type` char(2) NOT NULL,
`record_at` int(11) NOT NULL,
`created_at` int(11) NOT NULL,
`updated_at` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;