diff --git a/app/Http/Controllers/Base/DashboardController.php b/app/Http/Controllers/Base/DashboardController.php deleted file mode 100644 index 8351b037a..000000000 --- a/app/Http/Controllers/Base/DashboardController.php +++ /dev/null @@ -1,54 +0,0 @@ -repository = $repository; - } - - public function servers(Request $request) - { - $servers = $this->repository->setSearchTerm($request->input('query'))->filterUserAccessServers( - $request->user(), User::FILTER_LEVEL_ALL - ); - - $data = []; - foreach ($servers->items() as $server) { - $cleaned = collect($server)->only([ - 'uuidShort', - 'uuid', - 'name', - 'cpu', - 'memory', - ]); - - $data[] = array_merge($cleaned->toArray(), [ - 'allocation' => [ - 'ip' => $server->allocation->ip, - 'port' => $server->allocation->port, - ], - 'node_name' => $server->node->name, - ]); - } - - return response()->json($data); - } -} diff --git a/app/Transformers/Api/Client/StatsTransformer.php b/app/Transformers/Api/Client/StatsTransformer.php index 01d8e3f20..d3e66eb9a 100644 --- a/app/Transformers/Api/Client/StatsTransformer.php +++ b/app/Transformers/Api/Client/StatsTransformer.php @@ -3,6 +3,8 @@ namespace Pterodactyl\Transformers\Api\Client; use Pterodactyl\Models\Server; +use GuzzleHttp\Exception\RequestException; +use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException; use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface; class StatsTransformer extends BaseClientTransformer @@ -36,6 +38,8 @@ class StatsTransformer extends BaseClientTransformer * * @param \Pterodactyl\Models\Server $model * @return array + * + * @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException */ public function transform(Server $model) { @@ -61,7 +65,10 @@ class StatsTransformer extends BaseClientTransformer 'disk' => [ 'current' => round(object_get($object, 'proc.disk.used', 0)), 'limit' => floatval($model->disk), + 'io' => $model->io, ], + 'installed' => $model->installed === 1, + 'suspended' => (bool) $model->suspended, ]; } diff --git a/resources/assets/scripts/components/dashboard/Dashboard.vue b/resources/assets/scripts/components/dashboard/Dashboard.vue index 587d3dddb..6359534c7 100644 --- a/resources/assets/scripts/components/dashboard/Dashboard.vue +++ b/resources/assets/scripts/components/dashboard/Dashboard.vue @@ -19,7 +19,6 @@ v-for="(server, index) in servers.models" v-bind:key="index" v-bind:server="server" - v-bind:resources="resources[server.uuid]" /> @@ -39,14 +38,26 @@ loading: true, search: '', servers: new ServerCollection, - resources: {}, } }, - mounted: function () { + /** + * Start loading the servers before the DOM $.el is created. + */ + created: function () { this.loadServers(); }, + /** + * Once the page is mounted set a function to run every 5 seconds that will + * iterate through the visible servers and fetch their resource usage. + */ + mounted: function () { + setInterval(() => { + this.servers.each(this.getResourceUse) + }, 10000); + }, + methods: { /** * Load the user's servers and render them onto the dashboard. @@ -64,8 +75,9 @@ .then(response => { this.servers = new ServerCollection; response.data.data.forEach(obj => { - this.resources[obj.attributes.uuid] = { cpu: 0, memory: 0 }; - this.servers.add(obj.attributes); + this.getResourceUse( + this.servers.add(obj.attributes) + ); }); if (this.servers.models.length === 0) { @@ -93,6 +105,25 @@ onChange: _.debounce(function () { this.loadServers(this.$data.search); }, 500), + + /** + * Get resource usage for an individual server for rendering purposes. + * + * @param {Server} server + */ + getResourceUse: function (server) { + window.axios.get(this.route('api.client.servers.resources', { server: server.identifier })) + .then(response => { + if (!(response.data instanceof Object)) { + throw new Error('Received an invalid response object back from status endpoint.'); + } + + window.events.$emit(`server:${server.uuid}::resources`, response.data.attributes); + }) + .catch(err => { + console.error(err); + }); + }, } }; diff --git a/resources/assets/scripts/components/dashboard/ServerBox.vue b/resources/assets/scripts/components/dashboard/ServerBox.vue index a4f099849..b422f0503 100644 --- a/resources/assets/scripts/components/dashboard/ServerBox.vue +++ b/resources/assets/scripts/components/dashboard/ServerBox.vue @@ -2,7 +2,7 @@
-
+
@@ -19,11 +19,11 @@
- {{ resources.cpu > 0 ? resources.cpu : '—' }} + {{ cpu > 0 ? cpu : '—' }} %
- {{ resources.memory > 0 ? resources.memory : '—' }} + {{ memory > 0 ? memory : '—' }} Mb
@@ -38,13 +38,76 @@ diff --git a/routes/api-client.php b/routes/api-client.php index 23c79fd40..c8bfbc78d 100644 --- a/routes/api-client.php +++ b/routes/api-client.php @@ -23,7 +23,7 @@ Route::get('/', 'ClientController@index')->name('api.client.index'); Route::group(['prefix' => '/servers/{server}', 'middleware' => [AuthenticateClientAccess::class]], function () { Route::get('/', 'Servers\ServerController@index')->name('api.client.servers.view'); Route::get('/utilization', 'Servers\ResourceUtilizationController@index') - ->middleware(['throttle:15,1']) + ->middleware(['throttle:20,1']) ->name('api.client.servers.resources'); Route::post('/command', 'Servers\CommandController@index')->name('api.client.servers.command'); diff --git a/routes/base.php b/routes/base.php index 3c2a67829..4d86bada9 100644 --- a/routes/base.php +++ b/routes/base.php @@ -7,8 +7,6 @@ * https://opensource.org/licenses/MIT */ Route::get('/', 'IndexController@index')->name('index'); -Route::get('/dashboard/servers', 'DashboardController@servers')->name('dashboard.servers'); -Route::get('/status/{server}', 'IndexController@status')->name('index.status'); /* |--------------------------------------------------------------------------