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 80e03c4 commit 2aab30d
Show file tree
Hide file tree
Showing 14 changed files with 295 additions and 482 deletions.
20 changes: 20 additions & 0 deletions app/Concerns/HasTemporaryTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace App\Concerns;

use Illuminate\Support\Facades\DB;

trait HasTemporaryTable
{
public function getTemporaryTable(): string
{
return '_temp_' . $this->getTable();
}

public static function saveToTemporaryTable(array $values): void
{
DB::table((new static())->getTemporaryTable())->insert($values);
}
}
14 changes: 14 additions & 0 deletions app/Contracts/TemporaryTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace App\Contracts;

interface TemporaryTable
{
public function getTemporaryTable(): string;

public function getTemporaryTableUniqueColumns(): array;

public static function saveToTemporaryTable(array $values): void;
}
15 changes: 15 additions & 0 deletions app/Exceptions/CountryCodeNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace App\Exceptions;

use Exception;

class CountryCodeNotFoundException extends Exception
{
public function __construct(string $name)
{
parent::__construct("Could not find country code for: {$name}");
}
}
45 changes: 45 additions & 0 deletions app/Jobs/DeleteTemporaryTableData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace App\Jobs;

use App\Contracts\TemporaryTable;
use Exception;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\DB;

class DeleteTemporaryTableData implements ShouldQueue
{
use Queueable;

public string $model;

public int $electionId;

/**
* Create a new job instance.
*/
public function __construct(string $model, int $electionId)
{
$this->model = $model;
$this->electionId = $electionId;
}

/**
* Execute the job.
*/
public function handle(): void
{
$model = new $this->model;

if (! $model instanceof TemporaryTable) {
throw new Exception('Model must implement TemporaryTable contract');
}

DB::table($model->getTemporaryTable())
->where('election_id', $this->electionId)
->delete();
}
}
30 changes: 19 additions & 11 deletions app/Jobs/Europarl240609/Turnout/FetchTurnoutDataJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

namespace App\Jobs\Europarl240609\Turnout;

use App\Jobs\DeleteTemporaryTableData;
use App\Jobs\PersistTemporaryTableData;
use App\Jobs\SchedulableJob;
use App\Models\County;
use App\Models\Turnout;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Process;
use Illuminate\Support\Facades\Storage;
Expand Down Expand Up @@ -55,20 +58,25 @@ public function execute(): void
$counties = County::all();

$electionName = $this->scheduledJob->election->getFilamentName();
$electionId = $this->scheduledJob->election_id;

$time = now()->toDateTimeString();

Bus::chain([
Bus::batch(
$counties
->map(fn (County $county) => new ImportCountyTurnoutJob($this->scheduledJob, $county))
)->name("$electionName / Prezență / $time"),

Bus::batch(
$counties
->map(fn (County $county) => new ImportCountyStatisticsJob($this->scheduledJob, $county))
)->name("$electionName / Statistici / $time"),
])->onQueue('sequential')->dispatch();
$jobs = $counties
->map(fn (County $county) => new ImportCountyTurnoutJob($this->scheduledJob, $county))
->push(new ImportAbroadTurnoutJob($this->scheduledJob));

$persistAndClean = fn () => Bus::chain([
new PersistTemporaryTableData(Turnout::class, $electionId),
new DeleteTemporaryTableData(Turnout::class, $electionId),
])->dispatch();

Bus::batch($jobs)
->catch($persistAndClean)
->then($persistAndClean)
->name("$electionName / Prezență / $time")
->allowFailures()
->dispatch();
}

/**
Expand Down
108 changes: 0 additions & 108 deletions app/Jobs/Europarl240609/Turnout/ImportCountyStatisticsJob.php

This file was deleted.

18 changes: 7 additions & 11 deletions app/Jobs/Europarl240609/Turnout/ImportCountyTurnoutJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\Middleware\SkipIfBatchCancelled;
use Illuminate\Queue\SerializesModels;
use League\Csv\Reader;

Expand Down Expand Up @@ -49,6 +48,8 @@ public function handle(): void

$values = collect();

$segments = Turnout::segmentsMap();

$records = $reader->getRecords();
foreach ($records as $record) {
$values->push([
Expand All @@ -63,19 +64,14 @@ public function handle(): void
'complement' => $record['LC'],
'supplement' => $record['LS'],
'mobile' => $record['UM'],

'area' => $record['Mediu'],

...$segments->map(fn (string $segment) => $record[$segment]),
]);
}

Turnout::upsert(
$values->all(),
['election_id', 'county_id', 'section'],
['initial_permanent', 'initial_complement', 'permanent', 'complement', 'supplement', 'mobile']
);
}

public function middleware(): array
{
return [new SkipIfBatchCancelled];
Turnout::saveToTemporaryTable($values->all());
}

/**
Expand Down
59 changes: 59 additions & 0 deletions app/Jobs/PersistTemporaryTableData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace App\Jobs;

use App\Contracts\TemporaryTable;
use Exception;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\DB;

class PersistTemporaryTableData implements ShouldQueue
{
use Queueable;

public string $model;

public int $electionId;

/**
* Create a new job instance.
*/
public function __construct(string $model, int $electionId)
{
$this->model = $model;
$this->electionId = $electionId;
}

/**
* Execute the job.
*/
public function handle(): void
{
$model = new $this->model;

if (! $model instanceof TemporaryTable) {
throw new Exception('Model must implement TemporaryTable contract');
}

$columns = collect($model->getFillable())
->sort();

$selectColumns = $columns
->map(fn (string $column) => "`$column`")
->implode(', ');

$updateColumns = $columns
->reject(fn (string $column) => \in_array($column, $model->getTemporaryTableUniqueColumns()))
->map(fn (string $column) => "`$column` = `{$model->getTemporaryTable()}`.`$column`")
->implode(', ');

DB::unprepared(<<<"SQL"
INSERT INTO `{$model->getTable()}` ({$selectColumns})
SELECT {$selectColumns} FROM `{$model->getTemporaryTable()}` WHERE `election_id` = {$this->electionId}
ON DUPLICATE KEY UPDATE {$updateColumns};
SQL);
}
}
2 changes: 1 addition & 1 deletion app/Models/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static function getTypesenseModelSettings(): array
],
],
'search-parameters' => [
'query_by' => 'name',
'query_by' => 'name,aliases',
],
];
}
Expand Down
Loading

0 comments on commit 2aab30d

Please sign in to comment.