mirror of
https://github.com/v2board/v2board.git
synced 2025-03-13 06:04:42 +08:00
优化实时流量统计性能
This commit is contained in:
parent
3f3de69389
commit
30e2f156a3
@ -9,13 +9,15 @@ use Illuminate\Contracts\Queue\ShouldQueue;
|
|||||||
use Illuminate\Foundation\Bus\Dispatchable;
|
use Illuminate\Foundation\Bus\Dispatchable;
|
||||||
use Illuminate\Queue\InteractsWithQueue;
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
use Illuminate\Queue\SerializesModels;
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
class StatUserJob implements ShouldQueue
|
class StatUserJob implements ShouldQueue
|
||||||
{
|
{
|
||||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||||
protected $u;
|
//protected $u;
|
||||||
protected $d;
|
//protected $d;
|
||||||
protected $userId;
|
//protected $userId;
|
||||||
|
protected $data;
|
||||||
protected $server;
|
protected $server;
|
||||||
protected $protocol;
|
protected $protocol;
|
||||||
protected $recordType;
|
protected $recordType;
|
||||||
@ -28,12 +30,13 @@ class StatUserJob implements ShouldQueue
|
|||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function __construct($u, $d, $userId, array $server, $protocol, $recordType = 'd')
|
public function __construct(array $data, array $server, $protocol, $recordType = 'd')
|
||||||
{
|
{
|
||||||
$this->onQueue('stat');
|
$this->onQueue('stat');
|
||||||
$this->u = $u;
|
//$this->u = $u;
|
||||||
$this->d = $d;
|
//$this->d = $d;
|
||||||
$this->userId = $userId;
|
//$this->userId = $userId;
|
||||||
|
$this->data =$data;
|
||||||
$this->server = $server;
|
$this->server = $server;
|
||||||
$this->protocol = $protocol;
|
$this->protocol = $protocol;
|
||||||
$this->recordType = $recordType;
|
$this->recordType = $recordType;
|
||||||
@ -50,31 +53,36 @@ class StatUserJob implements ShouldQueue
|
|||||||
if ($this->recordType === 'm') {
|
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 {
|
try {
|
||||||
$data->update([
|
DB::beginTransaction();
|
||||||
'u' => $data['u'] + ($this->u * $this->server['rate']),
|
foreach(array_keys($this->data) as $userId){
|
||||||
'd' => $data['d'] + ($this->d * $this->server['rate'])
|
$userdata = StatUser::where('record_at', $recordAt)
|
||||||
|
->where('server_rate', $this->server['rate'])
|
||||||
|
->where('user_id', $userId)
|
||||||
|
->lockForUpdate()->first();
|
||||||
|
if ($userdata) {
|
||||||
|
$userdata->update([
|
||||||
|
'u' => $userdata['u'] + ($this->data[$userId][0] * $this->server['rate']),
|
||||||
|
'd' => $userdata['d'] + ($this->data[$userId][1] * $this->server['rate'])
|
||||||
]);
|
]);
|
||||||
} catch (\Exception $e) {
|
|
||||||
abort(500, '用户统计数据更新失败');
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if (!StatUser::create([
|
$insertData[] = [
|
||||||
'user_id' => $this->userId,
|
'user_id' => $userId,
|
||||||
'server_rate' => $this->server['rate'],
|
'server_rate' => $this->server['rate'],
|
||||||
'u' => $this->u,
|
'u' => $this->data[$userId][0],
|
||||||
'd' => $this->d,
|
'd' => $this->data[$userId][1],
|
||||||
'record_type' => $this->recordType,
|
'record_type' => $this->recordType,
|
||||||
'record_at' => $recordAt
|
'record_at' => $recordAt
|
||||||
])) {
|
];
|
||||||
abort(500, '用户统计数据创建失败');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!empty($insertData)) {
|
||||||
|
StatUser::upsert($insertData, ['user_id', 'server_rate', 'record_at']);
|
||||||
|
}
|
||||||
|
DB::commit();
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
DB::rollback();
|
||||||
|
abort(500, '用户统计数据失败'. $e->getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -178,10 +178,11 @@ class UserService
|
|||||||
$u = $data[$userId][0];
|
$u = $data[$userId][0];
|
||||||
$d = $data[$userId][1];
|
$d = $data[$userId][1];
|
||||||
StatServerJob::dispatch($u, $d, $server, $protocol, 'd');
|
StatServerJob::dispatch($u, $d, $server, $protocol, 'd');
|
||||||
StatUserJob::dispatch($u, $d, $userId, $server, $protocol, 'd');
|
//StatUserJob::dispatch($u, $d, $userId, $server, $protocol, 'd');
|
||||||
TrafficFetchJob::dispatch($u, $d, $userId, $server, $protocol);
|
TrafficFetchJob::dispatch($u, $d, $userId, $server, $protocol);
|
||||||
//$statService->statServer($server['id'], $protocol, $u, $d);
|
//$statService->statServer($server['id'], $protocol, $u, $d);
|
||||||
//$statService->statUser($server['rate'], $userId, $u, $d);
|
//$statService->statUser($server['rate'], $userId, $u, $d);
|
||||||
}
|
}
|
||||||
|
StatUserJob::dispatch($data, $server, $protocol, 'd');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user