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;
|
2026-08-12 00:00:49 +02:00
|
|
|
use App\Support\DateRange;
|
2025-08-03 01:34:11 +02:00
|
|
|
|
|
|
|
|
class DashboardStatsService
|
|
|
|
|
{
|
2026-03-08 14:18:28 +01:00
|
|
|
/**
|
|
|
|
|
* @return array<string, mixed>
|
|
|
|
|
*/
|
2026-08-12 00:00:49 +02:00
|
|
|
public function getStats(DateRange $range): array
|
2025-08-03 01:34:11 +02:00
|
|
|
{
|
2026-08-12 00:00:49 +02:00
|
|
|
$bounds = [$range->from, $range->to];
|
2025-08-10 01:26:56 +02:00
|
|
|
|
2026-08-12 00:00:49 +02:00
|
|
|
$articlesFetched = Article::query()
|
|
|
|
|
->whereBetween('created_at', $bounds)
|
|
|
|
|
->count();
|
2025-08-03 01:34:11 +02:00
|
|
|
|
2026-08-12 00:00:49 +02:00
|
|
|
$articlesPublished = ArticlePublication::query()
|
|
|
|
|
->whereBetween('published_at', $bounds)
|
|
|
|
|
->count();
|
2025-08-03 01:34:11 +02:00
|
|
|
|
2025-08-03 20:59:09 +02:00
|
|
|
$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
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-08 14:18:28 +01:00
|
|
|
/**
|
|
|
|
|
* @return array<string, int>
|
|
|
|
|
*/
|
2025-08-03 01:34:11 +02:00
|
|
|
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
|
|
|
}
|