Skip to content

Commit 09d28bf

Browse files
committed
adds support for viewing server stats from 'Your Servers' page
http://s3.pterodactyl.io/bnSTK.png
1 parent 69f0340 commit 09d28bf

File tree

5 files changed

+65
-38
lines changed

5 files changed

+65
-38
lines changed

app/Http/Controllers/Base/IndexController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __construct()
3333
public function getIndex(Request $request)
3434
{
3535
return view('base.index', [
36-
'servers' => Server::getUserServers(),
36+
'servers' => Server::getUserServers(10),
3737
]);
3838
}
3939

app/Http/Controllers/Server/AjaxController.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ public function getStatus(Request $request, $uuid)
5252
{
5353

5454
$server = Server::getByUUID($uuid);
55+
56+
if (!$server) {
57+
return response()->json([], 404);
58+
}
59+
5560
$client = Node::guzzleRequest($server->node);
5661

5762
try {
@@ -61,24 +66,19 @@ public function getStatus(Request $request, $uuid)
6166
]);
6267

6368
if($res->getStatusCode() === 200) {
64-
65-
$json = json_decode($res->getBody());
66-
67-
if (isset($json->status) && $json->status === 1) {
68-
return 'true';
69-
}
70-
69+
return response()->json(json_decode($res->getBody()));
70+
} else {
71+
return response()->json([]);
7172
}
7273

7374
} catch (RequestException $e) {
74-
Debugbar::error($e->getMessage());
75-
Log::notice('An exception was raised while attempting to contact a Scales instance to get server status information.', [
75+
Log::notice('An exception was raised while attempting to contact a daemon instance to get server status information.', [
7676
'exception' => $e->getMessage(),
7777
'path' => $request->path()
7878
]);
7979
}
8080

81-
return 'false';
81+
return response()->json([]);
8282
}
8383

8484
/**

app/Models/Server.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ protected static function getUserDaemonSecret(Server $server)
7979
*
8080
* @return \Illuminate\Database\Eloquent\Collection
8181
*/
82-
public static function getUserServers()
82+
public static function getUserServers($paginate = null)
8383
{
8484

85-
$query = self::select('servers.*', 'nodes.name as nodeName', 'locations.long as location')
85+
$query = self::select('servers.*', 'nodes.name as nodeName', 'locations.short as a_locationShort')
8686
->join('nodes', 'servers.node', '=', 'nodes.id')
8787
->join('locations', 'nodes.location', '=', 'locations.id')
8888
->where('active', 1);
@@ -91,6 +91,10 @@ public static function getUserServers()
9191
$query->whereIn('servers.id', Subuser::accessServers());
9292
}
9393

94+
if (is_numeric($paginate)) {
95+
return $query->paginate($paginate);
96+
}
97+
9498
return $query->get();
9599

96100
}

resources/lang/en/strings.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
'registered' => 'Registered',
3131
'root_administrator' => 'Root Administrator',
3232
'yes' => 'Yes',
33-
'no' => 'No'
33+
'no' => 'No',
34+
'memory' => 'Memory',
35+
'cpu' => 'CPU',
36+
'status' => 'Status',
37+
'players' => 'Players',
3438

3539
];

resources/views/base/index.blade.php

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@
1818
<th></th>
1919
@endif
2020
<th>{{ trans('base.server_name') }}</th>
21-
<th>{{ trans('strings.location') }}</th>
2221
<th>{{ trans('strings.node') }}</th>
2322
<th>{{ trans('strings.connection') }}</th>
24-
<th></th>
23+
<th class="text-center">{{ trans('strings.players') }}</th>
24+
<th class="text-center">{{ trans('strings.memory') }}</th>
25+
<th class="text-center">{{ trans('strings.cpu') }}</th>
26+
<th class="text-center">{{ trans('strings.status') }}</th>
2527
</tr>
2628
</thead>
2729
<tbody>
2830
@foreach ($servers as $server)
29-
<tr class="dynUpdate" id="{{ $server->uuidShort }}">
31+
<tr class="dynUpdate" data-server="{{ $server->uuidShort }}">
3032
@if (Auth::user()->root_admin == 1)
3133
<td style="width:26px;">
3234
@if ($server->owner === Auth::user()->id)
@@ -37,14 +39,19 @@
3739
</td>
3840
@endif
3941
<td><a href="/server/{{ $server->uuidShort }}">{{ $server->name }}</a></td>
40-
<td>{{ $server->location }}</td>
41-
<td>{{ $server->nodeName }}</td>
42+
<td>{{ $server->nodeName }} ({{ $server->a_locationShort }})</td>
4243
<td><code>{{ $server->ip }}:{{ $server->port }}</code></td>
43-
<td style="width:26px;"><i class="fa fa-circle-o-notch fa-spinner fa-spin applyUpdate"></i></td>
44+
<td class="text-center" data-action="players">--</td>
45+
<td class="text-center"><span data-action="memory">--</span> / {{ $server->memory }} MB</td>
46+
<td class="text-center"><span data-action="cpu" data-cpumax="{{ $server->cpu }}">--</span> %</td>
47+
<td class="text-center" data-action="status">--</td>
4448
</tr>
4549
@endforeach
4650
</tbody>
4751
</table>
52+
<div class="row">
53+
<div class="col-md-12 text-center">{!! $servers->render() !!}</div>
54+
</div>
4855
@else
4956
<div class="alert alert-info">{{ trans('base.no_servers') }}</div>
5057
@endif
@@ -53,38 +60,50 @@
5360
$(window).load(function () {
5461
$('#sidebar_links').find('a[href=\'/\']').addClass('active');
5562
function updateServerStatus () {
63+
var Status = {
64+
0: 'Off',
65+
1: 'On',
66+
2: 'Starting',
67+
3: 'Stopping'
68+
};
5669
$('.dynUpdate').each(function (index, data) {
57-
5870
var element = $(this);
59-
var serverShortUUID = $(this).attr('id');
60-
var updateElement = $(this).find('.applyUpdate');
61-
62-
updateElement.removeClass('fa-check-circle fa-times-circle').css({ color: '#000' });
63-
updateElement.addClass('fa-circle-o-notch fa-spinner fa-spin');
64-
71+
var serverShortUUID = $(this).data('server');
6572
$.ajax({
6673
type: 'GET',
6774
url: '/server/' + serverShortUUID + '/ajax/status',
68-
timeout: 10000
75+
headers: {
76+
'X-CSRF-TOKEN': '{{ csrf_token() }}'
77+
}
6978
}).done(function (data) {
70-
71-
var selector = (data == 'true') ? 'fa-check-circle' : 'fa-times-circle';
72-
var selectorColor = (data == 'true') ? 'rgb(83, 179, 12)' : 'rgb(227, 50, 0)';
73-
74-
updateElement.removeClass('fa-circle-o-notch fa-spinner fa-spin');
75-
updateElement.addClass(selector).css({ color: selectorColor });
76-
79+
if (typeof data.status === 'undefined') {
80+
return;
81+
}
82+
element.find('[data-action="status"]').html(Status[data.status]);
83+
if (data.status !== 0) {
84+
var cpuMax = element.find('[data-action="cpu"]').data('cpumax');
85+
var currentCpu = data.proc.cpu.total;
86+
if (cpuMax !== 0) {
87+
currentCpu = parseFloat(((data.proc.cpu.total / cpuMax) * 100).toFixed(2).toString());
88+
}
89+
element.find('[data-action="memory"]').html(parseInt(data.proc.memory.total / (1024 * 1024)));
90+
element.find('[data-action="cpu"]').html(currentCpu);
91+
element.find('[data-action="players"]').html(data.query.players.length);
92+
} else {
93+
element.find('[data-action="memory"]').html('--');
94+
element.find('[data-action="cpu"]').html('--');
95+
element.find('[data-action="players"]').html('--');
96+
}
7797
}).fail(function (jqXHR) {
78-
98+
console.error(jqXHR);
7999
updateElement.removeClass('fa-circle-o-notch fa-spinner fa-spin');
80100
updateElement.addClass('fa-question-circle').css({ color: 'rgb(227, 50, 0)' });
81-
82101
});
83102
84103
});
85104
}
86105
updateServerStatus();
87-
setInterval(updateServerStatus, 30000);
106+
setInterval(updateServerStatus, 10000);
88107
});
89108
</script>
90109
@endsection

0 commit comments

Comments
 (0)