Merge pull request #3357 from AdrienPoupa/duplicate-queries

Improve database performance by removing duplicated queries and using eager loading
This commit is contained in:
James Brooks
2019-01-07 13:21:25 +00:00
committed by GitHub
8 changed files with 110 additions and 15 deletions

View File

@@ -26,6 +26,69 @@ use Illuminate\Contracts\View\View;
*/ */
class DashboardComposer 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. * Bind data to the view.
* *
@@ -35,11 +98,11 @@ class DashboardComposer
*/ */
public function compose(View $view) public function compose(View $view)
{ {
$view->withComponentCount(Component::count()); $view->withComponentCount($this->componentCount);
$view->withIncidentCount(Incident::count()); $view->withIncidentCount($this->incidentCount);
$view->withIncidentTemplateCount(IncidentTemplate::count()); $view->withIncidentTemplateCount($this->incidentTemplateCount);
$view->withScheduleCount(Schedule::count()); $view->withScheduleCount($this->scheduleCount);
$view->withSubscriberCount(Subscriber::isVerified()->count()); $view->withSubscriberCount($this->subscriberCount);
$view->withIsWriteable(is_writable(app()->bootstrapPath().'/cachet')); $view->withIsWriteable(is_writable(app()->bootstrapPath().'/cachet'));
} }
} }

View File

@@ -57,6 +57,6 @@ class ComposerServiceProvider extends ServiceProvider
*/ */
public function register() public function register()
{ {
// $this->app->singleton(DashboardComposer::class);
} }
} }

View File

@@ -73,7 +73,7 @@ class ComponentController extends Controller
*/ */
public function showComponents() 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; $this->subMenu['components']['active'] = true;

View File

@@ -75,7 +75,7 @@ class IncidentController extends Controller
*/ */
public function showIncidents() 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') return View::make('dashboard.incidents.index')
->withPageTitle(trans('dashboard.incidents.incidents').' - '.trans('dashboard.dashboard')) ->withPageTitle(trans('dashboard.incidents.incidents').' - '.trans('dashboard.dashboard'))

View File

@@ -96,7 +96,8 @@ class StatusPageController extends AbstractApiController
$nextDate = $startDate->copy()->addDays($appIncidentDays)->toDateString(); $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', $endDate->format('Y-m-d').' 00:00:00',
$startDate->format('Y-m-d').' 23:59:59', $startDate->format('Y-m-d').' 23:59:59',
])->orderBy('occurred_at', 'desc')->get()->groupBy(function (Incident $incident) { ])->orderBy('occurred_at', 'desc')->get()->groupBy(function (Incident $incident) {

View File

@@ -20,6 +20,13 @@ class ComponentGroupPresenter extends BasePresenter implements Arrayable
{ {
use TimestampsTrait; use TimestampsTrait;
/**
* Flag for the enabled_components_lowest function.
*
* @var bool
*/
protected $enabledComponentsLowest = false;
/** /**
* Returns the lowest component status. * Returns the lowest component status.
* *
@@ -27,7 +34,7 @@ class ComponentGroupPresenter extends BasePresenter implements Arrayable
*/ */
public function lowest_status() public function lowest_status()
{ {
if ($component = $this->wrappedObject->enabled_components_lowest()->first()) { if ($component = $this->enabled_components_lowest()) {
return AutoPresenter::decorate($component)->status; return AutoPresenter::decorate($component)->status;
} }
} }
@@ -39,7 +46,7 @@ class ComponentGroupPresenter extends BasePresenter implements Arrayable
*/ */
public function lowest_human_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; return AutoPresenter::decorate($component)->human_status;
} }
} }
@@ -51,11 +58,25 @@ class ComponentGroupPresenter extends BasePresenter implements Arrayable
*/ */
public function lowest_status_color() 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 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. * Determine the class for collapsed/uncollapsed groups.
* *

View File

@@ -29,6 +29,13 @@ class IncidentPresenter extends BasePresenter implements Arrayable
*/ */
protected $dates; protected $dates;
/**
* Flag for the latest function.
*
* @var bool
*/
protected $latest = false;
/** /**
* Incident icon lookup. * Incident icon lookup.
* *
@@ -248,9 +255,11 @@ class IncidentPresenter extends BasePresenter implements Arrayable
*/ */
public function latest() public function latest()
{ {
if ($update = $this->wrappedObject->updates()->orderBy('created_at', 'desc')->first()) { if (is_bool($this->latest)) {
return $update; $this->latest = $this->wrappedObject->updates()->orderBy('created_at', 'desc')->first();
} }
return $this->latest;
} }
/** /**

View File

@@ -12,6 +12,7 @@
namespace CachetHQ\Cachet\Presenters; namespace CachetHQ\Cachet\Presenters;
use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Facades\Config;
use Laravolt\Avatar\Facade as Avatar; use Laravolt\Avatar\Facade as Avatar;
use McCool\LaravelAutoPresenter\BasePresenter; use McCool\LaravelAutoPresenter\BasePresenter;
@@ -29,7 +30,7 @@ class UserPresenter extends BasePresenter implements Arrayable
*/ */
public function avatar() 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); return sprintf('https://www.gravatar.com/avatar/%s?size=%d', md5(strtolower($this->email)), 200);
} }