Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
feat(User): Add User Active Seed/Leech Count
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhilip committed Mar 8, 2019
1 parent 94906eb commit e22b04c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 41 deletions.
5 changes: 4 additions & 1 deletion apps/views/layout/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@
<span>Ratio: <?= app()->user->getRatio() ?></span>&nbsp;
<span>Uploaded: <?= $this->e(app()->user->getUploaded(), 'format_bytes') ?></span>&nbsp;
<span>Downloaded: <?= $this->e(app()->user->getDownloaded(), 'format_bytes') ?></span>&nbsp;
<span>BT Activity: <span class="fas fa-arrow-up icon-seeding"></span>&nbsp;up&nbsp;<span class="fas fa-arrow-down icon-leeching"></span>&nbsp;dl&nbsp;</span>&nbsp;
<span>BT Activity:
<span class="fas fa-arrow-up icon-seeding"></span>&nbsp;<?= app()->user->getActiveSeed() ?>&nbsp;
<span class="fas fa-arrow-down icon-leeching"></span>&nbsp;<?= app()->user->getActiveLeech() ?>&nbsp;
</span>&nbsp;
</div>
<div class="pull-right">
<?php //TODO right information ?>
Expand Down
105 changes: 65 additions & 40 deletions framework/User/UserTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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');
}

/**
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -249,7 +251,6 @@ public function getRatio($real = false)

public function getSeedtime()
{
;
return $this->seedtime;
}

Expand All @@ -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;
}
}

0 comments on commit e22b04c

Please sign in to comment.