Summary Kurz: Implementiert Feature 054 — canonical OperationRun-flow, Monitoring UI, dispatch-safety, notifications, dedupe, plus small UX safety clarifications (RBAC group search delegated; Restore group mapping DB-only). What Changed Core service: OperationRun lifecycle, dedupe and dispatch helpers — OperationRunService.php. Model + migration: OperationRun model and migration — OperationRun.php, 2026_01_16_180642_create_operation_runs_table.php. Notifications: queued + terminal DB notifications (initiator-only) — OperationRunQueued.php, OperationRunCompleted.php. Monitoring UI: Filament list/detail + Livewire pieces (DB-only render) — OperationRunResource.php and related pages/views. Start surfaces / Jobs: instrumented start surfaces, job middleware, and job updates to use canonical runs — multiple app/Jobs/* and app/Filament/* updates (see tests for full coverage). RBAC + Restore UX clarifications: RBAC group search is delegated-Graph-based and disabled without delegated token; Restore group mapping remains DB-only (directory cache) and helper text always visible — TenantResource.php, RestoreRunResource.php. Specs / Constitution: updated spec & quickstart and added one-line constitution guideline about Graph usage: spec.md quickstart.md constitution.md Tests & Verification Unit / Feature tests added/updated for run lifecycle, notifications, idempotency, and UI guards: see tests/Feature/* (notably OperationRunServiceTest, MonitoringOperationsTest, OperationRunNotificationTest, and various Filament feature tests). Full test run locally: ./vendor/bin/sail artisan test → 587 passed, 5 skipped. Migrations Adds create_operation_runs_table migration; run php artisan migrate in staging after review. Notes / Rationale Monitoring pages are explicitly DB-only at render time (no Graph calls). Start surfaces enqueue work only and return a “View run” link. Delegated Graph access is used only for explicit user actions (RBAC group search); restore mapping intentionally uses cached DB data only to avoid render-time Graph calls. Dispatch wrapper marks runs failed immediately if background dispatch throws synchronously to avoid misleading “queued” states. Upgrade / Deploy Considerations Run migrations: ./vendor/bin/sail artisan migrate. Background workers should be running to process queued jobs (recommended to monitor queue health during rollout). No secret or token persistence changes. PR checklist Tests updated/added for changed behavior Specs updated: 054-unify-runs-suitewide docs + quickstart Constitution note added (.specify) Pint formatting applied Co-authored-by: Ahmed Darrazi <ahmeddarrazi@adsmac.local> Reviewed-on: #63
246 lines
9.5 KiB
PHP
246 lines
9.5 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources;
|
|
|
|
use App\Filament\Resources\OperationRunResource\Pages;
|
|
use App\Models\OperationRun;
|
|
use App\Models\Tenant;
|
|
use App\Support\OperationRunOutcome;
|
|
use App\Support\OperationRunStatus;
|
|
use BackedEnum;
|
|
use Filament\Actions;
|
|
use Filament\Forms\Components\DatePicker;
|
|
use Filament\Infolists\Components\TextEntry;
|
|
use Filament\Infolists\Components\ViewEntry;
|
|
use Filament\Resources\Resource;
|
|
use Filament\Schemas\Components\Section;
|
|
use Filament\Schemas\Schema;
|
|
use Filament\Tables;
|
|
use Filament\Tables\Table;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use UnitEnum;
|
|
|
|
class OperationRunResource extends Resource
|
|
{
|
|
protected static bool $isScopedToTenant = false;
|
|
|
|
protected static ?string $model = OperationRun::class;
|
|
|
|
protected static ?string $slug = 'operations';
|
|
|
|
protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-queue-list';
|
|
|
|
protected static string|UnitEnum|null $navigationGroup = 'Monitoring';
|
|
|
|
protected static ?string $navigationLabel = 'Operations';
|
|
|
|
public static function form(Schema $schema): Schema
|
|
{
|
|
return $schema;
|
|
}
|
|
|
|
public static function infolist(Schema $schema): Schema
|
|
{
|
|
return $schema
|
|
->schema([
|
|
Section::make('Run')
|
|
->schema([
|
|
TextEntry::make('type')->badge(),
|
|
TextEntry::make('status')
|
|
->badge()
|
|
->color(fn (OperationRun $record): string => static::statusColor($record->status)),
|
|
TextEntry::make('outcome')
|
|
->badge()
|
|
->color(fn (OperationRun $record): string => static::outcomeColor($record->outcome)),
|
|
TextEntry::make('initiator_name')->label('Initiator'),
|
|
TextEntry::make('created_at')->dateTime(),
|
|
TextEntry::make('started_at')->dateTime()->placeholder('—'),
|
|
TextEntry::make('completed_at')->dateTime()->placeholder('—'),
|
|
TextEntry::make('run_identity_hash')->label('Identity hash')->copyable(),
|
|
])
|
|
->columns(2)
|
|
->columnSpanFull(),
|
|
|
|
Section::make('Counts')
|
|
->schema([
|
|
ViewEntry::make('summary_counts')
|
|
->label('')
|
|
->view('filament.infolists.entries.snapshot-json')
|
|
->state(fn (OperationRun $record): array => $record->summary_counts ?? [])
|
|
->columnSpanFull(),
|
|
])
|
|
->visible(fn (OperationRun $record): bool => ! empty($record->summary_counts))
|
|
->columnSpanFull(),
|
|
|
|
Section::make('Failures')
|
|
->schema([
|
|
ViewEntry::make('failure_summary')
|
|
->label('')
|
|
->view('filament.infolists.entries.snapshot-json')
|
|
->state(fn (OperationRun $record): array => $record->failure_summary ?? [])
|
|
->columnSpanFull(),
|
|
])
|
|
->visible(fn (OperationRun $record): bool => ! empty($record->failure_summary))
|
|
->columnSpanFull(),
|
|
|
|
Section::make('Context')
|
|
->schema([
|
|
ViewEntry::make('context')
|
|
->label('')
|
|
->view('filament.infolists.entries.snapshot-json')
|
|
->state(fn (OperationRun $record): array => $record->context ?? [])
|
|
->columnSpanFull(),
|
|
])
|
|
->columnSpanFull(),
|
|
]);
|
|
}
|
|
|
|
public static function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->defaultSort('created_at', 'desc')
|
|
->modifyQueryUsing(function (Builder $query): Builder {
|
|
$tenantId = Tenant::current()?->getKey();
|
|
|
|
return $query->when($tenantId, fn (Builder $q) => $q->where('tenant_id', $tenantId));
|
|
})
|
|
->columns([
|
|
Tables\Columns\TextColumn::make('status')
|
|
->badge()
|
|
->color(fn (OperationRun $record): string => static::statusColor($record->status)),
|
|
Tables\Columns\TextColumn::make('type')
|
|
->label('Operation')
|
|
->searchable()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('initiator_name')
|
|
->label('Initiator')
|
|
->searchable(),
|
|
Tables\Columns\TextColumn::make('created_at')
|
|
->label('Started')
|
|
->since()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('duration')
|
|
->getStateUsing(function (OperationRun $record): string {
|
|
if ($record->started_at && $record->completed_at) {
|
|
return $record->completed_at->diffForHumans($record->started_at, true);
|
|
}
|
|
|
|
return '—';
|
|
}),
|
|
Tables\Columns\TextColumn::make('outcome')
|
|
->badge()
|
|
->color(fn (OperationRun $record): string => static::outcomeColor($record->outcome)),
|
|
])
|
|
->filters([
|
|
Tables\Filters\SelectFilter::make('type')
|
|
->options(function (): array {
|
|
$tenantId = Tenant::current()?->getKey();
|
|
|
|
if (! $tenantId) {
|
|
return [];
|
|
}
|
|
|
|
return OperationRun::query()
|
|
->where('tenant_id', $tenantId)
|
|
->select('type')
|
|
->distinct()
|
|
->orderBy('type')
|
|
->pluck('type', 'type')
|
|
->all();
|
|
}),
|
|
Tables\Filters\SelectFilter::make('status')
|
|
->options([
|
|
OperationRunStatus::Queued->value => 'Queued',
|
|
OperationRunStatus::Running->value => 'Running',
|
|
OperationRunStatus::Completed->value => 'Completed',
|
|
]),
|
|
Tables\Filters\SelectFilter::make('outcome')
|
|
->options(OperationRunOutcome::uiLabels(includeReserved: false)),
|
|
Tables\Filters\SelectFilter::make('initiator_name')
|
|
->label('Initiator')
|
|
->options(function (): array {
|
|
$tenantId = Tenant::current()?->getKey();
|
|
|
|
if (! $tenantId) {
|
|
return [];
|
|
}
|
|
|
|
return OperationRun::query()
|
|
->where('tenant_id', $tenantId)
|
|
->whereNotNull('initiator_name')
|
|
->select('initiator_name')
|
|
->distinct()
|
|
->orderBy('initiator_name')
|
|
->pluck('initiator_name', 'initiator_name')
|
|
->all();
|
|
})
|
|
->searchable(),
|
|
Tables\Filters\Filter::make('created_at')
|
|
->label('Created')
|
|
->form([
|
|
DatePicker::make('created_from')
|
|
->label('From'),
|
|
DatePicker::make('created_until')
|
|
->label('Until'),
|
|
])
|
|
->default(fn (): array => [
|
|
'created_from' => now()->subDays(30),
|
|
'created_until' => now(),
|
|
])
|
|
->query(function (Builder $query, array $data): Builder {
|
|
$from = $data['created_from'] ?? null;
|
|
if ($from) {
|
|
$query->whereDate('created_at', '>=', $from);
|
|
}
|
|
|
|
$until = $data['created_until'] ?? null;
|
|
if ($until) {
|
|
$query->whereDate('created_at', '<=', $until);
|
|
}
|
|
|
|
return $query;
|
|
}),
|
|
])
|
|
->actions([
|
|
Actions\ViewAction::make(),
|
|
])
|
|
->bulkActions([]);
|
|
}
|
|
|
|
public static function getEloquentQuery(): Builder
|
|
{
|
|
return parent::getEloquentQuery()
|
|
->with('user')
|
|
->latest('id');
|
|
}
|
|
|
|
public static function getPages(): array
|
|
{
|
|
return [
|
|
'index' => Pages\ListOperationRuns::route('/'),
|
|
'view' => Pages\ViewOperationRun::route('/{record}'),
|
|
];
|
|
}
|
|
|
|
private static function statusColor(?string $status): string
|
|
{
|
|
return match ($status) {
|
|
'queued' => 'gray',
|
|
'running' => 'info',
|
|
'completed' => 'success',
|
|
default => 'gray',
|
|
};
|
|
}
|
|
|
|
private static function outcomeColor(?string $outcome): string
|
|
{
|
|
return match ($outcome) {
|
|
'succeeded' => 'success',
|
|
'partially_succeeded' => 'warning',
|
|
'failed' => 'danger',
|
|
'cancelled' => 'gray',
|
|
default => 'gray',
|
|
};
|
|
}
|
|
}
|