feat: add ApiLogResource with filters and view page
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
134
app/Filament/Resources/ApiLogResource.php
Normal file
134
app/Filament/Resources/ApiLogResource.php
Normal file
@@ -0,0 +1,134 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Resources;
|
||||||
|
|
||||||
|
use App\Filament\Resources\ApiLogResource\Pages\ListApiLogs;
|
||||||
|
use App\Filament\Resources\ApiLogResource\Pages\ViewApiLog;
|
||||||
|
use App\Models\ApiLog;
|
||||||
|
use Filament\Actions\ViewAction;
|
||||||
|
use Filament\Forms\Components\DatePicker;
|
||||||
|
use Filament\Infolists\Components\TextEntry;
|
||||||
|
use Filament\Resources\Resource;
|
||||||
|
use Filament\Schemas\Components\Section;
|
||||||
|
use Filament\Schemas\Schema;
|
||||||
|
use Filament\Tables\Columns\TextColumn;
|
||||||
|
use Filament\Tables\Filters\Filter;
|
||||||
|
use Filament\Tables\Filters\SelectFilter;
|
||||||
|
use Filament\Tables\Table;
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
|
||||||
|
class ApiLogResource extends Resource
|
||||||
|
{
|
||||||
|
protected static ?string $model = ApiLog::class;
|
||||||
|
|
||||||
|
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-server';
|
||||||
|
|
||||||
|
protected static string|\UnitEnum|null $navigationGroup = 'System';
|
||||||
|
|
||||||
|
protected static ?string $navigationLabel = 'API Logs';
|
||||||
|
|
||||||
|
protected static ?int $navigationSort = 1;
|
||||||
|
|
||||||
|
public static function table(Table $table): Table
|
||||||
|
{
|
||||||
|
return $table
|
||||||
|
->columns([
|
||||||
|
TextColumn::make('service')
|
||||||
|
->badge()
|
||||||
|
->color(fn (string $state) => match ($state) {
|
||||||
|
'fuel_finder' => 'success',
|
||||||
|
'fred' => 'info',
|
||||||
|
'anthropic' => 'warning',
|
||||||
|
default => 'gray',
|
||||||
|
})
|
||||||
|
->sortable(),
|
||||||
|
TextColumn::make('method')
|
||||||
|
->badge()
|
||||||
|
->color('gray'),
|
||||||
|
TextColumn::make('url')
|
||||||
|
->limit(60)
|
||||||
|
->tooltip(fn (ApiLog $record) => $record->url),
|
||||||
|
TextColumn::make('status_code')
|
||||||
|
->badge()
|
||||||
|
->color(fn (?int $state) => match (true) {
|
||||||
|
$state === null => 'danger',
|
||||||
|
$state >= 500 => 'danger',
|
||||||
|
$state >= 400 => 'warning',
|
||||||
|
default => 'success',
|
||||||
|
}),
|
||||||
|
TextColumn::make('duration_ms')
|
||||||
|
->label('Duration (ms)')
|
||||||
|
->sortable(),
|
||||||
|
TextColumn::make('error')
|
||||||
|
->limit(40)
|
||||||
|
->placeholder('—'),
|
||||||
|
TextColumn::make('created_at')
|
||||||
|
->dateTime('d M Y H:i')
|
||||||
|
->sortable(),
|
||||||
|
])
|
||||||
|
->defaultSort('created_at', 'desc')
|
||||||
|
->filters([
|
||||||
|
SelectFilter::make('service')
|
||||||
|
->options([
|
||||||
|
'fuel_finder' => 'Fuel Finder',
|
||||||
|
'fred' => 'FRED',
|
||||||
|
'anthropic' => 'Anthropic',
|
||||||
|
'postcodes_io' => 'Postcodes.io',
|
||||||
|
]),
|
||||||
|
Filter::make('errors_only')
|
||||||
|
->label('Errors only')
|
||||||
|
->query(fn (Builder $query) => $query->where(
|
||||||
|
fn (Builder $q) => $q->where('status_code', '>=', 400)
|
||||||
|
->orWhereNotNull('error')
|
||||||
|
)),
|
||||||
|
Filter::make('created_at')
|
||||||
|
->schema([
|
||||||
|
DatePicker::make('from')->label('From'),
|
||||||
|
DatePicker::make('until')->label('Until'),
|
||||||
|
])
|
||||||
|
->query(function (Builder $query, array $data) {
|
||||||
|
$query
|
||||||
|
->when($data['from'], fn ($q, $d) => $q->whereDate('created_at', '>=', $d))
|
||||||
|
->when($data['until'], fn ($q, $d) => $q->whereDate('created_at', '<=', $d));
|
||||||
|
}),
|
||||||
|
])
|
||||||
|
->recordActions([
|
||||||
|
ViewAction::make(),
|
||||||
|
])
|
||||||
|
->toolbarActions([]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function infolist(Schema $schema): Schema
|
||||||
|
{
|
||||||
|
return $schema->components([
|
||||||
|
Section::make('Request')->schema([
|
||||||
|
TextEntry::make('service')->badge(),
|
||||||
|
TextEntry::make('method'),
|
||||||
|
TextEntry::make('url')->columnSpanFull(),
|
||||||
|
TextEntry::make('status_code')
|
||||||
|
->badge()
|
||||||
|
->color(fn (?int $state) => match (true) {
|
||||||
|
$state === null => 'danger',
|
||||||
|
$state >= 500 => 'danger',
|
||||||
|
$state >= 400 => 'warning',
|
||||||
|
default => 'success',
|
||||||
|
}),
|
||||||
|
TextEntry::make('duration_ms')->label('Duration (ms)'),
|
||||||
|
TextEntry::make('created_at')->dateTime('d M Y H:i:s'),
|
||||||
|
])->columns(3),
|
||||||
|
Section::make('Error')->schema([
|
||||||
|
TextEntry::make('error')
|
||||||
|
->columnSpanFull()
|
||||||
|
->placeholder('No error recorded'),
|
||||||
|
])->collapsed(fn (ApiLog $record) => $record->error === null),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getPages(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'index' => ListApiLogs::route('/'),
|
||||||
|
'view' => ViewApiLog::route('/{record}'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
16
app/Filament/Resources/ApiLogResource/Pages/ListApiLogs.php
Normal file
16
app/Filament/Resources/ApiLogResource/Pages/ListApiLogs.php
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Resources\ApiLogResource\Pages;
|
||||||
|
|
||||||
|
use App\Filament\Resources\ApiLogResource;
|
||||||
|
use Filament\Resources\Pages\ListRecords;
|
||||||
|
|
||||||
|
class ListApiLogs extends ListRecords
|
||||||
|
{
|
||||||
|
protected static string $resource = ApiLogResource::class;
|
||||||
|
|
||||||
|
protected function getHeaderActions(): array
|
||||||
|
{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
16
app/Filament/Resources/ApiLogResource/Pages/ViewApiLog.php
Normal file
16
app/Filament/Resources/ApiLogResource/Pages/ViewApiLog.php
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Resources\ApiLogResource\Pages;
|
||||||
|
|
||||||
|
use App\Filament\Resources\ApiLogResource;
|
||||||
|
use Filament\Resources\Pages\ViewRecord;
|
||||||
|
|
||||||
|
class ViewApiLog extends ViewRecord
|
||||||
|
{
|
||||||
|
protected static string $resource = ApiLogResource::class;
|
||||||
|
|
||||||
|
protected function getHeaderActions(): array
|
||||||
|
{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
32
tests/Feature/Admin/ApiLogResourceTest.php
Normal file
32
tests/Feature/Admin/ApiLogResourceTest.php
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Filament\Resources\ApiLogResource\Pages\ListApiLogs;
|
||||||
|
use App\Models\ApiLog;
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Livewire\Livewire;
|
||||||
|
|
||||||
|
uses(RefreshDatabase::class);
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
$this->admin = User::factory()->admin()->create();
|
||||||
|
$this->actingAs($this->admin);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('renders the api log list page', function () {
|
||||||
|
$logs = ApiLog::factory()->count(3)->create();
|
||||||
|
|
||||||
|
Livewire::test(ListApiLogs::class)
|
||||||
|
->assertOk()
|
||||||
|
->assertCanSeeTableRecords($logs);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('filters to errors only', function () {
|
||||||
|
$ok = ApiLog::factory()->create(['status_code' => 200, 'error' => null]);
|
||||||
|
$err = ApiLog::factory()->failed()->create();
|
||||||
|
|
||||||
|
Livewire::test(ListApiLogs::class)
|
||||||
|
->filterTable('errors_only')
|
||||||
|
->assertCanSeeTableRecords([$err])
|
||||||
|
->assertCanNotSeeTableRecords([$ok]);
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user