Skip to content

Commit

Permalink
Merge pull request #1376 from HDInnovations/Refactor-Count-Queries
Browse files Browse the repository at this point in the history
(Refactor) Count and Sum Queries
  • Loading branch information
HDVinnie authored Jun 4, 2020
2 parents 2be2650 + d3bc402 commit c22701b
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 58 deletions.
28 changes: 15 additions & 13 deletions app/Http/Controllers/RequestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use App\Repositories\RequestFacetedRepository;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use MarcReichel\IGDBLaravel\Models\Game;

Expand Down Expand Up @@ -63,7 +64,7 @@ public function __construct(RequestFacetedRepository $faceted, ChatRepository $c
}

/**
* Displays Torrent List View.
* Displays Requests List View.
*
* @param \Illuminate\Http\Request $request
*
Expand All @@ -72,12 +73,17 @@ public function __construct(RequestFacetedRepository $faceted, ChatRepository $c
public function requests(Request $request)
{
$user = $request->user();
$num_req = TorrentRequest::count();
$num_fil = TorrentRequest::whereNotNull('filled_by')->count();
$num_unfil = TorrentRequest::whereNull('filled_by')->count();
$total_bounty = TorrentRequest::all()->sum('bounty');
$claimed_bounty = TorrentRequest::whereNotNull('filled_by')->sum('bounty');
$unclaimed_bounty = TorrentRequest::whereNull('filled_by')->sum('bounty');

$requests = DB::table('requests')
->selectRaw('count(*) as total')
->selectRaw('count(case when filled_by != null then 1 end) as filled')
->selectRaw('count(case when filled_by = null then 1 end) as unfilled')
->first();
$bounties = DB::table('requests')
->selectRaw('coalesce(sum(bounty), 0) as total')
->selectRaw('coalesce(sum(case when filled_by != null then 1 end), 0) as claimed')
->selectRaw('coalesce(sum(case when filled_by = null then 1 end), 0) as unclaimed')
->first();

$torrentRequests = TorrentRequest::with(['user', 'category', 'type'])->paginate(25);
$repository = $this->faceted;
Expand All @@ -86,12 +92,8 @@ public function requests(Request $request)
'torrentRequests' => $torrentRequests,
'repository' => $repository,
'user' => $user,
'num_req' => $num_req,
'num_fil' => $num_fil,
'num_unfil' => $num_unfil,
'total_bounty' => $total_bounty,
'claimed_bounty' => $claimed_bounty,
'unclaimed_bounty' => $unclaimed_bounty,
'requests' => $requests,
'bounties' => $bounties,
]);
}

Expand Down
57 changes: 29 additions & 28 deletions app/Http/Controllers/Staff/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@

use App\Helpers\SystemInformation;
use App\Http\Controllers\Controller;
use App\Models\Application;
use App\Models\Group;
use App\Models\Peer;
use App\Models\Report;
use App\Models\Torrent;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Spatie\SslCertificate\SslCertificate;

class HomeController extends Controller
Expand All @@ -40,22 +36,36 @@ public function index(Request $request)
// User Info
$banned_group = cache()->rememberForever('banned_group', fn () => Group::where('slug', '=', 'banned')->pluck('id'));
$validating_group = cache()->rememberForever('validating_group', fn () => Group::where('slug', '=', 'validating')->pluck('id'));
$num_user = User::count();
$banned = User::where('group_id', '=', $banned_group[0])->count();
$validating = User::where('group_id', '=', $validating_group[0])->count();
$users = DB::table('users')
->selectRaw('count(*) as total')
->selectRaw("count(case when group_id = $banned_group[0] then 1 end) as banned")
->selectRaw("count(case when group_id = $validating_group[0] then 1 end) as validating")
->first();

// Torrent Info
$num_torrent = Torrent::count();
$pending = Torrent::pending()->count();
$rejected = Torrent::rejected()->count();
$torrents = DB::table('torrents')
->selectRaw('count(*) as total')
->selectRaw('count(case when status = 0 then 1 end) as pending')
->selectRaw('count(case when status = 2 then 1 end) as rejected')
->selectRaw('count(case when status = 3 then 1 end) as postponed')
->first();

// Peers Info
$peers = Peer::count();
$seeders = Peer::where('seeder', '=', 1)->count();
$leechers = Peer::where('seeder', '=', 0)->count();
$peers = DB::table('peers')
->selectRaw('count(*) as total')
->selectRaw('count(case when seeder = 0 then 1 end) as leechers')
->selectRaw('count(case when seeder = 1 then 1 end) as seeders')
->first();

// Reports Info
$reports_count = Report::where('solved', '=', 0)->count();
$reports = DB::table('reports')
->selectRaw('count(case when solved = 0 then 1 end) as unsolved')
->first();

// Pending Applications Count
$apps = DB::table('applications')
->selectRaw('count(case when status = 0 then 1 end) as pending')
->first();

// SSL Info
try {
Expand All @@ -75,28 +85,19 @@ public function index(Request $request)
// Directory Permissions
$file_permissions = $sys->directoryPermissions();

// Pending Applications Count
$app_count = Application::pending()->count();

return view('Staff.dashboard.index', [
'num_user' => $num_user,
'banned' => $banned,
'validating' => $validating,
'num_torrent' => $num_torrent,
'pending' => $pending,
'rejected' => $rejected,
'users' => $users,
'torrents' => $torrents,
'peers' => $peers,
'seeders' => $seeders,
'leechers' => $leechers,
'reports_count' => $reports_count,
'reports' => $reports,
'apps' => $apps,
'certificate' => $certificate,
'uptime' => $uptime,
'ram' => $ram,
'disk' => $disk,
'avg' => $avg,
'basic' => $basic,
'file_permissions' => $file_permissions,
'app_count' => $app_count,
]);
}
}
18 changes: 9 additions & 9 deletions resources/views/Staff/dashboard/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,32 +88,32 @@
<div class="col-xs-6 col-sm-4 col-md-4">
<div class="text-center black-item">
<h1 style=" color: #ffffff;">Torrents</h1>
<span class="badge-user">Total: {{ $num_torrent }}</span>
<span class="badge-user">Total: {{ $torrents->total }}</span>
<br>
<span class="badge-user">Pending: {{ $pending }}</span>
<span class="badge-user">Rejected: {{ $rejected }}</span>
<span class="badge-user">Pending: {{ $torrents->pending }}</span>
<span class="badge-user">Rejected: {{ $torrents->rejected }}</span>
<i class="fal fa-magnet black-icon text-green"></i>
</div>
</div>

<div class="col-xs-6 col-sm-4 col-md-4">
<div class="text-center black-item">
<h1 style=" color: #ffffff;">Peers</h1>
<span class="badge-user">Total: {{ $peers }}</span>
<span class="badge-user">Total: {{ $peers->total }}</span>
<br>
<span class="badge-user">Seeders: {{ $seeders }}</span>
<span class="badge-user">Leechers: {{ $leechers }}</span>
<span class="badge-user">Seeders: {{ $peers->seeders }}</span>
<span class="badge-user">Leechers: {{ $peers->leechers }}</span>
<i class="fal fa-wifi black-icon text-green"></i>
</div>
</div>

<div class="col-xs-6 col-sm-4 col-md-4">
<div class="text-center black-item">
<h1 style=" color: #ffffff;">Users</h1>
<span class="badge-user">Total: {{ $num_user }}</span>
<span class="badge-user">Total: {{ $users->total }}</span>
<br>
<span class="badge-user">Validating: {{ $validating }}</span>
<span class="badge-user">Banned: {{ $banned }}</span>
<span class="badge-user">Validating: {{ $users->validating }}</span>
<span class="badge-user">Banned: {{ $users->banned }}</span>
<i class="fal fa-users black-icon text-green"></i>
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions resources/views/partials/dashboardmenu.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<li>
<a href="{{ route('staff.applications.index') }}">
<i class="{{ config('other.font-awesome') }} fa-list"></i> @lang('staff.applications')
<span class="badge badge-danger"> {{ $app_count }} </span>
<span class="badge badge-danger"> {{ $apps->pending }} </span>
</a>
</li>
@if (auth()->user()->group->is_admin)
Expand Down Expand Up @@ -201,7 +201,7 @@
<li>
<a href="{{ route('staff.reports.index') }}">
<i class="{{ config('other.font-awesome') }} fa-file"></i> @lang('staff.reports-log')
<span class="badge badge-danger"> {{ $reports_count }} </span>
<span class="badge badge-danger"> {{ $reports->unsolved }} </span>
</a>
</li>
<li>
Expand Down
12 changes: 6 additions & 6 deletions resources/views/requests/requests.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,12 @@ class="form-horizontal form-condensed form-torrent-search form-bordered">
<div class="container-fluid">
<div class="block">
<span class="badge-user" style="float: right;">
<strong>@lang('request.requests'):</strong> {{ $num_req }} |
<strong>@lang('request.filled'):</strong> {{ $num_fil }} |
<strong>@lang('request.unfilled'):</strong> {{ $num_unfil }} |
<strong>@lang('request.total-bounty'):</strong> {{ $total_bounty }} @lang('bon.bon') |
<strong>@lang('request.bounty-claimed'):</strong> {{ $claimed_bounty }} @lang('bon.bon') |
<strong>@lang('request.bounty-unclaimed'):</strong> {{ $unclaimed_bounty }} @lang('bon.bon')
<strong>@lang('request.requests'):</strong> {{ $requests->total }} |
<strong>@lang('request.filled'):</strong> {{ $requests->filled }} |
<strong>@lang('request.unfilled'):</strong> {{ $requests->unfilled }} |
<strong>@lang('request.total-bounty'):</strong> {{ $bounties->total }} @lang('bon.bon') |
<strong>@lang('request.bounty-claimed'):</strong> {{ $bounties->claimed }} @lang('bon.bon') |
<strong>@lang('request.bounty-unclaimed'):</strong> {{ $bounties->unclaimed }} @lang('bon.bon')
</span>
<a href="{{ route('add_request') }}" role="button" data-toggle="tooltip"
data-original-title="@lang('request.add-request')!" class="btn btn btn-success">
Expand Down

0 comments on commit c22701b

Please sign in to comment.