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
|
|
|
$host = config('nntmux.manticore.host', '127.0.0.1'); |
113
|
|
|
$port = config('nntmux.manticore.port', 9308); |
114
|
|
|
|
115
|
|
|
// Use HTTP API endpoint |
116
|
|
|
$baseUrl = "http://{$host}:{$port}"; |
117
|
|
|
|
118
|
|
|
// Check if table exists using HTTP API |
119
|
|
|
$client = new \GuzzleHttp\Client([ |
120
|
|
|
'base_uri' => $baseUrl, |
121
|
|
|
'timeout' => 10, |
122
|
|
|
'http_errors' => false, // Don't throw on HTTP errors |
123
|
|
|
]); |
124
|
|
|
|
125
|
|
|
// Use raw mode which seems to work |
126
|
|
|
$query = "SELECT COUNT(*) FROM {$indexName}"; |
127
|
|
|
|
128
|
|
|
$response = $client->post('/sql?mode=raw', [ |
129
|
|
|
'body' => $query, |
130
|
|
|
'headers' => [ |
131
|
|
|
'Content-Type' => 'text/plain', |
132
|
|
|
], |
133
|
|
|
]); |
134
|
|
|
|
135
|
|
|
$statusCode = $response->getStatusCode(); |
136
|
|
|
$body = $response->getBody()->getContents(); |
137
|
|
|
|
138
|
|
|
if ($statusCode !== 200) { |
139
|
|
|
$this->error("HTTP Status: {$statusCode}"); |
140
|
|
|
|
141
|
|
|
// Check if it's a table not found error |
142
|
|
|
if (strpos($body, 'no such table') !== false || |
143
|
|
|
strpos($body, 'unknown table') !== false || |
144
|
|
|
strpos($body, "doesn't exist") !== false) { |
145
|
|
|
$this->error("ManticoreSearch table '{$indexName}' does not exist."); |
146
|
|
|
|
147
|
|
|
return; |
148
|
|
|
} |
149
|
|
|
$this->error("Response: {$body}"); |
150
|
|
|
|
151
|
|
|
return; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
// Parse the JSON response |
155
|
|
|
$jsonData = json_decode($body, true); |
156
|
|
|
$docCount = 0; |
157
|
|
|
|
158
|
|
|
if ($jsonData !== null && isset($jsonData['data'])) { |
159
|
|
|
// ManticoreSearch returns data as array of arrays |
160
|
|
|
$docCount = $jsonData['data'][0]['COUNT(*)'] ?? $jsonData['data'][0][0] ?? 0; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$this->info("ManticoreSearch table '{$indexName}' exists."); |
164
|
|
|
$this->info('Document count: '.number_format($docCount)); |
165
|
|
|
|
166
|
|
|
// Get a sample document if there are any |
167
|
|
|
if ($docCount > 0) { |
168
|
|
|
$sampleQuery = "SELECT * FROM {$indexName} LIMIT 1"; |
169
|
|
|
|
170
|
|
|
$sampleResponse = $client->post('/sql?mode=raw', [ |
171
|
|
|
'body' => $sampleQuery, |
172
|
|
|
'headers' => [ |
173
|
|
|
'Content-Type' => 'text/plain', |
174
|
|
|
], |
175
|
|
|
]); |
176
|
|
|
|
177
|
|
|
if ($sampleResponse->getStatusCode() === 200) { |
178
|
|
|
$sampleBody = $sampleResponse->getBody()->getContents(); |
179
|
|
|
$sampleJson = json_decode($sampleBody, true); |
180
|
|
|
|
181
|
|
|
if ($sampleJson !== null && isset($sampleJson['data'][0])) { |
182
|
|
|
$this->info('Sample document:'); |
183
|
|
|
|
184
|
|
|
$document = []; |
185
|
|
|
$columns = $sampleJson['columns'] ?? []; |
186
|
|
|
$data = $sampleJson['data'][0] ?? []; |
187
|
|
|
|
188
|
|
|
foreach ($columns as $i => $column) { |
189
|
|
|
// Extract column name from the nested structure |
190
|
|
|
$columnName = array_keys($column)[0]; |
191
|
|
|
$document[$columnName] = $data[$i] ?? null; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$this->info(json_encode($document, JSON_PRETTY_PRINT)); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
// Get table structure |
200
|
|
|
try { |
201
|
|
|
$describeQuery = "DESCRIBE {$indexName}"; |
202
|
|
|
$describeResponse = $client->post('/sql?mode=raw', [ |
203
|
|
|
'body' => $describeQuery, |
204
|
|
|
'headers' => [ |
205
|
|
|
'Content-Type' => 'text/plain', |
206
|
|
|
], |
207
|
|
|
]); |
208
|
|
|
|
209
|
|
|
if ($describeResponse->getStatusCode() === 200) { |
210
|
|
|
$describeBody = $describeResponse->getBody()->getContents(); |
211
|
|
|
$describeJson = json_decode($describeBody, true); |
212
|
|
|
|
213
|
|
|
if ($describeJson !== null && isset($describeJson['data'])) { |
214
|
|
|
$this->info("\nTable structure:"); |
215
|
|
|
$this->table( |
216
|
|
|
['Field', 'Type', 'Properties'], |
217
|
|
|
array_map(function ($row) { |
218
|
|
|
return [ |
219
|
|
|
$row['Field'] ?? $row[0] ?? '', |
220
|
|
|
$row['Type'] ?? $row[1] ?? '', |
221
|
|
|
$row['Properties'] ?? $row[2] ?? '', |
222
|
|
|
]; |
223
|
|
|
}, $describeJson['data']) |
224
|
|
|
); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
} catch (\Exception $e) { |
228
|
|
|
// Ignore describe errors |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
} catch (\Exception $e) { |
232
|
|
|
$this->error("Error checking ManticoreSearch table: {$e->getMessage()}"); |
233
|
|
|
if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) { |
234
|
|
|
$this->error('Response body: '.$e->getResponse()->getBody()); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Format bytes to human readable format |
241
|
|
|
*/ |
242
|
|
|
private function formatBytes(int $bytes): string |
243
|
|
|
{ |
244
|
|
|
$units = ['B', 'KB', 'MB', 'GB', 'TB']; |
245
|
|
|
$bytes = max($bytes, 0); |
246
|
|
|
$pow = floor(($bytes ? log($bytes) : 0) / log(1024)); |
247
|
|
|
$pow = min($pow, count($units) - 1); |
248
|
|
|
|
249
|
|
|
$bytes /= pow(1024, $pow); |
250
|
|
|
|
251
|
|
|
return round($bytes, 2).' '.$units[$pow]; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Get selected engine |
256
|
|
|
*/ |
257
|
|
|
private function getSelectedEngine(): ?string |
258
|
|
|
{ |
259
|
|
|
return $this->option('manticore') ? 'manticore' : ($this->option('elastic') ? 'elastic' : null); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Get selected index |
264
|
|
|
*/ |
265
|
|
|
private function getSelectedIndex(): ?string |
266
|
|
|
{ |
267
|
|
|
return $this->option('releases') ? 'releases' : ($this->option('predb') ? 'predb' : null); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|