From 86e70853960f89dd308aa475834deabcff44f661 Mon Sep 17 00:00:00 2001 From: stanjg Date: Sat, 26 May 2018 20:58:49 +0200 Subject: [PATCH] Cleaned up the controller and prepared for tests --- .../Repository/RepositoryInterface.php | 7 ++ .../Admin/StatisticsController.php | 65 +++++++++++++------ .../Eloquent/EloquentRepository.php | 10 +++ .../JavascriptStatisticsInjection.php | 24 +++++++ 4 files changed, 85 insertions(+), 21 deletions(-) create mode 100644 app/Traits/Controllers/JavascriptStatisticsInjection.php diff --git a/app/Contracts/Repository/RepositoryInterface.php b/app/Contracts/Repository/RepositoryInterface.php index 4a098c34f..3da9da009 100644 --- a/app/Contracts/Repository/RepositoryInterface.php +++ b/app/Contracts/Repository/RepositoryInterface.php @@ -200,4 +200,11 @@ interface RepositoryInterface * @return bool */ public function insertIgnore(array $values): bool; + + /** + * Get the amount of entries in the database + * + * @return int + */ + public function count(): int; } diff --git a/app/Http/Controllers/Admin/StatisticsController.php b/app/Http/Controllers/Admin/StatisticsController.php index aedca3d6b..984b6de22 100644 --- a/app/Http/Controllers/Admin/StatisticsController.php +++ b/app/Http/Controllers/Admin/StatisticsController.php @@ -5,52 +5,75 @@ namespace Pterodactyl\Http\Controllers\Admin; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use JavaScript; +use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface; +use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface; +use Pterodactyl\Contracts\Repository\EggRepositoryInterface; +use Pterodactyl\Contracts\Repository\NodeRepositoryInterface; +use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; +use Pterodactyl\Contracts\Repository\UserRepositoryInterface; use Pterodactyl\Http\Controllers\Controller; -use Pterodactyl\Models\Allocation; -use Pterodactyl\Models\Database; -use Pterodactyl\Models\Egg; -use Pterodactyl\Models\Node; -use Pterodactyl\Models\Server; -use Pterodactyl\Models\User; use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService; +use Pterodactyl\Traits\Controllers\JavascriptInjection; class StatisticsController extends Controller { + use JavascriptInjection; + + private $allocationRepository; + + private $databaseRepository; private $keyProviderService; - function __construct(DaemonKeyProviderService $keyProviderService) + private $eggRepository; + + private $nodeRepository; + + private $serverRepository; + + private $userRepository; + + function __construct( + AllocationRepositoryInterface $allocationRepository, + DatabaseRepositoryInterface $databaseRepository, + DaemonKeyProviderService $keyProviderService, + EggRepositoryInterface $eggRepository, + NodeRepositoryInterface $nodeRepository, + ServerRepositoryInterface $serverRepository, + UserRepositoryInterface $userRepository + ) { + $this->allocationRepository = $allocationRepository; + $this->databaseRepository = $databaseRepository; $this->keyProviderService = $keyProviderService; + $this->eggRepository = $eggRepository; + $this->nodeRepository = $nodeRepository; + $this->serverRepository = $serverRepository; + $this->userRepository = $userRepository; } public function index(Request $request) { - $servers = Server::all(); - $nodes = Node::all(); + $servers = $this->serverRepository->all(); $serversCount = count($servers); + $nodes = $this->nodeRepository->all(); $nodesCount = count($nodes); - $usersCount = User::count(); - $eggsCount = Egg::count(); - $databasesCount = Database::count(); + $usersCount = $this->userRepository->count(); + $eggsCount = $this->eggRepository->count(); + $databasesCount = $this->databaseRepository->count(); $totalServerRam = DB::table('servers')->sum('memory'); $totalNodeRam = DB::table('nodes')->sum('memory'); $totalServerDisk = DB::table('servers')->sum('disk'); $totalNodeDisk = DB::table('nodes')->sum('disk'); - $totalAllocations = Allocation::count(); - - $suspendedServersCount = Server::where('suspended', true)->count(); + $totalAllocations = $this->allocationRepository->count(); + $suspendedServersCount = $this->serverRepository->getBuilder()->where('suspended', true)->count(); $tokens = []; foreach ($nodes as $node) { - $server = Server::where('node_id', $node->id)->first(); - if ($server == null) - continue; - - $tokens[$node->id] = $this->keyProviderService->handle($server, $request->user()); + $tokens[$node->id] = $node->daemonSecret; } - Javascript::put([ + $this->injectJavascript([ 'servers' => $servers, 'serverCount' => $serversCount, 'suspendedServers' => $suspendedServersCount, diff --git a/app/Repositories/Eloquent/EloquentRepository.php b/app/Repositories/Eloquent/EloquentRepository.php index 74ec809fe..64e7cfb60 100644 --- a/app/Repositories/Eloquent/EloquentRepository.php +++ b/app/Repositories/Eloquent/EloquentRepository.php @@ -296,4 +296,14 @@ abstract class EloquentRepository extends Repository implements RepositoryInterf return $this->getBuilder()->getConnection()->statement($statement, $bindings); } + + /** + * Get the amount of entries in the database + * + * @return int + */ + public function count(): int + { + return $this->getBuilder()->count(); + } } diff --git a/app/Traits/Controllers/JavascriptStatisticsInjection.php b/app/Traits/Controllers/JavascriptStatisticsInjection.php new file mode 100644 index 000000000..779764233 --- /dev/null +++ b/app/Traits/Controllers/JavascriptStatisticsInjection.php @@ -0,0 +1,24 @@ +