Implements Spec 087: Legacy Runs Removal (rigorous). ### What changed - Canonicalized run history: **`operation_runs` is the only run system** for inventory sync, Entra group sync, backup schedule execution/retention/purge. - Removed legacy UI surfaces (Filament Resources / relation managers) for legacy run models. - Legacy run URLs now return **404** (no redirects), with RBAC semantics preserved (404 vs 403 as specified). - Canonicalized affected `operation_runs.type` values (dotted → underscore) via migration. - Drift + inventory references now point to canonical operation runs; includes backfills and then drops legacy FK columns. - Drops legacy run tables after cutover. - Added regression guards to prevent reintroducing legacy run tokens or “backfilling” canonical runs from legacy tables. ### Migrations - `2026_02_12_000001..000006_*` canonicalize types, add/backfill operation_run_id references, drop legacy columns, and drop legacy run tables. ### Tests Focused pack for this spec passed: - `tests/Feature/Guards/NoLegacyRunsTest.php` - `tests/Feature/Guards/NoLegacyRunBackfillTest.php` - `tests/Feature/Operations/LegacyRunRoutesNotFoundTest.php` - `tests/Feature/Monitoring/MonitoringOperationsTest.php` - `tests/Feature/Jobs/RunInventorySyncJobTest.php` ### Notes / impact - Destructive cleanup is handled via migrations (drops legacy tables) after code cutover; deploy should run migrations in the same release. Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de> Reviewed-on: #106
167 lines
6.0 KiB
PHP
167 lines
6.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Filament\Widgets\Inventory;
|
|
|
|
use App\Models\InventoryItem;
|
|
use App\Models\OperationRun;
|
|
use App\Models\Tenant;
|
|
use App\Services\Inventory\CoverageCapabilitiesResolver;
|
|
use App\Support\Badges\BadgeDomain;
|
|
use App\Support\Badges\BadgeRenderer;
|
|
use App\Support\Inventory\InventoryKpiBadges;
|
|
use App\Support\Inventory\InventoryPolicyTypeMeta;
|
|
use App\Support\OperationRunOutcome;
|
|
use App\Support\OperationRunStatus;
|
|
use Filament\Facades\Filament;
|
|
use Filament\Widgets\StatsOverviewWidget;
|
|
use Filament\Widgets\StatsOverviewWidget\Stat;
|
|
use Illuminate\Support\Facades\Blade;
|
|
use Illuminate\Support\HtmlString;
|
|
|
|
class InventoryKpiHeader extends StatsOverviewWidget
|
|
{
|
|
protected static bool $isLazy = false;
|
|
|
|
protected int|string|array $columnSpan = 'full';
|
|
|
|
/**
|
|
* Inventory KPI aggregation source-of-truth:
|
|
* - `inventory_items.policy_type`
|
|
* - `config('tenantpilot.supported_policy_types')` + `config('tenantpilot.foundation_types')` meta (`restore`, `risk`)
|
|
* - dependency capability via `CoverageCapabilitiesResolver`
|
|
*
|
|
* @return array<Stat>
|
|
*/
|
|
protected function getStats(): array
|
|
{
|
|
$tenant = Filament::getTenant();
|
|
|
|
if (! $tenant instanceof Tenant) {
|
|
return [
|
|
Stat::make('Total items', 0),
|
|
Stat::make('Coverage', '0%')->description('Restorable 0 • Partial 0'),
|
|
Stat::make('Last inventory sync', '—'),
|
|
Stat::make('Active ops', 0),
|
|
Stat::make('Inventory ops', 0)->description('Dependencies 0 • Risk 0'),
|
|
];
|
|
}
|
|
|
|
$tenantId = (int) $tenant->getKey();
|
|
|
|
/** @var array<string, int> $countsByPolicyType */
|
|
$countsByPolicyType = InventoryItem::query()
|
|
->where('tenant_id', $tenantId)
|
|
->selectRaw('policy_type, COUNT(*) as aggregate')
|
|
->groupBy('policy_type')
|
|
->pluck('aggregate', 'policy_type')
|
|
->map(fn ($value): int => (int) $value)
|
|
->all();
|
|
|
|
$totalItems = array_sum($countsByPolicyType);
|
|
|
|
$restorableItems = 0;
|
|
$partialItems = 0;
|
|
$riskItems = 0;
|
|
|
|
foreach ($countsByPolicyType as $policyType => $count) {
|
|
if (InventoryPolicyTypeMeta::isRestorable($policyType)) {
|
|
$restorableItems += $count;
|
|
} elseif (InventoryPolicyTypeMeta::isPartial($policyType)) {
|
|
$partialItems += $count;
|
|
}
|
|
|
|
if (InventoryPolicyTypeMeta::isHighRisk($policyType)) {
|
|
$riskItems += $count;
|
|
}
|
|
}
|
|
|
|
$coveragePercent = $totalItems > 0
|
|
? (int) round(($restorableItems / $totalItems) * 100)
|
|
: 0;
|
|
|
|
$lastRun = OperationRun::query()
|
|
->where('tenant_id', $tenantId)
|
|
->where('type', 'inventory_sync')
|
|
->where('status', OperationRunStatus::Completed->value)
|
|
->whereNotNull('completed_at')
|
|
->latest('completed_at')
|
|
->latest('id')
|
|
->first();
|
|
|
|
$lastInventorySyncTimeLabel = '—';
|
|
$lastInventorySyncStatusLabel = '—';
|
|
$lastInventorySyncStatusColor = 'gray';
|
|
$lastInventorySyncStatusIcon = 'heroicon-m-clock';
|
|
$lastInventorySyncViewUrl = null;
|
|
|
|
if ($lastRun instanceof OperationRun) {
|
|
$timestamp = $lastRun->completed_at ?? $lastRun->started_at ?? $lastRun->created_at;
|
|
|
|
if ($timestamp) {
|
|
$lastInventorySyncTimeLabel = $timestamp->diffForHumans(['short' => true]);
|
|
}
|
|
|
|
$outcome = (string) ($lastRun->outcome ?? OperationRunOutcome::Pending->value);
|
|
$badge = BadgeRenderer::spec(BadgeDomain::OperationRunOutcome, $outcome);
|
|
$lastInventorySyncStatusLabel = $badge->label;
|
|
$lastInventorySyncStatusColor = $badge->color;
|
|
$lastInventorySyncStatusIcon = (string) ($badge->icon ?? 'heroicon-m-clock');
|
|
|
|
$lastInventorySyncViewUrl = route('admin.operations.view', ['run' => (int) $lastRun->getKey()]);
|
|
}
|
|
|
|
$badgeColor = $lastInventorySyncStatusColor;
|
|
|
|
$lastInventorySyncDescription = Blade::render(<<<'BLADE'
|
|
<div class="flex items-center gap-2">
|
|
<x-filament::badge :color="$badgeColor" size="sm">
|
|
{{ $statusLabel }}
|
|
</x-filament::badge>
|
|
|
|
@if ($viewUrl)
|
|
<x-filament::link :href="$viewUrl" size="sm">
|
|
View run
|
|
</x-filament::link>
|
|
@endif
|
|
</div>
|
|
BLADE, [
|
|
'badgeColor' => $badgeColor,
|
|
'statusLabel' => $lastInventorySyncStatusLabel,
|
|
'viewUrl' => $lastInventorySyncViewUrl,
|
|
]);
|
|
|
|
$activeOps = (int) OperationRun::query()
|
|
->where('tenant_id', $tenantId)
|
|
->active()
|
|
->count();
|
|
|
|
$inventoryOps = (int) OperationRun::query()
|
|
->where('tenant_id', $tenantId)
|
|
->where('type', 'inventory_sync')
|
|
->active()
|
|
->count();
|
|
|
|
$resolver = app(CoverageCapabilitiesResolver::class);
|
|
|
|
$dependenciesItems = 0;
|
|
foreach ($countsByPolicyType as $policyType => $count) {
|
|
if ($policyType !== '' && $resolver->supportsDependencies($policyType)) {
|
|
$dependenciesItems += $count;
|
|
}
|
|
}
|
|
|
|
return [
|
|
Stat::make('Total items', $totalItems),
|
|
Stat::make('Coverage', $coveragePercent.'%')
|
|
->description(new HtmlString(InventoryKpiBadges::coverage($restorableItems, $partialItems))),
|
|
Stat::make('Last inventory sync', $lastInventorySyncTimeLabel)
|
|
->description(new HtmlString($lastInventorySyncDescription)),
|
|
Stat::make('Active ops', $activeOps),
|
|
Stat::make('Inventory ops', $inventoryOps)
|
|
->description(new HtmlString(InventoryKpiBadges::inventoryOps($dependenciesItems, $riskItems))),
|
|
];
|
|
}
|
|
}
|