84 lines
2.3 KiB
PHP
84 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Pages;
|
|
|
|
use App\Filament\Resources\FindingResource;
|
|
use App\Jobs\GenerateDriftFindingsJob;
|
|
use App\Models\Finding;
|
|
use App\Models\InventorySyncRun;
|
|
use App\Models\Tenant;
|
|
use App\Models\User;
|
|
use App\Services\Drift\DriftRunSelector;
|
|
use BackedEnum;
|
|
use Filament\Pages\Page;
|
|
use UnitEnum;
|
|
|
|
class DriftLanding extends Page
|
|
{
|
|
protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-arrows-right-left';
|
|
|
|
protected static string|UnitEnum|null $navigationGroup = 'Drift';
|
|
|
|
protected static ?string $navigationLabel = 'Drift';
|
|
|
|
protected string $view = 'filament.pages.drift-landing';
|
|
|
|
public function mount(): void
|
|
{
|
|
$tenant = Tenant::current();
|
|
|
|
$user = auth()->user();
|
|
if (! $user instanceof User) {
|
|
abort(403, 'Not allowed');
|
|
}
|
|
|
|
$latestSuccessful = InventorySyncRun::query()
|
|
->where('tenant_id', $tenant->getKey())
|
|
->where('status', InventorySyncRun::STATUS_SUCCESS)
|
|
->whereNotNull('finished_at')
|
|
->orderByDesc('finished_at')
|
|
->first();
|
|
|
|
if (! $latestSuccessful instanceof InventorySyncRun) {
|
|
return;
|
|
}
|
|
|
|
$scopeKey = (string) $latestSuccessful->selection_hash;
|
|
|
|
$selector = app(DriftRunSelector::class);
|
|
$comparison = $selector->selectBaselineAndCurrent($tenant, $scopeKey);
|
|
|
|
if ($comparison === null) {
|
|
return;
|
|
}
|
|
|
|
$baseline = $comparison['baseline'];
|
|
$current = $comparison['current'];
|
|
|
|
$exists = Finding::query()
|
|
->where('tenant_id', $tenant->getKey())
|
|
->where('finding_type', Finding::FINDING_TYPE_DRIFT)
|
|
->where('scope_key', $scopeKey)
|
|
->where('baseline_run_id', $baseline->getKey())
|
|
->where('current_run_id', $current->getKey())
|
|
->exists();
|
|
|
|
if ($exists) {
|
|
return;
|
|
}
|
|
|
|
GenerateDriftFindingsJob::dispatch(
|
|
tenantId: (int) $tenant->getKey(),
|
|
userId: (int) $user->getKey(),
|
|
baselineRunId: (int) $baseline->getKey(),
|
|
currentRunId: (int) $current->getKey(),
|
|
scopeKey: $scopeKey,
|
|
);
|
|
}
|
|
|
|
public function getFindingsUrl(): string
|
|
{
|
|
return FindingResource::getUrl('index', tenant: Tenant::current());
|
|
}
|
|
}
|