From e22b04cae935c043e7eff447faffb44d05e12736 Mon Sep 17 00:00:00 2001 From: Rhilip Date: Sat, 9 Mar 2019 00:44:11 +0800 Subject: [PATCH] feat(User): Add User Active Seed/Leech Count --- apps/views/layout/base.php | 5 +- framework/User/UserTrait.php | 105 ++++++++++++++++++++++------------- 2 files changed, 69 insertions(+), 41 deletions(-) diff --git a/apps/views/layout/base.php b/apps/views/layout/base.php index 2245bc6..ab91d46 100644 --- a/apps/views/layout/base.php +++ b/apps/views/layout/base.php @@ -68,7 +68,10 @@ Ratio: user->getRatio() ?>  Uploaded: e(app()->user->getUploaded(), 'format_bytes') ?>  Downloaded: e(app()->user->getDownloaded(), 'format_bytes') ?>  - BT Activity:  up  dl   + BT Activity: +  user->getActiveSeed() ?>  +  user->getActiveLeech() ?>  +  
diff --git a/framework/User/UserTrait.php b/framework/User/UserTrait.php index 4504b29..e99a646 100644 --- a/framework/User/UserTrait.php +++ b/framework/User/UserTrait.php @@ -15,46 +15,48 @@ trait UserTrait { use AttributesImportUtils; - public $id; - public $username; - public $email; - public $status; - public $class; - - public $passkey; - - public $avatar; - - public $create_at; - public $last_login_at; - public $last_access_at; - public $last_upload_at; - public $last_download_at; - public $last_connect_at; - - public $register_ip; - public $last_login_ip; - public $last_access_ip; - public $last_tracker_ip; - - public $uploaded; - public $downloaded; - public $seedtime; - public $leechtime; - - public $infoSaveKeyPrefix = 'USER:content_'; - + private $id; + private $username; + private $email; + private $status; + private $class; + + private $passkey; + + private $avatar; + + private $create_at; + private $last_login_at; + private $last_access_at; + private $last_upload_at; + private $last_download_at; + private $last_connect_at; + + private $register_ip; + private $last_login_ip; + private $last_access_ip; + private $last_tracker_ip; + + private $uploaded; + private $downloaded; + private $seedtime; + private $leechtime; + + protected $infoCacheKey; + public function loadUserContentById($id) { - $self = app()->redis->hGetAll($this->infoSaveKeyPrefix . $id); + $this->infoCacheKey = 'USER:id_' . $id . '_content'; + $self = app()->redis->hGetAll($this->infoCacheKey); if (empty($self)) { $self = app()->pdo->createCommand("SELECT * FROM `users` WHERE id = :id;")->bindParams([ "id" => $id ])->queryOne(); - app()->redis->hMset($this->infoSaveKeyPrefix . $id, $self); - app()->redis->expire($this->infoSaveKeyPrefix . $id, 3 * 60); + app()->redis->hMset($this->infoCacheKey, $self); + app()->redis->expire($this->infoCacheKey, 3 * 60); } $this->importAttributes($self); + } public function loadUserContentByName($name) @@ -66,10 +68,10 @@ public function loadUserContentByName($name) ])->queryScalar(); app()->redis->hSet('USER:map_name_to_id', $name, $uid); } - if (!$uid) { - throw new NotFoundException(''); + if ($uid) { + $this->loadTorrentContentById($uid); } - // TODO + throw new NotFoundException('This user is not found'); } /** @@ -209,12 +211,12 @@ public function getLastTrackerIp($readable = true) public function getUploaded($real = false) { if ($real) { - $upload = app()->redis->hGet($this->infoSaveKeyPrefix . $this->id, 'true_uploaded'); + $upload = app()->redis->hGet($this->infoCacheKey, 'true_uploaded'); if (false === $upload) { $upload = app()->pdo->createCommand('SELECT SUM(`true_uploaded`) FROM `snatched` WHERE `user_id` = :uid')->bindParams([ "uid" => $this->id ])->queryScalar() ?? 0; - app()->redis->hSet($this->infoSaveKeyPrefix . $this->id, 'true_uploaded', $upload); + app()->redis->hSet($this->infoCacheKey, 'true_uploaded', $upload); } return $upload; } @@ -228,12 +230,12 @@ public function getUploaded($real = false) public function getDownloaded($real = false) { if ($real) { - $download = app()->redis->hGet($this->infoSaveKeyPrefix . $this->id, 'true_downloaded'); + $download = app()->redis->hGet($this->infoCacheKey, 'true_downloaded'); if (false === $download) { $download = app()->pdo->createCommand('SELECT SUM(`true_downloaded`) FROM `snatched` WHERE `user_id` = :uid')->bindParams([ "uid" => $this->id ])->queryScalar() ?? 0; - app()->redis->hSet($this->infoSaveKeyPrefix . $this->id, 'true_downloaded', $download); + app()->redis->hSet($this->infoCacheKey, 'true_downloaded', $download); } return $download; } @@ -249,7 +251,6 @@ public function getRatio($real = false) public function getSeedtime() { - ; return $this->seedtime; } @@ -262,4 +263,28 @@ public function getTimeRatio() { return max(1, $this->seedtime) / max(1, $this->leechtime); } + + public function getActiveSeed() + { + $active_seed = app()->redis->hGet($this->infoCacheKey, 'active_seed_count'); + if ($active_seed === false) { + $active_seed = app()->pdo->createCommand("SELECT COUNT(id) FROM `peers` WHERE `user_id` = :uid AND `seeder`='yes'")->bindParams([ + 'uid' => $this->id + ])->queryScalar() ?: 0; + app()->redis->hSet($this->infoCacheKey, 'active_seed_count', $active_seed); + } + return $active_seed; + } + + public function getActiveLeech() + { + $active_leech = app()->redis->hGet($this->infoCacheKey, 'active_leech_count'); + if ($active_leech === false) { + $active_leech = app()->pdo->createCommand("SELECT COUNT(id) FROM `peers` WHERE `user_id` = :uid AND `seeder`='no'")->bindParams([ + 'uid' => $this->id + ])->queryScalar() ?: 0; + app()->redis->hSet($this->infoCacheKey, 'active_leech_count', $active_leech); + } + return $active_leech; + } }