Merge pull request #3357 from AdrienPoupa/duplicate-queries
Improve database performance by removing duplicated queries and using eager loading
This commit is contained in:
@@ -26,6 +26,69 @@ use Illuminate\Contracts\View\View;
|
||||
*/
|
||||
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.
|
||||
*
|
||||
@@ -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'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,6 +57,6 @@ class ComposerServiceProvider extends ServiceProvider
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
//
|
||||
$this->app->singleton(DashboardComposer::class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,7 +73,7 @@ class ComponentController extends Controller
|
||||
*/
|
||||
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;
|
||||
|
||||
|
||||
@@ -75,7 +75,7 @@ class IncidentController extends Controller
|
||||
*/
|
||||
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'))
|
||||
|
||||
@@ -96,7 +96,8 @@ class StatusPageController extends AbstractApiController
|
||||
$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) {
|
||||
|
||||
@@ -20,6 +20,13 @@ 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.
|
||||
*
|
||||
@@ -27,7 +34,7 @@ class ComponentGroupPresenter extends BasePresenter implements Arrayable
|
||||
*/
|
||||
public function lowest_status()
|
||||
{
|
||||
if ($component = $this->wrappedObject->enabled_components_lowest()->first()) {
|
||||
if ($component = $this->enabled_components_lowest()) {
|
||||
return AutoPresenter::decorate($component)->status;
|
||||
}
|
||||
}
|
||||
@@ -39,7 +46,7 @@ class ComponentGroupPresenter extends BasePresenter implements Arrayable
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -51,11 +58,25 @@ class ComponentGroupPresenter extends BasePresenter implements Arrayable
|
||||
*/
|
||||
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.
|
||||
*
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
@@ -248,9 +255,11 @@ class IncidentPresenter extends BasePresenter implements Arrayable
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user