Skip to content

Commit

Permalink
#398 - tidy up queries (#409)
Browse files Browse the repository at this point in the history
* #400 - fixed n+1 quries in app

* - fix

* #398 - wip

* #398 - tidy up queries

* #398 - wip

* - merged main

* #398 - fix

* - laod fix

* #398 - update

* - cr fixes
  • Loading branch information
kamilpiech97 authored Mar 25, 2024
1 parent c96aa4e commit eb00bd2
Show file tree
Hide file tree
Showing 15 changed files with 151 additions and 59 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ QUEUE_CONNECTION=redis
SESSION_DRIVER=redis
SESSION_LIFETIME=120
FILESYSTEM_DISK=local
CACHE_QUERY_STORE=redis

MAIL_HOST=toby-mailpit-dev
MAIL_PORT=1025
Expand Down
6 changes: 3 additions & 3 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", "vacations"])
->with(["user.profile", "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", "vacations"])
->with(["user.profile", "vacations"])
->whereDate("from", "<=", $date)
->whereDate("to", ">=", $date)
->states(VacationRequestStatesRetriever::notFailedStates())
Expand Down Expand Up @@ -77,7 +77,7 @@ public function getUpcomingAbsences(Carbon $date): Collection
public function getUpcomingRemoteDays(Carbon $date): Collection
{
return VacationRequest::query()
->with(["user", "vacations"])
->with(["user.profile", "vacations"])
->whereDate("from", ">", $date)
->states(VacationRequestStatesRetriever::notFailedStates())
->whereIn(
Expand Down
16 changes: 10 additions & 6 deletions app/Domain/DashboardAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,27 @@ public function aggregateCalendarData(User $user, YearPeriod $yearPeriod): array
{
$approvedVacations = $user
->vacations()
->with("vacationRequest.vacations")
->with(["vacationRequest.vacations", "vacationRequest.user.profile"])
->whereBelongsTo($yearPeriod)
->cache()
->approved()
->get()
->mapWithKeys(
fn(Vacation $vacation): array => [
$vacation->date->toDateString() => new DashboardVacationRequestResource($vacation->vacationRequest->load(["user", "vacations"])),
$vacation->date->toDateString() => new DashboardVacationRequestResource($vacation->vacationRequest),
],
);

$pendingVacations = $user
->vacations()
->with("vacationRequest.vacations")
->with(["vacationRequest.vacations", "vacationRequest.user.profile"])
->whereBelongsTo($yearPeriod)
->cache()
->pending()
->get()
->mapWithKeys(
fn(Vacation $vacation): array => [
$vacation->date->toDateString() => new DashboardVacationRequestResource($vacation->vacationRequest->load(["user", "vacations"])),
$vacation->date->toDateString() => new DashboardVacationRequestResource($vacation->vacationRequest),
],
);

Expand All @@ -86,17 +88,19 @@ public function aggregateVacationRequests(User $user, YearPeriod $yearPeriod): J
{
if ($user->can("listAllRequests")) {
$vacationRequests = $yearPeriod->vacationRequests()
->with(["user", "vacations"])
->with(["user", "vacations", "vacations.user", "vacations.user.profile", "user.permissions", "user.profile"])
->states(VacationRequestStatesRetriever::waitingForUserActionStates($user))
->latest("updated_at")
->limit(3)
->cache()
->get();
} else {
$vacationRequests = $user->vacationRequests()
->with(["user", "vacations"])
->with(["user", "vacations", "vacations.user", "vacations.user.profile", "user.permissions", "user.profile"])
->whereBelongsTo($yearPeriod)
->latest("updated_at")
->limit(3)
->cache()
->get();
}

Expand Down
2 changes: 2 additions & 0 deletions app/Domain/UserVacationStatsRetriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,15 @@ public function getVacationDaysLimit(User $user, YearPeriod $yearPeriod): int
{
return $user->vacationLimits()
->whereBelongsTo($yearPeriod)
->cache()
->first()?->limit ?? 0;
}

public function hasVacationDaysLimit(User $user, YearPeriod $yearPeriod): bool
{
return $user->vacationLimits()
->whereBelongsTo($yearPeriod)
->cache()
->first()?->hasVacation() ?? false;
}

Expand Down
12 changes: 8 additions & 4 deletions app/Eloquent/Helpers/YearPeriodRetriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Toby\Eloquent\Helpers;

use Illuminate\Cache\CacheManager;
use Illuminate\Contracts\Session\Session;
use Toby\Eloquent\Models\YearPeriod;

Expand All @@ -13,14 +14,17 @@ class YearPeriodRetriever

public function __construct(
protected Session $session,
protected CacheManager $cacheManager,
) {}

public function selected(): YearPeriod
{
/** @var YearPeriod $yearPeriod */
$yearPeriod = YearPeriod::query()->find($this->session->get(static::SESSION_KEY));
return $this->cacheManager->remember("selected_year_period", 60, function () {
/** @var YearPeriod $yearPeriod */
$yearPeriod = YearPeriod::query()->find($this->session->get(static::SESSION_KEY));

return $yearPeriod !== null ? $yearPeriod : $this->current();
return $yearPeriod !== null ? $yearPeriod : $this->current();
});
}

public function current(): YearPeriod
Expand All @@ -33,7 +37,7 @@ public function links(): array
$selected = $this->selected();
$current = $this->current();

$years = YearPeriod::all();
$years = YearPeriod::query()->cache()->get();

$navigation = $years->map(fn(YearPeriod $yearPeriod): array => $this->toNavigation($yearPeriod));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Toby\Eloquent\Helpers\YearPeriodRetriever;
use Toby\Eloquent\Models\YearPeriod;

Expand All @@ -14,6 +15,7 @@ class SelectYearPeriodController extends Controller
public function __invoke(Request $request, YearPeriod $yearPeriod): RedirectResponse
{
$request->session()->put(YearPeriodRetriever::SESSION_KEY, $yearPeriod->id);
Cache::forget("selected_year_period");

return redirect()
->back()
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(["user", "vacations"])
->with(["vacations.user.profile", "user.permissions", "user.profile"])
->whereBelongsTo($yearPeriodRetriever->selected())
->latest()
->states(VacationRequestStatesRetriever::filterByStatusGroup($status, $request->user()))
Expand Down Expand Up @@ -99,7 +99,7 @@ public function indexForApprovers(
$type = $request->get("type");

$vacationRequests = VacationRequest::query()
->with(["user", "vacations"])
->with(["vacations.user.profile", "user.permissions", "user.profile"])
->whereBelongsTo($yearPeriod)
->when($user !== null, fn(Builder $query): Builder => $query->where("user_id", $user))
->when($type !== null, fn(Builder $query): Builder => $query->where("type", $type))
Expand Down Expand Up @@ -131,7 +131,7 @@ public function show(VacationRequest $vacationRequest, UserVacationStatsRetrieve
{
$this->authorize("show", $vacationRequest);

$vacationRequest->load(["user", "vacations", "activities", "activities.user.profile"]);
$vacationRequest->load(["vacations.user.profile", "user.permissions", "user.profile", "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
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function share(Request $request): array

protected function getAuthData(Request $request): Closure
{
$user = $request->user();
$user = $request->user()?->load("profile");

return fn(): array => [
"user" => $user ? new UserResource($user) : null,
Expand Down
4 changes: 2 additions & 2 deletions app/Infrastructure/Http/Resources/VacationRequestResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ public function toArray($request): array
return [
"id" => $this->id,
"name" => $this->name,
"user" => new SimpleUserResource($this->user),
"user" => new SimpleUserResource($user),
"type" => $this->type,
"isVacation" => $this->configRetriever->isVacation($this->type),
"state" => $this->state,
"from" => $this->from->toDisplayString(),
"to" => $this->to->toDisplayString(),
"displayDate" => $this->getDate($this->from->toDisplayString(), $this->to->toDisplayString()),
"comment" => $this->comment,
"days" => VacationResource::collection($this->vacations->load("user")),
"days" => VacationResource::collection($this->vacations),
"can" => [
"acceptAsTechnical" => $this->resource->state->canTransitionTo(AcceptedByTechnical::class)
&& $user->can("acceptAsTechApprover", $this->resource),
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"fakerphp/faker": "^1.22.0",
"guzzlehttp/guzzle": "^7.7.0",
"inertiajs/inertia-laravel": "^0.6.11",
"laragear/cache-query": "^4.0",
"laravel/framework": "^10.33.0",
"laravel/sanctum": "^3.3.2",
"laravel/socialite": "^5.10.0",
Expand All @@ -30,6 +31,7 @@
"spatie/laravel-slack-slash-command": "^1.11.4"
},
"require-dev": {
"barryvdh/laravel-debugbar": "^3.12",
"blumilksoftware/codestyle": "^2.8.0",
"laravel/dusk": "^7.11.4",
"mockery/mockery": "^1.5.1",
Expand Down
Loading

0 comments on commit eb00bd2

Please sign in to comment.