Implements Spec 103 (IA semantics: Scope vs Filter vs Targeting) across Monitoring + Manage. Changes - Monitoring tenant indicator copy: “All tenants” / “Filtered by tenant: …” - Alerts KPI header resolves tenant via OperateHubShell::activeEntitledTenant() for consistency - Manage list pages (Alert Rules / Destinations) no longer show tenant indicator - AlertRule form uses targeting semantics + sections (Rule / Applies to / Delivery) - Additional UI polish: resource sections, tenant view widgets layout, RBAC progressive disclosure (“Not configured” when empty) Notes - US6 (“Add current tenant” convenience button) intentionally skipped (optional P3). Testing - CI=1 vendor/bin/sail artisan test tests/Feature/TenantRBAC/ tests/Feature/Onboarding/OnboardingIdentifyTenantTest.php - vendor/bin/sail bin pint --dirty --format agent Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de> Reviewed-on: #126
108 lines
3.2 KiB
PHP
108 lines
3.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Filament\Widgets\Alerts;
|
|
|
|
use App\Filament\Resources\AlertDeliveryResource;
|
|
use App\Filament\Resources\AlertDestinationResource;
|
|
use App\Filament\Resources\AlertRuleResource;
|
|
use App\Models\AlertDelivery;
|
|
use App\Models\AlertDestination;
|
|
use App\Models\AlertRule;
|
|
use App\Models\Tenant;
|
|
use App\Models\User;
|
|
use App\Support\OperateHub\OperateHubShell;
|
|
use App\Support\Workspaces\WorkspaceContext;
|
|
use Filament\Widgets\StatsOverviewWidget;
|
|
use Filament\Widgets\StatsOverviewWidget\Stat;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
class AlertsKpiHeader extends StatsOverviewWidget
|
|
{
|
|
protected static bool $isLazy = false;
|
|
|
|
protected int|string|array $columnSpan = 'full';
|
|
|
|
/**
|
|
* @return array<Stat>
|
|
*/
|
|
protected function getStats(): array
|
|
{
|
|
$user = auth()->user();
|
|
|
|
if (! $user instanceof User) {
|
|
return [];
|
|
}
|
|
|
|
$workspaceId = app(WorkspaceContext::class)->currentWorkspaceId(request());
|
|
|
|
if (! is_int($workspaceId)) {
|
|
return [];
|
|
}
|
|
|
|
$stats = [];
|
|
|
|
if (AlertRuleResource::canViewAny()) {
|
|
$totalRules = (int) AlertRule::query()
|
|
->where('workspace_id', $workspaceId)
|
|
->count();
|
|
|
|
$enabledRules = (int) AlertRule::query()
|
|
->where('workspace_id', $workspaceId)
|
|
->where('is_enabled', true)
|
|
->count();
|
|
|
|
$stats[] = Stat::make('Enabled rules', $enabledRules)
|
|
->description('Total '.$totalRules);
|
|
}
|
|
|
|
if (AlertDestinationResource::canViewAny()) {
|
|
$totalDestinations = (int) AlertDestination::query()
|
|
->where('workspace_id', $workspaceId)
|
|
->count();
|
|
|
|
$enabledDestinations = (int) AlertDestination::query()
|
|
->where('workspace_id', $workspaceId)
|
|
->where('is_enabled', true)
|
|
->count();
|
|
|
|
$stats[] = Stat::make('Enabled targets', $enabledDestinations)
|
|
->description('Total '.$totalDestinations);
|
|
}
|
|
|
|
if (AlertDeliveryResource::canViewAny()) {
|
|
$deliveriesQuery = $this->deliveriesQueryForViewer($user, $workspaceId);
|
|
|
|
$deliveries24Hours = (int) (clone $deliveriesQuery)
|
|
->where('created_at', '>=', now()->subDay())
|
|
->count();
|
|
|
|
$failed7Days = (int) (clone $deliveriesQuery)
|
|
->where('created_at', '>=', now()->subDays(7))
|
|
->where('status', AlertDelivery::STATUS_FAILED)
|
|
->count();
|
|
|
|
$stats[] = Stat::make('Deliveries (24h)', $deliveries24Hours);
|
|
$stats[] = Stat::make('Failed (7d)', $failed7Days);
|
|
}
|
|
|
|
return $stats;
|
|
}
|
|
|
|
private function deliveriesQueryForViewer(User $user, int $workspaceId): Builder
|
|
{
|
|
$query = AlertDelivery::query()
|
|
->where('workspace_id', $workspaceId)
|
|
->whereIn('tenant_id', $user->tenantMemberships()->select('tenant_id'));
|
|
|
|
$activeTenant = app(OperateHubShell::class)->activeEntitledTenant(request());
|
|
|
|
if ($activeTenant instanceof Tenant) {
|
|
$query->where('tenant_id', (int) $activeTenant->getKey());
|
|
}
|
|
|
|
return $query;
|
|
}
|
|
}
|