diff --git a/app/Http/Controllers/Admin/AdminSiteController.php b/app/Http/Controllers/Admin/AdminSiteController.php index dbbff770e..8717ef7f0 100644 --- a/app/Http/Controllers/Admin/AdminSiteController.php +++ b/app/Http/Controllers/Admin/AdminSiteController.php @@ -6,6 +6,7 @@ use App\Http\Controllers\BasePageController; use App\Models\Category; use App\Models\Release; use App\Models\Settings; +use App\Models\SiteStat; use App\Models\User; use Blacklight\utility\Utility; use Illuminate\Http\Request; @@ -184,23 +185,20 @@ class AdminSiteController extends BasePageController $meta_title = $title = 'Site Stats'; - $topgrabs = User::getTopGrabbers(); - $this->smarty->assign('topgrabs', $topgrabs); + $topGrabs = SiteStat::getTopGrabbers(); + $this->smarty->assign('topgrabs', $topGrabs); - $topdownloads = Release::getTopDownloads(); - $this->smarty->assign('topdownloads', $topdownloads); + $topDownloads = SiteStat::getTopDownloads(); + $this->smarty->assign('topdownloads', $topDownloads); - $topcomments = Release::getTopComments(); - $this->smarty->assign('topcomments', $topcomments); - - $recent = Category::getRecentlyAdded(); + $recent = SiteStat::getRecentlyAdded(); $this->smarty->assign('recent', $recent); - $usersbymonth = User::getUsersByMonth(); - $this->smarty->assign('usersbymonth', $usersbymonth); + $usersByMonth = SiteStat::getUsersByMonth(); + $this->smarty->assign('usersbymonth', $usersByMonth); - $usersbyrole = Role::query()->select(['name'])->withCount('users')->groupBy('name')->having('users_count', '>', 0)->orderByDesc('users_count')->get(); - $this->smarty->assign('usersbyrole', $usersbyrole); + $usersByRole = SiteStat::usersByRole(); + $this->smarty->assign('usersbyrole', $usersByRole); $this->smarty->assign('totusers', 0); $this->smarty->assign('totrusers', 0); diff --git a/app/Models/Category.php b/app/Models/Category.php index c0320ec6a..ddb3549fa 100644 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -277,30 +277,7 @@ class Category extends Model return $this->belongsTo(RootCategory::class, 'root_categories_id'); } - /** - * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|static[] - */ - public static function getRecentlyAdded() - { - $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long')); - $result = Cache::get(md5('RecentlyAdded')); - if ($result !== null) { - return $result; - } - $result = self::query() - ->with('parent') - ->where('r.adddate', '>', now()->subWeek()) - ->select(['root_categories_id', DB::raw('COUNT(r.id) as count'), 'title']) - ->join('releases as r', 'r.categories_id', '=', 'categories.id') - ->groupBy('title') - ->orderByDesc('count') - ->get(); - - Cache::put(md5('RecentlyAdded'), $result, $expiresAt); - - return $result; - } public static function getCategorySearch(array $cat = []): string { diff --git a/app/Models/Release.php b/app/Models/Release.php index 4e224f221..655a966a1 100644 --- a/app/Models/Release.php +++ b/app/Models/Release.php @@ -333,56 +333,6 @@ class Release extends Model return self::whereAnidbid($anidbID)->update(['anidbid' => -1]); } - /** - * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|static[] - */ - public static function getTopDownloads() - { - $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long')); - $releases = Cache::get(md5('topDownloads')); - if ($releases !== null) { - return $releases; - } - $releases = self::query() - ->where('grabs', '>', 0) - ->select(['id', 'searchname', 'guid', 'adddate']) - ->selectRaw('SUM(grabs) as grabs') - ->groupBy('id', 'searchname', 'adddate') - ->havingRaw('SUM(grabs) > 0') - ->orderByDesc('grabs') - ->limit(10) - ->get(); - - Cache::put(md5('topDownloads'), $releases, $expiresAt); - - return $releases; - } - - /** - * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|static[] - */ - public static function getTopComments() - { - $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long')); - $releases = Cache::get(md5('topComments')); - if ($releases !== null) { - return $releases; - } - $releases = self::query() - ->where('comments', '>', 0) - ->select(['id', 'guid', 'searchname']) - ->selectRaw('SUM(comments) AS comments') - ->groupBy('id', 'searchname', 'adddate') - ->havingRaw('SUM(comments) > 0') - ->orderByDesc('comments') - ->limit(10) - ->get(); - - Cache::put(md5('topComments'), $releases, $expiresAt); - - return $releases; - } - /** * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Query\Builder[]|\Illuminate\Support\Collection|mixed */ diff --git a/app/Models/SiteStat.php b/app/Models/SiteStat.php new file mode 100644 index 000000000..ffbca038d --- /dev/null +++ b/app/Models/SiteStat.php @@ -0,0 +1,112 @@ +addMinutes(config('nntmux.cache_expiry_long')); + $result = Cache::get(md5('topGrabbers')); + if ($result !== null) { + return $result; + } + $result = User::query()->selectRaw('id, username, SUM(grabs) as grabs')->groupBy('id', 'username')->having('grabs', '>', 0)->orderByDesc('grabs')->limit(10)->get(); + Cache::put(md5('topGrabbers'), $result, $expiresAt); + + return $result; + } + + /** + * @return Collection|\Illuminate\Support\Collection|static[] + */ + public static function getUsersByMonth() + { + $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long')); + $result = Cache::get(md5('usersByMonth')); + if ($result !== null) { + return $result; + } + $result = User::query()->whereNotNull('created_at')->where('created_at', '<>', '0000-00-00 00:00:00')->selectRaw("DATE_FORMAT(created_at, '%M %Y') as mth, COUNT(id) as num")->groupBy(['mth'])->orderByDesc('created_at')->get(); + + Cache::put(md5('usersByMonth'), $result, $expiresAt); + + return $result; + } + + /** + * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|static[] + */ + public static function getTopDownloads() + { + $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long')); + $result = Cache::get(md5('topDownloads')); + if ($result !== null) { + return $result; + } + $result = Release::query() + ->where('grabs', '>', 0) + ->select(['id', 'searchname', 'guid', 'adddate']) + ->selectRaw('SUM(grabs) as grabs') + ->groupBy('id', 'searchname', 'adddate') + ->havingRaw('SUM(grabs) > 0') + ->orderByDesc('grabs') + ->limit(10) + ->get(); + + Cache::put(md5('topDownloads'), $result, $expiresAt); + + return $result; + } + + /** + * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|static[] + */ + public static function getRecentlyAdded() + { + $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long')); + $result = Cache::get(md5('RecentlyAdded')); + if ($result !== null) { + return $result; + } + + $result = Category::query()->with('parent')->where('r.adddate', '>', now()->subWeek())->select([ + 'root_categories_id', DB::raw('COUNT(r.id) as count'), 'title' + ])->join('releases as r', 'r.categories_id', '=', + 'categories.id')->groupBy('title')->orderByDesc('count')->get(); + + Cache::put(md5('RecentlyAdded'), $result, $expiresAt); + + return $result; + } + + public static function usersByRole() + { + $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long')); + $result = Cache::get(md5('usersByRole')); + if ($result !== null) { + return $result; + } + + $result = Role::query()->select(['name'])->withCount('users')->groupBy('name')->having('users_count', '>', 0)->orderByDesc('users_count')->get(); + + Cache::put(md5('usersByRole'), $result, $expiresAt); + + return $result; + } + +} diff --git a/app/Models/User.php b/app/Models/User.php index 8e0cbba2b..ae8bdaa18 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -664,21 +664,6 @@ class User extends Authenticatable return self::getCategoryExclusionById($user->id); } - /** - * @return Collection|\Illuminate\Support\Collection|static[] - */ - public static function getTopGrabbers() - { - return self::query()->selectRaw('id, username, SUM(grabs) as grabs')->groupBy('id', 'username')->having('grabs', '>', 0)->orderByDesc('grabs')->limit(10)->get(); - } - - /** - * @return Collection|\Illuminate\Support\Collection|static[] - */ - public static function getUsersByMonth() - { - return self::query()->whereNotNull('created_at')->where('created_at', '<>', '0000-00-00 00:00:00')->selectRaw("DATE_FORMAT(created_at, '%M %Y') as mth, COUNT(id) as num")->groupBy(['mth'])->orderByDesc('created_at')->get(); - } /** * @throws \Exception