*/ protected function getViewData(): array { $window = SystemConsoleWindow::fromNullable((string) request()->query('window')); $start = $window->startAt(); /** @var Collection $grouped */ $grouped = OperationRun::query() ->selectRaw('workspace_id, tenant_id, type, COUNT(*) AS failed_count') ->where('created_at', '>=', $start) ->where('status', OperationRunStatus::Completed->value) ->where('outcome', OperationRunOutcome::Failed->value) ->groupBy('workspace_id', 'tenant_id', 'type') ->orderByDesc('failed_count') ->limit(10) ->get(); $workspaceIds = $grouped ->pluck('workspace_id') ->filter(fn ($value): bool => is_numeric($value)) ->map(fn ($value): int => (int) $value) ->unique() ->values() ->all(); $tenantIds = $grouped ->pluck('tenant_id') ->filter(fn ($value): bool => is_numeric($value)) ->map(fn ($value): int => (int) $value) ->unique() ->values() ->all(); $workspaceNames = Workspace::query() ->whereIn('id', $workspaceIds) ->pluck('name', 'id') ->all(); $tenantNames = Tenant::query() ->whereIn('id', $tenantIds) ->pluck('name', 'id') ->all(); return [ 'windowLabel' => SystemConsoleWindow::options()[$window->value] ?? 'Last 24 hours', 'offenders' => $grouped->map(function (OperationRun $record) use ($workspaceNames, $tenantNames): array { $workspaceId = is_numeric($record->workspace_id) ? (int) $record->workspace_id : null; $tenantId = is_numeric($record->tenant_id) ? (int) $record->tenant_id : null; return [ 'workspace_label' => $workspaceId !== null ? ($workspaceNames[$workspaceId] ?? ('Workspace #'.$workspaceId)) : 'Unknown workspace', 'tenant_label' => $tenantId !== null ? ($tenantNames[$tenantId] ?? ('Tenant #'.$tenantId)) : 'Tenantless', 'operation_label' => OperationCatalog::label((string) $record->type), 'failed_count' => (int) $record->getAttribute('failed_count'), ]; }), 'runsUrl' => SystemOperationRunLinks::index(), ]; } }