From 10b357b71e747f72e1474959c70efa04866d8a47 Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Wed, 4 Aug 2021 21:34:00 -0600 Subject: [PATCH] ui(server): fix used backup count (#3526) * ui(server): fix used backup count * ui(server): refactor backup count code --- .../Api/Client/Servers/BackupController.php | 38 ++++++++----------- .../Eloquent/BackupRepository.php | 15 ++++++++ .../Backups/InitiateBackupService.php | 6 +-- resources/scripts/api/swr/getServerBackups.ts | 5 ++- .../server/backups/BackupContainer.tsx | 6 +-- .../server/backups/BackupContextMenu.tsx | 1 + .../server/backups/CreateBackupButton.tsx | 2 +- 7 files changed, 41 insertions(+), 32 deletions(-) diff --git a/app/Http/Controllers/Api/Client/Servers/BackupController.php b/app/Http/Controllers/Api/Client/Servers/BackupController.php index fe9232e8..7f4c6380 100644 --- a/app/Http/Controllers/Api/Client/Servers/BackupController.php +++ b/app/Http/Controllers/Api/Client/Servers/BackupController.php @@ -12,6 +12,7 @@ use Illuminate\Auth\Access\AuthorizationException; use Pterodactyl\Services\Backups\DeleteBackupService; use Pterodactyl\Services\Backups\DownloadLinkService; use Pterodactyl\Services\Backups\InitiateBackupService; +use Pterodactyl\Repositories\Eloquent\BackupRepository; use Pterodactyl\Repositories\Wings\DaemonBackupRepository; use Pterodactyl\Transformers\Api\Client\BackupTransformer; use Pterodactyl\Http\Controllers\Api\Client\ClientApiController; @@ -20,34 +21,21 @@ use Pterodactyl\Http\Requests\Api\Client\Servers\Backups\StoreBackupRequest; class BackupController extends ClientApiController { - /** - * @var \Pterodactyl\Services\Backups\InitiateBackupService - */ - private $initiateBackupService; - - /** - * @var \Pterodactyl\Services\Backups\DeleteBackupService - */ - private $deleteBackupService; - - /** - * @var \Pterodactyl\Services\Backups\DownloadLinkService - */ - private $downloadLinkService; - - /** - * @var \Pterodactyl\Repositories\Wings\DaemonBackupRepository - */ - private $repository; + private InitiateBackupService $initiateBackupService; + private DeleteBackupService $deleteBackupService; + private DownloadLinkService $downloadLinkService; + private DaemonBackupRepository $daemonRepository; + private BackupRepository $repository; /** * BackupController constructor. */ public function __construct( - DaemonBackupRepository $repository, + DaemonBackupRepository $daemonRepository, DeleteBackupService $deleteBackupService, InitiateBackupService $initiateBackupService, - DownloadLinkService $downloadLinkService + DownloadLinkService $downloadLinkService, + BackupRepository $repository ) { parent::__construct(); @@ -55,10 +43,11 @@ class BackupController extends ClientApiController $this->initiateBackupService = $initiateBackupService; $this->deleteBackupService = $deleteBackupService; $this->downloadLinkService = $downloadLinkService; + $this->daemonRepository = $daemonRepository; } /** - * Returns all of the backups for a given server instance in a paginated + * Returns all the backups for a given server instance in a paginated * result set. * * @throws \Illuminate\Auth\Access\AuthorizationException @@ -73,6 +62,9 @@ class BackupController extends ClientApiController return $this->fractal->collection($server->backups()->paginate($limit)) ->transformWith($this->getTransformer(BackupTransformer::class)) + ->addMeta([ + 'backup_count' => $this->repository->getNonFailedBackups($server)->count(), + ]) ->toArray(); } @@ -242,7 +234,7 @@ class BackupController extends ClientApiController // actions against it via the Panel API. $server->update(['status' => Server::STATUS_RESTORING_BACKUP]); - $this->repository->setServer($server)->restore($backup, $url ?? null, $request->input('truncate')); + $this->daemonRepository->setServer($server)->restore($backup, $url ?? null, $request->input('truncate')); }); return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT); diff --git a/app/Repositories/Eloquent/BackupRepository.php b/app/Repositories/Eloquent/BackupRepository.php index fef80c48..7efdace0 100644 --- a/app/Repositories/Eloquent/BackupRepository.php +++ b/app/Repositories/Eloquent/BackupRepository.php @@ -4,6 +4,8 @@ namespace Pterodactyl\Repositories\Eloquent; use Carbon\Carbon; use Pterodactyl\Models\Backup; +use Pterodactyl\Models\Server; +use Illuminate\Database\Eloquent\Relations\HasMany; class BackupRepository extends EloquentRepository { @@ -33,4 +35,17 @@ class BackupRepository extends EloquentRepository ->get() ->toBase(); } + + /** + * Returns a query filtering only non-failed backups for a specific server. + * + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ + public function getNonFailedBackups(Server $server): HasMany + { + return $server->backups()->where(function ($query) { + $query->whereNull('completed_at') + ->orWhere('is_successful', true); + }); + } } diff --git a/app/Services/Backups/InitiateBackupService.php b/app/Services/Backups/InitiateBackupService.php index f51279f6..8bad8f4f 100644 --- a/app/Services/Backups/InitiateBackupService.php +++ b/app/Services/Backups/InitiateBackupService.php @@ -9,6 +9,7 @@ use Pterodactyl\Models\Backup; use Pterodactyl\Models\Server; use Illuminate\Database\ConnectionInterface; use Pterodactyl\Extensions\Backups\BackupManager; +use Illuminate\Database\Eloquent\Relations\HasMany; use Pterodactyl\Repositories\Eloquent\BackupRepository; use Pterodactyl\Repositories\Wings\DaemonBackupRepository; use Pterodactyl\Exceptions\Service\Backup\TooManyBackupsException; @@ -134,10 +135,7 @@ class InitiateBackupService // Check if the server has reached or exceeded its backup limit. // completed_at == null will cover any ongoing backups, while is_successful == true will cover any completed backups. - $successful = $server->backups()->where(function ($query) { - $query->whereNull('completed_at') - ->orWhere('is_successful', true); - }); + $successful = $this->repository->getNonFailedBackups($server); if (!$server->backup_limit || $successful->count() >= $server->backup_limit) { // Do not allow the user to continue if this server is already at its limit and can't override. if (!$override || $server->backup_limit <= 0) { diff --git a/resources/scripts/api/swr/getServerBackups.ts b/resources/scripts/api/swr/getServerBackups.ts index 85a8c188..6b76ddcd 100644 --- a/resources/scripts/api/swr/getServerBackups.ts +++ b/resources/scripts/api/swr/getServerBackups.ts @@ -12,16 +12,19 @@ interface ctx { export const Context = createContext({ page: 1, setPage: () => 1 }); +type BackupResponse = PaginatedResult & { backupCount: number }; + export default () => { const { page } = useContext(Context); const uuid = ServerContext.useStoreState(state => state.server.data!.uuid); - return useSWR>([ 'server:backups', uuid, page ], async () => { + return useSWR([ 'server:backups', uuid, page ], async () => { const { data } = await http.get(`/api/client/servers/${uuid}/backups`, { params: { page } }); return ({ items: (data.data || []).map(rawDataToServerBackup), pagination: getPaginationSet(data.meta.pagination), + backupCount: data.meta.backup_count, }); }); }; diff --git a/resources/scripts/components/server/backups/BackupContainer.tsx b/resources/scripts/components/server/backups/BackupContainer.tsx index 97dba20f..1201252b 100644 --- a/resources/scripts/components/server/backups/BackupContainer.tsx +++ b/resources/scripts/components/server/backups/BackupContainer.tsx @@ -65,12 +65,12 @@ const BackupContainer = () => { }
- {(backupLimit > 0 && backups.pagination.total > 0) && + {(backupLimit > 0 && backups.backupCount > 0) &&

- {backups.pagination.total} of {backupLimit} backups have been created for this server. + {backups.backupCount} of {backupLimit} backups have been created for this server.

} - {backupLimit > 0 && backupLimit !== backups.pagination.total && + {backupLimit > 0 && backupLimit > backups.backupCount && }
diff --git a/resources/scripts/components/server/backups/BackupContextMenu.tsx b/resources/scripts/components/server/backups/BackupContextMenu.tsx index 146ab50e..6da268a0 100644 --- a/resources/scripts/components/server/backups/BackupContextMenu.tsx +++ b/resources/scripts/components/server/backups/BackupContextMenu.tsx @@ -58,6 +58,7 @@ export default ({ backup }: Props) => { .then(() => mutate(data => ({ ...data, items: data.items.filter(b => b.uuid !== backup.uuid), + backupCount: data.backupCount - 1, }), false)) .catch(error => { console.error(error); diff --git a/resources/scripts/components/server/backups/CreateBackupButton.tsx b/resources/scripts/components/server/backups/CreateBackupButton.tsx index c26dd728..c74980a9 100644 --- a/resources/scripts/components/server/backups/CreateBackupButton.tsx +++ b/resources/scripts/components/server/backups/CreateBackupButton.tsx @@ -81,7 +81,7 @@ export default () => { clearFlashes('backups:create'); createServerBackup(uuid, values) .then(backup => { - mutate(data => ({ ...data, items: data.items.concat(backup) }), false); + mutate(data => ({ ...data, items: data.items.concat(backup), backupCount: data.backupCount + 1 }), false); setVisible(false); }) .catch(error => {