Browse Source

Run SD from command line on a directory

master
Orzu Ionut 3 years ago
parent
commit
30a838d29e
  1. 92
      app/Console/Commands/RunSearchDisplace.php
  2. 36
      app/Events/SDFailedToApply.php
  3. 24
      app/Listeners/AnalyzeOperationPerformance.php
  4. 5
      app/Providers/EventServiceProvider.php

92
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;
}
}

36
app/Events/SDFailedToApply.php

@ -0,0 +1,36 @@
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class SDFailedToApply
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}

24
app/Listeners/AnalyzeOperationPerformance.php

@ -29,17 +29,25 @@ class AnalyzeOperationPerformance
{
$redis = Redis::connection();
$directoryPath = $redis->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);
}
}

5
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,
],
];
/**

Loading…
Cancel
Save