## Summary - introduce a canonical admin tenant filter-state helper and route all in-scope workspace-admin tenant resolution through `OperateHubShell::activeEntitledTenant()` - align operations monitoring, operation-run deep links, Entra group admin list/view/search behavior, and shared context-bar rendering with the documented scope contract - add the Spec 135 design artifacts, architecture note, focused guardrail coverage, and non-regression tests for filter persistence, direct-record access, and global search safety ## Validation - `vendor/bin/sail bin pint --dirty --format agent` - `vendor/bin/sail artisan test --compact tests/Feature/Monitoring/OperationsKpiHeaderTenantContextTest.php tests/Feature/Monitoring/OperationsTenantScopeTest.php tests/Feature/Monitoring/OperationsCanonicalUrlsTest.php tests/Feature/Spec085/OperationsIndexHeaderTest.php tests/Feature/Spec085/RunDetailBackAffordanceTest.php tests/Feature/Filament/OperationRunListFiltersTest.php tests/Feature/Filament/EntraGroupAdminScopeTest.php tests/Feature/Filament/EntraGroupGlobalSearchScopeTest.php tests/Feature/DirectoryGroups/BrowseGroupsTest.php tests/Feature/Filament/EntraGroupEnterpriseDetailPageTest.php tests/Feature/Filament/PolicyVersionResolvedReferenceLinksTest.php tests/Feature/Filament/EntraGroupResolvedReferencePresentationTest.php tests/Feature/Guards/AdminTenantResolverGuardTest.php tests/Feature/OpsUx/OperateHubShellTest.php tests/Feature/Filament/Alerts/AlertsKpiHeaderTest.php tests/Feature/Alerts/AlertDeliveryDeepLinkFiltersTest.php` - `vendor/bin/sail artisan test --compact tests/Feature/Filament/TableStatePersistenceTest.php tests/Feature/Filament/TenantScopingTest.php tests/Feature/Filament/Alerts/AlertDeliveryViewerTest.php tests/Unit/Support/References/CapabilityAwareReferenceResolverTest.php` ## Notes - Filament v5 remains on Livewire v4.0+ compliant surfaces only. - No provider registration changes were needed; Laravel 12 provider registration remains in `bootstrap/providers.php`. - Entra group global search remains enabled and is now scoped to the canonical admin tenant contract. Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de> Reviewed-on: #164
139 lines
4.1 KiB
PHP
139 lines
4.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Filament\Widgets\Operations;
|
|
|
|
use App\Models\OperationRun;
|
|
use App\Models\Tenant;
|
|
use App\Support\OperateHub\OperateHubShell;
|
|
use App\Support\OperationRunOutcome;
|
|
use App\Support\OperationRunStatus;
|
|
use App\Support\OpsUx\ActiveRuns;
|
|
use Carbon\CarbonInterval;
|
|
use Filament\Widgets\StatsOverviewWidget;
|
|
use Filament\Widgets\StatsOverviewWidget\Stat;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class OperationsKpiHeader extends StatsOverviewWidget
|
|
{
|
|
protected static bool $isLazy = false;
|
|
|
|
protected int|string|array $columnSpan = 'full';
|
|
|
|
protected function getPollingInterval(): ?string
|
|
{
|
|
$tenant = $this->activeTenant();
|
|
|
|
if (! $tenant instanceof Tenant) {
|
|
return null;
|
|
}
|
|
|
|
return ActiveRuns::existForTenant($tenant) ? '10s' : null;
|
|
}
|
|
|
|
/**
|
|
* @return array<Stat>
|
|
*/
|
|
protected function getStats(): array
|
|
{
|
|
$tenant = $this->activeTenant();
|
|
|
|
if (! $tenant instanceof Tenant) {
|
|
return [];
|
|
}
|
|
|
|
$tenantId = (int) $tenant->getKey();
|
|
|
|
$totalRuns30Days = (int) OperationRun::query()
|
|
->where('tenant_id', $tenantId)
|
|
->where('created_at', '>=', now()->subDays(30))
|
|
->count();
|
|
|
|
$activeRuns = (int) OperationRun::query()
|
|
->where('tenant_id', $tenantId)
|
|
->whereIn('status', [
|
|
OperationRunStatus::Queued->value,
|
|
OperationRunStatus::Running->value,
|
|
])
|
|
->count();
|
|
|
|
$failedOrPartial7Days = (int) OperationRun::query()
|
|
->where('tenant_id', $tenantId)
|
|
->where('status', OperationRunStatus::Completed->value)
|
|
->whereIn('outcome', [
|
|
OperationRunOutcome::Failed->value,
|
|
OperationRunOutcome::PartiallySucceeded->value,
|
|
])
|
|
->where('completed_at', '>=', now()->subDays(7))
|
|
->count();
|
|
|
|
/** @var Collection<int, OperationRun> $recentCompletedRuns */
|
|
$recentCompletedRuns = OperationRun::query()
|
|
->where('tenant_id', $tenantId)
|
|
->where('status', OperationRunStatus::Completed->value)
|
|
->whereNotNull('started_at')
|
|
->whereNotNull('completed_at')
|
|
->where('completed_at', '>=', now()->subDays(7))
|
|
->latest('id')
|
|
->limit(200)
|
|
->get(['started_at', 'completed_at']);
|
|
|
|
$durations = $recentCompletedRuns
|
|
->map(function (OperationRun $run): ?int {
|
|
if (! $run->started_at || ! $run->completed_at) {
|
|
return null;
|
|
}
|
|
|
|
$seconds = $run->completed_at->diffInSeconds($run->started_at);
|
|
|
|
if (is_int($seconds)) {
|
|
return $seconds;
|
|
}
|
|
|
|
return (int) round((float) $seconds);
|
|
})
|
|
->filter(fn (?int $seconds): bool => is_int($seconds) && $seconds > 0)
|
|
->values();
|
|
|
|
$avgDuration7Days = '—';
|
|
if ($durations->isNotEmpty()) {
|
|
$avgDurationSeconds = (int) round($durations->avg() ?? 0);
|
|
$avgDuration7Days = self::formatDurationSeconds($avgDurationSeconds);
|
|
}
|
|
|
|
return [
|
|
Stat::make('Total Runs (30 days)', $totalRuns30Days),
|
|
Stat::make('Active Runs', $activeRuns),
|
|
Stat::make('Failed/Partial (7 days)', $failedOrPartial7Days),
|
|
Stat::make('Avg Duration (7 days)', $avgDuration7Days),
|
|
];
|
|
}
|
|
|
|
private function activeTenant(): ?Tenant
|
|
{
|
|
$tenant = app(OperateHubShell::class)->activeEntitledTenant(request());
|
|
|
|
return $tenant instanceof Tenant ? $tenant : null;
|
|
}
|
|
|
|
private static function formatDurationSeconds(int $seconds): string
|
|
{
|
|
if ($seconds <= 0) {
|
|
return '—';
|
|
}
|
|
|
|
if ($seconds < 60) {
|
|
return $seconds.'s';
|
|
}
|
|
|
|
$interval = CarbonInterval::seconds($seconds)->cascade();
|
|
|
|
if ($seconds < 3600) {
|
|
return sprintf('%dm %ds', $interval->minutes, $interval->seconds);
|
|
}
|
|
|
|
return sprintf('%dh %dm', $interval->hours, $interval->minutes);
|
|
}
|
|
}
|