Merge pull request #1130 from stanjg/feature/stats-page

Added a statistics page to monitor the panel usage
This commit is contained in:
Dane Everitt 2018-05-31 22:56:58 -07:00 committed by GitHub
commit fd8d7c3571
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 576 additions and 0 deletions

View file

@ -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.
*

View file

@ -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;
}

View file

@ -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;
}

View 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,
]);
}
}

View file

@ -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();
}
}

View file

@ -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.
*

View file

@ -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();
}
}

View 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);
}
}

View file

@ -473,3 +473,7 @@ label.control-label > span.field-optional:before {
height: 42px;
width: auto;
}
.number-info-box-content {
padding: 15px 10px 0;
}

View file

@ -0,0 +1,118 @@
var freeDisk = Pterodactyl.totalNodeDisk - Pterodactyl.totalServerDisk;
let diskChart = new Chart($('#disk_chart'), {
type: 'pie',
data: {
labels: ['Free Disk', 'Used Disk'],
datasets: [
{
label: 'Disk (in MB)',
backgroundColor: ['#51B060', '#ff0000'],
data: [freeDisk, Pterodactyl.totalServerDisk]
}
]
}
});
var freeRam = Pterodactyl.totalNodeRam - Pterodactyl.totalServerRam;
let ramChart = new Chart($('#ram_chart'), {
type: 'pie',
data: {
labels: ['Free RAM', 'Used RAM'],
datasets: [
{
label: 'Memory (in MB)',
backgroundColor: ['#51B060', '#ff0000'],
data: [freeRam, Pterodactyl.totalServerRam]
}
]
}
});
var activeServers = Pterodactyl.servers.length - Pterodactyl.suspendedServers;
let serversChart = new Chart($('#servers_chart'), {
type: 'pie',
data: {
labels: ['Active', 'Suspended'],
datasets: [
{
label: 'Servers',
backgroundColor: ['#51B060', '#E08E0B'],
data: [activeServers, Pterodactyl.suspendedServers]
}
]
}
});
let statusChart = new Chart($('#status_chart'), {
type: 'pie',
data: {
labels: ['Online', 'Offline', 'Installing', 'Error'],
datasets: [
{
label: '',
backgroundColor: ['#51B060', '#b7b7b7', '#E08E0B', '#ff0000'],
data: [0,0,0,0]
}
]
}
});
var servers = Pterodactyl.servers;
var nodes = Pterodactyl.nodes;
for (let i = 0; i < servers.length; i++) {
setTimeout(getStatus, 200 * i, servers[i]);
}
function getStatus(server) {
var uuid = server.uuid;
var node = getNodeByID(server.node_id);
$.ajax({
type: 'GET',
url: node.scheme + '://' + node.fqdn + ':'+node.daemonListen+'/v1/server',
timeout: 5000,
headers: {
'X-Access-Server': uuid,
'X-Access-Token': Pterodactyl.tokens[node.id],
}
}).done(function (data) {
if (typeof data.status === 'undefined') {
// Error
statusChart.data.datasets[0].data[3]++;
return;
}
switch (data.status) {
case 0:
case 3:
case 30:
// Offline
statusChart.data.datasets[0].data[1]++;
break;
case 1:
case 2:
// Online
statusChart.data.datasets[0].data[0]++;
break;
case 20:
// Installing
statusChart.data.datasets[0].data[2]++;
break;
}
statusChart.update();
}).fail(function (jqXHR) {
// Error
statusChart.data.datasets[0].data[3]++;
statusChart.update();
});
}
function getNodeByID(id) {
for (var i = 0; i < nodes.length; i++) {
if (nodes[i].id === id) {
return nodes[i];
}
}
}

View file

@ -0,0 +1,141 @@
@extends('layouts.admin')
@include('partials/admin.settings.nav', ['activeTab' => 'basic'])
@section('title')
Statistics Overview
@endsection
@section('content-header')
<h1>Statistics Overview<small>Monitor your panel usage.</small></h1>
<ol class="breadcrumb">
<li><a href="{{ route('admin.index') }}">Admin</a></li>
<li class="active">Statistics</li>
</ol>
@endsection
@section('content')
<div class="row">
<div class="col-xs-12 col-md-8">
<div class="box box-primary">
<div class="box-header with-border">
Servers
</div>
<div class="box-body">
<div class="col-xs-12 col-md-6">
<canvas id="servers_chart" width="100%" height="50"></canvas>
</div>
<div class="col-xs-12 col-md-6">
<canvas id="status_chart" width="100%" height="50"></canvas>
</div>
</div>
</div>
</div>
<div class="col-xs-12 col-md-4">
<div class="info-box bg-blue">
<span class="info-box-icon"><i class="fa fa-server"></i></span>
<div class="info-box-content number-info-box-content">
<span class="info-box-text">Servers</span>
<span class="info-box-number">{{ count($servers) }}</span>
</div>
</div>
<div class="info-box bg-blue">
<span class="info-box-icon"><i class="ion ion-ios-barcode-outline"></i></span>
<div class="info-box-content number-info-box-content">
<span class="info-box-text">Total used Memory (in MB)</span>
<span class="info-box-number">{{ $totalServerRam }}MB</span>
</div>
</div>
<div class="info-box bg-blue">
<span class="info-box-icon"><i class="ion ion-stats-bars"></i></span>
<div class="info-box-content number-info-box-content">
<span class="info-box-text">Total used Disk (in MB)</span>
<span class="info-box-number">{{ $totalServerDisk }}MB</span>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-md-8">
<div class="box box-primary">
<div class="box-header with-border">
Nodes
</div>
<div class="box-body">
<div class="col-xs-12 col-md-6">
<canvas id="ram_chart" width="100%" height="50"></canvas>
</div>
<div class="col-xs-12 col-md-6">
<canvas id="disk_chart" width="100%" height="50"></canvas>
</div>
</div>
</div>
</div>
<div class="col-xs-12 col-md-4">
<div class="info-box bg-blue">
<span class="info-box-icon"><i class="ion ion-ios-barcode-outline"></i></span>
<div class="info-box-content number-info-box-content">
<span class="info-box-text">Total RAM</span>
<span class="info-box-number">{{ $totalNodeRam }}MB</span>
</div>
</div>
<div class="info-box bg-blue">
<span class="info-box-icon"><i class="ion ion-stats-bars"></i></span>
<div class="info-box-content number-info-box-content">
<span class="info-box-text">Total Disk Space</span>
<span class="info-box-number">{{ $totalNodeDisk }}MB</span>
</div>
</div>
<div class="info-box bg-blue">
<span class="info-box-icon"><i class="fa fa-location-arrow"></i></span>
<div class="info-box-content number-info-box-content">
<span class="info-box-text">Total Allocations</span>
<span class="info-box-number">{{ $totalAllocations }}</span>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-md-3">
<div class="info-box bg-blue">
<span class="info-box-icon"><i class="fa fa-gamepad"></i></span>
<div class="info-box-content number-info-box-content">
<span class="info-box-text">Total Eggs</span>
<span class="info-box-number">{{ $eggsCount }}</span>
</div>
</div>
</div>
<div class="col-xs-12 col-md-3">
<div class="info-box bg-blue">
<span class="info-box-icon"><i class="fa fa-users"></i></span>
<div class="info-box-content number-info-box-content">
<span class="info-box-text">Total Users</span>
<span class="info-box-number">{{ $usersCount }}</span>
</div>
</div>
</div>
<div class="col-xs-12 col-md-3">
<div class="info-box bg-blue">
<span class="info-box-icon"><i class="fa fa-server"></i></span>
<div class="info-box-content number-info-box-content">
<span class="info-box-text">Total Nodes</span>
<span class="info-box-number">{{ count($nodes) }}</span>
</div>
</div>
</div>
<div class="col-xs-12 col-md-3">
<div class="info-box bg-blue">
<span class="info-box-icon"><i class="fa fa-database"></i></span>
<div class="info-box-content number-info-box-content">
<span class="info-box-text">Total Databases</span>
<span class="info-box-number">{{ $databasesCount }}</span>
</div>
</div>
</div>
</div>
@endsection
@section('footer-scripts')
@parent
{!! Theme::js('vendor/chartjs/chart.min.js') !!}
{!! Theme::js('js/admin/statistics.js') !!}
@endsection

View file

@ -80,6 +80,11 @@
<i class="fa fa-home"></i> <span>Overview</span>
</a>
</li>
<li class="{{ Route::currentRouteName() !== 'admin.statistics' ?: 'active' }}">
<a href="{{ route('admin.statistics') }}">
<i class="fa fa-tachometer"></i> <span>Statistics</span>
</a>
</li>
<li class="{{ ! starts_with(Route::currentRouteName(), 'admin.settings') ?: 'active' }}">
<a href="{{ route('admin.settings')}}">
<i class="fa fa-wrench"></i> <span>Settings</span>

View file

@ -1,6 +1,7 @@
<?php
Route::get('/', 'BaseController@index')->name('admin.index');
Route::get('/statistics', 'StatisticsController@index')->name('admin.statistics');
/*
|--------------------------------------------------------------------------

View 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]
);
}
}