From 4b89b84353f08dbe04a514fcb4992e0ab284754d Mon Sep 17 00:00:00 2001 From: Philip Heinser Date: Sun, 8 Dec 2024 15:26:51 +0700 Subject: [PATCH] paginate task logs --- .../Shared/ScheduledTask/Executions.php | 60 ++++++++++++++++++- .../scheduled-task/executions.blade.php | 34 ++++++++++- 2 files changed, 90 insertions(+), 4 deletions(-) diff --git a/app/Livewire/Project/Shared/ScheduledTask/Executions.php b/app/Livewire/Project/Shared/ScheduledTask/Executions.php index 0710e37ff..dae1733f5 100644 --- a/app/Livewire/Project/Shared/ScheduledTask/Executions.php +++ b/app/Livewire/Project/Shared/ScheduledTask/Executions.php @@ -24,6 +24,11 @@ class Executions extends Component #[Locked] public ?string $serverTimezone = null; + public $currentPage = 1; + public $logsPerPage = 100; + public $selectedExecution = null; + public $isPollingActive = false; + public function getListeners() { $teamId = Auth::user()->currentTeam()->id; @@ -54,16 +59,69 @@ class Executions extends Component public function refreshExecutions(): void { $this->executions = $this->task->executions()->take(20)->get(); + if ($this->selectedKey) { + $this->selectedExecution = $this->task->executions()->find($this->selectedKey); + if ($this->selectedExecution && $this->selectedExecution->status !== 'running') { + $this->isPollingActive = false; + } + } } public function selectTask($key): void { if ($key == $this->selectedKey) { $this->selectedKey = null; - + $this->selectedExecution = null; + $this->currentPage = 1; + $this->isPollingActive = false; return; } $this->selectedKey = $key; + $this->selectedExecution = $this->task->executions()->find($key); + $this->currentPage = 1; + + // Start polling if task is running + if ($this->selectedExecution && $this->selectedExecution->status === 'running') { + $this->isPollingActive = true; + } + } + + public function polling() + { + if ($this->selectedExecution && $this->isPollingActive) { + $this->selectedExecution->refresh(); + if ($this->selectedExecution->status !== 'running') { + $this->isPollingActive = false; + } + } + } + + public function loadMoreLogs() + { + $this->currentPage++; + } + + public function getLogLinesProperty() + { + if (!$this->selectedExecution) { + return collect(); + } + + if (!$this->selectedExecution->message) { + return collect(['Waiting for task output...']); + } + + $lines = collect(explode("\n", $this->selectedExecution->message)); + return $lines->take($this->currentPage * $this->logsPerPage); + } + + public function hasMoreLogs() + { + if (!$this->selectedExecution || !$this->selectedExecution->message) { + return false; + } + $lines = collect(explode("\n", $this->selectedExecution->message)); + return $lines->count() > ($this->currentPage * $this->logsPerPage); } public function formatDateInServerTimezone($date) diff --git a/resources/views/livewire/project/shared/scheduled-task/executions.blade.php b/resources/views/livewire/project/shared/scheduled-task/executions.blade.php index fcb4215de..2ded710a6 100644 --- a/resources/views/livewire/project/shared/scheduled-task/executions.blade.php +++ b/resources/views/livewire/project/shared/scheduled-task/executions.blade.php @@ -1,4 +1,18 @@ -
+