Files
fuel-alert/app/Filament/Resources/Searches/SearchResource.php
Ovidiu U ea22387c9d Reverse-geocode a general area label for logged searches
Each search now stores an `area_label` (district/town) reverse-geocoded from its
coarsened ~1km lat/lng bucket via postcodes.io, surfaced in the Filament Searches
admin as a sortable/searchable column plus an area filter. Geocoding is cached 30
days per bucket, queries a 2km radius so low-density buckets still match the
default 100m miss, and fails gracefully to null. Adds `searches:backfill-areas`
(scheduled hourly) to label existing rows and retry stragglers.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-12 09:05:03 +01:00

96 lines
3.5 KiB
PHP

<?php
namespace App\Filament\Resources\Searches;
use App\Filament\NavigationGroup;
use App\Filament\Resources\Searches\Pages\ListSearches;
use App\Models\Search;
use Filament\Resources\Resource;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;
class SearchResource extends Resource
{
protected static ?string $model = Search::class;
protected static string|\UnitEnum|null $navigationGroup = NavigationGroup::Data;
protected static ?string $navigationLabel = 'Searches';
protected static ?int $navigationSort = 4;
public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('searched_at')
->label('Searched At')
->dateTime('d M Y H:i')
->sortable(),
TextColumn::make('area_label')
->label('Area')
->placeholder('Unknown')
->searchable()
->sortable(),
TextColumn::make('fuel_type')
->label('Fuel Type')
->badge(),
TextColumn::make('results_count')
->label('Results')
->numeric()
->sortable(),
TextColumn::make('lowest_pence')
->label('Lowest')
->formatStateUsing(fn (int $state): string => number_format($state / 100, 1).'p')
->sortable(),
TextColumn::make('highest_pence')
->label('Highest')
->formatStateUsing(fn (int $state): string => number_format($state / 100, 1).'p')
->sortable(),
TextColumn::make('avg_pence')
->label('Average')
->formatStateUsing(fn (string $state): string => number_format((float) $state / 100, 1).'p')
->sortable(),
TextColumn::make('lat_bucket')
->label('Area (lat/lng)')
->formatStateUsing(fn (Search $record): string => $record->lat_bucket.', '.$record->lng_bucket)
->toggleable(isToggledHiddenByDefault: true),
TextColumn::make('ip_hash')
->label('IP Hash')
->limit(16)
->toggleable(isToggledHiddenByDefault: true),
])
->defaultSort('searched_at', 'desc')
->filters([
SelectFilter::make('fuel_type')
->options([
'E10' => 'E10',
'E5' => 'E5',
'B7_STANDARD' => 'B7 Standard',
'B7_PREMIUM' => 'B7 Premium',
'B10' => 'B10',
'HVO' => 'HVO',
]),
SelectFilter::make('area_label')
->label('Area')
->searchable()
->options(fn (): array => Search::query()
->whereNotNull('area_label')
->distinct()
->orderBy('area_label')
->pluck('area_label', 'area_label')
->all()),
])
->recordActions([])
->toolbarActions([]);
}
public static function getPages(): array
{
return [
'index' => ListSearches::route('/'),
];
}
}