-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
De-normalize new counters from 4.3.0 #5547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e9664e5
e57abaf
a741287
cbd8526
9d2a4bc
0eb844b
0dca30e
be0c85c
2264025
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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'); | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,7 +55,7 @@ public function logCheckout($note, $target /* What are we checking out to? */) | |
| if ($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 @@ public function logCheckin($target, $note) | |
| $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); | ||
| } | ||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd say the same thing here, but I don't know if we ever finished extracting a checkin function.... I thought I had but I may have gotten distracted. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same - not sure. |
||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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'); | ||
| }); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to put some console output here from the migration warning users about the delay? Not sure.. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Things don't always output as expected in migrations. Could try tho. |
||
| \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() | ||
| { | ||
| // | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is requests_count the number of times it's ever been requested or the number of times it's currently requested?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ever been requested - unless a user actively cancels that request