84 lines
3.2 KiB
PHP
84 lines
3.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Filament\Widgets\Dashboard;
|
|
|
|
use App\Filament\Resources\FindingResource;
|
|
use App\Models\Finding;
|
|
use App\Models\InventoryItem;
|
|
use App\Models\Tenant;
|
|
use App\Support\OpsUx\ActiveRuns;
|
|
use Filament\Facades\Filament;
|
|
use Filament\Tables\Columns\TextColumn;
|
|
use Filament\Tables\Table;
|
|
use Filament\Widgets\TableWidget;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
class RecentDriftFindings extends TableWidget
|
|
{
|
|
protected static bool $isLazy = false;
|
|
|
|
public function table(Table $table): Table
|
|
{
|
|
$tenant = Filament::getTenant();
|
|
|
|
return $table
|
|
->heading('Recent Drift Findings')
|
|
->query($this->getQuery())
|
|
->poll(fn (): ?string => ($tenant instanceof Tenant) && ActiveRuns::existForTenant($tenant) ? '10s' : null)
|
|
->paginated([10])
|
|
->columns([
|
|
TextColumn::make('short_id')
|
|
->label('ID')
|
|
->state(fn (Finding $record): string => '#'.$record->getKey())
|
|
->copyable()
|
|
->copyableState(fn (Finding $record): string => (string) $record->getKey()),
|
|
TextColumn::make('subject_display_name')
|
|
->label('Subject')
|
|
->placeholder('—')
|
|
->limit(40)
|
|
->tooltip(fn (Finding $record): ?string => $record->subject_display_name ?: null),
|
|
TextColumn::make('severity')
|
|
->badge()
|
|
->color(fn (Finding $record): string => match ($record->severity) {
|
|
Finding::SEVERITY_HIGH => 'danger',
|
|
Finding::SEVERITY_MEDIUM => 'warning',
|
|
default => 'gray',
|
|
}),
|
|
TextColumn::make('status')
|
|
->badge()
|
|
->color(fn (Finding $record): string => $record->status === Finding::STATUS_NEW ? 'warning' : 'gray'),
|
|
TextColumn::make('created_at')
|
|
->label('Created')
|
|
->since(),
|
|
])
|
|
->recordUrl(fn (Finding $record): ?string => $tenant instanceof Tenant
|
|
? FindingResource::getUrl('view', ['record' => $record], tenant: $tenant)
|
|
: null)
|
|
->emptyStateHeading('No drift findings')
|
|
->emptyStateDescription('You\'re looking good — no drift findings to review yet.');
|
|
}
|
|
|
|
/**
|
|
* @return Builder<Finding>
|
|
*/
|
|
private function getQuery(): Builder
|
|
{
|
|
$tenant = Filament::getTenant();
|
|
$tenantId = $tenant instanceof Tenant ? $tenant->getKey() : null;
|
|
|
|
return Finding::query()
|
|
->addSelect([
|
|
'subject_display_name' => InventoryItem::query()
|
|
->select('display_name')
|
|
->whereColumn('inventory_items.tenant_id', 'findings.tenant_id')
|
|
->whereColumn('inventory_items.external_id', 'findings.subject_external_id')
|
|
->limit(1),
|
|
])
|
|
->when($tenantId, fn (Builder $query) => $query->where('tenant_id', $tenantId))
|
|
->where('finding_type', Finding::FINDING_TYPE_DRIFT)
|
|
->latest('created_at');
|
|
}
|
|
}
|