From 30a838d29e688964bc621697e392eb4ecac333d9 Mon Sep 17 00:00:00 2001 From: Orzu Ionut Date: Tue, 21 Sep 2021 11:31:53 +0300 Subject: [PATCH] Run SD from command line on a directory --- app/Console/Commands/RunSearchDisplace.php | 92 +++++++++++++++++-- app/Events/SDFailedToApply.php | 36 ++++++++ app/Listeners/AnalyzeOperationPerformance.php | 24 +++-- app/Providers/EventServiceProvider.php | 5 + 4 files changed, 139 insertions(+), 18 deletions(-) create mode 100644 app/Events/SDFailedToApply.php diff --git a/app/Console/Commands/RunSearchDisplace.php b/app/Console/Commands/RunSearchDisplace.php index 3bb28e0..7713d25 100644 --- a/app/Console/Commands/RunSearchDisplace.php +++ b/app/Console/Commands/RunSearchDisplace.php @@ -2,6 +2,8 @@ namespace App\Console\Commands; +use App\Events\SDFailedToApply; +use App\Listeners\AnalyzeOperationPerformance; use App\SearchDisplace\Ingest\SendDocument; use App\SearchDisplace\SearchAndDisplaceOriginalDocument; use Illuminate\Console\Command; @@ -36,8 +38,6 @@ class RunSearchDisplace extends Command public function __construct() { parent::__construct(); - - // @TODO Add way to handle 'displace'.. Right now we are doing the 'replace' with ':'. } /** @@ -51,21 +51,73 @@ class RunSearchDisplace extends Command $original = $this->option('original'); try { - $this->bootAnalyzer($documentPath); + $isDirectory = is_dir($documentPath); - if ( ! $original) { - $this->runMarkdownOperation($documentPath, $searchers); + if ($isDirectory) { + $this->handleDirectory($documentPath, $original, $searchers); } else { - $this->runOriginalDocumentOperation($documentPath, $searchers); + $this->handleDocument($documentPath, $original, $searchers); } - $this->info('Processing document..'); + $this->info('Processing ' . $isDirectory ? 'directory' : 'document' . '..'); $this->info('After the processing will be done the result will show up at the same path as the input.'); } catch (\Exception $exception) { + \Illuminate\Support\Facades\Log::info($exception->getTraceAsString()); + $this->error('Something went wrong. (' . $exception->getMessage() . ')'); } } + /** + * @param $path + * @param $original + * @param $searchers + * @throws \Exception + */ + protected function handleDirectory($path, $original, $searchers) + { + $allFiles = $this->getDirContents($path); + + $filesCount = count($allFiles); + + $this->bootAnalyzer($path, $filesCount); + + foreach ($allFiles as $file) { + try { + if ( ! $original) { + $this->runMarkdownOperation($file, $searchers); + } else { + $this->runOriginalDocumentOperation($file, $searchers); + } + } catch (\Exception $exception) { + SDFailedToApply::dispatch(); + } + } + } + + /** + * @param $documentPath + * @param $original + * @param $searchers + * @throws \Exception + */ + protected function handleDocument($documentPath, $original, $searchers) + { + $path = pathinfo($documentPath,PATHINFO_DIRNAME); + + $this->bootAnalyzer($path, 1); + + try { + if ( ! $original) { + $this->runMarkdownOperation($documentPath, $searchers); + } else { + $this->runOriginalDocumentOperation($documentPath, $searchers); + } + } catch (\Exception $exception) { + SDFailedToApply::dispatch(); + } + } + /** * @param $documentPath * @param $searchers @@ -113,12 +165,14 @@ class RunSearchDisplace extends Command $storage->put("searchers/$id.json", json_encode($data)); } - protected function bootAnalyzer($filePath) + protected function bootAnalyzer($path, $filesCount) { $redis = Redis::connection(); $redis->set('analyze_performance_time', Carbon::now()->format('U')); - $redis->set('analyze_performance_path', pathinfo($filePath,PATHINFO_DIRNAME)); + $redis->set('analyze_performance_path', $path); + + $redis->set('analyze_performance_remaining_files', $filesCount); } protected function getSearchers($searchers) @@ -206,7 +260,8 @@ class RunSearchDisplace extends Command return $searchersList; } - protected function getFileMimeType($file) { + protected function getFileMimeType($file) + { if (function_exists('finfo_file')) { $finfo = finfo_open(FILEINFO_MIME_TYPE); $type = finfo_file($finfo, $file); @@ -233,4 +288,21 @@ class RunSearchDisplace extends Command return $type; } + + protected function getDirContents($dir, &$results = array()) + { + $files = scandir($dir); + + foreach ($files as $key => $value) { + $path = realpath($dir . DIRECTORY_SEPARATOR . $value); + + if (!is_dir($path)) { + $results[] = $path; + } else if ($value != "." && $value != "..") { + $this->getDirContents($path, $results); + } + } + + return $results; + } } diff --git a/app/Events/SDFailedToApply.php b/app/Events/SDFailedToApply.php new file mode 100644 index 0000000..8d6875f --- /dev/null +++ b/app/Events/SDFailedToApply.php @@ -0,0 +1,36 @@ +get('analyze_performance_path'); - $startedAt = $redis->get('analyze_performance_time'); + $remainingFiles = $redis->get('analyze_performance_remaining_files'); + $remainingFiles -= 1; - if ( ! $directoryPath || ! $startedAt) { - return; - } + if ($remainingFiles === 0) { + $startedAt = $redis->get('analyze_performance_time'); + $endedAt = Carbon::now()->format('U'); + $directoryPath = $redis->get('analyze_performance_path'); + + if ( ! $directoryPath || ! $startedAt) { + return; + } - $endedAt = Carbon::now()->format('U'); + $data = 'Time elapsed in seconds: ' . ($endedAt - $startedAt) . "\n"; - $data = 'Time elapsed in seconds: ' . ($endedAt - $startedAt) . "\n"; + file_put_contents($directoryPath . '/sd_analyze_performance.txt', $data); + + return; + } - file_put_contents($directoryPath . '/sd_analyze_performance.txt', $data); + $redis->set('analyze_performance_remaining_files', $remainingFiles); } } diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 987167c..7cd6320 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -5,6 +5,7 @@ namespace App\Providers; use App\Events\IngestDocumentReceived; use App\Events\SDAppliedOnMarkdownDocument; use App\Events\SDAppliedOnOriginalDocument; +use App\Events\SDFailedToApply; use App\Listeners\AnalyzeOperationPerformance; use App\Listeners\RunSearchAndDisplaceOnDocument; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; @@ -28,6 +29,10 @@ class EventServiceProvider extends ServiceProvider SDAppliedOnMarkdownDocument::class => [ AnalyzeOperationPerformance::class, ], + + SDFailedToApply::class => [ + AnalyzeOperationPerformance::class, + ], ]; /**