fedi-feed-router/backend/app/Services/DashboardStatsService.php

111 lines
3.5 KiB
PHP
Raw Normal View History

2025-08-03 01:34:11 +02:00
<?php
namespace App\Services;
use App\Models\Article;
use App\Models\ArticlePublication;
use Carbon\Carbon;
2025-08-06 20:01:28 +02:00
use Illuminate\Support\Facades\DB;
2025-08-03 01:34:11 +02:00
class DashboardStatsService
{
/**
* @return array
*/
public function getStats(string $period = 'today'): array
{
2025-08-03 20:59:09 +02:00
$dateRange = $this->getDateRange($period);
2025-08-03 01:34:11 +02:00
2025-08-03 20:59:09 +02:00
// Get articles fetched for the period
$articlesFetchedQuery = Article::query();
if ($dateRange) {
$articlesFetchedQuery->whereBetween('created_at', $dateRange);
}
$articlesFetched = $articlesFetchedQuery->count();
2025-08-03 01:34:11 +02:00
2025-08-03 20:59:09 +02:00
// Get articles published for the period
$articlesPublishedQuery = ArticlePublication::query()
->whereNotNull('published_at');
if ($dateRange) {
$articlesPublishedQuery->whereBetween('published_at', $dateRange);
}
$articlesPublished = $articlesPublishedQuery->count();
2025-08-03 01:34:11 +02:00
2025-08-03 20:59:09 +02:00
// Calculate published percentage
$publishedPercentage = $articlesFetched > 0 ? round(($articlesPublished / $articlesFetched) * 100, 1) : 0.0;
2025-08-03 01:34:11 +02:00
return [
2025-08-03 20:59:09 +02:00
'articles_fetched' => $articlesFetched,
'articles_published' => $articlesPublished,
'published_percentage' => $publishedPercentage,
2025-08-03 01:34:11 +02:00
];
}
/**
* @return array<string, string>
*/
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
{
2025-08-06 20:01:28 +02:00
// 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();
$channelStats = DB::table('platform_channels')
->selectRaw('
COUNT(*) as total_channels,
SUM(CASE WHEN is_active = 1 THEN 1 ELSE 0 END) as active_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();
2025-08-03 01:34:11 +02:00
return [
2025-08-06 20:01:28 +02:00
'total_feeds' => $feedStats->total_feeds,
'active_feeds' => $feedStats->active_feeds,
'total_channels' => $channelStats->total_channels,
'active_channels' => $channelStats->active_channels,
'total_routes' => $routeStats->total_routes,
'active_routes' => $routeStats->active_routes,
2025-08-03 01:34:11 +02:00
];
}
}