refactor: replace queries with cached versions for performance improvements

This commit is contained in:
Andras Bacsai committed 2025-12-08 13:39:33 +01:00
1 parent bb83f4e5c3
commit 5e8d11f732
28 files changed
+305 -125

No files matched your search

+14
View File
@@ -338,11 +338,25 @@ class Application extends BaseModel
return Application::whereRelation('environment.project.team', 'id', $teamId)->orderBy('name');
}
/**
* Get query builder for applications owned by current team.
* If you need all applications without further query chaining, use ownedByCurrentTeamCached() instead.
*/
public static function ownedByCurrentTeam()
{
return Application::whereRelation('environment.project.team', 'id', currentTeam()->id)->orderBy('name');
}
/**
* Get all applications owned by current team (cached for request duration).
*/
public static function ownedByCurrentTeamCached()
{
return once(function () {
return Application::ownedByCurrentTeam()->get();
});
}
public function getContainersToStop(Server $server, bool $previewDeployments = false): array
{
$containers = $previewDeployments
+14
View File
@@ -80,6 +80,10 @@ class PrivateKey extends BaseModel
return self::extractPublicKeyFromPrivate($this->private_key) ?? 'Error loading private key';
}
/**
* Get query builder for private keys owned by current team.
* If you need all private keys without further query chaining, use ownedByCurrentTeamCached() instead.
*/
public static function ownedByCurrentTeam(array $select = ['*'])
{
$teamId = currentTeam()->id;
@@ -88,6 +92,16 @@ class PrivateKey extends BaseModel
return self::whereTeamId($teamId)->select($selectArray->all());
}
/**
* Get all private keys owned by current team (cached for request duration).
*/
public static function ownedByCurrentTeamCached()
{
return once(function () {
return PrivateKey::ownedByCurrentTeam()->get();
});
}
public static function ownedAndOnlySShKeys(array $select = ['*'])
{
$teamId = currentTeam()->id;
+14
View File
@@ -30,11 +30,25 @@ class Project extends BaseModel
protected $guarded = [];
/**
* Get query builder for projects owned by current team.
* If you need all projects without further query chaining, use ownedByCurrentTeamCached() instead.
*/
public static function ownedByCurrentTeam()
{
return Project::whereTeamId(currentTeam()->id)->orderByRaw('LOWER(name)');
}
/**
* Get all projects owned by current team (cached for request duration).
*/
public static function ownedByCurrentTeamCached()
{
return once(function () {
return Project::ownedByCurrentTeam()->get();
});
}
protected static function booted()
{
static::created(function ($project) {
+14
View File
@@ -242,6 +242,10 @@ class Server extends BaseModel
return Server::ownedByCurrentTeam()->whereRelation('settings', 'is_reachable', true);
}
/**
* Get query builder for servers owned by current team.
* If you need all servers without further query chaining, use ownedByCurrentTeamCached() instead.
*/
public static function ownedByCurrentTeam(array $select = ['*'])
{
$teamId = currentTeam()->id;
@@ -250,6 +254,16 @@ class Server extends BaseModel
return Server::whereTeamId($teamId)->with('settings', 'swarmDockers', 'standaloneDockers')->select($selectArray->all())->orderBy('name');
}
/**
* Get all servers owned by current team (cached for request duration).
*/
public static function ownedByCurrentTeamCached()
{
return once(function () {
return Server::ownedByCurrentTeam()->get();
});
}
public static function isUsable()
{
return Server::ownedByCurrentTeam()->whereRelation('settings', 'is_reachable', true)->whereRelation('settings', 'is_usable', true)->whereRelation('settings', 'is_swarm_worker', false)->whereRelation('settings', 'is_build_server', false)->whereRelation('settings', 'force_disabled', false);
+14
View File
@@ -153,11 +153,25 @@ class Service extends BaseModel
return $this->morphToMany(Tag::class, 'taggable');
}
/**
* Get query builder for services owned by current team.
* If you need all services without further query chaining, use ownedByCurrentTeamCached() instead.
*/
public static function ownedByCurrentTeam()
{
return Service::whereRelation('environment.project.team', 'id', currentTeam()->id)->orderBy('name');
}
/**
* Get all services owned by current team (cached for request duration).
*/
public static function ownedByCurrentTeamCached()
{
return once(function () {
return Service::ownedByCurrentTeam()->get();
});
}
public function deleteConfigurations()
{
$server = data_get($this, 'destination.server');
+14
View File
@@ -37,11 +37,25 @@ class ServiceApplication extends BaseModel
return ServiceApplication::whereRelation('service.environment.project.team', 'id', $teamId)->orderBy('name');
}
/**
* Get query builder for service applications owned by current team.
* If you need all service applications without further query chaining, use ownedByCurrentTeamCached() instead.
*/
public static function ownedByCurrentTeam()
{
return ServiceApplication::whereRelation('service.environment.project.team', 'id', currentTeam()->id)->orderBy('name');
}
/**
* Get all service applications owned by current team (cached for request duration).
*/
public static function ownedByCurrentTeamCached()
{
return once(function () {
return ServiceApplication::ownedByCurrentTeam()->get();
});
}
public function isRunning()
{
return str($this->status)->contains('running');
+14
View File
@@ -30,11 +30,25 @@ class ServiceDatabase extends BaseModel
return ServiceDatabase::whereRelation('service.environment.project.team', 'id', $teamId)->orderBy('name');
}
/**
* Get query builder for service databases owned by current team.
* If you need all service databases without further query chaining, use ownedByCurrentTeamCached() instead.
*/
public static function ownedByCurrentTeam()
{
return ServiceDatabase::whereRelation('service.environment.project.team', 'id', currentTeam()->id)->orderBy('name');
}
/**
* Get all service databases owned by current team (cached for request duration).
*/
public static function ownedByCurrentTeamCached()
{
return once(function () {
return ServiceDatabase::ownedByCurrentTeam()->get();
});
}
public function restart()
{
$container_id = $this->name.'-'.$this->service->uuid;
+14
View File
@@ -44,11 +44,25 @@ class StandaloneClickhouse extends BaseModel
});
}
/**
* Get query builder for ClickHouse databases owned by current team.
* If you need all databases without further query chaining, use ownedByCurrentTeamCached() instead.
*/
public static function ownedByCurrentTeam()
{
return StandaloneClickhouse::whereRelation('environment.project.team', 'id', currentTeam()->id)->orderBy('name');
}
/**
* Get all ClickHouse databases owned by current team (cached for request duration).
*/
public static function ownedByCurrentTeamCached()
{
return once(function () {
return StandaloneClickhouse::ownedByCurrentTeam()->get();
});
}
protected function serverStatus(): Attribute
{
return Attribute::make(
+14
View File
@@ -44,11 +44,25 @@ class StandaloneDragonfly extends BaseModel
});
}
/**
* Get query builder for Dragonfly databases owned by current team.
* If you need all databases without further query chaining, use ownedByCurrentTeamCached() instead.
*/
public static function ownedByCurrentTeam()
{
return StandaloneDragonfly::whereRelation('environment.project.team', 'id', currentTeam()->id)->orderBy('name');
}
/**
* Get all Dragonfly databases owned by current team (cached for request duration).
*/
public static function ownedByCurrentTeamCached()
{
return once(function () {
return StandaloneDragonfly::ownedByCurrentTeam()->get();
});
}
protected function serverStatus(): Attribute
{
return Attribute::make(
+14
View File
@@ -44,11 +44,25 @@ class StandaloneKeydb extends BaseModel
});
}
/**
* Get query builder for KeyDB databases owned by current team.
* If you need all databases without further query chaining, use ownedByCurrentTeamCached() instead.
*/
public static function ownedByCurrentTeam()
{
return StandaloneKeydb::whereRelation('environment.project.team', 'id', currentTeam()->id)->orderBy('name');
}
/**
* Get all KeyDB databases owned by current team (cached for request duration).
*/
public static function ownedByCurrentTeamCached()
{
return once(function () {
return StandaloneKeydb::ownedByCurrentTeam()->get();
});
}
protected function serverStatus(): Attribute
{
return Attribute::make(
+14
View File
@@ -45,11 +45,25 @@ class StandaloneMariadb extends BaseModel
});
}
/**
* Get query builder for MariaDB databases owned by current team.
* If you need all databases without further query chaining, use ownedByCurrentTeamCached() instead.
*/
public static function ownedByCurrentTeam()
{
return StandaloneMariadb::whereRelation('environment.project.team', 'id', currentTeam()->id)->orderBy('name');
}
/**
* Get all MariaDB databases owned by current team (cached for request duration).
*/
public static function ownedByCurrentTeamCached()
{
return once(function () {
return StandaloneMariadb::ownedByCurrentTeam()->get();
});
}
protected function serverStatus(): Attribute
{
return Attribute::make(
+14
View File
@@ -47,11 +47,25 @@ class StandaloneMongodb extends BaseModel
});
}
/**
* Get query builder for MongoDB databases owned by current team.
* If you need all databases without further query chaining, use ownedByCurrentTeamCached() instead.
*/
public static function ownedByCurrentTeam()
{
return StandaloneMongodb::whereRelation('environment.project.team', 'id', currentTeam()->id)->orderBy('name');
}
/**
* Get all MongoDB databases owned by current team (cached for request duration).
*/
public static function ownedByCurrentTeamCached()
{
return once(function () {
return StandaloneMongodb::ownedByCurrentTeam()->get();
});
}
protected function serverStatus(): Attribute
{
return Attribute::make(
+14
View File
@@ -45,11 +45,25 @@ class StandaloneMysql extends BaseModel
});
}
/**
* Get query builder for MySQL databases owned by current team.
* If you need all databases without further query chaining, use ownedByCurrentTeamCached() instead.
*/
public static function ownedByCurrentTeam()
{
return StandaloneMysql::whereRelation('environment.project.team', 'id', currentTeam()->id)->orderBy('name');
}
/**
* Get all MySQL databases owned by current team (cached for request duration).
*/
public static function ownedByCurrentTeamCached()
{
return once(function () {
return StandaloneMysql::ownedByCurrentTeam()->get();
});
}
protected function serverStatus(): Attribute
{
return Attribute::make(
+14
View File
@@ -45,11 +45,25 @@ class StandalonePostgresql extends BaseModel
});
}
/**
* Get query builder for PostgreSQL databases owned by current team.
* If you need all databases without further query chaining, use ownedByCurrentTeamCached() instead.
*/
public static function ownedByCurrentTeam()
{
return StandalonePostgresql::whereRelation('environment.project.team', 'id', currentTeam()->id)->orderBy('name');
}
/**
* Get all PostgreSQL databases owned by current team (cached for request duration).
*/
public static function ownedByCurrentTeamCached()
{
return once(function () {
return StandalonePostgresql::ownedByCurrentTeam()->get();
});
}
public function workdir()
{
return database_configuration_dir()."/{$this->uuid}";
+14
View File
@@ -46,11 +46,25 @@ class StandaloneRedis extends BaseModel
});
}
/**
* Get query builder for Redis databases owned by current team.
* If you need all databases without further query chaining, use ownedByCurrentTeamCached() instead.
*/
public static function ownedByCurrentTeam()
{
return StandaloneRedis::whereRelation('environment.project.team', 'id', currentTeam()->id)->orderBy('name');
}
/**
* Get all Redis databases owned by current team (cached for request duration).
*/
public static function ownedByCurrentTeamCached()
{
return once(function () {
return StandaloneRedis::ownedByCurrentTeam()->get();
});
}
protected function serverStatus(): Attribute
{
return Attribute::make(