Skip to content

Commit

Permalink
Eager load many more things. Fixes a lot of n+1 queries in ajax/boots…
Browse files Browse the repository at this point in the history
…trap tables requests (#2832)
  • Loading branch information
dmeltzer authored and snipe committed Oct 28, 2016
1 parent 86ef44b commit 3a8edfd
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 43 deletions.
8 changes: 5 additions & 3 deletions app/Http/Controllers/AccessoriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,9 +530,11 @@ public function postCheckin(Request $request, $accessoryUserId = null, $backto =
**/
public function getDatatable(Request $request)
{
$accessories = Company::scopeCompanyables(Accessory::select('accessories.*')->with('category', 'company'))
->whereNull('accessories.deleted_at');

$accessories = Company::scopeCompanyables(
Accessory::select('accessories.*')
->whereNull('accessories.deleted_at')
->with('category', 'company', 'manufacturer', 'users', 'location')
);
if (Input::has('search')) {
$accessories = $accessories->TextSearch(e(Input::get('search')));
}
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/AssetModelsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ public function getCustomFields($modelId)

public function getDatatable($status = null)
{
$models = AssetModel::with('category', 'assets', 'depreciation');
$models = AssetModel::with('category', 'assets', 'depreciation', 'manufacturer');

switch ($status) {
case 'Deleted':
Expand Down Expand Up @@ -491,7 +491,7 @@ public function getDatatable($status = null)
*/
public function getDataView($modelID)
{
$assets = Asset::where('model_id', '=', $modelID)->with('company');
$assets = Asset::where('model_id', '=', $modelID)->with('company', 'assetstatus');

if (Input::has('search')) {
$assets = $assets->TextSearch(e(Input::get('search')));
Expand Down
7 changes: 3 additions & 4 deletions app/Http/Controllers/CategoriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,9 @@ public function getDatatable()
public function getDataViewAssets($categoryID)
{

$category = Category::with('assets.company')->find($categoryID);
$category = Category::find($categoryID);
$category = $category->load('assets.company', 'assets.model', 'assets.assetstatus', 'assets.assigneduser');
$category_assets = $category->assets();

if (Input::has('search')) {
$category_assets = $category_assets->TextSearch(e(Input::get('search')));
}
Expand All @@ -333,7 +333,6 @@ public function getDataViewAssets($categoryID)
$count = $category_assets->count();
$category_assets = $category_assets->skip($offset)->take($limit)->get();
$rows = array();

foreach ($category_assets as $asset) {

$actions = '';
Expand Down Expand Up @@ -364,7 +363,7 @@ public function getDataViewAssets($categoryID)
'assigned_to' => ($asset->assigneduser) ? (string)link_to('/admin/users/'.$asset->assigneduser->id.'/view', $asset->assigneduser->fullName()): '',
'change' => $inout,
'actions' => $actions,
'companyName' => Company::getName($asset),
'companyName' => is_null($asset->company) ? '' : e($asset->company->name)
);
}

Expand Down
7 changes: 5 additions & 2 deletions app/Http/Controllers/ConsumablesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,11 @@ public function postCheckout($consumableId)
*/
public function getDatatable()
{
$consumables = Company::scopeCompanyables(Consumable::select('consumables.*')->whereNull('consumables.deleted_at')
->with('company', 'location', 'category', 'users'));
$consumables = Company::scopeCompanyables(
Consumable::select('consumables.*')
->whereNull('consumables.deleted_at')
->with('company', 'location', 'category', 'users', 'manufacturer')
);

if (Input::has('search')) {
$consumables = $consumables->TextSearch(e(Input::get('search')));
Expand Down
5 changes: 3 additions & 2 deletions app/Http/Controllers/LicensesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,7 @@ public function getView($licenseId = null)
{

$license = License::find($licenseId);
$license = $license->load('assignedusers', 'licenseSeats.user', 'licenseSeats.asset');

if (isset($license->id)) {

Expand Down Expand Up @@ -947,7 +948,7 @@ public function displayFile($licenseId = null, $fileId = null)
*/
public function getDatatable()
{
$licenses = Company::scopeCompanyables(License::with('company'));
$licenses = Company::scopeCompanyables(License::with('company', 'licenseSeatsRelation', 'manufacturer'));

if (Input::has('search')) {
$licenses = $licenses->TextSearch(Input::get('search'));
Expand Down Expand Up @@ -991,7 +992,7 @@ public function getDatatable()
'id' => $license->id,
'name' => (string) link_to('/admin/licenses/'.$license->id.'/view', $license->name),
'serial' => (string) link_to('/admin/licenses/'.$license->id.'/view', mb_strimwidth($license->serial, 0, 50, "...")),
'totalSeats' => $license->totalSeatsByLicenseID(),
'totalSeats' => $license->licenseSeatsCount,
'remaining' => $license->remaincount(),
'license_name' => e($license->license_name),
'license_email' => e($license->license_email),
Expand Down
31 changes: 24 additions & 7 deletions app/Http/Controllers/ManufacturersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public function getDatatable()
*/
public function getDataView($manufacturerId, $itemtype = null)
{
$manufacturer = Manufacturer::with('assets.company')->find($manufacturerId);
$manufacturer = Manufacturer::find($manufacturerId);

switch ($itemtype) {
case "assets":
Expand All @@ -276,6 +276,7 @@ public function getDataView($manufacturerId, $itemtype = null)

protected function getDataAssetsView(Manufacturer $manufacturer)
{
$manufacturer = $manufacturer->load('assets.model', 'assets.assigneduser', 'assets.assetstatus', 'assets.company');
$manufacturer_assets = $manufacturer->assets;

if (Input::has('search')) {
Expand Down Expand Up @@ -329,20 +330,22 @@ protected function getDataAssetsView(Manufacturer $manufacturer)
'serial' => e($asset->serial),
'assigned_to' => ($asset->assigneduser) ? (string)link_to('/admin/users/'.$asset->assigneduser->id.'/view', e($asset->assigneduser->fullName())): '',
'actions' => $actions,
'companyName' => e(Company::getName($asset)),
// 'companyName' => e(Company::getName($asset)),
'companyName' => is_null($asset->company) ? '' : $asset->company->name
);

if (isset($inout)) {
$row['change'] = $inout;
}
if (isset($inout)) {
$row['change'] = $inout;
}

}

$data = array('total' => $count, 'rows' => $rows);
return $data;
}

protected function getDataLicensesView(Manufacturer $manufacturer)
{
$manufacturer = $manufacturer->load('licenses.company', 'licenses.manufacturer', 'licenses.licenseSeatsRelation');
$licenses = $manufacturer->licenses;

if (Input::has('search')) {
Expand Down Expand Up @@ -380,7 +383,7 @@ protected function getDataLicensesView(Manufacturer $manufacturer)
'id' => $license->id,
'name' => (string) link_to('/admin/licenses/'.$license->id.'/view', $license->name),
'serial' => (string) link_to('/admin/licenses/'.$license->id.'/view', mb_strimwidth($license->serial, 0, 50, "...")),
'totalSeats' => $license->totalSeatsByLicenseID(),
'totalSeats' => $license->licenseSeatCount,
'remaining' => $license->remaincount(),
'license_name' => e($license->license_name),
'license_email' => e($license->license_email),
Expand All @@ -403,6 +406,13 @@ protected function getDataLicensesView(Manufacturer $manufacturer)

public function getDataAccessoriesView(Manufacturer $manufacturer)
{
$manufacturer = $manufacturer->load(
'accessories.location',
'accessories.company',
'accessories.category',
'accessories.manufacturer',
'accessories.users'
);
$accessories = $manufacturer->accessories;

if (Input::has('search')) {
Expand Down Expand Up @@ -461,6 +471,13 @@ public function getDataAccessoriesView(Manufacturer $manufacturer)

public function getDataConsumablesView($manufacturer)
{
$manufacturer = $manufacturer->load(
'consumables.location',
'consumables.company',
'consumables.category',
'consumables.manufacturer',
'consumables.users'
);
$consumables = $manufacturer->consumables;

if (Input::has('search')) {
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/SuppliersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public function getView($supplierId = null)

public function getDatatable()
{
$suppliers = Supplier::select(array('id','name','address','address2','city','state','country','fax', 'phone','email','contact'))
$suppliers = Supplier::with('assets', 'licenses')->select(array('id','name','address','address2','city','state','country','fax', 'phone','email','contact'))
->whereNull('deleted_at');

if (Input::has('search')) {
Expand Down Expand Up @@ -283,8 +283,8 @@ public function getDatatable()
'phone' => e($supplier->phone),
'fax' => e($supplier->fax),
'email' => ($supplier->email!='') ? '<a href="mailto:'.e($supplier->email).'">'.e($supplier->email).'</a>' : '',
'assets' => $supplier->num_assets(),
'licenses' => $supplier->num_licenses(),
'assets' => $supplier->assets->count(),
'licenses' => $supplier->licenses->count(),
'actions' => $actions
);
}
Expand Down
60 changes: 42 additions & 18 deletions app/Models/License.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,20 @@ public function totalSeatsByLicenseID()
->count();
}

// We do this to eager load the "count" of seats from the controller. Otherwise calling "count()" on each model results in n+1
public function licenseSeatsRelation()
{
return $this->hasMany(LicenseSeat::class)->whereNull('deleted_at')->selectRaw('license_id, count(*) as count')->groupBy('license_id');
}

public function getLicenseSeatsCountAttribute()
{
if ($this->licenseSeatsRelation->first()) {
return $this->licenseSeatsRelation->first()->count;
}

return 0;
}

/**
* Get total licenses not checked out
Expand All @@ -116,37 +130,47 @@ public static function availassetcount()
/**
* Get the number of available seats
*/
public function availcount()
public function availCount()
{
return LicenseSeat::whereNull('assigned_to')
->whereNull('asset_id')
->where('license_id', '=', $this->id)
->whereNull('deleted_at')
->count();
return $this->licenseSeatsRelation()
->whereNull('asset_id');
}

public function getAvailSeatsCountAttribute()
{
if ($this->availCount->first()) {
return $this->availCount->first()->count;
}

return 0;
}

/**
* Get the number of assigned seats
*
*/
public function assignedcount()
public function assignedCount()
{
return $this->licenseSeatsRelation()->where(function ($query) {
$query->whereNotNull('assigned_to')
->orWhereNotNull('asset_id');
});
}

return \App\Models\LicenseSeat::where('license_id', '=', $this->id)
->where(function ($query) {

$query->whereNotNull('assigned_to')
->orWhereNotNull('asset_id');
})
->count();

public function getAssignedSeatsCountAttribute()
{
// dd($this->licenseSeatsRelation->first());
if ($this->assignedCount->first()) {
return $this->assignedCount->first()->count;
}

return 0;
}

public function remaincount()
{
$total = $this->totalSeatsByLicenseID();
$taken = $this->assignedcount();
$total = $this->licenseSeatsCount;
$taken = $this->assigned_seats;
$diff = ($total - $taken);
return $diff;
}
Expand All @@ -156,7 +180,7 @@ public function remaincount()
*/
public function totalcount()
{
$avail = $this->availcount();
$avail = $this->availSeatsCount;
$taken = $this->assignedcount();
$diff = ($avail + $taken);
return $diff;
Expand Down
23 changes: 21 additions & 2 deletions app/Models/Supplier.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ class Supplier extends Model
protected $fillable = ['name'];


// Eager load counts.
// We do this to eager load the "count" of seats from the controller. Otherwise calling "count()" on each model results in n+1
public function assetsRelation()
{
return $this->hasMany(Asset::class)->whereNull('deleted_at')->selectRaw('supplier_id, count(*) as count')->groupBy('supplier_id');
}

public function getLicenseSeatsCountAttribute()
{
if ($this->licenseSeatsRelation->first()) {
return $this->licenseSeatsRelation->first()->count;
}

return 0;
}
public function assets()
{
return $this->hasMany('\App\Models\Asset', 'supplier_id');
Expand All @@ -59,7 +74,11 @@ public function asset_maintenances()

public function num_assets()
{
return $this->hasMany('\App\Models\Asset', 'supplier_id')->count();
if ($this->assetsRelation->first()) {
return $this->assetsRelation->first()->count;
}

return 0;
}

public function licenses()
Expand All @@ -69,7 +88,7 @@ public function licenses()

public function num_licenses()
{
return $this->hasMany('\App\Models\License', 'supplier_id')->count();
return $this->licenses()->count();
}

public function addhttp($url)
Expand Down

0 comments on commit 3a8edfd

Please sign in to comment.