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

Improve database performance by removing duplicated queries and using eager loading #3357

Merged
merged 11 commits into from
Jan 7, 2019
46 changes: 41 additions & 5 deletions app/Composers/DashboardComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,42 @@
*/
class DashboardComposer
{
private $componentCount;
AdrienPoupa marked this conversation as resolved.
Show resolved Hide resolved

private $incidentCount;

private $incidentTemplateCount;

private $scheduleCount;

private $subscriberCount;

/**
* DashboardComposer constructor.
AdrienPoupa marked this conversation as resolved.
Show resolved Hide resolved
*/
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 +71,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
22 changes: 19 additions & 3 deletions app/Presenters/ComponentGroupPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ class ComponentGroupPresenter extends BasePresenter implements Arrayable
{
use TimestampsTrait;

protected $enabledComponentsLowest = false;
AdrienPoupa marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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 +41,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 +53,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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we checking is_bool if the variable is declared as false by default?

$this->enabledComponentsLowest = $this->wrappedObject->enabled_components_lowest()->first();
}

return $this->enabledComponentsLowest;
}

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

protected $latest = false;
AdrienPoupa marked this conversation as resolved.
Show resolved Hide resolved

/**
* Incident icon lookup.
*
Expand Down Expand Up @@ -248,9 +250,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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can just say if ($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