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

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