Skip to content

Commit

Permalink
fix: reduce the number of queries in the dashboard by eager loading t…
Browse files Browse the repository at this point in the history
…he contacts (#2138)
  • Loading branch information
AdrienPoupa authored and asbiin committed Dec 18, 2018
1 parent 5b4d889 commit a1c9361
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 41 deletions.
8 changes: 8 additions & 0 deletions app/Http/Controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ public function index()
// get last 3 changelog entries
$changelogs = InstanceHelper::getChangelogEntries(3);

// Load the reminder for the upcoming three months
$reminders = [
0 => auth()->user()->account->getRemindersForMonth(0),
1 => auth()->user()->account->getRemindersForMonth(1),
2 => auth()->user()->account->getRemindersForMonth(2),
];

$data = [
'lastUpdatedContacts' => $lastUpdatedContactsCollection,
'number_of_contacts' => $account->contacts()->real()->active()->count(),
Expand All @@ -83,6 +90,7 @@ public function index()
'debts' => $debt,
'user' => auth()->user(),
'changelogs' => $changelogs,
'reminders' => $reminders,
];

return view('dashboard.index', $data);
Expand Down
22 changes: 22 additions & 0 deletions app/Http/ViewComposers/InstanceViewComposer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Http\ViewComposers;

use Illuminate\View\View;
use App\Models\Instance\Instance;

class InstanceViewComposer
{
/**
* Bind data to the view.
*
* @param View $view
* @return void
*/
public function compose(View $view)
{
$instance = Instance::first();

$view->with('instance', $instance);
}
}
7 changes: 4 additions & 3 deletions app/Models/Account/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -727,9 +727,10 @@ public function getRemindersForMonth(int $month)
$endOfMonth = now(DateHelper::getTimezone())->addMonthsNoOverflow($month)->endOfMonth();

return $this->reminders()
->whereBetween('next_expected_date', [$startOfMonth, $endOfMonth])
->orderBy('next_expected_date', 'asc')
->get();
->with('contact')
->whereBetween('next_expected_date', [$startOfMonth, $endOfMonth])
->orderBy('next_expected_date', 'asc')
->get();
}

/**
Expand Down
14 changes: 7 additions & 7 deletions app/Models/Contact/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -1467,14 +1467,14 @@ public function getBirthdayRemindersAboutRelatedContacts()
*/
public function getRelatedRealContact()
{
$relatedContact = Relationship::where('account_id', $this->account_id)
->where('contact_is', $this->id)
->first();
$account = $this;

if ($relatedContact) {
return self::where('account_id', $this->account_id)
->find($relatedContact->of_contact);
}
return self::setEagerLoads([])->where('account_id', $this->account_id)
->where('id', function ($query) use ($account) {
$query->select('of_contact')->from('relationships')->where('account_id', $account->account_id)
->where('contact_is', $account->id);
})
->first();
}

/**
Expand Down
4 changes: 4 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public function boot()
'partials.components.date-select', 'App\Http\ViewComposers\DateSelectViewComposer'
);

View::composer(
'partials.check', 'App\Http\ViewComposers\InstanceViewComposer'
);

if (config('database.use_utf8mb4')
&& DB::connection()->getDriverName() == 'mysql'
&& ! DBHelper::testVersion('5.7.7')) {
Expand Down
42 changes: 22 additions & 20 deletions resources/views/dashboard/_monthReminder.blade.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
<h3 class="ttu fw5 f5 pb2">{{ \App\Helpers\DateHelper::getMonthAndYear($month) }}</h3>
<ul class="mb4">
@if (count(auth()->user()->account->getRemindersForMonth($month)) > 0)
@foreach (auth()->user()->account->getRemindersForMonth($month) as $reminder)
<li class="pb2">
<span class="ttu f6 mr2 black-60">{{ \App\Helpers\DateHelper::getShortDateWithoutYear($reminder->next_expected_date) }}</span>
<span>
@foreach($remindersList as $month => $reminders)
<h3 class="ttu fw5 f5 pb2">{{ \App\Helpers\DateHelper::getMonthAndYear($month) }}</h3>
<ul class="mb4">
@if(count($reminders) > 0)
@foreach($reminders as $reminder)
<li class="pb2">
<span class="ttu f6 mr2 black-60">{{ \App\Helpers\DateHelper::getShortDateWithoutYear($reminder->next_expected_date) }}</span>
<span>
@if ($reminder->contact->is_partial)

@if ($reminder->contact->is_partial)
@php($relatedRealContact = $reminder->contact->getRelatedRealContact())
<a href="{{ route('people.show', $relatedRealContact) }}">{{ $relatedRealContact->getIncompleteName() }}</a>

<a href="{{ route('people.show', $reminder->contact->getRelatedRealContact()) }}">{{ $reminder->contact->getRelatedRealContact()->getIncompleteName() }}</a>
@else

@else

<a href="{{ route('people.show', $reminder->contact) }}">{{ $reminder->contact->getIncompleteName() }}</a>
<a href="{{ route('people.show', $reminder->contact) }}">{{ $reminder->contact->getIncompleteName() }}</a>

@endif
</span>
{{ $reminder->title }}
</li>
@endforeach
@else
<p>{{ trans('dashboard.reminders_none') }}</p>
@endif
</span>
{{ $reminder->title }}
</li>
@endforeach
@else
<p>{{ trans('dashboard.reminders_none') }}</p>
@endif
</ul>
</ul>
@endforeach
9 changes: 1 addition & 8 deletions resources/views/dashboard/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,7 @@
</p>
</div>
<div class="pt3 pr3 pl3 mb4">
{{-- Current month --}}
@include('dashboard._monthReminder', ['month' => 0])

{{-- Current month + 1 --}}
@include('dashboard._monthReminder', ['month' => 1])

{{-- Current month + 2 --}}
@include('dashboard._monthReminder', ['month' => 2])
@include('dashboard._monthReminder', ['remindersList' => $reminders])
</div>
</div>
</div>
Expand Down
6 changes: 3 additions & 3 deletions resources/views/partials/check.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

@if (config('monica.check_version'))

@if (version_compare(\App\Models\Instance\Instance::first()->latest_version, config('monica.app_version')) > 0)
@if (version_compare($instance->latest_version, config('monica.app_version')) > 0)
<li>
<a href="#showVersion" data-toggle="modal" class="badge badge-success">{{ trans('app.footer_new_version') }}</a>
</li>
Expand All @@ -19,8 +19,8 @@
</button>
</div>
<div class="modal-body">
<p>{{ trans_choice('app.footer_modal_version_release_away', \App\Models\Instance\Instance::first()->number_of_versions_since_current_version, ['number' => \App\Models\Instance\Instance::first()->number_of_versions_since_current_version]) }}</p>
{!! \App\Models\Instance\Instance::first()->latest_release_notes !!}
<p>{{ trans_choice('app.footer_modal_version_release_away', $instance->number_of_versions_since_current_version, ['number' => $instance->number_of_versions_since_current_version]) }}</p>
{!! $instance->latest_release_notes !!}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">{{ trans('app.close') }}</button>
Expand Down

0 comments on commit a1c9361

Please sign in to comment.