update: redesign yearly overview system

This commit is contained in:
Roardom
2024-01-11 05:34:08 +00:00
parent 12ac3a0270
commit 91aba1303e
13 changed files with 611 additions and 274 deletions

View File

@@ -13,13 +13,16 @@
namespace App\Http\Controllers;
use App\Models\Category;
use App\Models\Comment;
use App\Models\Group;
use App\Models\History;
use App\Models\Post;
use App\Models\Thank;
use App\Models\Torrent;
use App\Models\TorrentRequest;
use App\Models\User;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
class YearlyOverviewController extends Controller
@@ -47,189 +50,226 @@ class YearlyOverviewController extends Controller
public function show(int $year): \Illuminate\Contracts\View\View|\Illuminate\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\Foundation\Application
{
// Year Validation
$yearValid = checkdate(1, 1, $year) && \strlen((string) $year) <= 4 && date('Y', strtotime(config('other.birthdate'))) <= $year && $year <= date('Y');
$currentYear = now()->year;
$birthYear = Carbon::parse(config('other.birthdate'))->year;
if (!$yearValid) {
abort(404);
}
// Site Years
$siteYears = collect();
for ($currentYear = (int) date('Y', strtotime('-1 year')); $currentYear >= date('Y', strtotime(config('other.birthdate'))); $currentYear--) {
$siteYears->push($currentYear);
}
// Top 10 Best Downloaded Movie
$topMovies = Torrent::with(['category'])
->where('category_id', '=', 1)
->whereYear('created_at', '=', $year)
->latest('times_completed')
->take(10)
->get();
// Bottom 5 Worst Downloaded Movie
$bottomMovies = Torrent::with(['category'])
->where('category_id', '=', 1)
->whereYear('created_at', '=', $year)
->oldest('times_completed')
->take(5)
->get();
// Top 10 Best Downloaded TV
$topTv = Torrent::with(['category'])
->where('category_id', '=', 2)
->whereYear('created_at', '=', $year)
->latest('times_completed')
->distinct('tmdb')
->take(10)
->get();
// Top 10 Best Downloaded TV
$bottomTv = Torrent::with(['category'])
->where('category_id', '=', 2)
->whereYear('created_at', '=', $year)
->oldest('times_completed')
->distinct('tmdb')
->take(5)
->get();
// Top 10 Best Downloaded FANRES
$topFanres = Torrent::with(['category'])
->where('category_id', '=', 3)
->whereYear('created_at', '=', $year)
->latest('times_completed')
->distinct('tmdb')
->take(10)
->get();
// Top 10 Best Downloaded FANRES
$bottomFanres = Torrent::with(['category'])
->where('category_id', '=', 3)
->whereYear('created_at', '=', $year)
->oldest('times_completed')
->distinct('tmdb')
->take(5)
->get();
// Top 10 Uploaders By Content
$uploaders = Torrent::with('user')
->whereYear('created_at', '=', $year)
->select(DB::raw('user_id, count(*) as value'))
->groupBy('user_id')
->latest('value')
->take(10)
->get();
// Top 10 Requesters
$requesters = TorrentRequest::with('user')
->whereYear('created_at', '=', $year)
->where('user_id', '!=', 1)
->select(DB::raw('user_id, count(*) as value'))
->groupBy('user_id')
->latest('value')
->take(10)
->get();
// Top 10 Request Fillers
$fillers = TorrentRequest::with('filler')
->whereYear('created_at', '=', $year)
->where('filled_by', '!=', 1)
->select(DB::raw('filled_by, count(*) as value'))
->groupBy('filled_by')
->latest('value')
->take(10)
->get();
// Top 10 Commenters
$commenters = Comment::with('user')
->whereYear('created_at', '=', $year)
->where('user_id', '!=', 1)
->select(DB::raw('user_id, count(*) as value'))
->groupBy('user_id')
->latest('value')
->take(10)
->get();
// Top 10 Thankers
$thankers = Thank::with('user')
->whereYear('created_at', '=', $year)
->where('user_id', '!=', 1)
->select(DB::raw('user_id, count(*) as value'))
->groupBy('user_id')
->latest('value')
->take(10)
->get();
// Top 10 Forum Users By Posts
$forums = Post::with('user')
->whereYear('created_at', '=', $year)
->select(DB::raw('user_id, count(*) as value'))
->groupBy('user_id')
->latest('value')
->take(10)
->get();
// New Users
$newUsers = User::whereYear('created_at', '=', $year)
->count();
// Uploads By Category
$movieUploads = Torrent::where('category_id', '=', 1)
->whereYear('created_at', '=', $year)
->count();
$tvUploads = Torrent::where('category_id', '=', 2)
->whereYear('created_at', '=', $year)
->count();
$fanresUploads = Torrent::where('category_id', '=', 3)
->whereYear('created_at', '=', $year)
->count();
$trailerUploads = Torrent::where('category_id', '=', 5)
->whereYear('created_at', '=', $year)
->count();
// Total Uploads
$totalUploads = Torrent::whereYear('created_at', '=', $year)
->count();
// Total Downloads
$totalDownloads = Torrent::whereYear('created_at', '=', $year)
->sum('times_completed');
// Staff List
$staffers = Group::query()
->with('users.group')
->where('is_modo', '=', 1)
->orWhere('is_admin', '=', 1)
->get()
->sortByDesc('position');
abort_unless($birthYear <= $year && $year < $currentYear, 404);
return view('stats.yearly_overviews.show', [
'topMovies' => $topMovies,
'bottomMovies' => $bottomMovies,
'topTv' => $topTv,
'bottomTv' => $bottomTv,
'topFanres' => $topFanres,
'bottomFanres' => $bottomFanres,
'uploaders' => $uploaders,
'forums' => $forums,
'requesters' => $requesters,
'fillers' => $fillers,
'commenters' => $commenters,
'thankers' => $thankers,
'newUsers' => $newUsers,
'movieUploads' => $movieUploads,
'tvUploads' => $tvUploads,
'fanresUploads' => $fanresUploads,
'trailerUploads' => $trailerUploads,
'totalUploads' => $totalUploads,
'totalDownloads' => $totalDownloads,
'staffers' => $staffers,
'siteYears' => $siteYears,
'year' => $year,
'topMovies' => cache()->rememberForever(
'yearly-overview:'.$year.':top-movies',
fn () => Torrent::with('movie')
->select([
'tmdb',
DB::raw('COUNT(h.id) as download_count'),
DB::raw('MIN(category_id) as category_id'),
])
->leftJoinSub(
History::query()
->whereNotNull('completed_at')
->where('history.created_at', '>=', $year.'-01-01 00:00:00')
->where('history.created_at', '<=', $year.'-12-31 23:59:59'),
'h',
fn ($join) => $join->on('torrents.id', '=', 'h.torrent_id')
)
->where('tmdb', '!=', 0)
->whereIn('category_id', Category::select('id')->where('movie_meta', '=', true))
->groupBy('tmdb')
->orderByDesc('download_count')
->take(10)
->get()
),
'bottomMovies' => cache()->rememberForever(
'yearly-overview:'.$year.':bottom-movies',
fn () => Torrent::with('movie')
->select([
'tmdb',
DB::raw('COUNT(h.id) as download_count'),
DB::raw('MIN(category_id) as category_id'),
])
->leftJoinSub(
History::query()
->whereNotNull('completed_at')
->where('history.created_at', '>=', $year.'-01-01 00:00:00')
->where('history.created_at', '<=', $year.'-12-31 23:59:59'),
'h',
fn ($join) => $join->on('torrents.id', '=', 'h.torrent_id')
)
->where('tmdb', '!=', 0)
->whereIn('category_id', Category::select('id')->where('movie_meta', '=', true))
->groupBy('tmdb')
->orderBy('download_count')
->take(5)
->get()
),
'topTv' => cache()->rememberForever(
'yearly-overview:'.$year.':top-tv',
fn () => Torrent::with('tv')
->select([
'tmdb',
DB::raw('COUNT(h.id) as download_count'),
DB::raw('MIN(category_id) as category_id'),
])
->leftJoinSub(
History::query()
->whereNotNull('completed_at')
->where('history.created_at', '>=', $year.'-01-01 00:00:00')
->where('history.created_at', '<=', $year.'-12-31 23:59:59'),
'h',
fn ($join) => $join->on('torrents.id', '=', 'h.torrent_id')
)
->where('tmdb', '!=', 0)
->whereIn('category_id', Category::select('id')->where('tv_meta', '=', true))
->groupBy('tmdb')
->orderByDesc('download_count')
->take(10)
->get()
),
'bottomTv' => cache()->rememberForever(
'yearly-overview:'.$year.':bottom-tv',
fn () => Torrent::with('tv')
->select([
'tmdb',
DB::raw('COUNT(h.id) as download_count'),
DB::raw('MIN(category_id) as category_id'),
])
->leftJoinSub(
History::query()
->whereNotNull('completed_at')
->where('history.created_at', '>=', $year.'-01-01 00:00:00')
->where('history.created_at', '<=', $year.'-12-31 23:59:59'),
'h',
fn ($join) => $join->on('torrents.id', '=', 'h.torrent_id')
)
->where('tmdb', '!=', 0)
->whereIn('category_id', Category::select('id')->where('tv_meta', '=', true))
->groupBy('tmdb')
->orderBy('download_count')
->take(5)
->get()
),
'uploaders' => cache()->rememberForever(
'yearly-overview:'.$year.':uploaders',
fn () => Torrent::with('user.group')
->where('created_at', '>=', $year.'-01-01 00:00:00')
->where('created_at', '<=', $year.'-12-31 23:59:59')
->where('anon', '=', false)
->select(DB::raw('user_id, COUNT(*) as value'))
->groupBy('user_id')
->orderByRaw('COALESCE(value, 0) DESC')
->take(10)
->get()
),
'posters' => $posters = cache()->rememberForever(
'yearly-overview:'.$year.':posts',
fn () => Post::with('user.group')
->where('created_at', '>=', $year.'-01-01 00:00:00')
->where('created_at', '<=', $year.'-12-31 23:59:59')
->select(DB::raw('user_id, COUNT(*) as value'))
->groupBy('user_id')
->orderByRaw('COALESCE(value, 0) DESC')
->take(10)
->get()
),
'requesters' => cache()->rememberForever(
'yearly-overview:'.$year.':requesters',
fn () => TorrentRequest::with(['user.group'])
->where('created_at', '>=', $year.'-01-01 00:00:00')
->where('created_at', '<=', $year.'-12-31 23:59:59')
->where('user_id', '!=', 1)
->where('anon', '=', false)
->select(DB::raw('user_id, COUNT(*) as value'))
->groupBy('user_id')
->orderByRaw('COALESCE(value, 0) DESC')
->take(10)
->get()
),
'fillers' => cache()->rememberForever(
'yearly-overview:'.$year.':fillers',
fn () => TorrentRequest::with('filler.group')
->where('filled_when', '>=', $year.'-01-01 00:00:00')
->where('filled_when', '<=', $year.'-12-31 23:59:59')
->where('filled_by', '!=', 1)
->where('filled_anon', '=', false)
->select(DB::raw('filled_by, COUNT(*) as value'))
->groupBy('filled_by')
->orderByRaw('COALESCE(value, 0) DESC')
->take(10)
->get()
),
'commenters' => cache()->rememberForever(
'yearly-overview:'.$year.':commenters',
fn () => Comment::with('user.group')
->where('created_at', '>=', $year.'-01-01 00:00:00')
->where('created_at', '<=', $year.'-12-31 23:59:59')
->where('user_id', '!=', 1)
->where('anon', '=', false)
->select(DB::raw('user_id, COUNT(*) as value'))
->groupBy('user_id')
->orderByRaw('COALESCE(value, 0) DESC')
->take(10)
->get()
),
'thankers' => cache()->rememberForever(
'yearly-overview:'.$year.':thankers',
fn () => Thank::with('user.group')
->where('created_at', '>=', $year.'-01-01 00:00:00')
->where('created_at', '<=', $year.'-12-31 23:59:59')
->where('user_id', '!=', 1)
->select(DB::raw('user_id, COUNT(*) as value'))
->groupBy('user_id')
->orderByRaw('COALESCE(value, 0) DESC')
->take(10)
->get()
),
'newUsers' => cache()->rememberForever(
'yearly-overview:'.$year.':new-users',
fn () => User::query()
->where('created_at', '>=', $year.'-01-01 00:00:00')
->where('created_at', '<=', $year.'-12-31 23:59:59')
->count()
),
'movieUploads' => cache()->rememberForever(
'yearly-overview:'.$year.':movie-uploads',
fn () => Torrent::query()
->where('created_at', '>=', $year.'-01-01 00:00:00')
->where('created_at', '<=', $year.'-12-31 23:59:59')
->whereIn('category_id', Category::select('id')->where('movie_meta', '=', true))
->count()
),
'tvUploads' => cache()->rememberForever(
'yearly-overview:'.$year.':tv-uploads',
fn () => Torrent::query()
->where('created_at', '>=', $year.'-01-01 00:00:00')
->where('created_at', '<=', $year.'-12-31 23:59:59')
->whereIn('category_id', Category::select('id')->where('tv_meta', '=', true))
->count()
),
'totalUploads' => cache()->rememberForever(
'yearly-overview:'.$year.':total-uploads',
fn () => Torrent::query()
->where('created_at', '>=', $year.'-01-01 00:00:00')
->where('created_at', '<=', $year.'-12-31 23:59:59')
->count()
),
'totalDownloads' => cache()->rememberForever(
'yearly-overview:'.$year.':total-downloads',
fn () => History::query()
->where('created_at', '>=', $year.'-01-01 00:00:00')
->where('created_at', '<=', $year.'-12-31 23:59:59')
->count()
),
'staffers' => cache()->rememberForever(
'yearly-overview:'.$year.':staffers',
fn () => Group::query()
->with('users.group')
->where('is_modo', '=', 1)
->orWhere('is_admin', '=', 1)
->orderByDesc('position')
->get()
),
'birthYear' => $birthYear,
'year' => $year,
]);
}
}