-
-
Notifications
You must be signed in to change notification settings - Fork 254
Improve "first task" checks #1926
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
📝 WalkthroughWalkthroughReplaces sequence_id-based first-task checks with explicit helpers: adds Schedule::firstTask() and Task::isFirst(), updates TasksRelationManager to use isFirst() for form/table time_offset logic, and switches ProcessScheduleService to call firstTask() instead of a direct query. Changes
Sequence Diagram(s)sequenceDiagram
participant UI as Filament UI
participant TM as TasksRelationManager
participant Task as Task model
participant Schedule as Schedule model
participant Service as ProcessScheduleService
rect rgb(220,240,255)
note right of UI: Rendering form/table or processing schedule
UI->>TM: Render time_offset field / column
TM->>Task: Call isFirst()
Task->>Schedule: Call firstTask()
Schedule-->>Task: Return first task (by sequence_id)
Task-->>TM: isFirst() result
TM-->>UI: Hide/format time_offset based on isFirst()
end
rect rgb(240,255,230)
note right of Service: When processing schedule
Service->>Schedule: Call firstTask()
Schedule-->>Service: Return first task
Service->>Service: Continue processing with returned task
end
Possibly related PRs
Pre-merge checks❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
app/Filament/Server/Resources/Schedules/RelationManagers/TasksRelationManager.php
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (3)
app/Models/Task.php (1)
124-127: Clean implementation of first-task check.The logic correctly determines if the task is first in its schedule by comparing IDs. The null-safe operator properly handles the edge case when
firstTask()returns null.Optional performance optimization:
If
isFirst()is called frequently or on multiple tasks in a loop, consider eager-loading the schedule relationship to avoid N+1 queries. However, this is not critical for the current implementation.app/Models/Schedule.php (1)
146-152: Method works correctly but could leverage eager-loaded tasks.The implementation correctly returns the first task by
sequence_id.Optional optimization:
Since the Schedule model eager-loads tasks (line 52:
protected $with = ['tasks']), consider using the loaded collection to avoid an extra query:public function firstTask(): ?Task { - /** @var ?Task $task */ - $task = $this->tasks()->orderBy('sequence_id')->first(); - - return $task; + return $this->tasks->sortBy('sequence_id')->first(); }This eliminates a database query when tasks are already loaded. Note that
sortByon a collection returns a new collection, so the original remains unaffected.app/Filament/Server/Resources/Schedules/RelationManagers/TasksRelationManager.php (1)
75-75: Redundant first-task checks in hidden condition.The condition checks both
$get('sequence_id') === 1and$task?->isFirst(), which appear redundant given the PR's goal to replace sequence-based checks withisFirst().Consider simplifying to rely solely on
isFirst():-->hidden(fn (Get $get, ?Task $task) => config('queue.default') === 'sync' || $get('sequence_id') === 1 || $schedule->tasks->isEmpty() || $task?->isFirst()) +->hidden(fn (Get $get, ?Task $task) => config('queue.default') === 'sync' || $schedule->tasks->isEmpty() || $task?->isFirst())This aligns with the PR objective to use dynamic first-task detection instead of static
sequence_idcomparisons.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
app/Filament/Server/Resources/Schedules/RelationManagers/TasksRelationManager.php(2 hunks)app/Models/Schedule.php(1 hunks)app/Models/Task.php(1 hunks)app/Services/Schedules/ProcessScheduleService.php(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (4)
app/Services/Schedules/ProcessScheduleService.php (2)
app/Models/Task.php (1)
schedule(104-107)app/Models/Schedule.php (1)
firstTask(146-152)
app/Models/Task.php (1)
app/Models/Schedule.php (1)
firstTask(146-152)
app/Models/Schedule.php (1)
app/Models/Task.php (1)
Task(27-128)
app/Filament/Server/Resources/Schedules/RelationManagers/TasksRelationManager.php (2)
app/Models/Task.php (3)
Task(27-128)schedule(104-107)isFirst(124-127)app/Models/Schedule.php (1)
tasks(133-136)
🔇 Additional comments (2)
app/Services/Schedules/ProcessScheduleService.php (1)
24-24: LGTM! Clean refactoring to use the new abstraction.The change to use
$schedule->firstTask()encapsulates the first-task retrieval logic and maintains identical behavior.app/Filament/Server/Resources/Schedules/RelationManagers/TasksRelationManager.php (1)
111-112: I'll verify this by examining the actual file and searching for documentation about Filament's TextColumn behavior with null states.Now let me search for information about Laravel's
trans_choicefunction and how it handles null values:Let me search more specifically for Filament's suffix behavior with null states:
Now let me examine the actual code file to understand the full context:Perfect! I found the key source code reference. Now I have enough information to provide a comprehensive final response.
Based on my research, I can now provide an informed rewritten review comment.
Suffix callback behavior with null state is not explicitly safeguarded in Filament.
The concern raised is partially valid but based on incomplete information. Filament's date formatting checks if state is blank and returns null, suggesting callbacks can receive null values. While formatStateUsing() does not execute when state is null, the suffix callback behavior with null states is not explicitly documented for TextColumn.
However, passing null values to trans() causes deprecation warnings in Laravel 8.1+, so if the suffix callback is invoked with null,
trans_choice(null, $state)would indeed cause issues. Given that this appears to be a calculated/conditional column, a defensive approach is recommended:-->suffix(fn ($state) => $state !== null ? ' ' . trans_choice('server/schedule.tasks.seconds', $state) : '')Alternatively, ensure the state is never null by returning 0 for first tasks (e.g.,
$task->isFirst() ? 0 : $task->time_offset), which may be semantically appropriate.
app/Filament/Server/Resources/Schedules/RelationManagers/TasksRelationManager.php
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
♻️ Duplicate comments (1)
app/Filament/Server/Resources/Schedules/RelationManagers/TasksRelationManager.php (1)
75-75: First-task detection correctly implemented.The change to use
$task?->isFirst()correctly addresses the issue where the first task should not display a time offset field. The null-safe operator appropriately handles both creation and edit scenarios.Note: This matches the suggestion from a previous review comment, confirming the approach is sound.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
app/Filament/Server/Resources/Schedules/RelationManagers/TasksRelationManager.php(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
app/Filament/Server/Resources/Schedules/RelationManagers/TasksRelationManager.php (2)
app/Models/Task.php (3)
Task(27-128)schedule(104-107)isFirst(124-127)app/Models/Schedule.php (1)
tasks(133-136)
Closes #1922