TenantAtlas/app/Filament/Widgets/Dashboard/DashboardKpis.php
ahmido 3a2a06e8d7 feat: align tenant dashboard truth surfaces (#204)
## Summary
- align tenant dashboard KPI, attention, compare, and operations truth so the page does not read calmer than the tenant's actual state
- preserve tenant-safe drill-through continuity into findings, baseline compare, and canonical operations, including disabled helper states for permission-limited members
- add the Spec 173 artifact set and focused regression coverage for dashboard truth alignment and drill-through behavior

## Validation
- `vendor/bin/sail bin pint --dirty --format agent`
- `vendor/bin/sail artisan test --compact tests/Feature/Filament/DashboardKpisWidgetTest.php tests/Feature/Filament/TenantDashboardTruthAlignmentTest.php tests/Feature/Monitoring/OperationsDashboardDrillthroughTest.php tests/Feature/Filament/NeedsAttentionWidgetTest.php tests/Feature/Filament/BaselineCompareNowWidgetTest.php tests/Feature/Filament/BaselineCompareSummaryConsistencyTest.php tests/Feature/Findings/FindingsListDefaultsTest.php tests/Feature/Findings/FindingsListFiltersTest.php tests/Feature/Findings/FindingAdminTenantParityTest.php tests/Feature/OpsUx/CanonicalViewRunLinksTest.php tests/Feature/Filament/TenantDashboardTenantScopeTest.php tests/Feature/Filament/TenantDashboardDbOnlyTest.php tests/Feature/Filament/TableStandardsBaselineTest.php tests/Feature/Filament/TableDetailVisibilityTest.php`
- integrated browser smoke on the tenant dashboard, including a permission-limited member scenario

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #204
2026-04-03 20:26:15 +00:00

147 lines
4.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Filament\Widgets\Dashboard;
use App\Filament\Resources\FindingResource;
use App\Models\Finding;
use App\Models\OperationRun;
use App\Models\Tenant;
use App\Models\User;
use App\Support\Auth\Capabilities;
use App\Support\OperationRunLinks;
use App\Support\OpsUx\ActiveRuns;
use App\Support\Rbac\UiTooltips;
use Filament\Facades\Filament;
use Filament\Widgets\StatsOverviewWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;
class DashboardKpis extends StatsOverviewWidget
{
protected int|string|array $columnSpan = 'full';
protected function getPollingInterval(): ?string
{
$tenant = Filament::getTenant();
if (! $tenant instanceof Tenant) {
return null;
}
return ActiveRuns::existForTenant($tenant) ? '10s' : null;
}
/**
* @return array<Stat>
*/
protected function getStats(): array
{
$tenant = Filament::getTenant();
if (! $tenant instanceof Tenant) {
return $this->emptyStats();
}
$tenantId = (int) $tenant->getKey();
$openDriftFindings = (int) Finding::query()
->where('tenant_id', $tenantId)
->openDrift()
->count();
$highSeverityActiveFindings = (int) Finding::query()
->where('tenant_id', $tenantId)
->highSeverityActive()
->count();
$activeRuns = (int) OperationRun::query()
->where('tenant_id', $tenantId)
->healthyActive()
->count();
$followUpRuns = (int) OperationRun::query()
->where('tenant_id', $tenantId)
->dashboardNeedsFollowUp()
->count();
$openDriftUrl = $openDriftFindings > 0
? $this->findingsUrl($tenant, [
'tab' => 'needs_action',
'finding_type' => Finding::FINDING_TYPE_DRIFT,
])
: null;
$highSeverityUrl = $highSeverityActiveFindings > 0
? $this->findingsUrl($tenant, [
'tab' => 'needs_action',
'high_severity' => 1,
])
: null;
$findingsHelperText = $this->findingsHelperText($tenant);
return [
Stat::make('Open drift findings', $openDriftFindings)
->description($openDriftUrl === null && $openDriftFindings > 0
? $findingsHelperText
: 'active drift workflow items')
->color($openDriftFindings > 0 ? 'warning' : 'gray')
->url($openDriftUrl),
Stat::make('High severity active findings', $highSeverityActiveFindings)
->description($highSeverityUrl === null && $highSeverityActiveFindings > 0
? $findingsHelperText
: 'high or critical findings needing review')
->color($highSeverityActiveFindings > 0 ? 'danger' : 'gray')
->url($highSeverityUrl),
Stat::make('Active operations', $activeRuns)
->description('healthy queued or running tenant work')
->color($activeRuns > 0 ? 'info' : 'gray')
->url($activeRuns > 0 ? OperationRunLinks::index($tenant, activeTab: 'active') : null),
Stat::make('Operations needing follow-up', $followUpRuns)
->description('failed, warning, or stalled runs')
->color($followUpRuns > 0 ? 'danger' : 'gray')
->url($followUpRuns > 0 ? OperationRunLinks::index($tenant, activeTab: 'blocked') : null),
];
}
/**
* @return array<Stat>
*/
private function emptyStats(): array
{
return [
Stat::make('Open drift findings', 0),
Stat::make('High severity active findings', 0),
Stat::make('Active operations', 0),
Stat::make('Operations needing follow-up', 0),
];
}
/**
* @param array<string, mixed> $parameters
*/
private function findingsUrl(Tenant $tenant, array $parameters): ?string
{
if (! $this->canOpenFindings($tenant)) {
return null;
}
return FindingResource::getUrl('index', $parameters, panel: 'tenant', tenant: $tenant);
}
private function findingsHelperText(Tenant $tenant): string
{
return $this->canOpenFindings($tenant)
? 'Open findings'
: UiTooltips::INSUFFICIENT_PERMISSION;
}
private function canOpenFindings(Tenant $tenant): bool
{
$user = auth()->user();
return $user instanceof User
&& $user->canAccessTenant($tenant)
&& $user->can(Capabilities::TENANT_FINDINGS_VIEW, $tenant);
}
}