From 63a07e7649a828557c6bccad2a2b50483abe1442 Mon Sep 17 00:00:00 2001 From: mahansky Date: Tue, 3 Sep 2024 20:06:03 +0200 Subject: [PATCH 1/3] deployment log improvements --- .../Project/Application/Deployment/Show.php | 20 ++++ bootstrap/helpers/remoteProcess.php | 32 ++++- .../application/deployment/show.blade.php | 111 +++++++++--------- 3 files changed, 108 insertions(+), 55 deletions(-) diff --git a/app/Livewire/Project/Application/Deployment/Show.php b/app/Livewire/Project/Application/Deployment/Show.php index 84a24255c..4b2904594 100644 --- a/app/Livewire/Project/Application/Deployment/Show.php +++ b/app/Livewire/Project/Application/Deployment/Show.php @@ -4,6 +4,7 @@ namespace App\Livewire\Project\Application\Deployment; use App\Models\Application; use App\Models\ApplicationDeploymentQueue; +use Illuminate\Support\Collection; use Livewire\Component; class Show extends Component @@ -12,6 +13,8 @@ class Show extends Component public ApplicationDeploymentQueue $application_deployment_queue; + public Collection $logLines; + public string $deployment_uuid; public $isKeepAliveOn = true; @@ -53,11 +56,13 @@ class Show extends Component $this->application = $application; $this->application_deployment_queue = $application_deployment_queue; $this->deployment_uuid = $deploymentUuid; + $this->buildLogLines(); } public function refreshQueue() { $this->application_deployment_queue->refresh(); + $this->buildLogLines(); } public function polling() @@ -67,10 +72,25 @@ class Show extends Component if (data_get($this->application_deployment_queue, 'status') == 'finished' || data_get($this->application_deployment_queue, 'status') == 'failed') { $this->isKeepAliveOn = false; } + $this->buildLogLines(); } public function render() { return view('livewire.project.application.deployment.show'); } + + private function buildLogLines() + { + $this->logLines = decode_remote_command_output($this->application_deployment_queue)->map(function ($logLine) { + $logLine['line'] = e($logLine['line']); + $logLine['line'] = preg_replace( + '/(https?:\/\/[^\s]+)/', + '$1', + $logLine['line'], + ); + + return $logLine; + }); + } } diff --git a/bootstrap/helpers/remoteProcess.php b/bootstrap/helpers/remoteProcess.php index 6ba7caeef..c3f8316e7 100644 --- a/bootstrap/helpers/remoteProcess.php +++ b/bootstrap/helpers/remoteProcess.php @@ -234,6 +234,7 @@ function decode_remote_command_output(?ApplicationDeploymentQueue $application_d return collect([]); } // ray($decoded ); + $seenCommands = collect(); $formatted = collect($decoded); if (! $is_debug_enabled) { $formatted = $formatted->filter(fn ($i) => $i['hidden'] === false ?? false); @@ -244,7 +245,36 @@ function decode_remote_command_output(?ApplicationDeploymentQueue $application_d data_set($i, 'timestamp', Carbon::parse(data_get($i, 'timestamp'))->format('Y-M-d H:i:s.u')); return $i; - }); + }) + ->reduce(function ($deploymentLogLines, $logItem) use ($seenCommands) { + $command = $logItem['command']; + $isStderr = $logItem['type'] === 'stderr'; + + if (! is_null($command) && ! $seenCommands->contains($command)) { + $deploymentLogLines->push([ + 'line' => $command, + 'timestamp' => $logItem['timestamp'], + 'stderr' => $isStderr, + 'hidden' => $logItem['hidden'], + 'command' => true, + ]); + + $seenCommands->push($command); + } + + $lines = explode(PHP_EOL, $logItem['output']); + + foreach ($lines as $line) { + $deploymentLogLines->push([ + 'line' => $line, + 'timestamp' => $logItem['timestamp'], + 'stderr' => $isStderr, + 'hidden' => $logItem['hidden'], + ]); + } + + return $deploymentLogLines; + }, collect()); return $formatted; } diff --git a/resources/views/livewire/project/application/deployment/show.blade.php b/resources/views/livewire/project/application/deployment/show.blade.php index f97914ec2..bc8ab3f34 100644 --- a/resources/views/livewire/project/application/deployment/show.blade.php +++ b/resources/views/livewire/project/application/deployment/show.blade.php @@ -9,6 +9,7 @@ fullscreen: false, alwaysScroll: false, intervalId: null, + showTimestamps: false, makeFullscreen() { this.fullscreen = !this.fullscreen; if (this.fullscreen === false) { @@ -53,63 +54,65 @@ class="dark:text-warning">{{ Str::headline(data_get($application_deployment_queue, 'status')) }}. @endif -
+
- - - + class="flex flex-col-reverse w-full p-2 px-4 mt-4 overflow-y-auto bg-white dark:text-white dark:bg-coolgray-100 scrollbar dark:border-coolgray-300" + :class="fullscreen ? '' : 'min-h-14 max-h-[40rem] border border-dotted rounded'" + > +
+
+ + + + + +
+
-
- @if (decode_remote_command_output($application_deployment_queue)->count() > 0) - @foreach (decode_remote_command_output($application_deployment_queue) as $line) - $line['hidden'], - 'text-red-500 font-bold whitespace-pre-line' => $line['type'] == 'stderr', - ])>[{{ $line['timestamp'] }}] @if ($line['hidden']) -

[COMMAND] {{ $line['command'] }}
[OUTPUT] - @endif @if (str($line['output'])->contains('http://') || str($line['output'])->contains('https://')) - @php - $line['output'] = preg_replace( - '/(https?:\/\/[^\s]+)/', - '$1', - $line['output'], - ); - @endphp {!! $line['output'] !!} - @else - {{ $line['output'] }} - - @endif -
- @endforeach - @else - No logs yet. - @endif + @forelse ($logLines as $line) +
$line['command'] ?? false, + 'flex gap-2 dark:hover:bg-coolgray-500 hover:bg-gray-100' + ]) + > + {{ $line['timestamp'] }} + $line['hidden'], + 'text-red-500' => $line['stderr'], + 'font-bold' => $line['command'] ?? false, + 'whitespace-pre-wrap', + ]) + >{!! $line['line'] !!} +
+ @empty + No logs yet. + @endforelse
From b0039885eb128d5bab116cb3f50651b749ae2a8c Mon Sep 17 00:00:00 2001 From: mahansky Date: Wed, 4 Sep 2024 03:39:50 +0200 Subject: [PATCH 2/3] use computed property --- .../Project/Application/Deployment/Show.php | 19 +++++++------------ .../application/deployment/show.blade.php | 2 +- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/app/Livewire/Project/Application/Deployment/Show.php b/app/Livewire/Project/Application/Deployment/Show.php index 4b2904594..f2968f6d9 100644 --- a/app/Livewire/Project/Application/Deployment/Show.php +++ b/app/Livewire/Project/Application/Deployment/Show.php @@ -13,8 +13,6 @@ class Show extends Component public ApplicationDeploymentQueue $application_deployment_queue; - public Collection $logLines; - public string $deployment_uuid; public $isKeepAliveOn = true; @@ -56,13 +54,11 @@ class Show extends Component $this->application = $application; $this->application_deployment_queue = $application_deployment_queue; $this->deployment_uuid = $deploymentUuid; - $this->buildLogLines(); } public function refreshQueue() { $this->application_deployment_queue->refresh(); - $this->buildLogLines(); } public function polling() @@ -72,17 +68,11 @@ class Show extends Component if (data_get($this->application_deployment_queue, 'status') == 'finished' || data_get($this->application_deployment_queue, 'status') == 'failed') { $this->isKeepAliveOn = false; } - $this->buildLogLines(); } - public function render() + public function getLogLinesProperty() { - return view('livewire.project.application.deployment.show'); - } - - private function buildLogLines() - { - $this->logLines = decode_remote_command_output($this->application_deployment_queue)->map(function ($logLine) { + return decode_remote_command_output($this->application_deployment_queue)->map(function ($logLine) { $logLine['line'] = e($logLine['line']); $logLine['line'] = preg_replace( '/(https?:\/\/[^\s]+)/', @@ -93,4 +83,9 @@ class Show extends Component return $logLine; }); } + + public function render() + { + return view('livewire.project.application.deployment.show'); + } } diff --git a/resources/views/livewire/project/application/deployment/show.blade.php b/resources/views/livewire/project/application/deployment/show.blade.php index bc8ab3f34..5ba0be0a2 100644 --- a/resources/views/livewire/project/application/deployment/show.blade.php +++ b/resources/views/livewire/project/application/deployment/show.blade.php @@ -93,7 +93,7 @@
- @forelse ($logLines as $line) + @forelse ($this->logLines as $line)
$line['command'] ?? false, From 3b533c7d065e66c2ed222d3bb7da935aa180ce8c Mon Sep 17 00:00:00 2001 From: mahansky Date: Wed, 4 Sep 2024 13:57:03 +0200 Subject: [PATCH 3/3] fix same commands different batch --- bootstrap/helpers/remoteProcess.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/bootstrap/helpers/remoteProcess.php b/bootstrap/helpers/remoteProcess.php index c3f8316e7..cccc41e71 100644 --- a/bootstrap/helpers/remoteProcess.php +++ b/bootstrap/helpers/remoteProcess.php @@ -249,8 +249,11 @@ function decode_remote_command_output(?ApplicationDeploymentQueue $application_d ->reduce(function ($deploymentLogLines, $logItem) use ($seenCommands) { $command = $logItem['command']; $isStderr = $logItem['type'] === 'stderr'; + $isNewCommand = ! is_null($command) && ! $seenCommands->first(function ($seenCommand) use ($logItem) { + return $seenCommand['command'] === $logItem['command'] && $seenCommand['batch'] === $logItem['batch']; + }); - if (! is_null($command) && ! $seenCommands->contains($command)) { + if ($isNewCommand) { $deploymentLogLines->push([ 'line' => $command, 'timestamp' => $logItem['timestamp'], @@ -259,7 +262,10 @@ function decode_remote_command_output(?ApplicationDeploymentQueue $application_d 'command' => true, ]); - $seenCommands->push($command); + $seenCommands->push([ + 'command' => $command, + 'batch' => $logItem['batch'], + ]); } $lines = explode(PHP_EOL, $logItem['output']);