Improve database performance by removing duplicated queries

This commit is contained in:
Adrien Poupa
2018-12-07 00:50:19 -05:00
parent 7fb6384860
commit 2c790270f6
3 changed files with 27 additions and 6 deletions

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,8 @@ class ComponentGroupPresenter extends BasePresenter implements Arrayable
{ {
use TimestampsTrait; use TimestampsTrait;
protected $enabledComponentsLowest = false;
/** /**
* Returns the lowest component status. * Returns the lowest component status.
* *
@@ -27,7 +29,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 +41,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 +53,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,8 @@ class IncidentPresenter extends BasePresenter implements Arrayable
*/ */
protected $dates; protected $dates;
protected $latest = false;
/** /**
* Incident icon lookup. * Incident icon lookup.
* *
@@ -248,9 +250,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;
} }
/** /**