Skip to content

Commit

Permalink
Merge pull request #3357 from AdrienPoupa/duplicate-queries
Browse files Browse the repository at this point in the history
Improve database performance by removing duplicated queries and using eager loading
  • Loading branch information
jbrooksuk committed Jan 7, 2019
2 parents 547773f + 88ad1f7 commit 73eea81
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 15 deletions.
73 changes: 68 additions & 5 deletions app/Composers/DashboardComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,69 @@
*/
class DashboardComposer
{
/**
* The component count.
*
* @var int
*/
protected $componentCount;

/**
* The incident count.
*
* @var int
*/
protected $incidentCount;

/**
* The incident template count.
*
* @var int
*/
protected $incidentTemplateCount;

/**
* The schedule count.
*
* @var int
*/
protected $scheduleCount;

/**
* The subscriber count.
*
* @var int
*/
protected $subscriberCount;

/**
* Create a new dashboard composer instance.
*
* @return void
*/
public function __construct()
{
if (is_null($this->componentCount)) {
$this->componentCount = Component::count();
}

if (is_null($this->incidentCount)) {
$this->incidentCount = Incident::count();
}

if (is_null($this->incidentTemplateCount)) {
$this->incidentTemplateCount = IncidentTemplate::count();
}

if (is_null($this->scheduleCount)) {
$this->scheduleCount = Schedule::count();
}

if (is_null($this->subscriberCount)) {
$this->subscriberCount = Subscriber::isVerified()->count();
}
}

/**
* Bind data to the view.
*
Expand All @@ -35,11 +98,11 @@ class DashboardComposer
*/
public function compose(View $view)
{
$view->withComponentCount(Component::count());
$view->withIncidentCount(Incident::count());
$view->withIncidentTemplateCount(IncidentTemplate::count());
$view->withScheduleCount(Schedule::count());
$view->withSubscriberCount(Subscriber::isVerified()->count());
$view->withComponentCount($this->componentCount);
$view->withIncidentCount($this->incidentCount);
$view->withIncidentTemplateCount($this->incidentTemplateCount);
$view->withScheduleCount($this->scheduleCount);
$view->withSubscriberCount($this->subscriberCount);
$view->withIsWriteable(is_writable(app()->bootstrapPath().'/cachet'));
}
}
2 changes: 1 addition & 1 deletion app/Foundation/Providers/ComposerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ public function boot(Factory $factory)
*/
public function register()
{
//
$this->app->singleton(DashboardComposer::class);
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Dashboard/ComponentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function __construct()
*/
public function showComponents()
{
$components = Component::orderBy('order')->orderBy('created_at')->get();
$components = Component::with('group')->orderBy('order')->orderBy('created_at')->get();

$this->subMenu['components']['active'] = true;

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Dashboard/IncidentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function __construct(Guard $auth, System $system)
*/
public function showIncidents()
{
$incidents = Incident::orderBy('created_at', 'desc')->get();
$incidents = Incident::with('user')->orderBy('created_at', 'desc')->get();

return View::make('dashboard.incidents.index')
->withPageTitle(trans('dashboard.incidents.incidents').' - '.trans('dashboard.dashboard'))
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/StatusPageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ public function showIndex()
$nextDate = $startDate->copy()->addDays($appIncidentDays)->toDateString();
}

$allIncidents = Incident::where('visible', '>=', (int) !Auth::check())->whereBetween('occurred_at', [
$allIncidents = Incident::with('component')->with('updates.incident')
->where('visible', '>=', (int) !Auth::check())->whereBetween('occurred_at', [
$endDate->format('Y-m-d').' 00:00:00',
$startDate->format('Y-m-d').' 23:59:59',
])->orderBy('occurred_at', 'desc')->get()->groupBy(function (Incident $incident) {
Expand Down
27 changes: 24 additions & 3 deletions app/Presenters/ComponentGroupPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,21 @@ class ComponentGroupPresenter extends BasePresenter implements Arrayable
{
use TimestampsTrait;

/**
* Flag for the enabled_components_lowest function.
*
* @var bool
*/
protected $enabledComponentsLowest = false;

/**
* Returns the lowest component status.
*
* @return string|null
*/
public function lowest_status()
{
if ($component = $this->wrappedObject->enabled_components_lowest()->first()) {
if ($component = $this->enabled_components_lowest()) {
return AutoPresenter::decorate($component)->status;
}
}
Expand All @@ -39,7 +46,7 @@ public function lowest_status()
*/
public function lowest_human_status()
{
if ($component = $this->wrappedObject->enabled_components_lowest()->first()) {
if ($component = $this->enabled_components_lowest()) {
return AutoPresenter::decorate($component)->human_status;
}
}
Expand All @@ -51,11 +58,25 @@ public function lowest_human_status()
*/
public function lowest_status_color()
{
if ($component = $this->wrappedObject->enabled_components_lowest()->first()) {
if ($component = $this->enabled_components_lowest()) {
return AutoPresenter::decorate($component)->status_color;
}
}

/**
* Return the enabled components from the wrapped object, and cache it if need be.
*
* @return bool
*/
public function enabled_components_lowest()
{
if (is_bool($this->enabledComponentsLowest)) {
$this->enabledComponentsLowest = $this->wrappedObject->enabled_components_lowest()->first();
}

return $this->enabledComponentsLowest;
}

/**
* Determine the class for collapsed/uncollapsed groups.
*
Expand Down
13 changes: 11 additions & 2 deletions app/Presenters/IncidentPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ class IncidentPresenter extends BasePresenter implements Arrayable
*/
protected $dates;

/**
* Flag for the latest function.
*
* @var bool
*/
protected $latest = false;

/**
* Incident icon lookup.
*
Expand Down Expand Up @@ -248,9 +255,11 @@ public function latest_icon()
*/
public function latest()
{
if ($update = $this->wrappedObject->updates()->orderBy('created_at', 'desc')->first()) {
return $update;
if (is_bool($this->latest)) {
$this->latest = $this->wrappedObject->updates()->orderBy('created_at', 'desc')->first();
}

return $this->latest;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion app/Presenters/UserPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace CachetHQ\Cachet\Presenters;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Facades\Config;
use Laravolt\Avatar\Facade as Avatar;
use McCool\LaravelAutoPresenter\BasePresenter;

Expand All @@ -29,7 +30,7 @@ class UserPresenter extends BasePresenter implements Arrayable
*/
public function avatar()
{
if (setting('enable_external_dependencies')) {
if (Config::get('setting.enable_external_dependencies')) {
return sprintf('https://www.gravatar.com/avatar/%s?size=%d', md5(strtolower($this->email)), 200);
}

Expand Down

0 comments on commit 73eea81

Please sign in to comment.