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

99 lines
3.4 KiB
PHP
Raw Permalink Normal View History

2025-08-03 01:34:11 +02:00
<?php
namespace App\Services;
use App\Models\Article;
use App\Models\ArticlePublication;
2025-08-10 01:26:56 +02:00
use App\Models\Feed;
use App\Models\PlatformAccount;
use App\Models\PlatformChannel;
use App\Models\Route;
2025-08-03 01:34:11 +02:00
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
{
public function getStats(string $period = 'today'): array
{
2025-08-03 20:59:09 +02:00
$dateRange = $this->getDateRange($period);
2025-08-10 01:26:56 +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();
2025-08-10 01:26:56 +02:00
2025-08-03 01:34:11 +02:00
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()],
};
}
public function getSystemStats(): array
{
2025-08-10 01:26:56 +02:00
$totalFeeds = Feed::query()->count();
$activeFeeds = Feed::query()->where('is_active', 1)->count();
$totalPlatformAccounts = PlatformAccount::query()->count();
$activePlatformAccounts = PlatformAccount::query()->where('is_active', 1)->count();
$totalPlatformChannels = PlatformChannel::query()->count();
$activePlatformChannels = PlatformChannel::query()->where('is_active', 1)->count();
$totalRoutes = Route::query()->count();
$activeRoutes = Route::query()->where('is_active', 1)->count();
2025-08-03 01:34:11 +02:00
return [
2025-08-10 01:26:56 +02:00
'total_feeds' => $totalFeeds,
'active_feeds' => $activeFeeds,
'total_platform_accounts' => $totalPlatformAccounts,
'active_platform_accounts' => $activePlatformAccounts,
'total_platform_channels' => $totalPlatformChannels,
'active_platform_channels' => $activePlatformChannels,
'total_routes' => $totalRoutes,
'active_routes' => $activeRoutes,
2025-08-03 01:34:11 +02:00
];
}
2025-08-10 01:26:56 +02:00
}