83 lines
No EOL
2.8 KiB
PHP
83 lines
No EOL
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Article;
|
|
use App\Models\ArticlePublication;
|
|
use Carbon\Carbon;
|
|
|
|
class DashboardStatsService
|
|
{
|
|
/**
|
|
* @return array{articles_fetched: int, articles_published: int, published_percentage: float}
|
|
*/
|
|
public function getStats(string $period = 'today'): array
|
|
{
|
|
$dateRange = $this->getDateRange($period);
|
|
|
|
$articlesFetched = Article::when($dateRange, function ($query) use ($dateRange) {
|
|
return $query->whereBetween('created_at', $dateRange);
|
|
})->count();
|
|
|
|
$articlesPublished = ArticlePublication::when($dateRange, function ($query) use ($dateRange) {
|
|
return $query->whereBetween('published_at', $dateRange);
|
|
})->count();
|
|
|
|
$publishedPercentage = $articlesFetched > 0
|
|
? round(($articlesPublished / $articlesFetched) * 100, 1)
|
|
: 0.0;
|
|
|
|
return [
|
|
'articles_fetched' => $articlesFetched,
|
|
'articles_published' => $articlesPublished,
|
|
'published_percentage' => $publishedPercentage,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @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
|
|
* @return array{total_feeds: int, active_feeds: int, total_channels: int, active_channels: int, total_routes: int, active_routes: int}
|
|
*/
|
|
public function getSystemStats(): array
|
|
{
|
|
return [
|
|
'total_feeds' => \App\Models\Feed::count(),
|
|
'active_feeds' => \App\Models\Feed::where('is_active', true)->count(),
|
|
'total_channels' => \App\Models\PlatformChannel::count(),
|
|
'active_channels' => \App\Models\PlatformChannel::where('is_active', true)->count(),
|
|
'total_routes' => \App\Models\Route::count(),
|
|
'active_routes' => \App\Models\Route::where('is_active', true)->count(),
|
|
];
|
|
}
|
|
} |