2020-04-04 19:26:39 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Repositories\Eloquent;
|
|
|
|
|
2020-04-10 05:35:38 +00:00
|
|
|
use Carbon\Carbon;
|
2020-04-04 19:26:39 +00:00
|
|
|
use Pterodactyl\Models\Backup;
|
2021-08-05 03:34:00 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2022-10-14 16:59:20 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2021-08-05 03:34:00 +00:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2020-04-04 19:26:39 +00:00
|
|
|
|
|
|
|
class BackupRepository extends EloquentRepository
|
|
|
|
{
|
2022-10-14 16:59:20 +00:00
|
|
|
public function model(): string
|
2020-04-04 19:26:39 +00:00
|
|
|
{
|
|
|
|
return Backup::class;
|
|
|
|
}
|
2020-04-10 05:35:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if too many backups have been generated by the server.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function getBackupsGeneratedDuringTimespan(int $server, int $seconds = 600): array|Collection
|
2020-04-10 05:35:38 +00:00
|
|
|
{
|
|
|
|
return $this->getBuilder()
|
|
|
|
->withTrashed()
|
|
|
|
->where('server_id', $server)
|
2021-08-04 02:45:25 +00:00
|
|
|
->where(function ($query) {
|
|
|
|
$query->whereNull('completed_at')
|
|
|
|
->orWhere('is_successful', '=', true);
|
|
|
|
})
|
2020-12-28 00:41:53 +00:00
|
|
|
->where('created_at', '>=', Carbon::now()->subSeconds($seconds)->toDateTimeString())
|
2020-04-10 05:35:38 +00:00
|
|
|
->get()
|
|
|
|
->toBase();
|
|
|
|
}
|
2021-08-05 03:34:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a query filtering only non-failed backups for a specific server.
|
|
|
|
*/
|
|
|
|
public function getNonFailedBackups(Server $server): HasMany
|
|
|
|
{
|
|
|
|
return $server->backups()->where(function ($query) {
|
|
|
|
$query->whereNull('completed_at')
|
|
|
|
->orWhere('is_successful', true);
|
|
|
|
});
|
|
|
|
}
|
2020-04-04 19:26:39 +00:00
|
|
|
}
|