adds support for viewing server stats from 'Your Servers' page

http://s3.pterodactyl.io/bnSTK.png
This commit is contained in:
Dane Everitt 2016-01-05 01:15:23 -05:00
parent 69f0340c48
commit 09d28bf145
5 changed files with 65 additions and 38 deletions

View file

@ -33,7 +33,7 @@ class IndexController extends Controller
public function getIndex(Request $request) public function getIndex(Request $request)
{ {
return view('base.index', [ return view('base.index', [
'servers' => Server::getUserServers(), 'servers' => Server::getUserServers(10),
]); ]);
} }

View file

@ -52,6 +52,11 @@ class AjaxController extends Controller
{ {
$server = Server::getByUUID($uuid); $server = Server::getByUUID($uuid);
if (!$server) {
return response()->json([], 404);
}
$client = Node::guzzleRequest($server->node); $client = Node::guzzleRequest($server->node);
try { try {
@ -61,24 +66,19 @@ class AjaxController extends Controller
]); ]);
if($res->getStatusCode() === 200) { if($res->getStatusCode() === 200) {
return response()->json(json_decode($res->getBody()));
$json = json_decode($res->getBody()); } else {
return response()->json([]);
if (isset($json->status) && $json->status === 1) {
return 'true';
}
} }
} catch (RequestException $e) { } catch (RequestException $e) {
Debugbar::error($e->getMessage()); Log::notice('An exception was raised while attempting to contact a daemon instance to get server status information.', [
Log::notice('An exception was raised while attempting to contact a Scales instance to get server status information.', [
'exception' => $e->getMessage(), 'exception' => $e->getMessage(),
'path' => $request->path() 'path' => $request->path()
]); ]);
} }
return 'false'; return response()->json([]);
} }
/** /**

View file

@ -79,10 +79,10 @@ class Server extends Model
* *
* @return \Illuminate\Database\Eloquent\Collection * @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('nodes', 'servers.node', '=', 'nodes.id')
->join('locations', 'nodes.location', '=', 'locations.id') ->join('locations', 'nodes.location', '=', 'locations.id')
->where('active', 1); ->where('active', 1);
@ -91,6 +91,10 @@ class Server extends Model
$query->whereIn('servers.id', Subuser::accessServers()); $query->whereIn('servers.id', Subuser::accessServers());
} }
if (is_numeric($paginate)) {
return $query->paginate($paginate);
}
return $query->get(); return $query->get();
} }

View file

@ -30,6 +30,10 @@ return [
'registered' => 'Registered', 'registered' => 'Registered',
'root_administrator' => 'Root Administrator', 'root_administrator' => 'Root Administrator',
'yes' => 'Yes', 'yes' => 'Yes',
'no' => 'No' 'no' => 'No',
'memory' => 'Memory',
'cpu' => 'CPU',
'status' => 'Status',
'players' => 'Players',
]; ];

View file

@ -18,15 +18,17 @@
<th></th> <th></th>
@endif @endif
<th>{{ trans('base.server_name') }}</th> <th>{{ trans('base.server_name') }}</th>
<th>{{ trans('strings.location') }}</th>
<th>{{ trans('strings.node') }}</th> <th>{{ trans('strings.node') }}</th>
<th>{{ trans('strings.connection') }}</th> <th>{{ trans('strings.connection') }}</th>
<th></th> <th class="text-center">{{ trans('strings.players') }}</th>
<th class="text-center">{{ trans('strings.memory') }}</th>
<th class="text-center">{{ trans('strings.cpu') }}</th>
<th class="text-center">{{ trans('strings.status') }}</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@foreach ($servers as $server) @foreach ($servers as $server)
<tr class="dynUpdate" id="{{ $server->uuidShort }}"> <tr class="dynUpdate" data-server="{{ $server->uuidShort }}">
@if (Auth::user()->root_admin == 1) @if (Auth::user()->root_admin == 1)
<td style="width:26px;"> <td style="width:26px;">
@if ($server->owner === Auth::user()->id) @if ($server->owner === Auth::user()->id)
@ -37,14 +39,19 @@
</td> </td>
@endif @endif
<td><a href="/server/{{ $server->uuidShort }}">{{ $server->name }}</a></td> <td><a href="/server/{{ $server->uuidShort }}">{{ $server->name }}</a></td>
<td>{{ $server->location }}</td> <td>{{ $server->nodeName }} ({{ $server->a_locationShort }})</td>
<td>{{ $server->nodeName }}</td>
<td><code>{{ $server->ip }}:{{ $server->port }}</code></td> <td><code>{{ $server->ip }}:{{ $server->port }}</code></td>
<td style="width:26px;"><i class="fa fa-circle-o-notch fa-spinner fa-spin applyUpdate"></i></td> <td class="text-center" data-action="players">--</td>
<td class="text-center"><span data-action="memory">--</span> / {{ $server->memory }} MB</td>
<td class="text-center"><span data-action="cpu" data-cpumax="{{ $server->cpu }}">--</span> %</td>
<td class="text-center" data-action="status">--</td>
</tr> </tr>
@endforeach @endforeach
</tbody> </tbody>
</table> </table>
<div class="row">
<div class="col-md-12 text-center">{!! $servers->render() !!}</div>
</div>
@else @else
<div class="alert alert-info">{{ trans('base.no_servers') }}</div> <div class="alert alert-info">{{ trans('base.no_servers') }}</div>
@endif @endif
@ -53,38 +60,50 @@
$(window).load(function () { $(window).load(function () {
$('#sidebar_links').find('a[href=\'/\']').addClass('active'); $('#sidebar_links').find('a[href=\'/\']').addClass('active');
function updateServerStatus () { function updateServerStatus () {
var Status = {
0: 'Off',
1: 'On',
2: 'Starting',
3: 'Stopping'
};
$('.dynUpdate').each(function (index, data) { $('.dynUpdate').each(function (index, data) {
var element = $(this); var element = $(this);
var serverShortUUID = $(this).attr('id'); var serverShortUUID = $(this).data('server');
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');
$.ajax({ $.ajax({
type: 'GET', type: 'GET',
url: '/server/' + serverShortUUID + '/ajax/status', url: '/server/' + serverShortUUID + '/ajax/status',
timeout: 10000 headers: {
'X-CSRF-TOKEN': '{{ csrf_token() }}'
}
}).done(function (data) { }).done(function (data) {
if (typeof data.status === 'undefined') {
var selector = (data == 'true') ? 'fa-check-circle' : 'fa-times-circle'; return;
var selectorColor = (data == 'true') ? 'rgb(83, 179, 12)' : 'rgb(227, 50, 0)'; }
element.find('[data-action="status"]').html(Status[data.status]);
updateElement.removeClass('fa-circle-o-notch fa-spinner fa-spin'); if (data.status !== 0) {
updateElement.addClass(selector).css({ color: selectorColor }); 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) { }).fail(function (jqXHR) {
console.error(jqXHR);
updateElement.removeClass('fa-circle-o-notch fa-spinner fa-spin'); updateElement.removeClass('fa-circle-o-notch fa-spinner fa-spin');
updateElement.addClass('fa-question-circle').css({ color: 'rgb(227, 50, 0)' }); updateElement.addClass('fa-question-circle').css({ color: 'rgb(227, 50, 0)' });
}); });
}); });
} }
updateServerStatus(); updateServerStatus();
setInterval(updateServerStatus, 30000); setInterval(updateServerStatus, 10000);
}); });
</script> </script>
@endsection @endsection