Skip to content
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

#400 - fixed n+1 queries in app #407

Merged
merged 5 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/Architecture/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Toby\Architecture\Providers;

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\ChannelManager;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Notification;
Expand All @@ -22,6 +23,7 @@ public function register(): void

public function boot(): void
{
Model::preventLazyLoading(!app()->isProduction());
Carbon::macro("toDisplayString", fn(): string => $this->translatedFormat("d.m.Y"));
Carbon::macro("toDisplayDateTimeString", fn(): string => $this->translatedFormat("d.m.Y H:i:s"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ protected function notifyTechApprovers(VacationRequest $vacationRequest): void
{
$users = Permission::findByName("receiveVacationRequestWaitsForApprovalNotification")
->users()
->with("permissions")
->get();

$users = $users->filter(fn(User $user): bool => $user->can("acceptAsTechApprover", $vacationRequest));
Expand Down
2 changes: 1 addition & 1 deletion app/Domain/CalendarGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected function getVacationsForPeriod(CarbonPeriod $period): Collection
->orWhere(function ($query): void {
$query->pending();
})
->with("vacationRequest")
->with("vacationRequest.vacations")
->get()
->groupBy(fn(Vacation $vacation): string => $vacation->date->toDateString());
}
Expand Down
8 changes: 4 additions & 4 deletions app/Domain/DailySummaryRetriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(
public function getAbsences(Carbon $date): Collection
{
return VacationRequest::query()
->with(["user"])
->with(["user", "vacations"])
->whereDate("from", "<=", $date)
->whereDate("to", ">=", $date)
->states(VacationRequestStatesRetriever::notFailedStates())
Expand All @@ -41,7 +41,7 @@ public function getAbsences(Carbon $date): Collection
public function getRemoteDays(Carbon $date): Collection
{
return VacationRequest::query()
->with(["user"])
->with(["user", "vacations"])
->whereDate("from", "<=", $date)
->whereDate("to", ">=", $date)
->states(VacationRequestStatesRetriever::notFailedStates())
Expand All @@ -59,7 +59,7 @@ public function getRemoteDays(Carbon $date): Collection
public function getUpcomingAbsences(Carbon $date): Collection
{
return VacationRequest::query()
->with(["user"])
->with(["user", "vacations"])
->whereDate("from", ">", $date)
->states(VacationRequestStatesRetriever::notFailedStates())
->whereIn(
Expand All @@ -77,7 +77,7 @@ public function getUpcomingAbsences(Carbon $date): Collection
public function getUpcomingRemoteDays(Carbon $date): Collection
{
return VacationRequest::query()
->with(["user"])
->with(["user", "vacations"])
->whereDate("from", ">", $date)
->states(VacationRequestStatesRetriever::notFailedStates())
->whereIn(
Expand Down
8 changes: 4 additions & 4 deletions app/Domain/DashboardAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function aggregateCalendarData(User $user, YearPeriod $yearPeriod): array
->get()
->mapWithKeys(
fn(Vacation $vacation): array => [
$vacation->date->toDateString() => new DashboardVacationRequestResource($vacation->vacationRequest),
$vacation->date->toDateString() => new DashboardVacationRequestResource($vacation->vacationRequest->load(["user", "vacations"])),
],
);

Expand All @@ -67,7 +67,7 @@ public function aggregateCalendarData(User $user, YearPeriod $yearPeriod): array
->get()
->mapWithKeys(
fn(Vacation $vacation): array => [
$vacation->date->toDateString() => new DashboardVacationRequestResource($vacation->vacationRequest),
$vacation->date->toDateString() => new DashboardVacationRequestResource($vacation->vacationRequest->load(["user", "vacations"])),
],
);

Expand All @@ -86,14 +86,14 @@ public function aggregateVacationRequests(User $user, YearPeriod $yearPeriod): J
{
if ($user->can("listAllRequests")) {
$vacationRequests = $yearPeriod->vacationRequests()
->with(["user"])
->with(["user", "vacations"])
->states(VacationRequestStatesRetriever::waitingForUserActionStates($user))
->latest("updated_at")
->limit(3)
->get();
} else {
$vacationRequests = $user->vacationRequests()
->with(["user"])
->with(["user", "vacations"])
->whereBelongsTo($yearPeriod)
->latest("updated_at")
->limit(3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function index(Request $request): RedirectResponse|Response
$searchQuery = $request->query("search");

$equipmentItems = EquipmentItem::query()
->with("assignee")
->search($searchQuery)
->when(
$request->query("assignee") && $request->query("assignee") !== "unassigned",
Expand Down Expand Up @@ -72,6 +73,7 @@ public function indexForEmployee(Request $request): RedirectResponse|Response
$searchQuery = $request->query("search");

$equipmentItems = EquipmentItem::query()
->with("assignee")
->search($searchQuery)
->labels($request->query("labels"))
->where("assignee_id", $request->user()->id)
Expand Down Expand Up @@ -172,7 +174,7 @@ public function downloadExcel(): BinaryFileResponse
{
$this->authorize("manageEquipment");

$equipmentItems = EquipmentItem::query()->get();
$equipmentItems = EquipmentItem::query()->with("assignee")->get();

$equipmentExport = new EquipmentExport($equipmentItems);

Expand Down
1 change: 1 addition & 0 deletions app/Infrastructure/Http/Controllers/KeysController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class KeysController extends Controller
public function index(Request $request): Response
{
$keys = Key::query()
->with("user")
->get()
->sortBy("id");

Expand Down
1 change: 1 addition & 0 deletions app/Infrastructure/Http/Controllers/ResumeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function index(): Response
$this->authorize("manageResumes");

$resumes = Resume::query()
->with("user")
->latest("updated_at")
->paginate();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function index(Request $request, YearPeriodRetriever $yearPeriodRetriever

$vacationRequests = $request->user()
->vacationRequests()
->with("vacations")
->with(["user", "vacations"])
->whereBelongsTo($yearPeriodRetriever->selected())
->latest()
->states(VacationRequestStatesRetriever::filterByStatusGroup($status, $request->user()))
Expand Down Expand Up @@ -131,6 +131,7 @@ public function show(VacationRequest $vacationRequest, UserVacationStatsRetrieve
{
$this->authorize("show", $vacationRequest);

$vacationRequest->load(["user", "vacations", "activities", "activities.user.profile"]);
$limit = $statsRetriever->getVacationDaysLimit($vacationRequest->user, $vacationRequest->yearPeriod);
$used = $statsRetriever->getUsedVacationDays($vacationRequest->user, $vacationRequest->yearPeriod);
$pending = $statsRetriever->getPendingVacationDays($vacationRequest->user, $vacationRequest->yearPeriod);
Expand Down
25 changes: 13 additions & 12 deletions app/Infrastructure/Http/Middleware/HandleInertiaRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ protected function getAuthData(Request $request): Closure

return fn(): array => [
"user" => $user ? new UserResource($user) : null,
"can" => Permission::all()->mapWithKeys(
fn(Permission $permission): array => [
$permission->name => $user ? $user->hasPermissionTo($permission) : false,
],
),
"can" => Permission::query()->with("roles")->get()
->mapWithKeys(
fn(Permission $permission): array => [
$permission->name => $user ? $user->hasPermissionTo($permission) : false,
],
),
];
}

Expand All @@ -63,13 +64,13 @@ protected function getVacationRequestsCount(Request $request): Closure
$user = $request->user();

return fn(): ?int => $user && $user->can("listAllRequests")
? VacationRequest::query()
->whereBelongsTo($this->yearPeriodRetriever->selected())
->states(
VacationRequestStatesRetriever::waitingForUserActionStates($user),
)
->count()
: null;
? VacationRequest::query()
->whereBelongsTo($this->yearPeriodRetriever->selected())
->states(
VacationRequestStatesRetriever::waitingForUserActionStates($user),
)
->count()
: null;
}

protected function getDeployInformation(): Closure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function toArray($request): array
"to" => $this->to->toDisplayString(),
"displayDate" => $this->getDate($this->from->toDisplayString(), $this->to->toDisplayString()),
"comment" => $this->comment,
"days" => VacationResource::collection($this->vacations),
"days" => VacationResource::collection($this->vacations->load("user")),
"can" => [
"acceptAsTechnical" => $this->resource->state->canTransitionTo(AcceptedByTechnical::class)
&& $user->can("acceptAsTechApprover", $this->resource),
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"ext-pdo": "*",
"ext-redis": "*",
"azuyalabs/yasumi": "^2.6.0",
"barryvdh/laravel-debugbar": "^3.12",
"barryvdh/laravel-dompdf": "^2.0.1",
"doctrine/dbal": "^3.7.2",
"fakerphp/faker": "^1.22.0",
Expand Down
152 changes: 151 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions storage/debugbar/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
krzysztofrewak marked this conversation as resolved.
Show resolved Hide resolved
!.gitignore
Loading