优化实时流量统计性能

This commit is contained in:
root 2023-10-12 14:59:23 +09:00
parent 3f3de69389
commit 30e2f156a3
No known key found for this signature in database
GPG Key ID: 8827A30FF1DB1379
2 changed files with 40 additions and 31 deletions

View File

@ -9,13 +9,15 @@ use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
class StatUserJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $u;
protected $d;
protected $userId;
//protected $u;
//protected $d;
//protected $userId;
protected $data;
protected $server;
protected $protocol;
protected $recordType;
@ -28,12 +30,13 @@ class StatUserJob implements ShouldQueue
*
* @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->u = $u;
$this->d = $d;
$this->userId = $userId;
//$this->u = $u;
//$this->d = $d;
//$this->userId = $userId;
$this->data =$data;
$this->server = $server;
$this->protocol = $protocol;
$this->recordType = $recordType;
@ -50,31 +53,36 @@ class StatUserJob implements ShouldQueue
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, '用户统计数据更新失败');
try {
DB::beginTransaction();
foreach(array_keys($this->data) as $userId){
$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'])
]);
} else {
$insertData[] = [
'user_id' => $userId,
'server_rate' => $this->server['rate'],
'u' => $this->data[$userId][0],
'd' => $this->data[$userId][1],
'record_type' => $this->recordType,
'record_at' => $recordAt
];
}
}
} 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, '用户统计数据创建失败');
if (!empty($insertData)) {
StatUser::upsert($insertData, ['user_id', 'server_rate', 'record_at']);
}
DB::commit();
} catch (\Exception $e) {
DB::rollback();
abort(500, '用户统计数据失败'. $e->getMessage());
}
}
}

View File

@ -178,10 +178,11 @@ class UserService
$u = $data[$userId][0];
$d = $data[$userId][1];
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);
//$statService->statServer($server['id'], $protocol, $u, $d);
//$statService->statUser($server['rate'], $userId, $u, $d);
}
StatUserJob::dispatch($data, $server, $protocol, 'd');
}
}