98 lines
No EOL
3.7 KiB
PHP
98 lines
No EOL
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Article;
|
|
use App\Models\ArticlePublication;
|
|
use Carbon\Carbon;
|
|
|
|
class DashboardStatsService
|
|
{
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function getStats(string $period = 'today'): array
|
|
{
|
|
$todayRange = $this->getDateRange('today');
|
|
$weekRange = $this->getDateRange('week');
|
|
$monthRange = $this->getDateRange('month');
|
|
|
|
// Get totals for different periods
|
|
$totalToday = Article::whereBetween('created_at', $todayRange)->count();
|
|
$totalWeek = Article::whereBetween('created_at', $weekRange)->count();
|
|
$totalMonth = Article::whereBetween('created_at', $monthRange)->count();
|
|
|
|
// Get approved articles (using approval_status field)
|
|
$approvedToday = Article::whereBetween('created_at', $todayRange)
|
|
->where('approval_status', 'approved')->count();
|
|
$approvedWeek = Article::whereBetween('created_at', $weekRange)
|
|
->where('approval_status', 'approved')->count();
|
|
$approvedMonth = Article::whereBetween('created_at', $monthRange)
|
|
->where('approval_status', 'approved')->count();
|
|
|
|
// Calculate approval percentages
|
|
$approvalPercentageToday = $totalToday > 0 ? round(($approvedToday / $totalToday) * 100, 1) : 0.0;
|
|
$approvalPercentageWeek = $totalWeek > 0 ? round(($approvedWeek / $totalWeek) * 100, 1) : 0.0;
|
|
$approvalPercentageMonth = $totalMonth > 0 ? round(($approvedMonth / $totalMonth) * 100, 1) : 0.0;
|
|
|
|
return [
|
|
'total_today' => $totalToday,
|
|
'total_week' => $totalWeek,
|
|
'total_month' => $totalMonth,
|
|
'approved_today' => $approvedToday,
|
|
'approved_week' => $approvedWeek,
|
|
'approved_month' => $approvedMonth,
|
|
'approval_percentage_today' => $approvalPercentageToday,
|
|
'approval_percentage_week' => $approvalPercentageWeek,
|
|
'approval_percentage_month' => $approvalPercentageMonth,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @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
|
|
{
|
|
return [
|
|
'total_feeds' => \App\Models\Feed::count(),
|
|
'active_feeds' => \App\Models\Feed::where('is_active', true)->count(),
|
|
'total_platform_accounts' => \App\Models\PlatformAccount::count(),
|
|
'active_platform_accounts' => \App\Models\PlatformAccount::where('is_active', true)->count(),
|
|
'total_platform_channels' => \App\Models\PlatformChannel::count(),
|
|
'active_platform_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(),
|
|
];
|
|
}
|
|
} |