Summary This PR introduces Unified Operations Runs + Monitoring Hub (053). Goal: Standardize how long-running operations are tracked and monitored using the existing tenant-scoped run record (BulkOperationRun) as the canonical “operation run”, and surface it in a single Monitoring → Operations hub (view-only, tenant-scoped, role-aware). Phase 1 adoption scope (per spec): • Drift generation (drift.generate) • Backup Set “Add Policies” (backup_set.add_policies) Note: This PR does not convert every run type yet (e.g. GroupSyncRuns / InventorySyncRuns remain separate for now). This is intentionally incremental. ⸻ What changed Monitoring / Operations hub • Moved/organized run monitoring under Monitoring → Operations • Added: • status buckets (queued / running / succeeded / partially succeeded / failed) • filters (run type, status bucket, time range) • run detail “Related” links (e.g. Drift findings, Backup Set context) • All hub pages are DB-only and view-only (no rerun/cancel/delete actions) Canonical run semantics • Added canonical helpers on BulkOperationRun: • runType() (resource.action) • statusBucket() derived from status + counts (testable semantics) Drift integration (Phase 1) • Drift generation start behavior now: • creates/reuses a BulkOperationRun with drift context payload (scope_key + baseline/current run ids) • dispatches generation job • emits DB notifications including “View run” link • On generation failure: stores sanitized failure entries + sends failure notification Permissions / tenant isolation • Monitoring run list/view is tenant-scoped and returns 403 for cross-tenant access • Readonly can view runs but cannot start drift generation ⸻ Tests Added/updated Pest coverage: • BulkOperationRunStatusBucketTest.php • DriftGenerationDispatchTest.php • GenerateDriftFindingsJobNotificationTest.php • RunAuthorizationTenantIsolationTest.php Validation run locally: • ./vendor/bin/pint --dirty • targeted tests from feature quickstart / drift monitoring tests ⸻ Manual QA 1. Go to Monitoring → Operations • verify filters (run type / status / time range) • verify run detail shows counts + sanitized failures + “Related” links 2. Open Drift Landing • with >=2 successful inventory runs for scope: should queue drift generation + show notification with “View run” • as readonly: should not start generation 3. Run detail • drift.generate runs show “Drift findings” related link • failure entries are sanitized (no secrets/tokens/raw payload dumps) ⸻ Notes / Ops • Queue workers must be restarted after deploy so they load the new code: • php artisan queue:restart (or Sail equivalent) • This PR standardizes monitoring for Phase 1 producers only; follow-ups will migrate additional run types into the unified pattern. ⸻ Spec / Docs • SpecKit artifacts added under specs/053-unify-runs-monitoring/ • Checklists are complete: • requirements checklist PASS • writing checklist PASS Co-authored-by: Ahmed Darrazi <ahmeddarrazi@adsmac.local> Reviewed-on: #60
175 lines
5.9 KiB
PHP
175 lines
5.9 KiB
PHP
<?php
|
|
|
|
use App\Filament\Pages\DriftLanding;
|
|
use App\Filament\Resources\BulkOperationRunResource;
|
|
use App\Jobs\GenerateDriftFindingsJob;
|
|
use App\Models\BulkOperationRun;
|
|
use App\Models\InventorySyncRun;
|
|
use App\Notifications\RunStatusChangedNotification;
|
|
use App\Support\RunIdempotency;
|
|
use Filament\Facades\Filament;
|
|
use Illuminate\Support\Facades\Queue;
|
|
use Livewire\Livewire;
|
|
|
|
test('opening Drift dispatches generation when findings are missing', function () {
|
|
Queue::fake();
|
|
|
|
[$user, $tenant] = createUserWithTenant(role: 'manager');
|
|
$this->actingAs($user);
|
|
Filament::setTenant($tenant, true);
|
|
|
|
$scopeKey = hash('sha256', 'scope-dispatch');
|
|
|
|
$baseline = InventorySyncRun::factory()->for($tenant)->create([
|
|
'selection_hash' => $scopeKey,
|
|
'status' => InventorySyncRun::STATUS_SUCCESS,
|
|
'finished_at' => now()->subDays(2),
|
|
]);
|
|
|
|
$current = InventorySyncRun::factory()->for($tenant)->create([
|
|
'selection_hash' => $scopeKey,
|
|
'status' => InventorySyncRun::STATUS_SUCCESS,
|
|
'finished_at' => now()->subDay(),
|
|
]);
|
|
|
|
Livewire::test(DriftLanding::class);
|
|
|
|
$idempotencyKey = RunIdempotency::buildKey(
|
|
tenantId: (int) $tenant->getKey(),
|
|
operationType: 'drift.generate',
|
|
targetId: $scopeKey,
|
|
context: [
|
|
'scope_key' => $scopeKey,
|
|
'baseline_run_id' => (int) $baseline->getKey(),
|
|
'current_run_id' => (int) $current->getKey(),
|
|
],
|
|
);
|
|
|
|
$bulkRun = BulkOperationRun::query()
|
|
->where('tenant_id', $tenant->getKey())
|
|
->where('idempotency_key', $idempotencyKey)
|
|
->latest('id')
|
|
->first();
|
|
|
|
expect($bulkRun)->not->toBeNull();
|
|
expect($bulkRun->resource)->toBe('drift');
|
|
expect($bulkRun->action)->toBe('generate');
|
|
expect($bulkRun->status)->toBe('pending');
|
|
expect($bulkRun->item_ids)->toBe([
|
|
'scope_key' => $scopeKey,
|
|
'baseline_run_id' => (int) $baseline->getKey(),
|
|
'current_run_id' => (int) $current->getKey(),
|
|
]);
|
|
|
|
$this->assertDatabaseHas('notifications', [
|
|
'notifiable_id' => $user->getKey(),
|
|
'notifiable_type' => $user->getMorphClass(),
|
|
'type' => RunStatusChangedNotification::class,
|
|
]);
|
|
|
|
$notification = $user->notifications()->latest('id')->first();
|
|
expect($notification)->not->toBeNull();
|
|
expect($notification->data['actions'][0]['url'] ?? null)
|
|
->toBe(BulkOperationRunResource::getUrl('view', ['record' => $bulkRun->getKey()], tenant: $tenant));
|
|
|
|
Queue::assertPushed(GenerateDriftFindingsJob::class, function (GenerateDriftFindingsJob $job) use ($tenant, $user, $baseline, $current, $scopeKey, $bulkRun): bool {
|
|
return $job->tenantId === (int) $tenant->getKey()
|
|
&& $job->userId === (int) $user->getKey()
|
|
&& $job->baselineRunId === (int) $baseline->getKey()
|
|
&& $job->currentRunId === (int) $current->getKey()
|
|
&& $job->scopeKey === $scopeKey
|
|
&& $job->bulkOperationRunId === (int) $bulkRun->getKey();
|
|
});
|
|
});
|
|
|
|
test('opening Drift is idempotent while a run is pending', function () {
|
|
Queue::fake();
|
|
|
|
[$user, $tenant] = createUserWithTenant(role: 'manager');
|
|
$this->actingAs($user);
|
|
Filament::setTenant($tenant, true);
|
|
|
|
$scopeKey = hash('sha256', 'scope-idempotent');
|
|
|
|
$baseline = InventorySyncRun::factory()->for($tenant)->create([
|
|
'selection_hash' => $scopeKey,
|
|
'status' => InventorySyncRun::STATUS_SUCCESS,
|
|
'finished_at' => now()->subDays(2),
|
|
]);
|
|
|
|
$current = InventorySyncRun::factory()->for($tenant)->create([
|
|
'selection_hash' => $scopeKey,
|
|
'status' => InventorySyncRun::STATUS_SUCCESS,
|
|
'finished_at' => now()->subDay(),
|
|
]);
|
|
|
|
Livewire::test(DriftLanding::class);
|
|
Livewire::test(DriftLanding::class);
|
|
|
|
Queue::assertPushed(GenerateDriftFindingsJob::class, 1);
|
|
|
|
$idempotencyKey = RunIdempotency::buildKey(
|
|
tenantId: (int) $tenant->getKey(),
|
|
operationType: 'drift.generate',
|
|
targetId: $scopeKey,
|
|
context: [
|
|
'scope_key' => $scopeKey,
|
|
'baseline_run_id' => (int) $baseline->getKey(),
|
|
'current_run_id' => (int) $current->getKey(),
|
|
],
|
|
);
|
|
|
|
expect(BulkOperationRun::query()
|
|
->where('tenant_id', $tenant->getKey())
|
|
->where('idempotency_key', $idempotencyKey)
|
|
->count())->toBe(1);
|
|
});
|
|
|
|
test('opening Drift does not dispatch generation when fewer than two successful runs exist', function () {
|
|
Queue::fake();
|
|
|
|
[$user, $tenant] = createUserWithTenant(role: 'manager');
|
|
$this->actingAs($user);
|
|
Filament::setTenant($tenant, true);
|
|
|
|
$scopeKey = hash('sha256', 'scope-blocked');
|
|
|
|
InventorySyncRun::factory()->for($tenant)->create([
|
|
'selection_hash' => $scopeKey,
|
|
'status' => InventorySyncRun::STATUS_SUCCESS,
|
|
'finished_at' => now()->subDay(),
|
|
]);
|
|
|
|
Livewire::test(DriftLanding::class);
|
|
|
|
Queue::assertNothingPushed();
|
|
expect(BulkOperationRun::query()->where('tenant_id', $tenant->getKey())->count())->toBe(0);
|
|
});
|
|
|
|
test('opening Drift does not dispatch generation for readonly users', function () {
|
|
Queue::fake();
|
|
|
|
[$user, $tenant] = createUserWithTenant(role: 'readonly');
|
|
$this->actingAs($user);
|
|
Filament::setTenant($tenant, true);
|
|
|
|
$scopeKey = hash('sha256', 'scope-readonly-blocked');
|
|
|
|
InventorySyncRun::factory()->for($tenant)->create([
|
|
'selection_hash' => $scopeKey,
|
|
'status' => InventorySyncRun::STATUS_SUCCESS,
|
|
'finished_at' => now()->subDays(2),
|
|
]);
|
|
|
|
InventorySyncRun::factory()->for($tenant)->create([
|
|
'selection_hash' => $scopeKey,
|
|
'status' => InventorySyncRun::STATUS_SUCCESS,
|
|
'finished_at' => now()->subDay(),
|
|
]);
|
|
|
|
Livewire::test(DriftLanding::class);
|
|
|
|
Queue::assertNothingPushed();
|
|
expect(BulkOperationRun::query()->where('tenant_id', $tenant->getKey())->count())->toBe(0);
|
|
});
|