Improve database performance by removing duplicated queries

This commit is contained in:
Adrien Poupa
2018-12-06 22:53:33 -05:00
parent a7789dc1a6
commit 5433e1308f
3 changed files with 43 additions and 7 deletions

View File

@@ -26,6 +26,41 @@ use Illuminate\Contracts\View\View;
*/
class DashboardComposer
{
private $componentCount;
private $incidentCount;
private $incidentTemplateCount;
private $scheduleCount;
private $subscriberCount;
/**
* DashboardComposer constructor
*/
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 +70,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'));
}
}

View File

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

View File

@@ -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);
}