Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiio committed Nov 19, 2024
1 parent 6b9aa17 commit fbe7e22
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 57 deletions.
80 changes: 71 additions & 9 deletions app/Console/Commands/Import/ImportOldIdsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Console\Commands\Import;

use App\Models\Country;
use App\Models\County;
use App\Models\Locality;
use stdClass;
Expand Down Expand Up @@ -44,7 +45,35 @@ public function handle(): int

protected function importCountryIds(): void
{
//
$query = $this->db
->table('countries')
->orderBy('countries.Id');

$this->createProgressBar(
'Importing country IDs...',
$query->count()
);

$query->each(function (stdClass $row) {
$country = Country::search($row->Name)->first();

if (blank($country)) {
logger()->error("Country not found: {$row->Name}");

return;
}

$oldIds = $country->old_ids ?? [];
$oldIds[] = $row->Id;

$country->updateQuietly([
'old_ids' => $oldIds,
]);

$this->progressBar->advance();
}, (int) $this->option('chunk'));

$this->finishProgressBar('Imported country IDs');
}

protected function importCountyIds(): void
Expand Down Expand Up @@ -81,22 +110,55 @@ protected function importLocalityIds(): void
{
$query = $this->db
->table('localities')
->orderBy('localities.Siruta')
->where('localities.Siruta', '!=', 0);
->orderBy('localities.Siruta');

$this->createProgressBar(
'Importing locality IDs...',
$query->count()
);

$query->each(function (stdClass $row) {
Locality::query()
->where('id', $row->Siruta)
->update([
'old_id' => $row->LocalityId,
]);
$counties = County::pluck('id', 'old_id');

$query->each(function (stdClass $row) use ($counties) {
// $siruta = match ($row->Siruta) {
// 116921 => 61069, // Băneasa, Constanța
// 713, 21469 => 9280, // Fântânele, Arad
// default => $row->Siruta
// };

if ($row->Siruta === 0) {
$locality = $this->searchLocalities($row->Name, $counties->get($row->CountyId));
} else {
$locality = Locality::query()
->where('id', $row->Siruta)
->firstOr(fn () => $this->searchLocalities($row->Name, $counties->get($row->CountyId)));
}

logger()->info("{$row->LocalityId} | {$row->Name} | Siruta: {$row->Siruta} => " . $locality?->name ?? 'NULL');

if (blank($locality)) {
logger()->error("Locality not found: {$row->Name}");

return;
}

$oldIds = $locality->old_ids ?? [];
$oldIds[] = $row->LocalityId;

$locality->updateQuietly([
'old_ids' => $oldIds,
]);

$this->progressBar->advance();
}, (int) $this->option('chunk'));

$this->finishProgressBar('Imported locality IDs');
}

protected function searchLocalities(string $name, int $county_id): ?Locality
{
return Locality::search($name)
->where('county_id', $county_id)
->first();
}
}
73 changes: 45 additions & 28 deletions app/Console/Commands/Import/ImportTurnoutsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@ public function handle(): int
->whereIn('turnouts.Division', [3]) // 3 = locality, 4 = diaspora_country
->orderBy('turnouts.Id')
->leftJoin('localities', 'turnouts.LocalityId', '=', 'localities.LocalityId')
->leftJoin('countries', 'turnouts.CountryId', '=', 'countries.Id')
->select([
'*',
'turnouts.*',
'localities.Name as LocalityName',
'countries.Name as CountryName',
]);

$this->createProgressBar(
'Importing turnouts...',
$query->count()
);

// $this->countries = Country::pluck('id', 'old_id');
$this->countries = $this->getCountries();
$this->counties = County::pluck('id', 'old_id');
$this->localities = Locality::query()
->whereNull('parent_id')
->pluck('id', 'old_id');
$this->localities = $this->getLocalities();

Election::all()->each(function (Election $election) use ($query) {
$index = 1;
Expand Down Expand Up @@ -106,6 +106,8 @@ public function handle(): int
Turnout::insert($turnouts);

// Record::insert($records);

$this->progressBar->advance($rows->count());
});
});

Expand All @@ -114,10 +116,38 @@ public function handle(): int
return static::SUCCESS;
}

protected function getCountries(): Collection
{
$countries = collect();

Country::each(function (Country $country) use ($countries) {
collect($country->old_ids)->each(
fn (int $oldId) => $countries->put($oldId, $country->id)
);
});

return $countries;
}

protected function getLocalities(): Collection
{
$localities = collect();

Locality::query()
->whereNotNull('old_ids')
->each(function (Locality $locality) use ($localities) {
collect($locality->old_ids)->each(
fn (int $oldId) => $localities->put($oldId, $locality->id)
);
});

return $localities;
}

protected function getPlace(stdClass $row): ?array
{
$place = [
// 'country_id' => $this->countries->get($row->CountryId),
'country_id' => $this->countries->get($row->CountryId),
'county_id' => $this->counties->get($row->CountyId),
'locality_id' => $this->localities->get($row->LocalityId),
];
Expand All @@ -129,28 +159,15 @@ protected function getPlace(stdClass $row): ?array
]);

if ($validation->fails()) {
if (blank($place['locality_id'])) {
$place['locality_id'] = match ((int) $row->LocalityId) {
116921 => 61069, // Băneasa, Constanța
713, 21469 => 9280, // Fântânele, Arad
default => Locality::search($row->LocalityName)
->where('county_id', $place['county_id'])
->first()
?->id,
};
}

$validation->setData($place);

if ($validation->fails()) {
logger()->error("Could not determine location for turnout id {$row->Id}", [
'BallotId' => $row->BallotId,
'LocalityId' => $row->LocalityId,
'place' => $place,
]);

return null;
}
logger()->error('Could not determine location.', [
'BallotId' => $row->BallotId,
'TurnoutId' => $row->Id,
'CountyId' => $row->CountyId,
'LocalityId' => $row->LocalityId,
'locality_id' => $place['locality_id'],
]);

return null;
}

return $place;
Expand Down
26 changes: 23 additions & 3 deletions app/Livewire/Pages/ElectionPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Filament\Forms\Get;
use Filament\Forms\Set;
use Filament\Support\Enums\MaxWidth;
use Illuminate\Database\Eloquent\Builder;
use Livewire\Attributes\Computed;
use Livewire\Attributes\On;
use Livewire\Attributes\Url;
Expand All @@ -42,6 +43,11 @@ abstract class ElectionPage extends Component implements HasForms

public function form(Form $form): Form
{
$whereHasKey = match (static::class) {
ElectionResults::class => 'records',
ElectionTurnouts::class => 'turnouts',
};

return $form
->schema([
Grid::make()
Expand All @@ -67,7 +73,13 @@ public function form(Form $form): Form
->label(__('app.field.country'))
->placeholder(__('app.field.country'))
->hiddenLabel()
->options(Country::pluck('name', 'id'))
->options(
fn () => Country::query()
->whereHas($whereHasKey, function (Builder $query) {
$query->whereBelongsTo($this->election);
})
->pluck('name', 'id')
)
->afterStateUpdated(function (Set $set) {
$set('county', null);
$set('locality', null);
Expand All @@ -80,7 +92,13 @@ public function form(Form $form): Form
->label(__('app.field.county'))
->placeholder(__('app.field.county'))
->hiddenLabel()
->options(County::pluck('name', 'id'))
->options(
fn () => County::query()
->whereHas($whereHasKey, function (Builder $query) {
$query->whereBelongsTo($this->election);
})
->pluck('name', 'id')
)
->afterStateUpdated(function (Set $set) {
$set('locality', null);
})
Expand All @@ -95,7 +113,9 @@ public function form(Form $form): Form
->options(
fn (Get $get) => Locality::query()
->where('county_id', $get('county'))
->whereNull('parent_id')
->whereHas($whereHasKey, function (Builder $query) {
$query->whereBelongsTo($this->election);
})
->limit(1000)
->pluck('name', 'id')
)
Expand Down
14 changes: 13 additions & 1 deletion app/Models/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Laravel\Scout\Searchable;

class Country extends Model
Expand All @@ -21,7 +22,7 @@ class Country extends Model
'id',
'name',
'aliases',
'old_id',
'old_ids',
];

/**
Expand All @@ -33,9 +34,20 @@ protected function casts(): array
{
return [
'aliases' => 'array',
'old_ids' => 'array',
];
}

public function records(): HasMany
{
return $this->hasMany(Record::class);
}

public function turnouts(): HasMany
{
return $this->hasMany(Turnout::class);
}

/**
* Get the indexable data array for the model.
*
Expand Down
5 changes: 5 additions & 0 deletions app/Models/County.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public function localities(): HasMany
return $this->hasMany(Locality::class);
}

public function records(): HasManyThrough
{
return $this->hasManyThrough(Record::class, Locality::class);
}

public function turnouts(): HasManyThrough
{
return $this->hasManyThrough(Turnout::class, Locality::class);
Expand Down
25 changes: 24 additions & 1 deletion app/Models/Locality.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Laravel\Scout\Searchable;

class Locality extends Model
Expand All @@ -22,9 +23,21 @@ class Locality extends Model
'level',
'type',
'parent_id',
'old_id',
'old_ids',
];

/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'old_ids' => 'array',
];
}

public function county(): BelongsTo
{
return $this->belongsTo(County::class);
Expand All @@ -35,6 +48,16 @@ public function parent(): BelongsTo
return $this->belongsTo(static::class, 'parent_id');
}

public function records(): HasMany
{
return $this->hasMany(Record::class);
}

public function turnouts(): HasMany
{
return $this->hasMany(Turnout::class);
}

protected function getNameWithCountyAttribute(): string
{
return "{$this->name}, {$this->county->name}";
Expand Down
Loading

0 comments on commit fbe7e22

Please sign in to comment.