Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiio committed Oct 26, 2024
1 parent 2aab30d commit 7af863f
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 26 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,9 @@ AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

VITE_APP_NAME="${APP_NAME}"

# SCOUT_DRIVER=typesense
# TYPESENSE_HOST=127.0.0.1
# TYPESENSE_PORT=8108
# TYPESENSE_PROTOCOL=http
# TYPESENSE_API_KEY=xyz
38 changes: 38 additions & 0 deletions app/Console/Commands/DeleteAllIndexesCommand.php
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;
}
}
39 changes: 39 additions & 0 deletions app/Console/Commands/RebuildScoutCommand.php
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 app/Jobs/Europarl240609/Turnout/ImportAbroadTurnoutJob.php
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',
];
}
}
37 changes: 13 additions & 24 deletions config/scout.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

declare(strict_types=1);

use App\Models\Country;
use App\Models\County;
use App\Models\Locality;

return [

/*
Expand Down Expand Up @@ -176,30 +180,15 @@
'retry_interval_seconds' => env('TYPESENSE_RETRY_INTERVAL_SECONDS', 1),
],
// 'max_total_results' => env('TYPESENSE_MAX_TOTAL_RESULTS', 1000),
'model-settings' => [
// User::class => [
// 'collection-schema' => [
// 'fields' => [
// [
// 'name' => 'id',
// 'type' => 'string',
// ],
// [
// 'name' => 'name',
// 'type' => 'string',
// ],
// [
// 'name' => 'created_at',
// 'type' => 'int64',
// ],
// ],
// 'default_sorting_field' => 'created_at',
// ],
// 'search-parameters' => [
// 'query_by' => 'name'
// ],
// ],
],
'model-settings' => collect([
Locality::class,
County::class,
Country::class,
])
->mapWithKeys(fn (string $model) => [
$model => $model::getTypesenseModelSettings(),
])
->all(),
],

];
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ public function up(): void
$table->float('percent', 2)
->unsigned()
->storedAs(<<<'SQL'
ROUND(total / initial_total * 100, 2)
IF(initial_total > 0, ROUND(total / initial_total * 100, 2), 0)
SQL);

// $table->unique(['election_id', 'locality_id']);
$table->unique(['election_id', 'county_id', 'section']);
$table->unique(['election_id', 'country_id', 'section']);

Expand Down
3 changes: 3 additions & 0 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Models\User;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Artisan;

class DatabaseSeeder extends Seeder
{
Expand Down Expand Up @@ -47,5 +48,7 @@ public function run(): void
// ->withAbroadTurnout()
->recycle($electionTypes)
->create();

Artisan::call('scout:rebuild');
}
}

0 comments on commit 7af863f

Please sign in to comment.