mirror of
https://github.com/snipe/snipe-it.git
synced 2025-01-11 13:57:41 -08:00
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
This commit is contained in:
parent
66976d9359
commit
81084fa717
|
@ -26,7 +26,7 @@ class LicensesController extends Controller
|
||||||
public function index(Request $request)
|
public function index(Request $request)
|
||||||
{
|
{
|
||||||
$this->authorize('view', License::class);
|
$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')) {
|
if ($request->filled('company_id')) {
|
||||||
$licenses->where('company_id', '=', $request->input('company_id'));
|
$licenses->where('company_id', '=', $request->input('company_id'));
|
||||||
|
@ -144,7 +144,6 @@ class LicensesController extends Controller
|
||||||
}
|
}
|
||||||
|
|
||||||
$total = $licenses->count();
|
$total = $licenses->count();
|
||||||
|
|
||||||
$licenses = $licenses->skip($offset)->take($limit)->get();
|
$licenses = $licenses->skip($offset)->take($limit)->get();
|
||||||
|
|
||||||
return (new LicensesTransformer)->transformLicenses($licenses, $total);
|
return (new LicensesTransformer)->transformLicenses($licenses, $total);
|
||||||
|
|
|
@ -230,7 +230,7 @@ class LicensesController extends Controller
|
||||||
*/
|
*/
|
||||||
public function show($licenseId = null)
|
public function show($licenseId = null)
|
||||||
{
|
{
|
||||||
$license = License::with('assignedusers', 'licenseSeats.user', 'licenseSeats.asset')->find($licenseId);
|
$license = License::with('assignedusers')->find($licenseId);
|
||||||
|
|
||||||
if ($license) {
|
if ($license) {
|
||||||
$this->authorize('view', $license);
|
$this->authorize('view', $license);
|
||||||
|
|
|
@ -10,6 +10,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
use Illuminate\Support\Facades\Session;
|
use Illuminate\Support\Facades\Session;
|
||||||
|
use phpDocumentor\Reflection\Types\Collection;
|
||||||
use Watson\Validating\ValidatingTrait;
|
use Watson\Validating\ValidatingTrait;
|
||||||
|
|
||||||
class License extends Depreciable
|
class License extends Depreciable
|
||||||
|
@ -42,7 +43,7 @@ class License extends Depreciable
|
||||||
|
|
||||||
protected $rules = [
|
protected $rules = [
|
||||||
'name' => 'required|string|min:3|max:255',
|
'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_email' => 'email|nullable|max:120',
|
||||||
'license_name' => 'string|nullable|max:100',
|
'license_name' => 'string|nullable|max:100',
|
||||||
'notes' => 'string|nullable',
|
'notes' => 'string|nullable',
|
||||||
|
@ -175,12 +176,24 @@ class License extends Depreciable
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
// Else we're adding seats.
|
// Else we're adding seats.
|
||||||
DB::transaction(function () use ($license, $oldSeats, $newSeats) {
|
//Create enough seats for the change.
|
||||||
for ($i = $oldSeats; $i < $newSeats; $i++) {
|
$licenseInsert = [];
|
||||||
$license->licenseSeatsRelation()->save(new LicenseSeat, ['user_id' => Auth::id()]);
|
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) {
|
if ($license->id) {
|
||||||
//Log the addition of license to the log.
|
//Log the addition of license to the log.
|
||||||
$logAction = new Actionlog();
|
$logAction = new Actionlog();
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace Database\Factories;
|
namespace Database\Factories;
|
||||||
|
|
||||||
|
use App\Models\Category;
|
||||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -37,18 +38,22 @@ class LicenseFactory extends Factory
|
||||||
*/
|
*/
|
||||||
public function definition()
|
public function definition()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'user_id' => 1,
|
'user_id' => 1,
|
||||||
'license_name' => $this->faker->name,
|
'name' => $this->faker->name,
|
||||||
'license_email' => $this->faker->safeEmail,
|
'license_email' => $this->faker->safeEmail,
|
||||||
'serial' => $this->faker->uuid,
|
'serial' => $this->faker->uuid,
|
||||||
'notes' => 'Created by DB seeder',
|
'notes' => 'Created by DB seeder',
|
||||||
|
'seats' => $this->faker->numberBetween(1, 10),
|
||||||
'purchase_date' => $this->faker->dateTimeBetween('-1 years', 'now', date_default_timezone_get()),
|
'purchase_date' => $this->faker->dateTimeBetween('-1 years', 'now', date_default_timezone_get()),
|
||||||
'order_number' => $this->faker->numberBetween(1000000, 50000000),
|
'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'),
|
'expiration_date' => $this->faker->dateTimeBetween('now', '+3 years', date_default_timezone_get())->format('Y-m-d H:i:s'),
|
||||||
'reassignable' => $this->faker->boolean(),
|
'reassignable' => $this->faker->boolean(),
|
||||||
'termination_date' => $this->faker->dateTimeBetween('-1 years', 'now', date_default_timezone_get())->format('Y-m-d H:i:s'),
|
'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),
|
'supplier_id' => $this->faker->numberBetween(1, 5),
|
||||||
|
'category_id' => Category::where('category_type', '=', 'license')->inRandomOrder()->first()->id
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue