ui(server): fix used backup count

This commit is contained in:
Matthew Penner 2021-08-04 17:22:57 -06:00
parent 7e91a33a67
commit f44833ca9c
No known key found for this signature in database
GPG key ID: 5396CC4C3C1C9704
6 changed files with 23 additions and 10 deletions

View file

@ -58,7 +58,7 @@ class BackupController extends ClientApiController
} }
/** /**
* 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. * result set.
* *
* @throws \Illuminate\Auth\Access\AuthorizationException * @throws \Illuminate\Auth\Access\AuthorizationException
@ -73,6 +73,9 @@ class BackupController extends ClientApiController
return $this->fractal->collection($server->backups()->paginate($limit)) return $this->fractal->collection($server->backups()->paginate($limit))
->transformWith($this->getTransformer(BackupTransformer::class)) ->transformWith($this->getTransformer(BackupTransformer::class))
->addMeta([
'used_backup_count' => $this->initiateBackupService->getNonFailedBackups($server)->count(),
])
->toArray(); ->toArray();
} }

View file

@ -9,6 +9,7 @@ use Pterodactyl\Models\Backup;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Extensions\Backups\BackupManager; use Pterodactyl\Extensions\Backups\BackupManager;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Pterodactyl\Repositories\Eloquent\BackupRepository; use Pterodactyl\Repositories\Eloquent\BackupRepository;
use Pterodactyl\Repositories\Wings\DaemonBackupRepository; use Pterodactyl\Repositories\Wings\DaemonBackupRepository;
use Pterodactyl\Exceptions\Service\Backup\TooManyBackupsException; use Pterodactyl\Exceptions\Service\Backup\TooManyBackupsException;
@ -134,10 +135,7 @@ class InitiateBackupService
// Check if the server has reached or exceeded its backup limit. // 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. // completed_at == null will cover any ongoing backups, while is_successful == true will cover any completed backups.
$successful = $server->backups()->where(function ($query) { $successful = $this->getNonFailedBackups($server);
$query->whereNull('completed_at')
->orWhere('is_successful', true);
});
if (!$server->backup_limit || $successful->count() >= $server->backup_limit) { 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. // 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) { if (!$override || $server->backup_limit <= 0) {
@ -174,4 +172,12 @@ class InitiateBackupService
return $backup; return $backup;
}); });
} }
public function getNonFailedBackups(Server $server): HasMany
{
return $server->backups()->where(function ($query) {
$query->whereNull('completed_at')
->orWhere('is_successful', true);
});
}
} }

View file

@ -12,16 +12,19 @@ interface ctx {
export const Context = createContext<ctx>({ page: 1, setPage: () => 1 }); export const Context = createContext<ctx>({ page: 1, setPage: () => 1 });
type BackupResponse = PaginatedResult<ServerBackup> & { usedBackupCount: number };
export default () => { export default () => {
const { page } = useContext(Context); const { page } = useContext(Context);
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid); const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
return useSWR<PaginatedResult<ServerBackup>>([ 'server:backups', uuid, page ], async () => { return useSWR<BackupResponse>([ 'server:backups', uuid, page ], async () => {
const { data } = await http.get(`/api/client/servers/${uuid}/backups`, { params: { page } }); const { data } = await http.get(`/api/client/servers/${uuid}/backups`, { params: { page } });
return ({ return ({
items: (data.data || []).map(rawDataToServerBackup), items: (data.data || []).map(rawDataToServerBackup),
pagination: getPaginationSet(data.meta.pagination), pagination: getPaginationSet(data.meta.pagination),
usedBackupCount: data.meta.used_backup_count,
}); });
}); });
}; };

View file

@ -65,12 +65,12 @@ const BackupContainer = () => {
} }
<Can action={'backup.create'}> <Can action={'backup.create'}>
<div css={tw`mt-6 sm:flex items-center justify-end`}> <div css={tw`mt-6 sm:flex items-center justify-end`}>
{(backupLimit > 0 && backups.pagination.total > 0) && {(backupLimit > 0 && backups.usedBackupCount > 0) &&
<p css={tw`text-sm text-neutral-300 mb-4 sm:mr-6 sm:mb-0`}> <p css={tw`text-sm text-neutral-300 mb-4 sm:mr-6 sm:mb-0`}>
{backups.pagination.total} of {backupLimit} backups have been created for this server. {backups.usedBackupCount} of {backupLimit} backups have been created for this server.
</p> </p>
} }
{backupLimit > 0 && backupLimit !== backups.pagination.total && {backupLimit > 0 && backupLimit > backups.usedBackupCount &&
<CreateBackupButton css={tw`w-full sm:w-auto`}/> <CreateBackupButton css={tw`w-full sm:w-auto`}/>
} }
</div> </div>

View file

@ -58,6 +58,7 @@ export default ({ backup }: Props) => {
.then(() => mutate(data => ({ .then(() => mutate(data => ({
...data, ...data,
items: data.items.filter(b => b.uuid !== backup.uuid), items: data.items.filter(b => b.uuid !== backup.uuid),
usedBackupCount: data.usedBackupCount - 1,
}), false)) }), false))
.catch(error => { .catch(error => {
console.error(error); console.error(error);

View file

@ -81,7 +81,7 @@ export default () => {
clearFlashes('backups:create'); clearFlashes('backups:create');
createServerBackup(uuid, values) createServerBackup(uuid, values)
.then(backup => { .then(backup => {
mutate(data => ({ ...data, items: data.items.concat(backup) }), false); mutate(data => ({ ...data, items: data.items.concat(backup), usedBackupCount: data.usedBackupCount + 1 }), false);
setVisible(false); setVisible(false);
}) })
.catch(error => { .catch(error => {