Merge pull request #12316 from snipe/features/chunk_data_in_sync_script

Chunk sync script
This commit is contained in:
snipe 2023-01-05 18:07:30 -08:00 committed by GitHub
commit 19e703980d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -39,33 +39,39 @@ class SyncAssetCounters extends Command
public function handle() public function handle()
{ {
$start = microtime(true); $start = microtime(true);
// We need the whole count of all assets in order to set up the progress bar
$assets_count = Asset::withTrashed()->count();
$bar = $this->output->createProgressBar($assets_count);
$assets = Asset::withCount('checkins as checkins_count', 'checkouts as checkouts_count', 'userRequests as user_requests_count') $assets = Asset::withCount('checkins as checkins_count', 'checkouts as checkouts_count', 'userRequests as user_requests_count')
->withTrashed()->get(); ->withTrashed()->chunk(100, function ($assets) use ($bar) {
if ($assets) { if ($assets->count() > 0) {
if ($assets->count() > 0) {
$bar = $this->output->createProgressBar($assets->count());
foreach ($assets as $asset) { 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) { $asset->checkin_counter = (int) $asset->checkins_count;
$this->info($output_text); $asset->checkout_counter = (int) $asset->checkouts_count;
} $asset->requests_counter = (int) $asset->user_requests_count;
$asset->unsetEventDispatcher();
$asset->save();
$bar->advance();
\Log::debug('Asset: '.$asset->id.' has '.$asset->checkin_counter.' checkins, '.$asset->checkout_counter.' checkouts, and '.$asset->requests_counter.' requests');
}
$time_elapsed_secs = microtime(true) - $start;
$this->info('Sync executed in '.$time_elapsed_secs.' seconds');
} else { } else {
$this->info('No assets to sync'); $this->info('No assets to sync');
} }
} });
$bar->finish();
$time_elapsed_secs = microtime(true) - $start;
$this->info("\nSync of ".$assets_count.' assets executed in '.$time_elapsed_secs.' seconds');
} }
} }