|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace HoudaSlassi\Vantage\Console\Commands; |
| 4 | + |
| 5 | +use HoudaSlassi\Vantage\Models\VantageJob; |
| 6 | +use HoudaSlassi\Vantage\Support\TagAggregator; |
| 7 | +use Illuminate\Console\Command; |
| 8 | +use Illuminate\Support\Facades\DB; |
| 9 | + |
| 10 | +class BackfillJobTags extends Command |
| 11 | +{ |
| 12 | + protected $signature = 'vantage:backfill-tags |
| 13 | + {--days= : Only backfill jobs from the last X days (default: all)} |
| 14 | + {--chunk=1000 : Number of jobs to process per batch} |
| 15 | + {--force : Skip confirmation prompt}'; |
| 16 | + |
| 17 | + protected $description = 'Backfill the vantage_job_tags table from existing jobs for optimized tag queries'; |
| 18 | + |
| 19 | + public function handle(): int |
| 20 | + { |
| 21 | + $aggregator = new TagAggregator; |
| 22 | + |
| 23 | + // Check if tags table exists |
| 24 | + if (! $aggregator->hasTagsTable()) { |
| 25 | + $this->error('The vantage_job_tags table does not exist.'); |
| 26 | + $this->line(''); |
| 27 | + $this->line('Run the migration first:'); |
| 28 | + $this->line(' php artisan migrate'); |
| 29 | + $this->line(''); |
| 30 | + $this->line('Or publish and run Vantage migrations:'); |
| 31 | + $this->line(' php artisan vendor:publish --tag=vantage-migrations'); |
| 32 | + $this->line(' php artisan migrate'); |
| 33 | + |
| 34 | + return self::FAILURE; |
| 35 | + } |
| 36 | + |
| 37 | + $days = $this->option('days') ? (int) $this->option('days') : null; |
| 38 | + $chunkSize = (int) $this->option('chunk'); |
| 39 | + $force = $this->option('force'); |
| 40 | + |
| 41 | + // Build query for jobs to backfill |
| 42 | + $query = VantageJob::whereNotNull('job_tags'); |
| 43 | + |
| 44 | + if ($days !== null) { |
| 45 | + $query->where('created_at', '>', now()->subDays($days)); |
| 46 | + $period = "from the last {$days} days"; |
| 47 | + } else { |
| 48 | + $period = 'all time'; |
| 49 | + } |
| 50 | + |
| 51 | + // Count jobs to process |
| 52 | + $totalJobs = $query->count(); |
| 53 | + |
| 54 | + if ($totalJobs === 0) { |
| 55 | + $this->info('No jobs with tags found to backfill.'); |
| 56 | + |
| 57 | + return self::SUCCESS; |
| 58 | + } |
| 59 | + |
| 60 | + // Check if already populated |
| 61 | + $existingCount = DB::connection($this->getConnectionName()) |
| 62 | + ->table('vantage_job_tags') |
| 63 | + ->count(); |
| 64 | + |
| 65 | + if ($existingCount > 0) { |
| 66 | + $this->warn("The vantage_job_tags table already contains {$existingCount} records."); |
| 67 | + $this->line(''); |
| 68 | + |
| 69 | + if (! $force && ! $this->confirm('Do you want to clear existing records and re-backfill?', false)) { |
| 70 | + $this->info('Backfill cancelled. Existing data preserved.'); |
| 71 | + $this->line(''); |
| 72 | + $this->line('Options:'); |
| 73 | + $this->line(' - Use --force to overwrite without confirmation'); |
| 74 | + $this->line(' - Use --days=X to only backfill recent jobs (appends to existing)'); |
| 75 | + |
| 76 | + return self::SUCCESS; |
| 77 | + } |
| 78 | + |
| 79 | + // Clear existing records matching the time range |
| 80 | + $this->info('Clearing existing tag records...'); |
| 81 | + if ($days !== null) { |
| 82 | + $cutoff = now()->subDays($days); |
| 83 | + DB::connection($this->getConnectionName()) |
| 84 | + ->table('vantage_job_tags') |
| 85 | + ->where('created_at', '>', $cutoff) |
| 86 | + ->delete(); |
| 87 | + } else { |
| 88 | + DB::connection($this->getConnectionName()) |
| 89 | + ->table('vantage_job_tags') |
| 90 | + ->truncate(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + $this->info("Backfilling tags for {$totalJobs} jobs ({$period})..."); |
| 95 | + $this->line("Processing in chunks of {$chunkSize}..."); |
| 96 | + $this->line(''); |
| 97 | + |
| 98 | + $bar = $this->output->createProgressBar($totalJobs); |
| 99 | + $bar->setFormat(' %current%/%max% [%bar%] %percent:3s%% - %message%'); |
| 100 | + $bar->setMessage('Starting...'); |
| 101 | + $bar->start(); |
| 102 | + |
| 103 | + $processed = 0; |
| 104 | + $tagsInserted = 0; |
| 105 | + $errors = 0; |
| 106 | + |
| 107 | + $query->select(['id', 'job_tags', 'created_at']) |
| 108 | + ->orderBy('id') |
| 109 | + ->chunk($chunkSize, function ($jobs) use (&$processed, &$tagsInserted, &$errors, $bar) { |
| 110 | + $records = []; |
| 111 | + |
| 112 | + foreach ($jobs as $job) { |
| 113 | + $processed++; |
| 114 | + |
| 115 | + if (empty($job->job_tags) || ! is_array($job->job_tags)) { |
| 116 | + continue; |
| 117 | + } |
| 118 | + |
| 119 | + foreach ($job->job_tags as $tag) { |
| 120 | + if (is_string($tag) && trim($tag) !== '') { |
| 121 | + $records[] = [ |
| 122 | + 'job_id' => $job->id, |
| 123 | + 'tag' => trim($tag), |
| 124 | + 'created_at' => $job->created_at, |
| 125 | + ]; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // Batch insert for performance |
| 131 | + if (! empty($records)) { |
| 132 | + try { |
| 133 | + // Insert in smaller batches to avoid MySQL max_allowed_packet issues |
| 134 | + foreach (array_chunk($records, 500) as $batch) { |
| 135 | + DB::connection($this->getConnectionName()) |
| 136 | + ->table('vantage_job_tags') |
| 137 | + ->insert($batch); |
| 138 | + $tagsInserted += count($batch); |
| 139 | + } |
| 140 | + } catch (\Throwable $e) { |
| 141 | + $errors++; |
| 142 | + // Log but continue |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + $bar->setMessage("{$tagsInserted} tags inserted"); |
| 147 | + $bar->setProgress($processed); |
| 148 | + }); |
| 149 | + |
| 150 | + $bar->finish(); |
| 151 | + $this->line(''); |
| 152 | + $this->line(''); |
| 153 | + |
| 154 | + // Summary |
| 155 | + $this->info('✓ Backfill completed!'); |
| 156 | + $this->line(''); |
| 157 | + $this->table( |
| 158 | + ['Metric', 'Value'], |
| 159 | + [ |
| 160 | + ['Jobs processed', number_format($processed)], |
| 161 | + ['Tags inserted', number_format($tagsInserted)], |
| 162 | + ['Avg tags per job', $processed > 0 ? round($tagsInserted / $processed, 2) : 0], |
| 163 | + ['Errors', $errors], |
| 164 | + ] |
| 165 | + ); |
| 166 | + |
| 167 | + // Show optimization tip |
| 168 | + if ($aggregator->supportsEfficientJsonOperations()) { |
| 169 | + $this->line(''); |
| 170 | + $this->info('✓ Your database now supports efficient tag aggregation!'); |
| 171 | + $this->line(' Dashboard tag queries will be ~100x faster for large datasets.'); |
| 172 | + } else { |
| 173 | + $this->line(''); |
| 174 | + $this->warn('Note: Your database driver does not support efficient JSON operations.'); |
| 175 | + $this->line(' The tags table will be used as a fallback for fast aggregation.'); |
| 176 | + } |
| 177 | + |
| 178 | + return self::SUCCESS; |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Get the database connection name. |
| 183 | + */ |
| 184 | + protected function getConnectionName(): ?string |
| 185 | + { |
| 186 | + return config('vantage.database_connection'); |
| 187 | + } |
| 188 | +} |
0 commit comments