Merge pull request #1130 from stanjg/feature/stats-page
Added a statistics page to monitor the panel usage
This commit is contained in:
commit
fd8d7c3571
14 changed files with 576 additions and 0 deletions
|
@ -21,6 +21,14 @@ interface NodeRepositoryInterface extends RepositoryInterface, SearchableInterfa
|
|||
*/
|
||||
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.
|
||||
*
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -145,4 +145,11 @@ interface ServerRepositoryInterface extends RepositoryInterface, SearchableInter
|
|||
* @return bool
|
||||
*/
|
||||
public function isUniqueUuidCombo(string $uuid, string $short): bool;
|
||||
|
||||
/**
|
||||
* Get the amount of servers that are suspended
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getSuspendedServersCount(): int;
|
||||
}
|
||||
|
|
101
app/Http/Controllers/Admin/StatisticsController.php
Normal file
101
app/Http/Controllers/Admin/StatisticsController.php
Normal file
|
@ -0,0 +1,101 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Http\Controllers\Admin;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
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\Traits\Controllers\PlainJavascriptInjection;
|
||||
|
||||
class StatisticsController extends Controller
|
||||
{
|
||||
use PlainJavascriptInjection;
|
||||
|
||||
private $allocationRepository;
|
||||
|
||||
private $databaseRepository;
|
||||
|
||||
private $eggRepository;
|
||||
|
||||
private $nodeRepository;
|
||||
|
||||
private $serverRepository;
|
||||
|
||||
private $userRepository;
|
||||
|
||||
function __construct(
|
||||
AllocationRepositoryInterface $allocationRepository,
|
||||
DatabaseRepositoryInterface $databaseRepository,
|
||||
EggRepositoryInterface $eggRepository,
|
||||
NodeRepositoryInterface $nodeRepository,
|
||||
ServerRepositoryInterface $serverRepository,
|
||||
UserRepositoryInterface $userRepository
|
||||
)
|
||||
{
|
||||
$this->allocationRepository = $allocationRepository;
|
||||
$this->databaseRepository = $databaseRepository;
|
||||
$this->eggRepository = $eggRepository;
|
||||
$this->nodeRepository = $nodeRepository;
|
||||
$this->serverRepository = $serverRepository;
|
||||
$this->userRepository = $userRepository;
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
$servers = $this->serverRepository->all();
|
||||
$nodes = $this->nodeRepository->all();
|
||||
$usersCount = $this->userRepository->count();
|
||||
$eggsCount = $this->eggRepository->count();
|
||||
$databasesCount = $this->databaseRepository->count();
|
||||
$totalAllocations = $this->allocationRepository->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 = [];
|
||||
foreach ($nodes as $node) {
|
||||
$tokens[$node->id] = $node->daemonSecret;
|
||||
}
|
||||
|
||||
$this->injectJavascript([
|
||||
'servers' => $servers,
|
||||
'suspendedServers' => $suspendedServersCount,
|
||||
'totalServerRam' => $totalServerRam,
|
||||
'totalNodeRam' => $totalNodeRam,
|
||||
'totalServerDisk' => $totalServerDisk,
|
||||
'totalNodeDisk' => $totalNodeDisk,
|
||||
'nodes' => $nodes,
|
||||
'tokens' => $tokens,
|
||||
]);
|
||||
|
||||
return view('admin.statistics', [
|
||||
'servers' => $servers,
|
||||
'nodes' => $nodes,
|
||||
'usersCount' => $usersCount,
|
||||
'eggsCount' => $eggsCount,
|
||||
'totalServerRam' => $totalServerRam,
|
||||
'databasesCount' => $databasesCount,
|
||||
'totalNodeRam' => $totalNodeRam,
|
||||
'totalNodeDisk' => $totalNodeDisk,
|
||||
'totalServerDisk' => $totalServerDisk,
|
||||
'totalAllocations' => $totalAllocations,
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -56,6 +56,33 @@ class NodeRepository extends EloquentRepository implements NodeRepositoryInterfa
|
|||
})->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.
|
||||
*
|
||||
|
|
|
@ -328,4 +328,14 @@ class ServerRepository extends EloquentRepository implements ServerRepositoryInt
|
|||
$this->app->make(SubuserRepository::class)->getBuilder()->select('server_id')->where('user_id', $user)
|
||||
)->pluck('id')->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the amount of servers that are suspended
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getSuspendedServersCount(): int
|
||||
{
|
||||
return $this->getBuilder()->where('suspended', true)->count();
|
||||
}
|
||||
}
|
||||
|
|
24
app/Traits/Controllers/PlainJavascriptInjection.php
Normal file
24
app/Traits/Controllers/PlainJavascriptInjection.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;
|
||||
|
||||
trait PlainJavascriptInjection
|
||||
{
|
||||
|
||||
/**
|
||||
* Injects statistics into javascript
|
||||
*/
|
||||
public function injectJavascript($data)
|
||||
{
|
||||
Javascript::put($data);
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue