diff --git a/app/Composers/DashboardComposer.php b/app/Composers/DashboardComposer.php index 1bbdf3eae069..c5ccef937ce1 100644 --- a/app/Composers/DashboardComposer.php +++ b/app/Composers/DashboardComposer.php @@ -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. * @@ -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')); } } diff --git a/app/Foundation/Providers/ComposerServiceProvider.php b/app/Foundation/Providers/ComposerServiceProvider.php index bd762dd44aaf..51f241fc8016 100644 --- a/app/Foundation/Providers/ComposerServiceProvider.php +++ b/app/Foundation/Providers/ComposerServiceProvider.php @@ -57,6 +57,6 @@ public function boot(Factory $factory) */ public function register() { - // + $this->app->singleton(DashboardComposer::class); } } diff --git a/app/Http/Controllers/Dashboard/ComponentController.php b/app/Http/Controllers/Dashboard/ComponentController.php index 2745cf4b063e..152e345d49b7 100644 --- a/app/Http/Controllers/Dashboard/ComponentController.php +++ b/app/Http/Controllers/Dashboard/ComponentController.php @@ -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; diff --git a/app/Http/Controllers/Dashboard/IncidentController.php b/app/Http/Controllers/Dashboard/IncidentController.php index a64b361c5c92..f3f0dd9f396d 100644 --- a/app/Http/Controllers/Dashboard/IncidentController.php +++ b/app/Http/Controllers/Dashboard/IncidentController.php @@ -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')) diff --git a/app/Http/Controllers/StatusPageController.php b/app/Http/Controllers/StatusPageController.php index 87ced74b4eb4..cd431b4c9407 100644 --- a/app/Http/Controllers/StatusPageController.php +++ b/app/Http/Controllers/StatusPageController.php @@ -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) { diff --git a/app/Presenters/ComponentGroupPresenter.php b/app/Presenters/ComponentGroupPresenter.php index 597563940bde..456831a4ddd7 100644 --- a/app/Presenters/ComponentGroupPresenter.php +++ b/app/Presenters/ComponentGroupPresenter.php @@ -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 @@ 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; } } @@ -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. * diff --git a/app/Presenters/IncidentPresenter.php b/app/Presenters/IncidentPresenter.php index adab69c1a461..139860cb2bf6 100644 --- a/app/Presenters/IncidentPresenter.php +++ b/app/Presenters/IncidentPresenter.php @@ -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 @@ 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; } /** diff --git a/app/Presenters/UserPresenter.php b/app/Presenters/UserPresenter.php index 00aa9b102bf2..b14022d7595b 100644 --- a/app/Presenters/UserPresenter.php +++ b/app/Presenters/UserPresenter.php @@ -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); }