coolify/app/Livewire/Project/Resource/Index.php

199 lines
7.1 KiB
PHP
Raw Normal View History

2024-01-07 15:23:41 +00:00
<?php
namespace App\Livewire\Project\Resource;
use App\Models\Environment;
use App\Models\Project;
use Illuminate\Support\Collection;
2024-01-07 15:23:41 +00:00
use Livewire\Component;
class Index extends Component
{
public Project $project;
2024-06-10 20:43:34 +00:00
2024-01-07 15:23:41 +00:00
public Environment $environment;
2024-06-10 20:43:34 +00:00
public Collection $allProjects;
2024-06-10 20:43:34 +00:00
public Collection $allEnvironments;
2024-06-10 20:43:34 +00:00
public array $parameters;
2024-06-10 20:43:34 +00:00
protected Collection $applications;
2024-06-10 20:43:34 +00:00
protected Collection $postgresqls;
2024-06-10 20:43:34 +00:00
protected Collection $redis;
2024-06-10 20:43:34 +00:00
protected Collection $mongodbs;
2024-06-10 20:43:34 +00:00
protected Collection $mysqls;
2024-06-10 20:43:34 +00:00
protected Collection $mariadbs;
2024-06-10 20:43:34 +00:00
protected Collection $keydbs;
2024-06-10 20:43:34 +00:00
protected Collection $dragonflies;
protected Collection $clickhouses;
protected Collection $services;
public function mount(): void
2024-01-12 10:25:20 +00:00
{
$this->applications = $this->postgresqls = $this->redis = $this->mongodbs = $this->mysqls = $this->mariadbs = $this->keydbs = $this->dragonflies = $this->clickhouses = $this->services = collect();
$this->parameters = get_route_parameters();
$project = currentTeam()
->projects()
->select('id', 'uuid', 'team_id', 'name')
->where('uuid', request()->route('project_uuid'))
->firstOrFail();
$environment = $project->environments()
->select('id', 'uuid', 'name', 'project_id')
->where('uuid', request()->route('environment_uuid'))
->firstOrFail();
2024-01-07 15:23:41 +00:00
$this->project = $project;
// Load projects and environments for breadcrumb navigation
$this->allProjects = Project::ownedByCurrentTeamCached();
$this->allEnvironments = $project->environments()
->select('id', 'uuid', 'name', 'project_id')
->with([
'applications:id,uuid,name,environment_id',
'services:id,uuid,name,environment_id',
'postgresqls:id,uuid,name,environment_id',
'redis:id,uuid,name,environment_id',
'mongodbs:id,uuid,name,environment_id',
'mysqls:id,uuid,name,environment_id',
'mariadbs:id,uuid,name,environment_id',
'keydbs:id,uuid,name,environment_id',
'dragonflies:id,uuid,name,environment_id',
'clickhouses:id,uuid,name,environment_id',
])
->get();
2024-12-11 16:09:53 +00:00
$this->environment = $environment->loadCount([
'applications',
'redis',
'postgresqls',
'mysqls',
'keydbs',
'dragonflies',
'clickhouses',
'mariadbs',
'mongodbs',
'services',
]);
// Eager load relationships for applications
2024-12-11 16:09:53 +00:00
$this->applications = $this->environment->applications()->with([
'tags',
'destination.server.settings',
'settings',
])->get()->sortBy('name');
$projectUuid = $this->project->uuid;
$environmentUuid = $this->environment->uuid;
$this->applications = $this->applications->map(function ($application) use ($projectUuid, $environmentUuid) {
2024-12-11 16:09:53 +00:00
$application->hrefLink = route('project.application.configuration', [
'project_uuid' => $projectUuid,
'environment_uuid' => $environmentUuid,
'application_uuid' => $application->uuid,
2024-12-11 16:09:53 +00:00
]);
2024-06-10 20:43:34 +00:00
2024-01-12 10:25:20 +00:00
return $application;
});
2024-06-10 20:43:34 +00:00
2024-12-11 16:09:53 +00:00
// Load all database resources in a single query per type
$databaseTypes = [
'postgresqls' => 'postgresqls',
'redis' => 'redis',
'mongodbs' => 'mongodbs',
'mysqls' => 'mysqls',
'mariadbs' => 'mariadbs',
'keydbs' => 'keydbs',
'dragonflies' => 'dragonflies',
'clickhouses' => 'clickhouses',
];
foreach ($databaseTypes as $property => $relation) {
$this->{$property} = $this->environment->{$relation}()->with([
'tags',
'destination.server.settings',
])->get()->sortBy('name');
$this->{$property} = $this->{$property}->map(function ($db) use ($projectUuid, $environmentUuid) {
2024-12-11 16:09:53 +00:00
$db->hrefLink = route('project.database.configuration', [
'project_uuid' => $projectUuid,
2024-12-11 16:09:53 +00:00
'database_uuid' => $db->uuid,
'environment_uuid' => $environmentUuid,
2024-01-12 10:25:20 +00:00
]);
2024-12-11 16:09:53 +00:00
return $db;
});
}
2024-06-10 20:43:34 +00:00
2024-12-11 16:09:53 +00:00
// Load services with their tags and server
$this->services = $this->environment->services()->with([
'tags',
'destination.server.settings',
])->get()->sortBy('name');
$this->services = $this->services->map(function ($service) use ($projectUuid, $environmentUuid) {
2024-12-11 16:09:53 +00:00
$service->hrefLink = route('project.service.configuration', [
'project_uuid' => $projectUuid,
'environment_uuid' => $environmentUuid,
'service_uuid' => $service->uuid,
2024-12-11 16:09:53 +00:00
]);
2024-01-12 10:25:20 +00:00
return $service;
});
2024-01-07 15:23:41 +00:00
}
2024-06-10 20:43:34 +00:00
2024-01-07 15:23:41 +00:00
public function render()
{
return view('livewire.project.resource.index', [
'applications' => $this->applications,
'postgresqls' => $this->postgresqls,
'redis' => $this->redis,
'mongodbs' => $this->mongodbs,
'mysqls' => $this->mysqls,
'mariadbs' => $this->mariadbs,
'keydbs' => $this->keydbs,
'dragonflies' => $this->dragonflies,
'clickhouses' => $this->clickhouses,
'services' => $this->services,
'applicationsJs' => $this->toSearchableArray($this->applications),
'postgresqlsJs' => $this->toSearchableArray($this->postgresqls),
'redisJs' => $this->toSearchableArray($this->redis),
'mongodbsJs' => $this->toSearchableArray($this->mongodbs),
'mysqlsJs' => $this->toSearchableArray($this->mysqls),
'mariadbsJs' => $this->toSearchableArray($this->mariadbs),
'keydbsJs' => $this->toSearchableArray($this->keydbs),
'dragonfliesJs' => $this->toSearchableArray($this->dragonflies),
'clickhousesJs' => $this->toSearchableArray($this->clickhouses),
'servicesJs' => $this->toSearchableArray($this->services),
]);
}
private function toSearchableArray(Collection $items): array
{
return $items->map(fn ($item) => [
'uuid' => $item->uuid,
'name' => $item->name,
'fqdn' => $item->fqdn ?? null,
'description' => $item->description ?? null,
'status' => $item->status ?? '',
'server_status' => $item->server_status ?? null,
'hrefLink' => $item->hrefLink ?? '',
'destination' => [
'server' => [
'name' => $item->destination?->server?->name ?? 'Unknown',
],
],
'tags' => $item->tags->map(fn ($tag) => [
'id' => $tag->id,
'name' => $tag->name,
])->values()->toArray(),
])->values()->toArray();
2024-01-07 15:23:41 +00:00
}
}