From 09d28bf145ec33cea5449bafda270b350aab496e Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Tue, 5 Jan 2016 01:15:23 -0500 Subject: [PATCH] adds support for viewing server stats from 'Your Servers' page http://s3.pterodactyl.io/bnSTK.png --- app/Http/Controllers/Base/IndexController.php | 2 +- .../Controllers/Server/AjaxController.php | 20 +++--- app/Models/Server.php | 8 ++- resources/lang/en/strings.php | 6 +- resources/views/base/index.blade.php | 67 ++++++++++++------- 5 files changed, 65 insertions(+), 38 deletions(-) diff --git a/app/Http/Controllers/Base/IndexController.php b/app/Http/Controllers/Base/IndexController.php index 5346613bb..a4ac4e217 100644 --- a/app/Http/Controllers/Base/IndexController.php +++ b/app/Http/Controllers/Base/IndexController.php @@ -33,7 +33,7 @@ class IndexController extends Controller public function getIndex(Request $request) { return view('base.index', [ - 'servers' => Server::getUserServers(), + 'servers' => Server::getUserServers(10), ]); } diff --git a/app/Http/Controllers/Server/AjaxController.php b/app/Http/Controllers/Server/AjaxController.php index b519427cd..db5a094a7 100644 --- a/app/Http/Controllers/Server/AjaxController.php +++ b/app/Http/Controllers/Server/AjaxController.php @@ -52,6 +52,11 @@ class AjaxController extends Controller { $server = Server::getByUUID($uuid); + + if (!$server) { + return response()->json([], 404); + } + $client = Node::guzzleRequest($server->node); try { @@ -61,24 +66,19 @@ class AjaxController extends Controller ]); if($res->getStatusCode() === 200) { - - $json = json_decode($res->getBody()); - - if (isset($json->status) && $json->status === 1) { - return 'true'; - } - + return response()->json(json_decode($res->getBody())); + } else { + return response()->json([]); } } catch (RequestException $e) { - Debugbar::error($e->getMessage()); - Log::notice('An exception was raised while attempting to contact a Scales instance to get server status information.', [ + Log::notice('An exception was raised while attempting to contact a daemon instance to get server status information.', [ 'exception' => $e->getMessage(), 'path' => $request->path() ]); } - return 'false'; + return response()->json([]); } /** diff --git a/app/Models/Server.php b/app/Models/Server.php index 838564010..fee44d474 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -79,10 +79,10 @@ class Server extends Model * * @return \Illuminate\Database\Eloquent\Collection */ - public static function getUserServers() + public static function getUserServers($paginate = null) { - $query = self::select('servers.*', 'nodes.name as nodeName', 'locations.long as location') + $query = self::select('servers.*', 'nodes.name as nodeName', 'locations.short as a_locationShort') ->join('nodes', 'servers.node', '=', 'nodes.id') ->join('locations', 'nodes.location', '=', 'locations.id') ->where('active', 1); @@ -91,6 +91,10 @@ class Server extends Model $query->whereIn('servers.id', Subuser::accessServers()); } + if (is_numeric($paginate)) { + return $query->paginate($paginate); + } + return $query->get(); } diff --git a/resources/lang/en/strings.php b/resources/lang/en/strings.php index 9c9de3446..94b87bdda 100644 --- a/resources/lang/en/strings.php +++ b/resources/lang/en/strings.php @@ -30,6 +30,10 @@ return [ 'registered' => 'Registered', 'root_administrator' => 'Root Administrator', 'yes' => 'Yes', - 'no' => 'No' + 'no' => 'No', + 'memory' => 'Memory', + 'cpu' => 'CPU', + 'status' => 'Status', + 'players' => 'Players', ]; diff --git a/resources/views/base/index.blade.php b/resources/views/base/index.blade.php index 849a36593..f6551f49c 100644 --- a/resources/views/base/index.blade.php +++ b/resources/views/base/index.blade.php @@ -18,15 +18,17 @@ @endif {{ trans('base.server_name') }} - {{ trans('strings.location') }} {{ trans('strings.node') }} {{ trans('strings.connection') }} - + {{ trans('strings.players') }} + {{ trans('strings.memory') }} + {{ trans('strings.cpu') }} + {{ trans('strings.status') }} @foreach ($servers as $server) - + @if (Auth::user()->root_admin == 1) @if ($server->owner === Auth::user()->id) @@ -37,14 +39,19 @@ @endif {{ $server->name }} - {{ $server->location }} - {{ $server->nodeName }} + {{ $server->nodeName }} ({{ $server->a_locationShort }}) {{ $server->ip }}:{{ $server->port }} - + -- + -- / {{ $server->memory }} MB + -- % + -- @endforeach +
+
{!! $servers->render() !!}
+
@else
{{ trans('base.no_servers') }}
@endif @@ -53,38 +60,50 @@ $(window).load(function () { $('#sidebar_links').find('a[href=\'/\']').addClass('active'); function updateServerStatus () { + var Status = { + 0: 'Off', + 1: 'On', + 2: 'Starting', + 3: 'Stopping' + }; $('.dynUpdate').each(function (index, data) { - var element = $(this); - var serverShortUUID = $(this).attr('id'); - var updateElement = $(this).find('.applyUpdate'); - - updateElement.removeClass('fa-check-circle fa-times-circle').css({ color: '#000' }); - updateElement.addClass('fa-circle-o-notch fa-spinner fa-spin'); - + var serverShortUUID = $(this).data('server'); $.ajax({ type: 'GET', url: '/server/' + serverShortUUID + '/ajax/status', - timeout: 10000 + headers: { + 'X-CSRF-TOKEN': '{{ csrf_token() }}' + } }).done(function (data) { - - var selector = (data == 'true') ? 'fa-check-circle' : 'fa-times-circle'; - var selectorColor = (data == 'true') ? 'rgb(83, 179, 12)' : 'rgb(227, 50, 0)'; - - updateElement.removeClass('fa-circle-o-notch fa-spinner fa-spin'); - updateElement.addClass(selector).css({ color: selectorColor }); - + if (typeof data.status === 'undefined') { + return; + } + element.find('[data-action="status"]').html(Status[data.status]); + if (data.status !== 0) { + var cpuMax = element.find('[data-action="cpu"]').data('cpumax'); + var currentCpu = data.proc.cpu.total; + if (cpuMax !== 0) { + currentCpu = parseFloat(((data.proc.cpu.total / cpuMax) * 100).toFixed(2).toString()); + } + element.find('[data-action="memory"]').html(parseInt(data.proc.memory.total / (1024 * 1024))); + element.find('[data-action="cpu"]').html(currentCpu); + element.find('[data-action="players"]').html(data.query.players.length); + } else { + element.find('[data-action="memory"]').html('--'); + element.find('[data-action="cpu"]').html('--'); + element.find('[data-action="players"]').html('--'); + } }).fail(function (jqXHR) { - + console.error(jqXHR); updateElement.removeClass('fa-circle-o-notch fa-spinner fa-spin'); updateElement.addClass('fa-question-circle').css({ color: 'rgb(227, 50, 0)' }); - }); }); } updateServerStatus(); - setInterval(updateServerStatus, 30000); + setInterval(updateServerStatus, 10000); }); @endsection