Skip to content

Commit

Permalink
added alpha scout:status command
Browse files Browse the repository at this point in the history
  • Loading branch information
stokic committed May 12, 2020
1 parent 9164556 commit a9a34e0
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 2 deletions.
158 changes: 158 additions & 0 deletions src/Console/StatusCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php

namespace TeamTNT\Scout\Console;

use Illuminate\Console\Command;
use Illuminate\Contracts\Events\Dispatcher;
use TeamTNT\TNTSearch\Exceptions\IndexNotFoundException;
use TeamTNT\TNTSearch\Indexer\TNTIndexer;
use TeamTNT\TNTSearch\TNTSearch;
use Illuminate\Support\Facades\Schema;
use Symfony\Component\Finder\Finder;

class StatusCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'scout:status {model? : The name of the model}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Status of the search index by TNTSeach';

/**
* @var array
*/
private static $declaredClasses;


/**
* Execute the console command.
*
* @param \Illuminate\Contracts\Events\Dispatcher $events
*
* @return void
*/
public function handle(Dispatcher $events)
{

$searchableModels = $this->getSearchableModels();

$this->output->text('🔎 Analysing information from: <info>['.implode(',', $searchableModels).']</info>');
$this->output->newLine();
$this->output->progressStart(count($searchableModels));

$headers = ['Searchable', 'Index', 'Indexed Columns', 'Index Records', 'DB Records', 'Records difference'];
$rows = [];
foreach ($searchableModels as $class) {
$model = new $class();

$tnt = $this->loadTNTEngine($model);
$indexName = $model->searchableAs().'.index';

try {
$tnt->selectIndex($indexName);
$rowsIndexed = $tnt->totalDocumentsInCollection();
} catch (IndexNotFoundException $e) {
$rowsIndexed = 0;
}

$indexedColumns = implode(",", array_keys($model->toSearchableArray()));

$rowsTotal = $model->count();
$recordsDifference = $rowsTotal - $rowsIndexed;

if($recordsDifference == 0) {
$recordsDifference = '<fg=green>Synchronized</>';
} else {
$recordsDifference = "<fg=red>$recordsDifference</>";
}

array_push($rows, [$class, $indexName, $indexedColumns, $rowsIndexed, $rowsTotal, $recordsDifference]);

}

$this->output->progressFinish();
$this->output->table($headers, $rows, $tableStyle = 'default', $columnStyles = []);
}

/**
* @return array
*/
private function getProjectClasses(): array
{

if (self::$declaredClasses === null) {
$configFiles = Finder::create()->files()->name('*.php')->notName('*.blade.php')->in(app()->path());

foreach ($configFiles->files() as $file) {
require_once $file;
}

self::$declaredClasses = get_declared_classes();
}

return self::$declaredClasses;
}

/**
* @return array|void
*/
private function getSearchableModelsFromClasses($trait = 'Laravel\Scout\Searchable')
{
$projectClasses = $this->getProjectClasses();
$classes = array_filter(
$projectClasses,
$this->isSearchableModel($trait)
);

return $classes;
}

/**
* @return array
*/
private function getSearchableModels()
{
$searchableModels = (array)$this->argument('model');
if (empty($searchableModels)) {
$searchableModels = $this->getSearchableModelsFromClasses();
}

return $searchableModels;
}

/**
* @param $trait
* @return \Closure
*/
private function isSearchableModel($trait)
{
return function ($className) use ($trait) {
$traits = class_uses($className);

return isset($traits[$trait]);
};
}

/**
* @param $model
* @return TNTSearch
*/
private function loadTNTEngine($model)
{
$tnt = new TNTSearch();

$driver = $model->getConnectionName() ?: config('database.default');
$config = config('scout.tntsearch') + config("database.connections.$driver");
$tnt->loadConfig($config);

return $tnt;
}
}
6 changes: 4 additions & 2 deletions src/TNTSearchScoutServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace TeamTNT\Scout;

use TeamTNT\Scout\Console\StatusCommand;
use TeamTNT\TNTSearch\TNTSearch;
use Laravel\Scout\EngineManager;
use Laravel\Scout\Builder;
Expand All @@ -18,13 +19,13 @@ public function boot()
{
$this->app[EngineManager::class]->extend('tntsearch', function ($app) {
$tnt = new TNTSearch();

$driver = config('database.default');
$config = config('scout.tntsearch') + config("database.connections.{$driver}");

$tnt->loadConfig($config);
$tnt->setDatabaseHandle(app('db')->connection()->getPdo());

$this->setFuzziness($tnt);
$this->setAsYouType($tnt);

Expand All @@ -34,6 +35,7 @@ public function boot()
if ($this->app->runningInConsole()) {
$this->commands([
ImportCommand::class,
StatusCommand::class
]);
}

Expand Down

0 comments on commit a9a34e0

Please sign in to comment.