From 81084fa71727ecdeeda9e3a7443fd02eaadd7584 Mon Sep 17 00:00:00 2001 From: Brad Date: Mon, 10 Jan 2022 13:54:57 -0500 Subject: [PATCH] Fixed #7824 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 --- .../Controllers/Api/LicensesController.php | 3 +- .../Licenses/LicensesController.php | 2 +- app/Models/License.php | 25 +++++++++++---- database/factories/LicenseFactory.php | 7 +++- ..._add_license_id_index_to_license_seats.php | 32 +++++++++++++++++++ 5 files changed, 59 insertions(+), 10 deletions(-) create mode 100644 database/migrations/2022_01_10_182548_add_license_id_index_to_license_seats.php diff --git a/app/Http/Controllers/Api/LicensesController.php b/app/Http/Controllers/Api/LicensesController.php index ad6e75ab77..d03a45f029 100644 --- a/app/Http/Controllers/Api/LicensesController.php +++ b/app/Http/Controllers/Api/LicensesController.php @@ -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); diff --git a/app/Http/Controllers/Licenses/LicensesController.php b/app/Http/Controllers/Licenses/LicensesController.php index 5272164b18..62b7529902 100755 --- a/app/Http/Controllers/Licenses/LicensesController.php +++ b/app/Http/Controllers/Licenses/LicensesController.php @@ -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); diff --git a/app/Models/License.php b/app/Models/License.php index 21af9549cb..0034ecb6b6 100755 --- a/app/Models/License.php +++ b/app/Models/License.php @@ -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(); diff --git a/database/factories/LicenseFactory.php b/database/factories/LicenseFactory.php index 7b4c1bc787..30bcaca755 100644 --- a/database/factories/LicenseFactory.php +++ b/database/factories/LicenseFactory.php @@ -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 ]; } diff --git a/database/migrations/2022_01_10_182548_add_license_id_index_to_license_seats.php b/database/migrations/2022_01_10_182548_add_license_id_index_to_license_seats.php new file mode 100644 index 0000000000..0eb0dfc463 --- /dev/null +++ b/database/migrations/2022_01_10_182548_add_license_id_index_to_license_seats.php @@ -0,0 +1,32 @@ +index(['license_id']); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('license_seats', function (Blueprint $table) { + $table->dropIndex(['license_id']); + }); + } +}