*/ 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 = Filament::getTenant(); if ($activeTenant instanceof Tenant) { $query->where('tenant_id', (int) $activeTenant->getKey()); } return $query; } }