Added a test for the controller and cleaned up the controller
This commit is contained in:
parent
7a81c61ad8
commit
60e1ffa564
9 changed files with 188 additions and 23 deletions
|
@ -21,6 +21,14 @@ interface NodeRepositoryInterface extends RepositoryInterface, SearchableInterfa
|
||||||
*/
|
*/
|
||||||
public function getUsageStats(Node $node): array;
|
public function getUsageStats(Node $node): array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the usage stats for a single node.
|
||||||
|
*
|
||||||
|
* @param \Pterodactyl\Models\Node $node
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getUsageStatsRaw(Node $node): array;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return all available nodes with a searchable interface.
|
* Return all available nodes with a searchable interface.
|
||||||
*
|
*
|
||||||
|
|
|
@ -145,4 +145,11 @@ interface ServerRepositoryInterface extends RepositoryInterface, SearchableInter
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function isUniqueUuidCombo(string $uuid, string $short): bool;
|
public function isUniqueUuidCombo(string $uuid, string $short): bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the amount of servers that are suspended
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getSuspendedServersCount(): int;
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,19 +11,16 @@ use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
|
||||||
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
||||||
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
||||||
use Pterodactyl\Http\Controllers\Controller;
|
use Pterodactyl\Http\Controllers\Controller;
|
||||||
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
|
use Pterodactyl\Traits\Controllers\PlainJavascriptInjection;
|
||||||
use Pterodactyl\Traits\Controllers\JavascriptStatisticsInjection;
|
|
||||||
|
|
||||||
class StatisticsController extends Controller
|
class StatisticsController extends Controller
|
||||||
{
|
{
|
||||||
use JavascriptStatisticsInjection;
|
use PlainJavascriptInjection;
|
||||||
|
|
||||||
private $allocationRepository;
|
private $allocationRepository;
|
||||||
|
|
||||||
private $databaseRepository;
|
private $databaseRepository;
|
||||||
|
|
||||||
private $keyProviderService;
|
|
||||||
|
|
||||||
private $eggRepository;
|
private $eggRepository;
|
||||||
|
|
||||||
private $nodeRepository;
|
private $nodeRepository;
|
||||||
|
@ -35,7 +32,6 @@ class StatisticsController extends Controller
|
||||||
function __construct(
|
function __construct(
|
||||||
AllocationRepositoryInterface $allocationRepository,
|
AllocationRepositoryInterface $allocationRepository,
|
||||||
DatabaseRepositoryInterface $databaseRepository,
|
DatabaseRepositoryInterface $databaseRepository,
|
||||||
DaemonKeyProviderService $keyProviderService,
|
|
||||||
EggRepositoryInterface $eggRepository,
|
EggRepositoryInterface $eggRepository,
|
||||||
NodeRepositoryInterface $nodeRepository,
|
NodeRepositoryInterface $nodeRepository,
|
||||||
ServerRepositoryInterface $serverRepository,
|
ServerRepositoryInterface $serverRepository,
|
||||||
|
@ -44,28 +40,33 @@ class StatisticsController extends Controller
|
||||||
{
|
{
|
||||||
$this->allocationRepository = $allocationRepository;
|
$this->allocationRepository = $allocationRepository;
|
||||||
$this->databaseRepository = $databaseRepository;
|
$this->databaseRepository = $databaseRepository;
|
||||||
$this->keyProviderService = $keyProviderService;
|
|
||||||
$this->eggRepository = $eggRepository;
|
$this->eggRepository = $eggRepository;
|
||||||
$this->nodeRepository = $nodeRepository;
|
$this->nodeRepository = $nodeRepository;
|
||||||
$this->serverRepository = $serverRepository;
|
$this->serverRepository = $serverRepository;
|
||||||
$this->userRepository = $userRepository;
|
$this->userRepository = $userRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function index(Request $request)
|
public function index()
|
||||||
{
|
{
|
||||||
$servers = $this->serverRepository->all();
|
$servers = $this->serverRepository->all();
|
||||||
$serversCount = count($servers);
|
|
||||||
$nodes = $this->nodeRepository->all();
|
$nodes = $this->nodeRepository->all();
|
||||||
$nodesCount = count($nodes);
|
|
||||||
$usersCount = $this->userRepository->count();
|
$usersCount = $this->userRepository->count();
|
||||||
$eggsCount = $this->eggRepository->count();
|
$eggsCount = $this->eggRepository->count();
|
||||||
$databasesCount = $this->databaseRepository->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 = $this->allocationRepository->count();
|
$totalAllocations = $this->allocationRepository->count();
|
||||||
$suspendedServersCount = $this->serverRepository->getBuilder()->where('suspended', true)->count();
|
$suspendedServersCount = $this->serverRepository->getSuspendedServersCount();
|
||||||
|
|
||||||
|
$totalServerRam = 0;
|
||||||
|
$totalNodeRam = 0;
|
||||||
|
$totalServerDisk = 0;
|
||||||
|
$totalNodeDisk = 0;
|
||||||
|
foreach ($nodes as $node) {
|
||||||
|
$stats = $this->nodeRepository->getUsageStatsRaw($node);
|
||||||
|
$totalServerRam += $stats['memory']['value'];
|
||||||
|
$totalNodeRam += $stats['memory']['max'];
|
||||||
|
$totalServerDisk += $stats['disk']['value'];
|
||||||
|
$totalNodeDisk += $stats['disk']['max'];
|
||||||
|
}
|
||||||
|
|
||||||
$tokens = [];
|
$tokens = [];
|
||||||
foreach ($nodes as $node) {
|
foreach ($nodes as $node) {
|
||||||
|
@ -74,7 +75,6 @@ class StatisticsController extends Controller
|
||||||
|
|
||||||
$this->injectJavascript([
|
$this->injectJavascript([
|
||||||
'servers' => $servers,
|
'servers' => $servers,
|
||||||
'serverCount' => $serversCount,
|
|
||||||
'suspendedServers' => $suspendedServersCount,
|
'suspendedServers' => $suspendedServersCount,
|
||||||
'totalServerRam' => $totalServerRam,
|
'totalServerRam' => $totalServerRam,
|
||||||
'totalNodeRam' => $totalNodeRam,
|
'totalNodeRam' => $totalNodeRam,
|
||||||
|
@ -85,8 +85,8 @@ class StatisticsController extends Controller
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return view('admin.statistics', [
|
return view('admin.statistics', [
|
||||||
'serversCount' => $serversCount,
|
'servers' => $servers,
|
||||||
'nodesCount' => $nodesCount,
|
'nodes' => $nodes,
|
||||||
'usersCount' => $usersCount,
|
'usersCount' => $usersCount,
|
||||||
'eggsCount' => $eggsCount,
|
'eggsCount' => $eggsCount,
|
||||||
'totalServerRam' => $totalServerRam,
|
'totalServerRam' => $totalServerRam,
|
||||||
|
|
|
@ -56,6 +56,33 @@ class NodeRepository extends EloquentRepository implements NodeRepositoryInterfa
|
||||||
})->toArray();
|
})->toArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the usage stats for a single node.
|
||||||
|
*
|
||||||
|
* @param \Pterodactyl\Models\Node $node
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getUsageStatsRaw(Node $node): array
|
||||||
|
{
|
||||||
|
$stats = $this->getBuilder()->select(
|
||||||
|
$this->getBuilder()->raw('IFNULL(SUM(servers.memory), 0) as sum_memory, IFNULL(SUM(servers.disk), 0) as sum_disk')
|
||||||
|
)->join('servers', 'servers.node_id', '=', 'nodes.id')->where('node_id', $node->id)->first();
|
||||||
|
|
||||||
|
return collect(['disk' => $stats->sum_disk, 'memory' => $stats->sum_memory])->mapWithKeys(function ($value, $key) use ($node) {
|
||||||
|
$maxUsage = $node->{$key};
|
||||||
|
if ($node->{$key . '_overallocate'} > 0) {
|
||||||
|
$maxUsage = $node->{$key} * (1 + ($node->{$key . '_overallocate'} / 100));
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
$key => [
|
||||||
|
'value' => $value,
|
||||||
|
'max' => $maxUsage,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
})->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return all available nodes with a searchable interface.
|
* Return all available nodes with a searchable interface.
|
||||||
*
|
*
|
||||||
|
|
|
@ -328,4 +328,14 @@ class ServerRepository extends EloquentRepository implements ServerRepositoryInt
|
||||||
$this->app->make(SubuserRepository::class)->getBuilder()->select('server_id')->where('user_id', $user)
|
$this->app->make(SubuserRepository::class)->getBuilder()->select('server_id')->where('user_id', $user)
|
||||||
)->pluck('id')->all();
|
)->pluck('id')->all();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the amount of servers that are suspended
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getSuspendedServersCount(): int
|
||||||
|
{
|
||||||
|
return $this->getBuilder()->where('suspended', true)->count();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ namespace Pterodactyl\Traits\Controllers;
|
||||||
|
|
||||||
use JavaScript;
|
use JavaScript;
|
||||||
|
|
||||||
trait JavascriptStatisticsInjection
|
trait PlainJavascriptInjection
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
|
@ -28,7 +28,7 @@ let ramChart = new Chart($('#ram_chart'), {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
var activeServers = Pterodactyl.serverCount - Pterodactyl.suspendedServers;
|
var activeServers = Pterodactyl.servers.length - Pterodactyl.suspendedServers;
|
||||||
let serversChart = new Chart($('#servers_chart'), {
|
let serversChart = new Chart($('#servers_chart'), {
|
||||||
type: 'pie',
|
type: 'pie',
|
||||||
data: {
|
data: {
|
||||||
|
|
|
@ -35,7 +35,7 @@
|
||||||
<span class="info-box-icon"><i class="fa fa-server"></i></span>
|
<span class="info-box-icon"><i class="fa fa-server"></i></span>
|
||||||
<div class="info-box-content number-info-box-content">
|
<div class="info-box-content number-info-box-content">
|
||||||
<span class="info-box-text">Servers</span>
|
<span class="info-box-text">Servers</span>
|
||||||
<span class="info-box-number">{{ $serversCount }}</span>
|
<span class="info-box-number">{{ count($servers) }}</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="info-box bg-blue">
|
<div class="info-box bg-blue">
|
||||||
|
@ -118,7 +118,7 @@
|
||||||
<span class="info-box-icon"><i class="fa fa-server"></i></span>
|
<span class="info-box-icon"><i class="fa fa-server"></i></span>
|
||||||
<div class="info-box-content number-info-box-content">
|
<div class="info-box-content number-info-box-content">
|
||||||
<span class="info-box-text">Total Nodes</span>
|
<span class="info-box-text">Total Nodes</span>
|
||||||
<span class="info-box-number">{{ $nodesCount }}</span>
|
<span class="info-box-number">{{ count($nodes) }}</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
113
tests/Unit/Http/Controllers/Admin/StatisticsControllerTest.php
Normal file
113
tests/Unit/Http/Controllers/Admin/StatisticsControllerTest.php
Normal file
|
@ -0,0 +1,113 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: Stan
|
||||||
|
* Date: 26-5-2018
|
||||||
|
* Time: 21:06
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Tests\Unit\Http\Controllers\Admin;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Routing\Controller;
|
||||||
|
use Mockery as m;
|
||||||
|
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\Admin\StatisticsController;
|
||||||
|
use Pterodactyl\Models\Node;
|
||||||
|
use Tests\Assertions\ControllerAssertionsTrait;
|
||||||
|
use Tests\Unit\Http\Controllers\ControllerTestCase;
|
||||||
|
|
||||||
|
class StatisticsControllerTest extends ControllerTestCase
|
||||||
|
{
|
||||||
|
use ControllerAssertionsTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface|\Mockery\Mock
|
||||||
|
*/
|
||||||
|
private $allocationRepository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface|\Mockery\Mock
|
||||||
|
*/
|
||||||
|
private $databaseRepository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock
|
||||||
|
*/
|
||||||
|
private $eggRepository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface|\Mockery\Mock
|
||||||
|
*/
|
||||||
|
private $nodeRepository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
|
||||||
|
*/
|
||||||
|
private $serverRepository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface|\Mockery\Mock
|
||||||
|
*/
|
||||||
|
private $userRepository;
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->allocationRepository = m::mock(AllocationRepositoryInterface::class);
|
||||||
|
$this->databaseRepository = m::mock(DatabaseRepositoryInterface::class);
|
||||||
|
$this->eggRepository = m::mock(EggRepositoryInterface::class);
|
||||||
|
$this->nodeRepository = m::mock(NodeRepositoryInterface::class);
|
||||||
|
$this->serverRepository = m::mock(ServerRepositoryInterface::class);
|
||||||
|
$this->userRepository = m::mock(UserRepositoryInterface::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testIndexController()
|
||||||
|
{
|
||||||
|
$controller = $this->getController();
|
||||||
|
|
||||||
|
$this->serverRepository->shouldReceive('all')->withNoArgs();
|
||||||
|
$this->nodeRepository->shouldReceive('all')->withNoArgs()->andReturn(collect([factory(Node::class)->make(), factory(Node::class)->make()]));
|
||||||
|
$this->userRepository->shouldReceive('count')->withNoArgs();
|
||||||
|
$this->eggRepository->shouldReceive('count')->withNoArgs();
|
||||||
|
$this->databaseRepository->shouldReceive('count')->withNoArgs();
|
||||||
|
$this->allocationRepository->shouldReceive('count')->withNoArgs();
|
||||||
|
$this->serverRepository->shouldReceive('getSuspendedServersCount')->withNoArgs();
|
||||||
|
|
||||||
|
$this->nodeRepository->shouldReceive('getUsageStatsRaw')->twice()->andReturn([
|
||||||
|
'memory' => [
|
||||||
|
'value' => 1024,
|
||||||
|
'max' => 512,
|
||||||
|
],
|
||||||
|
'disk' => [
|
||||||
|
'value' => 1024,
|
||||||
|
'max' => 512,
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
|
||||||
|
$controller->shouldReceive('injectJavascript')->once();
|
||||||
|
|
||||||
|
$response = $controller->index();
|
||||||
|
|
||||||
|
$this->assertIsViewResponse($response);
|
||||||
|
$this->assertViewNameEquals('admin.statistics', $response);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getController()
|
||||||
|
{
|
||||||
|
return $this->buildMockedController(StatisticsController::class, [$this->allocationRepository,
|
||||||
|
$this->databaseRepository,
|
||||||
|
$this->eggRepository,
|
||||||
|
$this->nodeRepository,
|
||||||
|
$this->serverRepository,
|
||||||
|
$this->userRepository]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue