getDateRange($period); // Get articles fetched for the period $articlesFetchedQuery = Article::query(); if ($dateRange) { $articlesFetchedQuery->whereBetween('created_at', $dateRange); } $articlesFetched = $articlesFetchedQuery->count(); // Get articles published for the period $articlesPublishedQuery = ArticlePublication::query() ->whereNotNull('published_at'); if ($dateRange) { $articlesPublishedQuery->whereBetween('published_at', $dateRange); } $articlesPublished = $articlesPublishedQuery->count(); // Calculate published percentage $publishedPercentage = $articlesFetched > 0 ? round(($articlesPublished / $articlesFetched) * 100, 1) : 0.0; return [ 'articles_fetched' => $articlesFetched, 'articles_published' => $articlesPublished, 'published_percentage' => $publishedPercentage, ]; } /** * @return array */ public function getAvailablePeriods(): array { return [ 'today' => 'Today', 'week' => 'This Week', 'month' => 'This Month', 'year' => 'This Year', 'all' => 'All Time', ]; } /** * @return array{0: Carbon, 1: Carbon}|null */ private function getDateRange(string $period): ?array { $now = Carbon::now(); return match ($period) { 'today' => [$now->copy()->startOfDay(), $now->copy()->endOfDay()], 'week' => [$now->copy()->startOfWeek(), $now->copy()->endOfWeek()], 'month' => [$now->copy()->startOfMonth(), $now->copy()->endOfMonth()], 'year' => [$now->copy()->startOfYear(), $now->copy()->endOfYear()], 'all' => null, // No date filtering for all-time stats default => [$now->copy()->startOfDay(), $now->copy()->endOfDay()], }; } /** * Get additional stats for dashboard */ public function getSystemStats(): array { // Optimize with single queries using conditional aggregation $feedStats = DB::table('feeds') ->selectRaw(' COUNT(*) as total_feeds, SUM(CASE WHEN is_active = 1 THEN 1 ELSE 0 END) as active_feeds ') ->first(); $accountStats = DB::table('platform_accounts') ->selectRaw(' COUNT(*) as total_platform_accounts, SUM(CASE WHEN is_active = 1 THEN 1 ELSE 0 END) as active_platform_accounts ') ->first(); $channelStats = DB::table('platform_channels') ->selectRaw(' COUNT(*) as total_platform_channels, SUM(CASE WHEN is_active = 1 THEN 1 ELSE 0 END) as active_platform_channels ') ->first(); $routeStats = DB::table('routes') ->selectRaw(' COUNT(*) as total_routes, SUM(CASE WHEN is_active = 1 THEN 1 ELSE 0 END) as active_routes ') ->first(); return [ 'total_feeds' => $feedStats->total_feeds, 'active_feeds' => $feedStats->active_feeds, 'total_platform_accounts' => $accountStats->total_platform_accounts, 'active_platform_accounts' => $accountStats->active_platform_accounts, 'total_platform_channels' => $channelStats->total_platform_channels, 'active_platform_channels' => $channelStats->active_platform_channels, 'total_routes' => $routeStats->total_routes, 'active_routes' => $routeStats->active_routes, ]; } }