Previously there was a 999 max seats on Licenses as anything above that seemed to cause slowdowns and failure.

This commit allievates those pain points

- removed freeSeats as a hydrated Eloquent model on JSON requests for the licenses index
- removed 'licenseSeats.user', 'licenseSeats.asset' from the 'with' clause as it's not needed in the view (Datatabales takes care of that)
- removed the 999 max seats limit from the License Model,
- reworked how new license seats are created when increasing seats or creating licenses
- Added an index the license_seats table to help speed up lookups
This commit is contained in:
Brad 2022-01-10 13:54:57 -05:00
parent 66976d9359
commit 81084fa717
5 changed files with 59 additions and 10 deletions

View file

@ -26,7 +26,7 @@ class LicensesController extends Controller
public function index(Request $request)
{
$this->authorize('view', License::class);
$licenses = Company::scopeCompanyables(License::with('company', 'manufacturer', 'freeSeats', 'supplier', 'category')->withCount('freeSeats as free_seats_count'));
$licenses = Company::scopeCompanyables(License::with('company', 'manufacturer', 'supplier', 'category')->withCount('freeSeats as free_seats_count'));
if ($request->filled('company_id')) {
$licenses->where('company_id', '=', $request->input('company_id'));
@ -144,7 +144,6 @@ class LicensesController extends Controller
}
$total = $licenses->count();
$licenses = $licenses->skip($offset)->take($limit)->get();
return (new LicensesTransformer)->transformLicenses($licenses, $total);

View file

@ -230,7 +230,7 @@ class LicensesController extends Controller
*/
public function show($licenseId = null)
{
$license = License::with('assignedusers', 'licenseSeats.user', 'licenseSeats.asset')->find($licenseId);
$license = License::with('assignedusers')->find($licenseId);
if ($license) {
$this->authorize('view', $license);

View file

@ -10,6 +10,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
use phpDocumentor\Reflection\Types\Collection;
use Watson\Validating\ValidatingTrait;
class License extends Depreciable
@ -42,7 +43,7 @@ class License extends Depreciable
protected $rules = [
'name' => 'required|string|min:3|max:255',
'seats' => 'required|min:1|max:999|integer',
'seats' => 'required|min:1|integer',
'license_email' => 'email|nullable|max:120',
'license_name' => 'string|nullable|max:100',
'notes' => 'string|nullable',
@ -175,12 +176,24 @@ class License extends Depreciable
return true;
}
// Else we're adding seats.
DB::transaction(function () use ($license, $oldSeats, $newSeats) {
for ($i = $oldSeats; $i < $newSeats; $i++) {
$license->licenseSeatsRelation()->save(new LicenseSeat, ['user_id' => Auth::id()]);
}
//Create enough seats for the change.
$licenseInsert = [];
for ($i = $oldSeats; $i < $newSeats; $i++) {
$licenseInsert[] = [
'user_id' => Auth::id(),
'license_id' => $license->id,
'created_at' => now(),
'updated_at' => now()
];
}
//Chunk and use DB transactions to prevent timeouts.
collect($licenseInsert)->chunk(1000)->each(function ($chunk) {
DB::transaction(function () use ($chunk) {
LicenseSeat::insert($chunk->toArray());
});
});
// On initail create, we shouldn't log the addition of seats.
// On initial create, we shouldn't log the addition of seats.
if ($license->id) {
//Log the addition of license to the log.
$logAction = new Actionlog();

View file

@ -2,6 +2,7 @@
namespace Database\Factories;
use App\Models\Category;
use Illuminate\Database\Eloquent\Factories\Factory;
/*
@ -37,18 +38,22 @@ class LicenseFactory extends Factory
*/
public function definition()
{
return [
'user_id' => 1,
'license_name' => $this->faker->name,
'name' => $this->faker->name,
'license_email' => $this->faker->safeEmail,
'serial' => $this->faker->uuid,
'notes' => 'Created by DB seeder',
'seats' => $this->faker->numberBetween(1, 10),
'purchase_date' => $this->faker->dateTimeBetween('-1 years', 'now', date_default_timezone_get()),
'order_number' => $this->faker->numberBetween(1000000, 50000000),
'expiration_date' => $this->faker->dateTimeBetween('now', '+3 years', date_default_timezone_get())->format('Y-m-d H:i:s'),
'reassignable' => $this->faker->boolean(),
'termination_date' => $this->faker->dateTimeBetween('-1 years', 'now', date_default_timezone_get())->format('Y-m-d H:i:s'),
'supplier_id' => $this->faker->numberBetween(1, 5),
'category_id' => Category::where('category_type', '=', 'license')->inRandomOrder()->first()->id
];
}

View file

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddLicenseIdIndexToLicenseSeats extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('license_seats', function (Blueprint $table) {
$table->index(['license_id']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('license_seats', function (Blueprint $table) {
$table->dropIndex(['license_id']);
});
}
}