A free open source IT asset/license management system
Fixed N+1 on models endpoint
snipef7d26e69d1e5
diff --git a/app/Models/AssetModel.php b/app/Models/AssetModel.phpindex 31d1474e86..21e5ac3ea0 100755--- a/app/Models/AssetModel.php+++ b/app/Models/AssetModel.php@@ -160,17 +160,22 @@ class AssetModel extends SnipeModelpublic function percentRemaining(){- // Cache the available count locally — the old code called- // availableAssets()->count() twice, which under the API transformer- // loop showed up as a duplicated `select count(*) … assets where- // model_id = ?` for every model in the response (visible in any- // /api/v1/models?category_id=… query log).- $available = $this->availableAssets()->count();+ // Prefer the pre-loaded withCount attributes — Api\AssetModelsController+ // ::index already eager-loads these as `remaining` (available count)+ // and `assets_count` (total) via correlated subqueries on the parent+ // models SELECT. Reading them here avoids re-running the same counts+ // as N+1 queries per model in the transformer loop. Read straight off+ // getAttributes() rather than via __get so we don't accidentally+ // trigger a relation load when the attribute isn't there.+ $raw = $this->getAttributes();+ $available = $raw['remaining'] ?? $this->availableAssets()->count();if ($available === 0) {return 0;}- return $available / $this->assets()->count() * 100;+ $total = $raw['assets_count'] ?? $this->assets()->count();++ return $available / $total * 100;}/**diff --git a/tests/Unit/AssetModelTest.php b/tests/Unit/AssetModelTest.phpindex a5e718b0e5..ece6253e0f 100644--- a/tests/Unit/AssetModelTest.php+++ b/tests/Unit/AssetModelTest.php@@ -85,6 +85,32 @@ class AssetModelTest extends TestCase$this->assertEquals(40.0, $model->percentRemaining());}+ public function test_percent_remaining_skips_queries_when_counts_were_eager_loaded(): void+ {+ // Api\AssetModelsController::index loads `remaining` and `assets_count`+ // on every model via withCount. percentRemaining() must read those+ // attributes instead of re-running the same counts — otherwise the+ // transformer loop turns into per-model N+1 (visible as duplicated+ // `count(*) … assets where model_id = ?` rows in /api/v1/models).+ $category = Category::factory()->create(['category_type' => 'asset']);+ $model = AssetModel::factory()->create(['category_id' => $category->id]);+ Asset::factory()->count(4)->create(['model_id' => $model->id]);++ // Simulate the withCount load by setting the raw attributes directly,+ // matching exactly what Api\AssetModelsController::index does.+ $model->forceFill(['remaining' => 3, 'assets_count' => 4]);++ $queryCount = 0;+ DB::listen(function () use (&$queryCount) {+ $queryCount++;+ });++ $result = $model->percentRemaining();++ $this->assertSame(0, $queryCount, 'No queries should fire when the counts were eager-loaded');+ $this->assertSame(75.0, $result);+ }+public function test_percent_remaining_only_calls_available_assets_count_once(): void{// Regression: the old implementation called $this->availableAssets()