fix: refactor tags view / remove obsolete one

This commit is contained in:
Andras Bacsai
2024-11-05 10:11:15 +01:00
parent 9b578b2003
commit ae7d88df9f
6 changed files with 118 additions and 241 deletions

View File

@@ -1,82 +0,0 @@
<?php
namespace App\Livewire\Tags;
use App\Http\Controllers\Api\DeployController;
use App\Models\Tag;
use Illuminate\Support\Collection;
use Livewire\Attributes\Title;
use Livewire\Attributes\Url;
use Livewire\Component;
#[Title('Tags | Coolify')]
class Index extends Component
{
#[Url()]
public ?string $tag = null;
public Collection $tags;
public Collection $applications;
public Collection $services;
public $webhook = null;
public $deploymentsPerTagPerServer = [];
protected $listeners = ['deployments' => 'updateDeployments'];
public function render()
{
return view('livewire.tags.index');
}
public function mount()
{
$this->tags = Tag::ownedByCurrentTeam()->get()->unique('name')->sortBy('name');
if ($this->tag) {
$this->tagUpdated();
}
}
public function updateDeployments($deployments)
{
$this->deploymentsPerTagPerServer = $deployments;
}
public function tagUpdated()
{
if ($this->tag === '') {
return;
}
$sanitizedTag = htmlspecialchars($this->tag, ENT_QUOTES, 'UTF-8');
$tag = $this->tags->where('name', $sanitizedTag)->first();
if (! $tag) {
$this->dispatch('error', 'Tag ('.e($sanitizedTag).') not found.');
$this->tag = '';
return;
}
$this->webhook = generateTagDeployWebhook($tag->name);
$this->applications = $tag->applications()->get();
$this->services = $tag->services()->get();
}
public function redeployAll()
{
try {
$this->applications->each(function ($resource) {
$deploy = new DeployController;
$deploy->deploy_resource($resource);
});
$this->services->each(function ($resource) {
$deploy = new DeployController;
$deploy->deploy_resource($resource);
});
$this->dispatch('success', 'Mass deployment started.');
} catch (\Exception $e) {
return handleError($e, $this);
}
}
}

View File

@@ -5,43 +5,57 @@ namespace App\Livewire\Tags;
use App\Http\Controllers\Api\DeployController;
use App\Models\ApplicationDeploymentQueue;
use App\Models\Tag;
use Illuminate\Support\Collection;
use Livewire\Attributes\Locked;
use Livewire\Attributes\Title;
use Livewire\Component;
#[Title('Tags | Coolify')]
class Show extends Component
{
public $tags;
#[Locked]
public ?string $tagName = null;
public Tag $tag;
#[Locked]
public ?Collection $tags = null;
public $applications;
#[Locked]
public ?Tag $tag = null;
public $services;
#[Locked]
public ?Collection $applications = null;
public $webhook = null;
#[Locked]
public ?Collection $services = null;
public $deployments_per_tag_per_server = [];
#[Locked]
public ?string $webhook = null;
#[Locked]
public ?array $deploymentsPerTagPerServer = null;
public function mount()
{
$this->tags = Tag::ownedByCurrentTeam()->get()->unique('name')->sortBy('name');
$tag = $this->tags->where('name', request()->tag_name)->first();
if (! $tag) {
return redirect()->route('tags.index');
try {
$this->tags = Tag::ownedByCurrentTeam()->get()->unique('name')->sortBy('name');
if (str($this->tagName)->isNotEmpty()) {
$tag = $this->tags->where('name', $this->tagName)->first();
$this->webhook = generateTagDeployWebhook($tag->name);
$this->applications = $tag->applications()->get();
$this->services = $tag->services()->get();
$this->tag = $tag;
$this->getDeployments();
}
} catch (\Exception $e) {
return handleError($e, $this);
}
$this->webhook = generateTagDeployWebhook($tag->name);
$this->applications = $tag->applications()->get();
$this->services = $tag->services()->get();
$this->tag = $tag;
$this->get_deployments();
}
public function get_deployments()
public function getDeployments()
{
try {
$resource_ids = $this->applications->pluck('id');
$this->deployments_per_tag_per_server = ApplicationDeploymentQueue::whereIn('status', ['in_progress', 'queued'])->whereIn('application_id', $resource_ids)->get([
$this->deploymentsPerTagPerServer = ApplicationDeploymentQueue::whereIn('status', ['in_progress', 'queued'])->whereIn('application_id', $resource_ids)->get([
'id',
'application_id',
'application_name',
@@ -56,7 +70,7 @@ class Show extends Component
}
}
public function redeploy_all()
public function redeployAll()
{
try {
$message = collect([]);

View File

@@ -215,7 +215,7 @@
<li>
<a title="Tags"
class="{{ request()->is('tags*') ? 'menu-item-active menu-item' : 'menu-item' }}"
href="{{ route('tags.index') }}">
href="{{ route('tags.show') }}">
<svg class="icon" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<g fill="none" stroke="currentColor" stroke-linecap="round"
stroke-linejoin="round" stroke-width="2">

View File

@@ -1,64 +0,0 @@
<div>
<h1>Tags</h1>
<div class="flex flex-col pb-6">
<div class="subtitle">Tags help you to perform actions on multiple resources.</div>
<div class="">
@if ($tags->count() === 0)
<div>No tags yet defined yet. Go to a resource and add a tag there.</div>
@else
<x-forms.datalist wire:model="tag" onUpdate='tagUpdated'>
@foreach ($tags as $oneTag)
<option value="{{ $oneTag->name }}">{{ $oneTag->name }}</option>
@endforeach
</x-forms.datalist>
@if ($tag)
<div class="pt-5">
<div class="flex items-end gap-2 ">
<div class="w-[500px]">
<x-forms.input readonly label="Deploy Webhook URL" id="webhook" />
</div>
<x-modal-confirmation title="Redeploy all resources with this tag?" isHighlighted
buttonTitle="Redeploy All" submitAction="redeployAll" :actions="[
'All resources with this tag will be redeployed.',
'During redeploy resources will be temporarily unavailable.',
]"
confirmationText="{{ $tag }}"
confirmationLabel="Please confirm the execution of the actions by entering the Tag Name below"
shortConfirmationLabel="Tag Name" :confirmWithPassword="false" step2ButtonText="Redeploy All" />
</div>
<div class="grid grid-cols-1 gap-2 pt-4 lg:grid-cols-2 xl:grid-cols-3">
@foreach ($applications as $application)
<a href="{{ $application->link() }}"class="box group">
<div class="flex flex-col">
<div class="box-title">{{ $application->name }}</div>
<div class="box-description">
{{ $application->project()->name }}/{{ $application->environment->name }}
</div>
<div class="box-description">{{ $application->description }}</div>
</div>
</a>
@endforeach
@foreach ($services as $service)
<a href="{{ $service->link() }}" class="box group">
<div class="flex flex-col ">
<div class="box-title">{{ $service->name }}</div>
<div class="box-description">
{{ $service->project()->name }}/{{ $service->environment->name }}</div>
<div class="box-description">{{ $service->description }}</div>
</div>
</a>
@endforeach
</div>
<div class="flex items-center gap-2">
<h3 class="py-4">Deployments</h3>
@if (count($deploymentsPerTagPerServer) > 0)
<x-loading />
@endif
</div>
<livewire:tags.deployments :deploymentsPerTagPerServer="$deploymentsPerTagPerServer" :resourceIds="$applications->pluck('id')" />
</div>
@endif
@endif
</div>
</div>
</div>

View File

@@ -1,87 +1,96 @@
<div>
<div class="flex items-start gap-2">
<div class="flex items-start gap-2 pb-10">
<div>
<h1>Tags</h1>
<div>Tags help you to perform actions on multiple resources.</div>
</div>
</div>
<div class="flex flex-col gap-2 pb-6 ">
<div>Available tags</div>
<div class="flex flex-wrap gap-2 ">
@forelse ($tags as $oneTag)
<a :class="{{ $tag->id == $oneTag->id }} && 'bg-coollabs hover:bg-coollabs-100'" class="w-64 box"
href="{{ route('tags.show', ['tag_name' => $oneTag->name]) }}">{{ $oneTag->name }}</a>
@empty
<div>No tags yet defined yet. Go to a resource and add a tag there.</div>
@endforelse
</div>
<div class="flex flex-wrap gap-2 ">
@forelse ($tags as $oneTag)
<a wire:navigate :class="{{ $tag?->id == $oneTag->id }} && 'dark:bg-coollabs hover:bg-coollabs-100'"
class="w-64 box-without-bg dark:bg-coolgray-100 dark:text-white font-bold"
href="{{ route('tags.show', ['tagName' => $oneTag->name]) }}">{{ $oneTag->name }}</a>
@empty
<div>No tags yet defined yet. Go to a resource and add a tag there.</div>
@endforelse
</div>
<div>
<h3 class="py-4">Details</h3>
<div class="flex items-end gap-2 ">
<div class="w-[500px]">
<x-forms.input readonly label="Deploy Webhook URL" id="webhook" />
@if (isset($tag))
<div>
<h3 class="py-4">Details</h3>
<div class="flex items-end gap-2 ">
<div class="w-[500px]">
<x-forms.input readonly label="Deploy Webhook URL" id="webhook" />
</div>
<x-modal-confirmation title="Redeploy all resources with this tag?" isHighlighted
buttonTitle="Redeploy All" submitAction="redeployAll" :actions="[
'All resources with this tag will be redeployed.',
'During redeploy resources will be temporarily unavailable.',
]"
confirmationText="{{ $tag->name }}"
confirmationLabel="Please confirm the execution of the actions by entering the Tag Name below"
shortConfirmationLabel="Tag Name" :confirmWithPassword="false" step2ButtonText="Redeploy All" />
</div>
<x-modal-confirmation title="Redeploy all resources with this tag?" isHighlighted buttonTitle="Redeploy All"
submitAction="redeploy_all" :actions="[
'All resources with this tag will be redeployed.',
'During redeploy resources will be temporarily unavailable.',
]" confirmationText="{{ $tag }}"
confirmationLabel="Please confirm the execution of the actions by entering the Tag Name below"
shortConfirmationLabel="Tag Name" :confirmWithPassword="false" step2ButtonText="Redeploy All" />
</div>
<div class="grid grid-cols-1 gap-2 pt-4 lg:grid-cols-2 xl:grid-cols-3">
@foreach ($applications as $application)
<a href="{{ $application->link() }}" class="box group">
<div class="flex flex-col">
<div class="box-title">{{ $application->project()->name }}/{{ $application->environment->name }}
</div>
<div class="box-description">{{ $application->name }}</div>
<div class="box-description">{{ $application->description }}</div>
</div>
</a>
@endforeach
@foreach ($services as $service)
<a href="{{ $service->link() }}" class="flex flex-col box group">
<div class="flex flex-col">
<div class="box-title">{{ $service->project()->name }}/{{ $service->environment->name }}
</div>
<div class="box-description">{{ $service->name }}</div>
<div class="box-description">{{ $service->description }}</div>
</div>
</a>
@endforeach
</div>
<div class="flex items-center gap-2">
<h3 class="py-4">Deployments</h3>
@if (count($deployments_per_tag_per_server) > 0)
<x-loading />
@endif
</div>
<div wire:poll.1000ms="get_deployments" class="grid grid-cols-1">
@forelse ($deployments_per_tag_per_server as $server_name => $deployments)
<h4 class="py-4">{{ $server_name }}</h4>
<div class="grid grid-cols-1 gap-2 lg:grid-cols-3">
@foreach ($deployments as $deployment)
<a href="{{ data_get($deployment, 'deployment_url') }}" @class([
'gap-2 cursor-pointer box group border-l-2 border-dotted',
'dark:border-coolgray-300' => data_get($deployment, 'status') === 'queued',
'border-yellow-500' => data_get($deployment, 'status') === 'in_progress',
])>
<div class="flex flex-col mx-6">
<div class="font-bold dark:text-white">
{{ data_get($deployment, 'application_name') }}
</div>
<div class="description">
{{ str(data_get($deployment, 'status'))->headline() }}
<div class="grid grid-cols-1 gap-2 pt-4 lg:grid-cols-2 xl:grid-cols-3">
@if (isset($applications) && count($applications) > 0)
@foreach ($applications as $application)
<a href="{{ $application->link() }}" class="box group">
<div class="flex flex-col">
<div class="box-title">
{{ $application->project()->name }}/{{ $application->environment->name }}
</div>
<div class="box-description">{{ $application->name }}</div>
<div class="box-description">{{ $application->description }}</div>
</div>
<div class="flex-1"></div>
</a>
@endforeach
</div>
@empty
<div>No deployments running.</div>
@endforelse
@endif
@if (isset($services) && count($services) > 0)
@foreach ($services as $service)
<a href="{{ $service->link() }}" class="flex flex-col box group">
<div class="flex flex-col">
<div class="box-title">
{{ $service->project()->name }}/{{ $service->environment->name }}
</div>
<div class="box-description">{{ $service->name }}</div>
<div class="box-description">{{ $service->description }}</div>
</div>
</a>
@endforeach
@endif
</div>
<div class="flex items-center gap-2">
<h3 class="py-4">Deployments</h3>
@if (count($deploymentsPerTagPerServer) > 0)
<x-loading />
@endif
</div>
<div wire:poll="getDeployments" class="grid grid-cols-1">
@forelse ($deploymentsPerTagPerServer as $serverName => $deployments)
<h4 class="py-4">{{ $serverName }}</h4>
<div class="grid grid-cols-1 gap-2 lg:grid-cols-3">
@foreach ($deployments as $deployment)
<a href="{{ data_get($deployment, 'deployment_url') }}" @class([
'gap-2 cursor-pointer box group border-l-2 border-dotted',
'dark:border-coolgray-300' => data_get($deployment, 'status') === 'queued',
'border-yellow-500' => data_get($deployment, 'status') === 'in_progress',
])>
<div class="flex flex-col mx-6">
<div class="font-bold dark:text-white">
{{ data_get($deployment, 'application_name') }}
</div>
<div class="description">
{{ str(data_get($deployment, 'status'))->headline() }}
</div>
</div>
<div class="flex-1"></div>
</a>
@endforeach
</div>
@empty
<div>No deployments running.</div>
@endforelse
</div>
</div>
</div>
@endif
</div>

View File

@@ -127,8 +127,8 @@ Route::middleware(['auth', 'verified'])->group(function () {
Route::get('/profile', ProfileIndex::class)->name('profile');
Route::prefix('tags')->group(function () {
Route::get('/', TagsIndex::class)->name('tags.index');
Route::get('/{tag_name}', TagsShow::class)->name('tags.show');
// Route::get('/', TagsIndex::class)->name('tags.index');
Route::get('/{tagName?}', TagsShow::class)->name('tags.show');
});
Route::prefix('notifications')->group(function () {