Skip to content

(Add) Top 10 Torrents Feature #2057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions app/Http/Controllers/Top10Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author HDVinnie <[email protected]>
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
*/

namespace App\Http\Controllers;

class Top10Controller extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(): \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Contracts\Foundation\Application
{
return \view('top10.index');
}
}
99 changes: 99 additions & 0 deletions app/Http/Livewire/Top10.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author HDVinnie <[email protected]>
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
*/

namespace App\Http\Livewire;

use App\Models\Torrent;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Livewire\Component;

class Top10 extends Component
{
final public function getTorrentsDayProperty()
{
$matches = Cache::remember('top10DayMatches', 3_600, function () {
return DB::select('SELECT info_hash, count(*) FROM history WHERE completed_at >= DATE_ADD(CURRENT_TIMESTAMP, INTERVAL -1 DAY) AND deleted_at IS NULL GROUP BY info_hash ORDER BY count(*) DESC LIMIT 10');
});

return Cache::remember('top10DayTorrents', 3_600, function () use ($matches) {
return Torrent::with(['user:id,username,group_id', 'category', 'type', 'resolution'])
->whereIn('info_hash', collect($matches)->pluck('info_hash')->toArray())
->get();
});
}

final public function getTorrentsWeekProperty()
{
$matches = Cache::remember('top10WeekMatches', 3_600, function () {
return DB::select('SELECT info_hash, count(*) FROM history WHERE completed_at >= DATE_ADD(CURRENT_TIMESTAMP, INTERVAL -1 WEEK) AND deleted_at IS NULL GROUP BY info_hash ORDER BY count(*) DESC LIMIT 10');
});

return Cache::remember('top10WeekTorrents', 3_600, function () use ($matches) {
return Torrent::with(['user:id,username,group_id', 'category', 'type', 'resolution'])
->whereIn('info_hash', collect($matches)->pluck('info_hash')->toArray())
->get();
});
}

final public function getTorrentsMonthProperty()
{
$matches = Cache::remember('top10MonthMatches', 3_600, function () {
return DB::select('SELECT info_hash, count(*) FROM history WHERE completed_at >= DATE_ADD(CURRENT_TIMESTAMP, INTERVAL -1 MONTH) AND deleted_at IS NULL GROUP BY info_hash ORDER BY count(*) DESC LIMIT 10');
});

return Cache::remember('top10MonthTorrents', 3_600, function () use ($matches) {
return Torrent::with(['user:id,username,group_id', 'category', 'type', 'resolution'])
->whereIn('info_hash', collect($matches)->pluck('info_hash')->toArray())
->get();
});
}

final public function getTorrentsYearProperty()
{
$matches = Cache::remember('top10YearMatches', 3_600, function () {
return DB::select('SELECT info_hash, count(*) FROM history WHERE completed_at >= DATE_ADD(CURRENT_TIMESTAMP, INTERVAL -1 YEAR) AND deleted_at IS NULL GROUP BY info_hash ORDER BY count(*) DESC LIMIT 10');
});

return Cache::remember('top10YearTorrents', 3_600, function () use ($matches) {
return Torrent::with(['user:id,username,group_id', 'category', 'type', 'resolution'])
->whereIn('info_hash', collect($matches)->pluck('info_hash')->toArray())
->get();
});
}

final public function getTorrentsAllProperty()
{
$matches = Cache::remember('top10AllMatches', 3_600, function () {
return DB::select('SELECT info_hash, count(*) FROM history WHERE deleted_at IS NULL GROUP BY info_hash ORDER BY count(*) DESC LIMIT 10');
});

return Cache::remember('top10AllTorrents', 3_600, function () use ($matches) {
return Torrent::with(['user:id,username,group_id', 'category', 'type', 'resolution'])
->whereIn('info_hash', collect($matches)->pluck('info_hash')->toArray())
->get();
});
}

final public function render(): \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Contracts\Foundation\Application
{
return \view('livewire.top10', [
'user' => \auth()->user(),
'torrentsDay' => $this->torrentsDay,
'torrentsWeek' => $this->torrentsWeek,
'torrentsMonth' => $this->torrentsMonth,
'torrentsYear' => $this->torrentsYear,
'torrentsAll' => $this->torrentsAll,
]);
}
}
Loading