Cleaned up the controller and prepared for tests
This commit is contained in:
parent
095d85bb60
commit
86e7085396
4 changed files with 85 additions and 21 deletions
|
@ -200,4 +200,11 @@ interface RepositoryInterface
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function insertIgnore(array $values): bool;
|
public function insertIgnore(array $values): bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the amount of entries in the database
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function count(): int;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,52 +5,75 @@ namespace Pterodactyl\Http\Controllers\Admin;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use JavaScript;
|
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\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\Services\DaemonKeys\DaemonKeyProviderService;
|
||||||
|
use Pterodactyl\Traits\Controllers\JavascriptInjection;
|
||||||
|
|
||||||
class StatisticsController extends Controller
|
class StatisticsController extends Controller
|
||||||
{
|
{
|
||||||
|
use JavascriptInjection;
|
||||||
|
|
||||||
|
private $allocationRepository;
|
||||||
|
|
||||||
|
private $databaseRepository;
|
||||||
|
|
||||||
private $keyProviderService;
|
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->keyProviderService = $keyProviderService;
|
||||||
|
$this->eggRepository = $eggRepository;
|
||||||
|
$this->nodeRepository = $nodeRepository;
|
||||||
|
$this->serverRepository = $serverRepository;
|
||||||
|
$this->userRepository = $userRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function index(Request $request)
|
public function index(Request $request)
|
||||||
{
|
{
|
||||||
$servers = Server::all();
|
$servers = $this->serverRepository->all();
|
||||||
$nodes = Node::all();
|
|
||||||
$serversCount = count($servers);
|
$serversCount = count($servers);
|
||||||
|
$nodes = $this->nodeRepository->all();
|
||||||
$nodesCount = count($nodes);
|
$nodesCount = count($nodes);
|
||||||
$usersCount = User::count();
|
$usersCount = $this->userRepository->count();
|
||||||
$eggsCount = Egg::count();
|
$eggsCount = $this->eggRepository->count();
|
||||||
$databasesCount = Database::count();
|
$databasesCount = $this->databaseRepository->count();
|
||||||
$totalServerRam = DB::table('servers')->sum('memory');
|
$totalServerRam = DB::table('servers')->sum('memory');
|
||||||
$totalNodeRam = DB::table('nodes')->sum('memory');
|
$totalNodeRam = DB::table('nodes')->sum('memory');
|
||||||
$totalServerDisk = DB::table('servers')->sum('disk');
|
$totalServerDisk = DB::table('servers')->sum('disk');
|
||||||
$totalNodeDisk = DB::table('nodes')->sum('disk');
|
$totalNodeDisk = DB::table('nodes')->sum('disk');
|
||||||
$totalAllocations = Allocation::count();
|
$totalAllocations = $this->allocationRepository->count();
|
||||||
|
$suspendedServersCount = $this->serverRepository->getBuilder()->where('suspended', true)->count();
|
||||||
$suspendedServersCount = Server::where('suspended', true)->count();
|
|
||||||
|
|
||||||
$tokens = [];
|
$tokens = [];
|
||||||
foreach ($nodes as $node) {
|
foreach ($nodes as $node) {
|
||||||
$server = Server::where('node_id', $node->id)->first();
|
$tokens[$node->id] = $node->daemonSecret;
|
||||||
if ($server == null)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
$tokens[$node->id] = $this->keyProviderService->handle($server, $request->user());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Javascript::put([
|
$this->injectJavascript([
|
||||||
'servers' => $servers,
|
'servers' => $servers,
|
||||||
'serverCount' => $serversCount,
|
'serverCount' => $serversCount,
|
||||||
'suspendedServers' => $suspendedServersCount,
|
'suspendedServers' => $suspendedServersCount,
|
||||||
|
|
|
@ -296,4 +296,14 @@ abstract class EloquentRepository extends Repository implements RepositoryInterf
|
||||||
|
|
||||||
return $this->getBuilder()->getConnection()->statement($statement, $bindings);
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
24
app/Traits/Controllers/JavascriptStatisticsInjection.php
Normal file
24
app/Traits/Controllers/JavascriptStatisticsInjection.php
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: Stan
|
||||||
|
* Date: 26-5-2018
|
||||||
|
* Time: 20:56
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Pterodactyl\Traits\Controllers;
|
||||||
|
|
||||||
|
use JavaScript;
|
||||||
|
|
||||||
|
class JavascriptStatisticsInjection
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Injects statistics into javascript
|
||||||
|
*/
|
||||||
|
public function injectJavascript($data)
|
||||||
|
{
|
||||||
|
Javascript::put($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue