|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PDPhilip\Elasticsearch\Commands; |
| 6 | + |
| 7 | +use Exception; |
| 8 | +use Illuminate\Console\Command; |
| 9 | +use Illuminate\Support\Facades\DB; |
| 10 | +use OmniTerm\HasOmniTerm; |
| 11 | +use PDPhilip\Elasticsearch\Connection; |
| 12 | + |
| 13 | +class IndicesCommand extends Command |
| 14 | +{ |
| 15 | + use HasOmniTerm; |
| 16 | + |
| 17 | + protected $signature = 'elastic:indices {--all : Show all indices, not just prefixed} {--connection=elasticsearch}'; |
| 18 | + |
| 19 | + protected $description = 'List all Elasticsearch indices'; |
| 20 | + |
| 21 | + public function handle(): int |
| 22 | + { |
| 23 | + $connectionName = $this->option('connection'); |
| 24 | + $showAll = $this->option('all'); |
| 25 | + |
| 26 | + $this->newLine(); |
| 27 | + $this->omni->roundedBox('Elasticsearch Indices', 'text-cyan-500', 'text-cyan-500'); |
| 28 | + $this->newLine(); |
| 29 | + |
| 30 | + try { |
| 31 | + /** @var Connection $connection */ |
| 32 | + $connection = DB::connection($connectionName); |
| 33 | + $schema = $connection->getSchemaBuilder(); |
| 34 | + |
| 35 | + if ($showAll) { |
| 36 | + $indices = $connection->getPostProcessor()->processTables( |
| 37 | + $connection->catIndices(['index' => '*', 'format' => 'json']) |
| 38 | + ); |
| 39 | + } else { |
| 40 | + $indices = $schema->getTables(); |
| 41 | + } |
| 42 | + } catch (Exception $e) { |
| 43 | + $this->omni->statusError('ERROR', $e->getMessage()); |
| 44 | + $this->newLine(); |
| 45 | + |
| 46 | + return self::FAILURE; |
| 47 | + } |
| 48 | + |
| 49 | + $prefix = $connection->getIndexPrefix(); |
| 50 | + |
| 51 | + $this->omni->tableRow('Connection', $connectionName); |
| 52 | + $this->omni->tableRow('Index Prefix', $prefix ?: '(none)'); |
| 53 | + if ($showAll) { |
| 54 | + $this->omni->tableRow('Filter', 'All indices'); |
| 55 | + } |
| 56 | + $this->newLine(); |
| 57 | + |
| 58 | + if (empty($indices)) { |
| 59 | + $this->omni->warning('No indices found'); |
| 60 | + $this->newLine(); |
| 61 | + |
| 62 | + return self::SUCCESS; |
| 63 | + } |
| 64 | + |
| 65 | + $this->omni->tableHeader('Index', 'Health', 'Docs / Size'); |
| 66 | + |
| 67 | + $totalDocs = 0; |
| 68 | + |
| 69 | + foreach ($indices as $index) { |
| 70 | + $name = $index['name']; |
| 71 | + $health = $index['health'] ?? 'unknown'; |
| 72 | + $docsCount = (int) ($index['docs_count'] ?? 0); |
| 73 | + $storeSize = $index['store_size'] ?? '0b'; |
| 74 | + |
| 75 | + $totalDocs += $docsCount; |
| 76 | + |
| 77 | + $displayName = $name; |
| 78 | + if (! $showAll && $prefix && str_starts_with($name, $prefix)) { |
| 79 | + $displayName = substr($name, strlen($prefix)); |
| 80 | + } |
| 81 | + |
| 82 | + $details = number_format($docsCount).' docs / '.$storeSize; |
| 83 | + |
| 84 | + match ($health) { |
| 85 | + 'green' => $this->omni->tableRowSuccess($displayName, $details), |
| 86 | + 'yellow' => $this->omni->tableRowWarning($displayName, $details), |
| 87 | + 'red' => $this->omni->tableRowError($displayName, $details), |
| 88 | + default => $this->omni->tableRow($displayName, $health, $details), |
| 89 | + }; |
| 90 | + } |
| 91 | + |
| 92 | + $this->newLine(); |
| 93 | + $this->omni->info(count($indices).' indices, '.number_format($totalDocs).' total documents'); |
| 94 | + $this->newLine(); |
| 95 | + |
| 96 | + return self::SUCCESS; |
| 97 | + } |
| 98 | +} |
0 commit comments