DocketEntry::count(), 'total_documents' => Document::count(), 'total_subscribers' => Subscription::where('is_active', true)->count(), 'recent_entries' => DocketEntry::with('documents') ->latest('date') ->take(5) ->get() ->map(function ($entry) { return [ 'id' => $entry->id, 'date' => $entry->date->format('m/d/Y'), 'title' => $entry->title, 'summary' => $entry->summary, 'documents_count' => $entry->documents->count(), ]; }), ]; $stats += $this->documentAccessSummary(); return Inertia::render('Admin/Dashboard', $stats); } /** * Headline document-access numbers for the last 60 days, plus the most * requested documents. Crawlers are excluded so the figures reflect people. */ private function documentAccessSummary(): array { $since = Carbon::now()->subDays(60)->startOfDay(); return [ 'downloads_60d' => DocumentAccess::excludingBots() ->where('accessed_at', '>=', $since) ->count(), 'top_documents_60d' => DocumentAccess::query() ->excludingBots() ->join('documents', 'documents.id', '=', 'document_accesses.document_id') ->where('document_accesses.accessed_at', '>=', $since) ->groupBy('documents.id', 'documents.title') ->orderByDesc('downloads') ->take(5) ->get([ 'documents.id', 'documents.title', DB::raw('count(*) as downloads'), ]) ->map(fn ($row) => [ 'id' => $row->id, 'title' => $row->title, 'downloads' => (int) $row->downloads, ]), ]; } }