-
-
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
16 changed files
with
564 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Enums; | ||
|
||
use App\Concerns\Enums\Arrayable; | ||
use App\Concerns\Enums\Comparable; | ||
use Filament\Support\Contracts\HasLabel; | ||
|
||
enum Area: string implements HasLabel | ||
{ | ||
use Arrayable; | ||
use Comparable; | ||
|
||
case URBAN = 'U'; | ||
case RURAL = 'R'; | ||
|
||
public function getLabel(): ?string | ||
{ | ||
return match ($this) { | ||
self::URBAN => __('app.area.urban'), | ||
self::RURAL => __('app.area.rural'), | ||
}; | ||
} | ||
} |
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,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Exceptions; | ||
|
||
use Exception; | ||
|
||
class MissingSourceFileException extends Exception | ||
{ | ||
public function __construct(string $path) | ||
{ | ||
parent::__construct("Missing source file: {$path}"); | ||
} | ||
} |
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
108 changes: 108 additions & 0 deletions
108
app/Jobs/Europarl240609/Turnout/ImportCountyStatisticsJob.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,108 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Jobs\Europarl240609\Turnout; | ||
|
||
use App\Exceptions\MissingSourceFileException; | ||
use App\Models\County; | ||
use App\Models\ScheduledJob; | ||
use App\Models\Statistic; | ||
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\Middleware\SkipIfBatchCancelled; | ||
use Illuminate\Queue\SerializesModels; | ||
use Illuminate\Support\Collection; | ||
use League\Csv\Reader; | ||
|
||
class ImportCountyStatisticsJob implements ShouldQueue | ||
{ | ||
use Batchable; | ||
use Dispatchable; | ||
use InteractsWithQueue; | ||
use Queueable; | ||
use SerializesModels; | ||
|
||
public ScheduledJob $scheduledJob; | ||
|
||
public County $county; | ||
|
||
public function __construct(ScheduledJob $scheduledJob, County $county) | ||
{ | ||
$this->scheduledJob = $scheduledJob; | ||
$this->county = $county; | ||
} | ||
|
||
public function handle(): void | ||
{ | ||
$disk = $this->scheduledJob->disk(); | ||
$path = $this->scheduledJob->getSourcePath("{$this->county->code}.csv"); | ||
|
||
if (! $disk->exists($path)) { | ||
throw new MissingSourceFileException($path); | ||
} | ||
|
||
$reader = Reader::createFromStream($disk->readStream($path)); | ||
$reader->setHeaderOffset(0); | ||
|
||
logger()->info('ImportCountyStatisticsJob', [ | ||
'county' => $this->county->code, | ||
'first' => $reader->count(), | ||
]); | ||
|
||
$values = collect(); | ||
|
||
$records = $reader->getRecords(); | ||
foreach ($records as $record) { | ||
$values->push([ | ||
'election_id' => $this->scheduledJob->election_id, | ||
'county_id' => $this->county->id, | ||
'locality_id' => $record['Siruta'], | ||
'area' => $record['Mediu'], | ||
'section' => $record['Nr sectie de votare'], | ||
...Statistic::segments() | ||
->mapWithKeys(function (array $segment) use ($record) { | ||
$gender = match ($segment[0]) { | ||
'men' => 'Barbati', | ||
'women' => 'Femei', | ||
}; | ||
|
||
return [ | ||
"{$segment[0]}_{$segment[1]}" => data_get($record, "{$gender} {$segment[1]}", 0), | ||
]; | ||
}) | ||
->all(), | ||
]); | ||
} | ||
|
||
$values->chunk(200) | ||
->each(fn (Collection $chunk) => Statistic::upsert( | ||
$chunk->all(), | ||
['election_id', 'county_id', 'section'], | ||
)); | ||
} | ||
|
||
public function middleware(): array | ||
{ | ||
return [new SkipIfBatchCancelled]; | ||
} | ||
|
||
/** | ||
* Get the tags that should be assigned to the job. | ||
* | ||
* @return array<int, string> | ||
*/ | ||
public function tags(): array | ||
{ | ||
return [ | ||
'import', | ||
'statistics', | ||
'scheduled_job:' . $this->scheduledJob->id, | ||
'election:' . $this->scheduledJob->election_id, | ||
'county:' . $this->county->code, | ||
]; | ||
} | ||
} |
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
Oops, something went wrong.