From f0ced9cdf9cd14d0cf3ed71b2666a462bada8a2a Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Thu, 10 Mar 2022 21:45:23 +0800 Subject: [PATCH] fix getusertorrentlistajax hr --- .env.example | 2 +- app/Console/Commands/AttendanceCleanup.php | 2 +- app/Console/Commands/AttendanceMigrate.php | 49 ++++++++++++++++++++++ app/Console/Commands/Test.php | 4 +- app/Repositories/AttendanceRepository.php | 48 +++++++++++++++++++++ nexus/Install/Update.php | 35 ++-------------- nexus/Install/install/install.php | 1 + nexus/Install/update/update.php | 1 + public/getusertorrentlistajax.php | 10 ++--- 9 files changed, 113 insertions(+), 39 deletions(-) create mode 100644 app/Console/Commands/AttendanceMigrate.php diff --git a/.env.example b/.env.example index d37e0183..e4a23bc7 100644 --- a/.env.example +++ b/.env.example @@ -50,7 +50,7 @@ REDIS_PASSWORD= REDIS_PORT= REDIS_DB= -USE_CRON_TRIGGER_CLEANUP=false +USE_CRON_TRIGGER_CLEANUP=true LOG_FILE=/tmp/nexus.log diff --git a/app/Console/Commands/AttendanceCleanup.php b/app/Console/Commands/AttendanceCleanup.php index c11a9f7b..0b800863 100644 --- a/app/Console/Commands/AttendanceCleanup.php +++ b/app/Console/Commands/AttendanceCleanup.php @@ -40,7 +40,7 @@ class AttendanceCleanup extends Command { $query = Attendance::query()->groupBy('uid')->selectRaw('uid, max(id) as max_id'); $page = 1; - $size = 1000; + $size = 10000; while (true) { $rows = $query->forPage($page, $size)->get(); $log = "sql: " . last_query() . ", count: " . $rows->count(); diff --git a/app/Console/Commands/AttendanceMigrate.php b/app/Console/Commands/AttendanceMigrate.php new file mode 100644 index 00000000..d3e22f29 --- /dev/null +++ b/app/Console/Commands/AttendanceMigrate.php @@ -0,0 +1,49 @@ +migrateAttendance(); + $log = sprintf('[%s], %s, result: %s, query: %s', REQUEST_ID, __METHOD__, var_export($result, true), last_query()); + $this->info($log); + do_log($log); + return 0; + } +} diff --git a/app/Console/Commands/Test.php b/app/Console/Commands/Test.php index 15a45733..55c5a74d 100644 --- a/app/Console/Commands/Test.php +++ b/app/Console/Commands/Test.php @@ -63,7 +63,9 @@ class Test extends Command */ public function handle() { - + $rep = new AttendanceRepository(); + $r = $rep->migrateAttendance(); + dd($r); } } diff --git a/app/Repositories/AttendanceRepository.php b/app/Repositories/AttendanceRepository.php index 6abac622..7c75acc6 100644 --- a/app/Repositories/AttendanceRepository.php +++ b/app/Repositories/AttendanceRepository.php @@ -5,6 +5,7 @@ use App\Models\Attendance; use App\Models\Setting; use App\Models\User; use Carbon\Carbon; +use Nexus\Database\NexusDB; class AttendanceRepository extends BaseRepository { @@ -90,4 +91,51 @@ class AttendanceRepository extends BaseRepository } return $points; } + + public function migrateAttendance(): int + { + $page = 1; + $size = 10000; + $caseWhens = []; + $idArr = []; + while (true) { + $logPrefix = "[MIGRATE_ATTENDANCE], page: $page, size: $size"; + $result = Attendance::query() + ->groupBy(['uid']) + ->selectRaw('uid, max(id) as id, count(*) as counts') + ->forPage($page, $size) + ->get(); + do_log("$logPrefix, " . last_query() . ", count: " . $result->count()); + if ($result->isEmpty()) { + do_log("$logPrefix, no more data..."); + break; + } + foreach ($result as $row) { + //use case when instead. + $caseWhens[] = sprintf('when %s then %s', $row->id, $row->counts); + $idArr[] = $row->id; +// $update = [ +// 'total_days' => $row->counts, +// ]; +// $updateResult = $row->update($update); + do_log(sprintf( + "$logPrefix, update user: %s(ID: %s) => %s", + $row->uid, $row->id, $row->counts + )); + } + $page++; + } + if (empty($caseWhens)) { + do_log("no data to update..."); + return 0; + } + $caseWhenStr = sprintf('case id %s end', implode(' ', $caseWhens)); + $result = Attendance::query() + ->whereIn('id', $idArr) + ->update(['total_days' => NexusDB::raw($caseWhenStr)]); + + do_log("[MIGRATE_ATTENDANCE] DONE! $caseWhenStr, result: " . var_export($result, true)); + + return count($idArr); + } } diff --git a/nexus/Install/Update.php b/nexus/Install/Update.php index f1735771..9c180ef3 100644 --- a/nexus/Install/Update.php +++ b/nexus/Install/Update.php @@ -14,6 +14,7 @@ use App\Models\Tag; use App\Models\Torrent; use App\Models\TorrentTag; use App\Models\User; +use App\Repositories\AttendanceRepository; use App\Repositories\BonusRepository; use App\Repositories\ExamRepository; use App\Repositories\TagRepository; @@ -140,7 +141,9 @@ class Update extends Install } if (!NexusDB::schema()->hasColumn('attendance', 'total_days')) { $this->runMigrate('database/migrations/2021_06_13_215440_add_total_days_to_attendance_table.php'); - $this->migrateAttendance(); + $attendanceRep = new AttendanceRepository(); + $count = $attendanceRep->migrateAttendance(); + $this->doLog("[MIGRATE_ATTENDANCE] $count"); } } @@ -203,36 +206,6 @@ class Update extends Install } - private function migrateAttendance() - { - $page = 1; - $size = 1000; - while (true) { - $logPrefix = "[MIGRATE_ATTENDANCE], page: $page, size: $size"; - $result = Attendance::query() - ->groupBy(['uid']) - ->selectRaw('uid, max(id) as id, count(*) as counts') - ->forPage($page, $size) - ->get(); - $this->doLog("$logPrefix, " . last_query() . ", count: " . $result->count()); - if ($result->isEmpty()) { - $this->doLog("$logPrefix, no more data..."); - break; - } - foreach ($result as $row) { - $update = [ - 'total_days' => $row->counts, - ]; - $updateResult = $row->update($update); - $this->doLog(sprintf( - "$logPrefix, update user: %s(ID: %s) => %s, result: %s", - $row->uid, $row->id, json_encode($update), var_export($updateResult, true) - )); - } - $page++; - } - - } public function listVersions() { diff --git a/nexus/Install/install/install.php b/nexus/Install/install/install.php index e8dc9333..0487bfa0 100644 --- a/nexus/Install/install/install.php +++ b/nexus/Install/install/install.php @@ -104,6 +104,7 @@ if ($currentStep == 4) { $tableRows = $settingTableRows['table_rows']; $pass = $settingTableRows['pass']; while ($isPost) { + set_time_limit(300); try { $install->createSymbolicLinks($symbolicLinks); $install->runDatabaseSeeder(); diff --git a/nexus/Install/update/update.php b/nexus/Install/update/update.php index 36e20a2d..b7d0b432 100644 --- a/nexus/Install/update/update.php +++ b/nexus/Install/update/update.php @@ -157,6 +157,7 @@ if ($currentStep == 4) { $tableRows = $settingTableRows['table_rows']; $pass = $settingTableRows['pass']; while ($isPost) { + set_time_limit(300); try { // $update->updateDependencies(); $update->createSymbolicLinks($symbolicLinks); diff --git a/public/getusertorrentlistajax.php b/public/getusertorrentlistajax.php index 70d5ee1e..ba9a7813 100644 --- a/public/getusertorrentlistajax.php +++ b/public/getusertorrentlistajax.php @@ -183,7 +183,7 @@ switch ($type) { case 'uploaded': { - $res = sql_query("SELECT torrents.id AS torrent, torrents.name as torrentname, small_descr, seeders, leechers, anonymous, categories.name AS catname, categories.image, category, sp_state, size, hr, snatched.seedtime, snatched.uploaded FROM torrents LEFT JOIN snatched ON torrents.id = snatched.torrentid LEFT JOIN categories ON torrents.category = categories.id WHERE torrents.owner=$id AND snatched.userid=$id " . (($CURUSER["id"] != $id)?((get_user_class() < $viewanonymous_class) ? " AND anonymous = 'no'":""):"") ." ORDER BY torrents.added DESC") or sqlerr(__FILE__, __LINE__); + $res = sql_query("SELECT torrents.id AS torrent, torrents.name as torrentname, small_descr, seeders, leechers, anonymous, categories.name AS catname, categories.image, category, sp_state, size, torrents.hr, snatched.seedtime, snatched.uploaded FROM torrents LEFT JOIN snatched ON torrents.id = snatched.torrentid LEFT JOIN categories ON torrents.category = categories.id WHERE torrents.owner=$id AND snatched.userid=$id " . (($CURUSER["id"] != $id)?((get_user_class() < $viewanonymous_class) ? " AND anonymous = 'no'":""):"") ." ORDER BY torrents.added DESC") or sqlerr(__FILE__, __LINE__); $count = mysql_num_rows($res); if ($count > 0) { @@ -195,7 +195,7 @@ switch ($type) // Current Seeding case 'seeding': { - $res = sql_query("SELECT torrent,added,snatched.uploaded,snatched.downloaded,torrents.name as torrentname, torrents.small_descr, torrents.sp_state, categories.name as catname,size,hr,image,category,seeders,leechers FROM peers LEFT JOIN torrents ON peers.torrent = torrents.id LEFT JOIN categories ON torrents.category = categories.id LEFT JOIN snatched ON torrents.id = snatched.torrentid WHERE peers.userid=$id AND snatched.userid = $id AND peers.seeder='yes' ORDER BY torrents.added DESC") or sqlerr(); + $res = sql_query("SELECT torrent,added,snatched.uploaded,snatched.downloaded,torrents.name as torrentname, torrents.small_descr, torrents.sp_state, categories.name as catname,size,torrents.hr,image,category,seeders,leechers FROM peers LEFT JOIN torrents ON peers.torrent = torrents.id LEFT JOIN categories ON torrents.category = categories.id LEFT JOIN snatched ON torrents.id = snatched.torrentid WHERE peers.userid=$id AND snatched.userid = $id AND peers.seeder='yes' ORDER BY torrents.added DESC") or sqlerr(); $count = mysql_num_rows($res); if ($count > 0){ list($torrentlist, $total_size) = maketable ( $res, 'seeding' ); @@ -206,7 +206,7 @@ switch ($type) // Current Leeching case 'leeching': { - $res = sql_query("SELECT torrent,snatched.uploaded,snatched.downloaded,torrents.name as torrentname, torrents.small_descr, torrents.sp_state, categories.name as catname,size,hr,image,category,seeders,leechers FROM peers LEFT JOIN torrents ON peers.torrent = torrents.id LEFT JOIN categories ON torrents.category = categories.id LEFT JOIN snatched ON torrents.id = snatched.torrentid WHERE peers.userid=$id AND snatched.userid = $id AND peers.seeder='no' ORDER BY torrents.added DESC") or sqlerr(); + $res = sql_query("SELECT torrent,snatched.uploaded,snatched.downloaded,torrents.name as torrentname, torrents.small_descr, torrents.sp_state, categories.name as catname,size,torrents.hr,image,category,seeders,leechers FROM peers LEFT JOIN torrents ON peers.torrent = torrents.id LEFT JOIN categories ON torrents.category = categories.id LEFT JOIN snatched ON torrents.id = snatched.torrentid WHERE peers.userid=$id AND snatched.userid = $id AND peers.seeder='no' ORDER BY torrents.added DESC") or sqlerr(); $count = mysql_num_rows($res); if ($count > 0){ list($torrentlist, $total_size) = maketable ( $res, 'leeching' ); @@ -217,7 +217,7 @@ switch ($type) // Completed torrents case 'completed': { - $res = sql_query("SELECT torrents.id AS torrent, torrents.name AS torrentname, small_descr, categories.name AS catname, categories.image, category, sp_state, size, hr,snatched.uploaded, snatched.seedtime, snatched.leechtime, snatched.completedat FROM torrents LEFT JOIN snatched ON torrents.id = snatched.torrentid LEFT JOIN categories on torrents.category = categories.id WHERE snatched.finished='yes' AND torrents.owner != $id AND userid=$id ORDER BY snatched.completedat DESC") or sqlerr(); + $res = sql_query("SELECT torrents.id AS torrent, torrents.name AS torrentname, small_descr, categories.name AS catname, categories.image, category, sp_state, size, torrents.hr,snatched.uploaded, snatched.seedtime, snatched.leechtime, snatched.completedat FROM torrents LEFT JOIN snatched ON torrents.id = snatched.torrentid LEFT JOIN categories on torrents.category = categories.id WHERE snatched.finished='yes' AND torrents.owner != $id AND userid=$id ORDER BY snatched.completedat DESC") or sqlerr(); $count = mysql_num_rows($res); if ($count > 0) { @@ -229,7 +229,7 @@ switch ($type) // Incomplete torrents case 'incomplete': { - $res = sql_query("SELECT torrents.id AS torrent, torrents.name AS torrentname, small_descr, categories.name AS catname, categories.image, category, sp_state, size, hr,snatched.uploaded, snatched.downloaded, snatched.leechtime FROM torrents LEFT JOIN snatched ON torrents.id = snatched.torrentid LEFT JOIN categories on torrents.category = categories.id WHERE snatched.finished='no' AND userid=$id AND torrents.owner != $id ORDER BY snatched.startdat DESC") or sqlerr(); + $res = sql_query("SELECT torrents.id AS torrent, torrents.name AS torrentname, small_descr, categories.name AS catname, categories.image, category, sp_state, size, torrents.hr,snatched.uploaded, snatched.downloaded, snatched.leechtime FROM torrents LEFT JOIN snatched ON torrents.id = snatched.torrentid LEFT JOIN categories on torrents.category = categories.id WHERE snatched.finished='no' AND userid=$id AND torrents.owner != $id ORDER BY snatched.startdat DESC") or sqlerr(); $count = mysql_num_rows($res); if ($count > 0) {