Skip to content

Commit a9a34e0

Browse files
committed
added alpha scout:status command
1 parent 9164556 commit a9a34e0

File tree

2 files changed

+162
-2
lines changed

2 files changed

+162
-2
lines changed

src/Console/StatusCommand.php

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
<?php
2+
3+
namespace TeamTNT\Scout\Console;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Contracts\Events\Dispatcher;
7+
use TeamTNT\TNTSearch\Exceptions\IndexNotFoundException;
8+
use TeamTNT\TNTSearch\Indexer\TNTIndexer;
9+
use TeamTNT\TNTSearch\TNTSearch;
10+
use Illuminate\Support\Facades\Schema;
11+
use Symfony\Component\Finder\Finder;
12+
13+
class StatusCommand extends Command
14+
{
15+
/**
16+
* The name and signature of the console command.
17+
*
18+
* @var string
19+
*/
20+
protected $signature = 'scout:status {model? : The name of the model}';
21+
22+
/**
23+
* The console command description.
24+
*
25+
* @var string
26+
*/
27+
protected $description = 'Status of the search index by TNTSeach';
28+
29+
/**
30+
* @var array
31+
*/
32+
private static $declaredClasses;
33+
34+
35+
/**
36+
* Execute the console command.
37+
*
38+
* @param \Illuminate\Contracts\Events\Dispatcher $events
39+
*
40+
* @return void
41+
*/
42+
public function handle(Dispatcher $events)
43+
{
44+
45+
$searchableModels = $this->getSearchableModels();
46+
47+
$this->output->text('🔎 Analysing information from: <info>['.implode(',', $searchableModels).']</info>');
48+
$this->output->newLine();
49+
$this->output->progressStart(count($searchableModels));
50+
51+
$headers = ['Searchable', 'Index', 'Indexed Columns', 'Index Records', 'DB Records', 'Records difference'];
52+
$rows = [];
53+
foreach ($searchableModels as $class) {
54+
$model = new $class();
55+
56+
$tnt = $this->loadTNTEngine($model);
57+
$indexName = $model->searchableAs().'.index';
58+
59+
try {
60+
$tnt->selectIndex($indexName);
61+
$rowsIndexed = $tnt->totalDocumentsInCollection();
62+
} catch (IndexNotFoundException $e) {
63+
$rowsIndexed = 0;
64+
}
65+
66+
$indexedColumns = implode(",", array_keys($model->toSearchableArray()));
67+
68+
$rowsTotal = $model->count();
69+
$recordsDifference = $rowsTotal - $rowsIndexed;
70+
71+
if($recordsDifference == 0) {
72+
$recordsDifference = '<fg=green>Synchronized</>';
73+
} else {
74+
$recordsDifference = "<fg=red>$recordsDifference</>";
75+
}
76+
77+
array_push($rows, [$class, $indexName, $indexedColumns, $rowsIndexed, $rowsTotal, $recordsDifference]);
78+
79+
}
80+
81+
$this->output->progressFinish();
82+
$this->output->table($headers, $rows, $tableStyle = 'default', $columnStyles = []);
83+
}
84+
85+
/**
86+
* @return array
87+
*/
88+
private function getProjectClasses(): array
89+
{
90+
91+
if (self::$declaredClasses === null) {
92+
$configFiles = Finder::create()->files()->name('*.php')->notName('*.blade.php')->in(app()->path());
93+
94+
foreach ($configFiles->files() as $file) {
95+
require_once $file;
96+
}
97+
98+
self::$declaredClasses = get_declared_classes();
99+
}
100+
101+
return self::$declaredClasses;
102+
}
103+
104+
/**
105+
* @return array|void
106+
*/
107+
private function getSearchableModelsFromClasses($trait = 'Laravel\Scout\Searchable')
108+
{
109+
$projectClasses = $this->getProjectClasses();
110+
$classes = array_filter(
111+
$projectClasses,
112+
$this->isSearchableModel($trait)
113+
);
114+
115+
return $classes;
116+
}
117+
118+
/**
119+
* @return array
120+
*/
121+
private function getSearchableModels()
122+
{
123+
$searchableModels = (array)$this->argument('model');
124+
if (empty($searchableModels)) {
125+
$searchableModels = $this->getSearchableModelsFromClasses();
126+
}
127+
128+
return $searchableModels;
129+
}
130+
131+
/**
132+
* @param $trait
133+
* @return \Closure
134+
*/
135+
private function isSearchableModel($trait)
136+
{
137+
return function ($className) use ($trait) {
138+
$traits = class_uses($className);
139+
140+
return isset($traits[$trait]);
141+
};
142+
}
143+
144+
/**
145+
* @param $model
146+
* @return TNTSearch
147+
*/
148+
private function loadTNTEngine($model)
149+
{
150+
$tnt = new TNTSearch();
151+
152+
$driver = $model->getConnectionName() ?: config('database.default');
153+
$config = config('scout.tntsearch') + config("database.connections.$driver");
154+
$tnt->loadConfig($config);
155+
156+
return $tnt;
157+
}
158+
}

src/TNTSearchScoutServiceProvider.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace TeamTNT\Scout;
22

3+
use TeamTNT\Scout\Console\StatusCommand;
34
use TeamTNT\TNTSearch\TNTSearch;
45
use Laravel\Scout\EngineManager;
56
use Laravel\Scout\Builder;
@@ -18,13 +19,13 @@ public function boot()
1819
{
1920
$this->app[EngineManager::class]->extend('tntsearch', function ($app) {
2021
$tnt = new TNTSearch();
21-
22+
2223
$driver = config('database.default');
2324
$config = config('scout.tntsearch') + config("database.connections.{$driver}");
2425

2526
$tnt->loadConfig($config);
2627
$tnt->setDatabaseHandle(app('db')->connection()->getPdo());
27-
28+
2829
$this->setFuzziness($tnt);
2930
$this->setAsYouType($tnt);
3031

@@ -34,6 +35,7 @@ public function boot()
3435
if ($this->app->runningInConsole()) {
3536
$this->commands([
3637
ImportCommand::class,
38+
StatusCommand::class
3739
]);
3840
}
3941

0 commit comments

Comments
 (0)