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

137 lines
4.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 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
2024-01-12 10:25:20 +00:00
public $applications = [];
2024-06-10 20:43:34 +00:00
2024-01-12 10:25:20 +00:00
public $postgresqls = [];
2024-06-10 20:43:34 +00:00
2024-01-12 10:25:20 +00:00
public $redis = [];
2024-06-10 20:43:34 +00:00
2024-01-12 10:25:20 +00:00
public $mongodbs = [];
2024-06-10 20:43:34 +00:00
2024-01-12 10:25:20 +00:00
public $mysqls = [];
2024-06-10 20:43:34 +00:00
2024-01-12 10:25:20 +00:00
public $mariadbs = [];
2024-06-10 20:43:34 +00:00
2024-04-10 13:00:46 +00:00
public $keydbs = [];
2024-06-10 20:43:34 +00:00
2024-04-10 13:00:46 +00:00
public $dragonflies = [];
2024-06-10 20:43:34 +00:00
2024-04-10 13:00:46 +00:00
public $clickhouses = [];
2024-06-10 20:43:34 +00:00
2024-01-12 10:25:20 +00:00
public $services = [];
2024-06-10 20:43:34 +00:00
public array $parameters;
2024-01-12 10:25:20 +00:00
public function mount()
{
$this->parameters = get_route_parameters();
2024-01-07 15:23:41 +00:00
$project = currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
2024-06-10 20:43:34 +00:00
if (! $project) {
2024-01-07 15:23:41 +00:00
return redirect()->route('dashboard');
}
$environment = $project->load(['environments'])->environments->where('name', request()->route('environment_name'))->first();
2024-06-10 20:43:34 +00:00
if (! $environment) {
2024-01-07 15:23:41 +00:00
return redirect()->route('dashboard');
}
$this->project = $project;
2024-12-11 16:09:53 +00:00
$this->environment = $environment->loadCount([
'applications',
'redis',
'postgresqls',
'mysqls',
'keydbs',
'dragonflies',
'clickhouses',
'mariadbs',
'mongodbs',
'services',
]);
// Eager load all relationships for applications including nested ones
$this->applications = $this->environment->applications()->with([
'tags',
'additional_servers.settings',
'additional_networks',
'destination.server.settings',
'settings',
])->get()->sortBy('name');
2024-01-12 10:25:20 +00:00
$this->applications = $this->applications->map(function ($application) {
2024-12-11 16:09:53 +00:00
$application->hrefLink = route('project.application.configuration', [
'project_uuid' => $this->project->uuid,
'application_uuid' => $application->uuid,
'environment_name' => $this->environment->name,
]);
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',
];
// Load all server-related data first to prevent duplicate queries
$serverData = $this->environment->applications()
->with(['destination.server.settings'])
->get()
->pluck('destination.server')
->filter()
->unique('id');
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) {
$db->hrefLink = route('project.database.configuration', [
'project_uuid' => $this->project->uuid,
'database_uuid' => $db->uuid,
'environment_name' => $this->environment->name,
2024-01-12 10:25:20 +00:00
]);
2024-06-10 20:43:34 +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');
2024-01-12 10:25:20 +00:00
$this->services = $this->services->map(function ($service) {
2024-12-11 16:09:53 +00:00
$service->hrefLink = route('project.service.configuration', [
'project_uuid' => $this->project->uuid,
'service_uuid' => $service->uuid,
'environment_name' => $this->environment->name,
]);
2024-06-10 20:43:34 +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');
}
}