-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
207 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Console\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
|
||
class DeleteAllIndexesCommand extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'scout:delete-all-indexes'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Delete all indexes'; | ||
|
||
/** | ||
* Execute the console command. | ||
*/ | ||
public function handle(): int | ||
{ | ||
collect(config('scout.typesense.model-settings')) | ||
->keys() | ||
->each(function (string $model): void { | ||
$this->call('scout:delete-index', ['name' => (new $model)->searchableAs()]); | ||
}); | ||
|
||
return self::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Console\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
|
||
class RebuildScoutCommand extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'scout:rebuild'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Rebuilds the search index for all searchable models'; | ||
|
||
/** | ||
* Execute the console command. | ||
*/ | ||
public function handle(): int | ||
{ | ||
collect(config('scout.typesense.model-settings')) | ||
->keys() | ||
->each(function (string $model): void { | ||
$this->call('scout:flush', ['model' => $model]); | ||
$this->call('scout:import', ['model' => $model]); | ||
}); | ||
|
||
return self::SUCCESS; | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
app/Jobs/Europarl240609/Turnout/ImportAbroadTurnoutJob.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Jobs\Europarl240609\Turnout; | ||
|
||
use App\Exceptions\CountryCodeNotFoundException; | ||
use App\Exceptions\MissingSourceFileException; | ||
use App\Models\Country; | ||
use App\Models\ScheduledJob; | ||
use App\Models\Turnout; | ||
use Illuminate\Bus\Batchable; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Queue\SerializesModels; | ||
use League\Csv\Reader; | ||
use Throwable; | ||
|
||
class ImportAbroadTurnoutJob implements ShouldQueue | ||
{ | ||
use Batchable; | ||
use Dispatchable; | ||
use InteractsWithQueue; | ||
use Queueable; | ||
use SerializesModels; | ||
|
||
public ScheduledJob $scheduledJob; | ||
|
||
public function __construct(ScheduledJob $scheduledJob) | ||
{ | ||
$this->scheduledJob = $scheduledJob; | ||
} | ||
|
||
public function handle(): void | ||
{ | ||
$disk = $this->scheduledJob->disk(); | ||
$path = $this->scheduledJob->getSourcePath('SR.csv'); | ||
|
||
if (! $disk->exists($path)) { | ||
throw new MissingSourceFileException($path); | ||
} | ||
|
||
$reader = Reader::createFromStream($disk->readStream($path)); | ||
$reader->setHeaderOffset(0); | ||
|
||
$values = collect(); | ||
|
||
$segments = Turnout::segmentsMap(); | ||
|
||
$records = $reader->getRecords(); | ||
foreach ($records as $record) { | ||
try { | ||
$values->push([ | ||
'election_id' => $this->scheduledJob->election_id, | ||
'country_id' => $this->getCountryId($record['UAT']), | ||
'section' => $record['Nr sectie de votare'], | ||
|
||
'initial_permanent' => $record['Înscriși pe liste permanente'], | ||
'initial_complement' => 0, | ||
'permanent' => $record['LP'], | ||
'complement' => $record['LC'], | ||
'supplement' => $record['LS'], | ||
'mobile' => $record['UM'], | ||
|
||
'area' => $record['Mediu'], | ||
|
||
...$segments->map(fn (string $segment) => $record[$segment]), | ||
]); | ||
} catch (CountryCodeNotFoundException $th) { | ||
logger()->info($th->getMessage()); | ||
} catch (Throwable $th) { | ||
// TODO: filament notification | ||
} | ||
} | ||
|
||
Turnout::saveToTemporaryTable($values->all()); | ||
} | ||
|
||
protected function getCountryId(string $name): string | ||
{ | ||
$country = Country::search($name)->first(); | ||
|
||
if (! $country) { | ||
throw new CountryCodeNotFoundException($name); | ||
} | ||
|
||
return $country->id; | ||
} | ||
|
||
/** | ||
* Get the tags that should be assigned to the job. | ||
* | ||
* @return array<int, string> | ||
*/ | ||
public function tags(): array | ||
{ | ||
return [ | ||
'import', | ||
'turnout', | ||
'scheduled_job:' . $this->scheduledJob->id, | ||
'election:' . $this->scheduledJob->election_id, | ||
'abroad', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters