exam add index SEED_POINTS + UPLOAD_TORRENT_COUNT

This commit is contained in:
xiaomlove
2023-11-08 03:17:23 +08:00
parent a85c2d3658
commit d9c449d00a
8 changed files with 52 additions and 17 deletions

View File

@@ -36,12 +36,16 @@ class Exam extends NexusModel
const INDEX_SEED_TIME_AVERAGE = 2;
const INDEX_DOWNLOADED = 3;
const INDEX_SEED_BONUS = 4;
const INDEX_SEED_POINTS = 5;
const INDEX_UPLOAD_TORRENT_COUNT = 6;
public static $indexes = [
public static array $indexes = [
self::INDEX_UPLOADED => ['name' => 'Uploaded', 'unit' => 'GB', 'source_user_field' => 'uploaded'],
self::INDEX_DOWNLOADED => ['name' => 'Downloaded', 'unit' => 'GB', 'source_user_field' => 'downloaded'],
self::INDEX_SEED_TIME_AVERAGE => ['name' => 'Seed time average', 'unit' => 'Hour', 'source_user_field' => 'seedtime'],
self::INDEX_SEED_BONUS => ['name' => 'Bonus', 'unit' => '', 'source_user_field' => 'seedbonus'],
self::INDEX_SEED_POINTS => ['name' => 'Seed points', 'unit' => '', 'source_user_field' => ''],
self::INDEX_UPLOAD_TORRENT_COUNT => ['name' => 'Upload torrent', 'unit' => '', 'source_user_field' => ''],
];
const FILTER_USER_CLASS = 'classes';

View File

@@ -488,6 +488,11 @@ class Torrent extends NexusModel
$query->where('visible', $visible);
}
public function scopeNormal($query)
{
$query->where('visible', self::VISIBLE_YES)->where('banned', self::BANNED_NO);
}
public function torrent_tags(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(TorrentTag::class, 'torrent_id');

View File

@@ -223,7 +223,7 @@ class User extends Authenticatable implements FilamentUser, HasName
'uploaded', 'downloaded', 'seedbonus', 'seedtime', 'leechtime',
'invited_by', 'enabled', 'seed_points', 'last_access', 'invites',
'lang', 'attendance_card', 'privacy', 'noad', 'downloadpos', 'donoruntil', 'donor',
'seedbonus', 'bonuscomment', 'downloadpos', 'vip_added', 'vip_until', 'title', 'invites', 'attendance_card',
'bonuscomment', 'downloadpos', 'vip_added', 'vip_until', 'title', 'invites', 'attendance_card',
'seed_points_per_hour'
];

View File

@@ -455,9 +455,9 @@ class ExamRepository extends BaseRepository
*
* @param $examUser
* @param null $user
* @return bool
* @return ExamUser|bool
*/
public function updateProgress($examUser, $user = null)
public function updateProgress($examUser, $user = null): ExamUser|bool
{
$beginTimestamp = microtime(true);
if (!$examUser instanceof ExamUser) {
@@ -490,7 +490,7 @@ class ExamRepository extends BaseRepository
}
$exam = $examUser->exam;
if (!$user instanceof User) {
$user = $examUser->user()->select(['id', 'uploaded', 'downloaded', 'seedtime', 'leechtime', 'seedbonus'])->first();
$user = $examUser->user()->select(['id', 'uploaded', 'downloaded', 'seedtime', 'leechtime', 'seedbonus', 'seed_points'])->first();
}
$attributes = [
'exam_user_id' => $examUser->id,
@@ -522,7 +522,7 @@ class ExamRepository extends BaseRepository
$attributes['index'] = $index['index'];
$attributes['created_at'] = $now;
$attributes['updated_at'] = $now;
$attributes['value'] = $user->{Exam::$indexes[$index['index']]['source_user_field']} ?? 0;
$attributes['value'] = $this->getProgressValue($user, $index['index']);
do_log("[GET_TOTAL_VALUE]: " . $attributes['value']);
$newVersionProgress = ExamProgress::query()
->where('exam_user_id', $examUser->id)
@@ -541,12 +541,8 @@ class ExamRepository extends BaseRepository
}
$attributes['init_value'] = $newVersionProgress->init_value;
} else {
//do insert. check the init value
$progressData = $this->calculateProgress($examUser, true);
$increment = $progressData[$index['index']] ?? 0;
$initValue = $attributes['value'] - $increment;
$attributes['init_value'] = max($initValue, 0);
do_log("total: {$attributes['value']}, increment: $increment, init_value: $initValue, final init_value: {$attributes['init_value']}");
//do insert.
$attributes['init_value'] = $attributes['value'];
$attributes['torrent_id'] = -1;
ExamProgress::query()->insert($attributes);
do_log("newVersionProgress [NOT EXISTS], doInsert with: " . json_encode($attributes));
@@ -600,6 +596,29 @@ class ExamRepository extends BaseRepository
return $examUser;
}
private function getProgressValue(User $user, int $index)
{
if ($index == Exam::INDEX_UPLOADED) {
return $user->uploaded;
}
if ($index == Exam::INDEX_DOWNLOADED) {
return $user->downloaded;
}
if ($index == Exam::INDEX_SEED_BONUS) {
return $user->seedbonus;
}
if ($index == Exam::INDEX_SEED_TIME_AVERAGE) {
return $user->seedtime;
}
if ($index == Exam::INDEX_SEED_POINTS) {
return $user->seed_points;
}
if ($index == Exam::INDEX_UPLOAD_TORRENT_COUNT) {
return Torrent::query()->where("owner", $user->id)->normal()->count();
}
throw new \InvalidArgumentException("Invalid index: $index");
}
/**
* get user exam status
@@ -645,11 +664,12 @@ class ExamRepository extends BaseRepository
}
/**
* @deprecated
* @param ExamUser $examUser
* @param false $allSum
* @param bool $allSum
* @return array|null
*/
public function calculateProgress(ExamUser $examUser, $allSum = false)
public function calculateProgress(ExamUser $examUser, bool $allSum = false)
{
$logPrefix = "examUser: " . $examUser->id;
$begin = $examUser->begin;
@@ -685,7 +705,7 @@ class ExamRepository extends BaseRepository
->first()
->torrent_count;
$progressSum[$index] = intval($progressSum[$index] / $torrentCount);
$logPrefix .= ", get torrent count: $torrentCount, from query: " . last_query();
$logPrefix .= ", index: INDEX_SEED_TIME_AVERAGE, get torrent count: $torrentCount, from query: " . last_query();
}
do_log("$logPrefix, final progressSum: " . json_encode($progressSum));
@@ -1050,7 +1070,7 @@ class ExamRepository extends BaseRepository
foreach ($rows as $row) {
$result = $this->updateProgress($row);
do_log("$logPrefix, examUser: " . $row->toJson() . ", result type: " . gettype($result));
if ($result != false) {
if ($result) {
$success += 1;
}
}

View File

@@ -1,6 +1,6 @@
<?php
defined('VERSION_NUMBER') || define('VERSION_NUMBER', '1.8.9');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2023-10-12');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2023-11-08');
defined('IN_TRACKER') || define('IN_TRACKER', false);
defined('PROJECTNAME') || define("PROJECTNAME","NexusPHP");
defined('NEXUSPHPURL') || define("NEXUSPHPURL","https://nexusphp.org");

View File

@@ -9,6 +9,8 @@ return [
'index_text_' . \App\Models\Exam::INDEX_SEED_TIME_AVERAGE => 'Seed time average',
'index_text_' . \App\Models\Exam::INDEX_DOWNLOADED => 'Download increment',
'index_text_' . \App\Models\Exam::INDEX_SEED_BONUS => 'Bonus increment',
'index_text_' . \App\Models\Exam::INDEX_SEED_POINTS => 'Seed points increment',
'index_text_' . \App\Models\Exam::INDEX_UPLOAD_TORRENT_COUNT => 'Upload torrent increment',
'filters' => [
\App\Models\Exam::FILTER_USER_CLASS => 'User class',
\App\Models\Exam::FILTER_USER_REGISTER_TIME_RANGE => 'Register time range',

View File

@@ -9,6 +9,8 @@ return [
'index_text_' . \App\Models\Exam::INDEX_SEED_TIME_AVERAGE => '平均做种时间',
'index_text_' . \App\Models\Exam::INDEX_DOWNLOADED => '下载增量',
'index_text_' . \App\Models\Exam::INDEX_SEED_BONUS => '魔力增量',
'index_text_' . \App\Models\Exam::INDEX_SEED_POINTS => '做种积分增量',
'index_text_' . \App\Models\Exam::INDEX_UPLOAD_TORRENT_COUNT => '发种增量',
'filters' => [
\App\Models\Exam::FILTER_USER_CLASS => '用户等级',
\App\Models\Exam::FILTER_USER_REGISTER_TIME_RANGE => '注册时间范围',

View File

@@ -9,6 +9,8 @@ return [
'index_text_' . \App\Models\Exam::INDEX_SEED_TIME_AVERAGE => '平均做種時間',
'index_text_' . \App\Models\Exam::INDEX_DOWNLOADED => '下載增量',
'index_text_' . \App\Models\Exam::INDEX_SEED_BONUS => '魔力增量',
'index_text_' . \App\Models\Exam::INDEX_SEED_POINTS => '做種積分增量',
'index_text_' . \App\Models\Exam::INDEX_UPLOAD_TORRENT_COUNT => '發種增量',
'filters' => [
\App\Models\Exam::FILTER_USER_CLASS => '用戶等級',
\App\Models\Exam::FILTER_USER_REGISTER_TIME_RANGE => '註冊時間範圍',