A free open source IT asset/license management system
De-normalize new counters from 4.3.0 (#5547)
snipe4c656c0321c9
diff --git a/app/Console/Commands/SyncAssetCounters.php b/app/Console/Commands/SyncAssetCounters.phpnew file mode 100644index 0000000000..bf63eaff9d--- /dev/null+++ b/app/Console/Commands/SyncAssetCounters.php@@ -0,0 +1,76 @@+<?php++namespace App\Console\Commands;++use Illuminate\Console\Command;+use App\Models\Asset;++class SyncAssetCounters extends Command+{+ /**+ * The name and signature of the console command.+ *+ * @var string+ */+ protected $signature = 'snipeit:counter-sync';++ /**+ * The console command description.+ *+ * @var string+ */+ protected $description = 'Syncs checkedout, checked in, and requested counters for assets';++ /**+ * Create a new command instance.+ *+ * @return void+ */+ public function __construct()+ {+ parent::__construct();+ }++ /**+ * Execute the console command.+ *+ * @return mixed+ */+ public function handle()+ {+ $start = microtime(true);+ $assets = Asset::withCount('checkins', 'checkouts', 'userRequests')+ ->withTrashed()->get();++ if ($assets) {+ if ($assets->count() > 0) {+ $bar = $this->output->createProgressBar($assets->count());++ foreach ($assets as $asset) {+ $asset->checkin_counter = (int) $asset->checkins_count;+ $asset->checkout_counter = (int) $asset->checkouts_count;+ $asset->requests_counter = (int) $asset->user_requests_count;+ $asset->unsetEventDispatcher();+ $asset->save();+ $output['info'][] = 'Asset: ' . $asset->id . ' has ' . $asset->checkin_counter . ' checkins, ' . $asset->checkout_counter . ' checkouts, and ' . $asset->requests_counter . ' requests';+ $bar->advance();+ }+ $bar->finish();++ foreach ($output['info'] as $key => $output_text) {+ $this->info($output_text);+ }++ $time_elapsed_secs = microtime(true) - $start;+ $this->info('Sync executed in ' . $time_elapsed_secs . ' seconds');++ } else {+ $this->info('No assets to sync');+ }++ }++++ }+}diff --git a/app/Console/Kernel.php b/app/Console/Kernel.phpindex 9a0d3090e5..4d66056732 100644--- a/app/Console/Kernel.php+++ b/app/Console/Kernel.php@@ -29,6 +29,7 @@ class Kernel extends ConsoleKernelCommands\ResetDemoSettings::class,Commands\SyncAssetLocations::class,Commands\RegenerateAssetTags::class,+ Commands\SyncAssetCounters::class,];/**diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.phpindex ee4f45365a..02f0d12b0c 100644--- a/app/Http/Controllers/Api/AssetsController.php+++ b/app/Http/Controllers/Api/AssetsController.php@@ -77,9 +77,9 @@ class AssetsController extends Controller'last_audit_date','next_audit_date','warranty_months',- 'checkouts_count',- 'checkins_count',- 'user_requests_count',+ 'checkout_counter',+ 'checkin_counter',+ 'requests_counter',];$filter = array();@@ -95,7 +95,7 @@ class AssetsController extends Controller$assets = Company::scopeCompanyables(Asset::select('assets.*'),"company_id","assets")->with('location', 'assetstatus', 'assetlog', 'company', 'defaultLoc','assignedTo',- 'model.category', 'model.manufacturer', 'model.fieldset','supplier')->withCount('checkins', 'checkouts', 'userRequests');+ 'model.category', 'model.manufacturer', 'model.fieldset','supplier');// These are used by the API to query against specific ID numbers.diff --git a/app/Http/Controllers/ViewAssetsController.php b/app/Http/Controllers/ViewAssetsController.phpindex 631ed25886..af7626765e 100755--- a/app/Http/Controllers/ViewAssetsController.php+++ b/app/Http/Controllers/ViewAssetsController.php@@ -181,6 +181,8 @@ class ViewAssetsController extends Controller// If it's already requested, cancel the request.if ($asset->isRequestedBy(Auth::user())) {$asset->cancelRequest();+ $asset->decrement('requests_counter', 1);+$logaction->logaction('request canceled');$settings->notify(new RequestAssetCancelationNotification($data));return redirect()->route('requestable-assets')@@ -188,8 +190,8 @@ class ViewAssetsController extends Controller} else {$logaction->logaction('requested');-$asset->request();+ $asset->increment('requests_counter', 1);$settings->notify(new RequestAssetNotification($data));diff --git a/app/Http/Transformers/AssetsTransformer.php b/app/Http/Transformers/AssetsTransformer.phpindex 5664b5fda4..84b5af4193 100644--- a/app/Http/Transformers/AssetsTransformer.php+++ b/app/Http/Transformers/AssetsTransformer.php@@ -77,9 +77,9 @@ class AssetsTransformer'last_checkout' => Helper::getFormattedDateObject($asset->last_checkout, 'datetime'),'expected_checkin' => Helper::getFormattedDateObject($asset->expected_checkin, 'date'),'purchase_cost' => Helper::formatCurrencyOutput($asset->purchase_cost),- 'checkins_count' => (int) $asset->checkins_count,- 'checkouts_count' => (int) $asset->checkouts_count,- 'user_requests_count' => (int) $asset->user_requests_count,+ 'checkin_counter' => (int) $asset->checkin_counter,+ 'checkout_counter' => (int) $asset->checkout_counter,+ 'requests_counter' => (int) $asset->requests_counter,'user_can_checkout' => (bool) $asset->availableForCheckout(),];diff --git a/app/Models/Asset.php b/app/Models/Asset.phpindex 0549d80106..3198ff36f9 100644--- a/app/Models/Asset.php+++ b/app/Models/Asset.php@@ -199,6 +199,8 @@ class Asset extends Depreciableif ($this->save()) {$this->logCheckout($note, $target);+ \Log::debug('Increment the checkout count for asset: '.$this->id);+ $this->increment('checkout_counter', 1);return true;}return false;diff --git a/app/Models/Loggable.php b/app/Models/Loggable.phpindex a325762d4a..f74f453c26 100644--- a/app/Models/Loggable.php+++ b/app/Models/Loggable.php@@ -55,7 +55,7 @@ trait Loggableif ($log->target_type == Location::class) {$log->location_id = $target->id;} elseif ($log->target_type == Asset::class) {- $log->location_id = $target->rtd_location_id;+ $log->location_id = $target->location_id;} else {$log->location_id = $target->location_id;}@@ -121,8 +121,17 @@ trait Loggable$log->item_type = License::class;$log->item_id = $this->license_id;} else {+$log->item_type = static::class;$log->item_id = $this->id;++ if (static::class == Asset::class) {+ if ($asset = Asset::find($log->item_id)) {+ \Log::debug('Increment the checkin count for asset: '.$log->item_id);+ $asset->increment('checkin_counter', 1);+ }+ }+}diff --git a/app/Presenters/AssetPresenter.php b/app/Presenters/AssetPresenter.phpindex 11f00e6904..d971b09e39 100644--- a/app/Presenters/AssetPresenter.php+++ b/app/Presenters/AssetPresenter.php@@ -182,21 +182,21 @@ class AssetPresenter extends Presenter"title" => trans('general.notes'),], [- "field" => "checkouts_count",+ "field" => "checkout_counter","searchable" => false,"sortable" => true,"visible" => false,"title" => trans('general.checkouts_count')],[- "field" => "checkins_count",+ "field" => "checkin_counter","searchable" => false,"sortable" => true,"visible" => false,"title" => trans('general.checkins_count')], [- "field" => "user_requests_count",+ "field" => "requests_counter","searchable" => false,"sortable" => true,"visible" => false,diff --git a/database/migrations/2018_05_14_233638_denorm_counters_on_assets.php b/database/migrations/2018_05_14_233638_denorm_counters_on_assets.phpnew file mode 100644index 0000000000..d7f3971f22--- /dev/null+++ b/database/migrations/2018_05_14_233638_denorm_counters_on_assets.php@@ -0,0 +1,36 @@+<?php++use Illuminate\Support\Facades\Schema;+use Illuminate\Database\Schema\Blueprint;+use Illuminate\Database\Migrations\Migration;++class DenormCountersOnAssets extends Migration+{+ /**+ * Run the migrations.+ *+ * @return void+ */+ public function up()+ {+ Schema::table('assets', function (Blueprint $table) {+ $table->integer('checkin_counter')->default(0);+ $table->integer('checkout_counter')->default(0);+ $table->integer('requests_counter')->default(0);+ });+ }++ /**+ * Reverse the migrations.+ *+ * @return void+ */+ public function down()+ {+ Schema::table('assets', function (Blueprint $table) {+ $table->dropColumn('checkin_counter');+ $table->dropColumn('checkout_counter');+ $table->dropColumn('requests_counter');+ });+ }+}diff --git a/database/migrations/2018_05_16_153409_add_first_counter_totals_to_assets.php b/database/migrations/2018_05_16_153409_add_first_counter_totals_to_assets.phpnew file mode 100644index 0000000000..f3f70f001c--- /dev/null+++ b/database/migrations/2018_05_16_153409_add_first_counter_totals_to_assets.php@@ -0,0 +1,34 @@+<?php++use Illuminate\Support\Facades\Schema;+use Illuminate\Database\Schema\Blueprint;+use Illuminate\Database\Migrations\Migration;+use App\Models\Asset;++class AddFirstCounterTotalsToAssets extends Migration+{+ /**+ * Run the migrations.+ *+ * @return void+ */+ public function up()+ {+ // This artisan call may take a while+ \Log::info('This could take a while.... ');+ Artisan::call('snipeit:counter-sync');+ $output = Artisan::output();+ \Log::info($output);++ }++ /**+ * Reverse the migrations.+ *+ * @return void+ */+ public function down()+ {+ //+ }+}