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