mirror of
https://github.com/NNTmux/newznab-tmux.git
synced 2026-08-29 01:08:56 +00:00
Move stat functions to SiteStat model
This commit is contained in:
@@ -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);
|
||||
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Spatie\Permission\Models\Role;
|
||||
|
||||
class SiteStat extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $guarded = [];
|
||||
|
||||
/**
|
||||
* @return Collection|\Illuminate\Support\Collection|static[]
|
||||
*/
|
||||
public static function getTopGrabbers()
|
||||
{
|
||||
$expiresAt = now()->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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user