优化实时流量统计性能

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\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') {
// //
} }
try {
$data = StatUser::where('record_at', $recordAt) DB::beginTransaction();
->where('server_rate', $this->server['rate']) foreach(array_keys($this->data) as $userId){
->where('user_id', $this->userId) $userdata = StatUser::where('record_at', $recordAt)
->first(); ->where('server_rate', $this->server['rate'])
if ($data) { ->where('user_id', $userId)
try { ->lockForUpdate()->first();
$data->update([ if ($userdata) {
'u' => $data['u'] + ($this->u * $this->server['rate']), $userdata->update([
'd' => $data['d'] + ($this->d * $this->server['rate']) '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 {
$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 (!empty($insertData)) {
if (!StatUser::create([ StatUser::upsert($insertData, ['user_id', 'server_rate', 'record_at']);
'user_id' => $this->userId,
'server_rate' => $this->server['rate'],
'u' => $this->u,
'd' => $this->d,
'record_type' => $this->recordType,
'record_at' => $recordAt
])) {
abort(500, '用户统计数据创建失败');
} }
DB::commit();
} catch (\Exception $e) {
DB::rollback();
abort(500, '用户统计数据失败'. $e->getMessage());
} }
} }
} }

View File

@ -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');
} }
} }