|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Illuminate\Console\Command; |
|
7
|
|
|
|
|
8
|
|
|
class NntmuxCheckIndex extends Command |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* The name and signature of the console command. |
|
12
|
|
|
* |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $signature = 'nntmux:check-index |
|
16
|
|
|
{--manticore : Check ManticoreSearch} |
|
17
|
|
|
{--elastic : Check ElasticSearch} |
|
18
|
|
|
{--releases : Check the releases index} |
|
19
|
|
|
{--predb : Check the predb index}'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The console command description. |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $description = 'Check if data exists in search indexes'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Execute the console command. |
|
30
|
|
|
*/ |
|
31
|
|
|
public function handle(): int |
|
32
|
|
|
{ |
|
33
|
|
|
$engine = $this->getSelectedEngine(); |
|
34
|
|
|
$index = $this->getSelectedIndex(); |
|
35
|
|
|
|
|
36
|
|
|
if (! $engine || ! $index) { |
|
37
|
|
|
$this->error('You must specify both an engine (--manticore or --elastic) and an index (--releases or --predb).'); |
|
38
|
|
|
|
|
39
|
|
|
return Command::FAILURE; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$this->info("Checking {$engine} {$index} index..."); |
|
43
|
|
|
|
|
44
|
|
|
try { |
|
45
|
|
|
if ($engine === 'elastic') { |
|
46
|
|
|
$this->checkElasticIndex($index); |
|
47
|
|
|
} else { |
|
48
|
|
|
$this->checkManticoreIndex($index); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return Command::SUCCESS; |
|
52
|
|
|
} catch (Exception $e) { |
|
53
|
|
|
$this->error("Error checking index: {$e->getMessage()}"); |
|
54
|
|
|
|
|
55
|
|
|
return Command::FAILURE; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Check ElasticSearch index |
|
61
|
|
|
*/ |
|
62
|
|
|
private function checkElasticIndex(string $index): void |
|
63
|
|
|
{ |
|
64
|
|
|
try { |
|
65
|
|
|
// Check if index exists |
|
66
|
|
|
$exists = \Elasticsearch::indices()->exists(['index' => $index]); |
|
67
|
|
|
|
|
68
|
|
|
if (! $exists) { |
|
69
|
|
|
$this->error("ElasticSearch index '{$index}' does not exist."); |
|
70
|
|
|
|
|
71
|
|
|
return; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$this->info("ElasticSearch index '{$index}' exists."); |
|
75
|
|
|
|
|
76
|
|
|
// Get index stats |
|
77
|
|
|
$stats = \Elasticsearch::indices()->stats(['index' => $index]); |
|
78
|
|
|
$docCount = $stats['indices'][$index]['total']['docs']['count'] ?? 0; |
|
79
|
|
|
$storeSize = $stats['indices'][$index]['total']['store']['size_in_bytes'] ?? 0; |
|
80
|
|
|
|
|
81
|
|
|
$this->info('Document count: '.number_format($docCount)); |
|
82
|
|
|
$this->info('Index size: '.$this->formatBytes($storeSize)); |
|
83
|
|
|
|
|
84
|
|
|
// Get a sample document |
|
85
|
|
|
if ($docCount > 0) { |
|
86
|
|
|
$sample = \Elasticsearch::search([ |
|
87
|
|
|
'index' => $index, |
|
88
|
|
|
'size' => 1, |
|
89
|
|
|
'body' => [ |
|
90
|
|
|
'query' => ['match_all' => (object) []], |
|
91
|
|
|
], |
|
92
|
|
|
]); |
|
93
|
|
|
|
|
94
|
|
|
if (isset($sample['hits']['hits'][0])) { |
|
95
|
|
|
$this->info('Sample document:'); |
|
96
|
|
|
$this->info(json_encode($sample['hits']['hits'][0]['_source'], JSON_PRETTY_PRINT)); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
} catch (Exception $e) { |
|
101
|
|
|
$this->error("Error checking ElasticSearch index: {$e->getMessage()}"); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Check ManticoreSearch index |
|
107
|
|
|
*/ |
|
108
|
|
|
private function checkManticoreIndex(string $index): void |
|
109
|
|
|
{ |
|
110
|
|
|
try { |
|
111
|
|
|
$indexName = $index === 'releases' ? 'releases_rt' : 'predb_rt'; |
|
112
|
|
|
|
|
113
|
|
|
// This is a basic check - you may need to adjust based on your ManticoreSearch setup |
|
114
|
|
|
$this->info("Checking ManticoreSearch index '{$indexName}'..."); |
|
115
|
|
|
$this->warn('ManticoreSearch index checking not fully implemented yet.'); |
|
116
|
|
|
|
|
117
|
|
|
} catch (Exception $e) { |
|
118
|
|
|
$this->error("Error checking ManticoreSearch index: {$e->getMessage()}"); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Format bytes to human readable format |
|
124
|
|
|
*/ |
|
125
|
|
|
private function formatBytes(int $bytes): string |
|
126
|
|
|
{ |
|
127
|
|
|
$units = ['B', 'KB', 'MB', 'GB', 'TB']; |
|
128
|
|
|
$bytes = max($bytes, 0); |
|
129
|
|
|
$pow = floor(($bytes ? log($bytes) : 0) / log(1024)); |
|
130
|
|
|
$pow = min($pow, count($units) - 1); |
|
131
|
|
|
|
|
132
|
|
|
$bytes /= pow(1024, $pow); |
|
133
|
|
|
|
|
134
|
|
|
return round($bytes, 2).' '.$units[$pow]; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Get selected engine |
|
139
|
|
|
*/ |
|
140
|
|
|
private function getSelectedEngine(): ?string |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->option('manticore') ? 'manticore' : ($this->option('elastic') ? 'elastic' : null); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Get selected index |
|
147
|
|
|
*/ |
|
148
|
|
|
private function getSelectedIndex(): ?string |
|
149
|
|
|
{ |
|
150
|
|
|
return $this->option('releases') ? 'releases' : ($this->option('predb') ? 'predb' : null); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|