|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Filament\Widgets; |
| 4 | + |
| 5 | +use App\Models\Article; |
| 6 | +use Filament\Widgets\StatsOverviewWidget as BaseWidget; |
| 7 | +use Filament\Widgets\StatsOverviewWidget\Stat; |
| 8 | + |
| 9 | +class ArticlesStatsOverview extends BaseWidget |
| 10 | +{ |
| 11 | + protected ?string $pollingInterval = '30s'; |
| 12 | + |
| 13 | + protected function getStats(): array |
| 14 | + { |
| 15 | + $window = 30; |
| 16 | + $cacheTtlSeconds = 300; // 5 minutes |
| 17 | + |
| 18 | + $total = cache()->remember( |
| 19 | + "widgets:articles:total", |
| 20 | + now()->addSeconds($cacheTtlSeconds), |
| 21 | + fn() => Article::query()->count(), |
| 22 | + ); |
| 23 | + |
| 24 | + $publishedTotal = cache()->remember( |
| 25 | + "widgets:articles:published:total", |
| 26 | + now()->addSeconds($cacheTtlSeconds), |
| 27 | + fn() => Article::query()->published()->count(), |
| 28 | + ); |
| 29 | + |
| 30 | + $publishedWindow = cache()->remember( |
| 31 | + "widgets:articles:published:{$window}", |
| 32 | + now()->addSeconds($cacheTtlSeconds), |
| 33 | + fn() => Article::query() |
| 34 | + ->published() |
| 35 | + ->where('submitted_at', '>=', now()->subDays($window)) |
| 36 | + ->count(), |
| 37 | + ); |
| 38 | + |
| 39 | + $awaiting = cache()->remember( |
| 40 | + "widgets:articles:awaiting", |
| 41 | + now()->addSeconds(60), |
| 42 | + fn() => Article::query()->awaitingApproval()->count(), |
| 43 | + ); |
| 44 | + |
| 45 | + return [ |
| 46 | + Stat::make('Articles', number_format($total)) |
| 47 | + ->description('Total articles') |
| 48 | + ->icon('heroicon-o-newspaper'), |
| 49 | + |
| 50 | + Stat::make('Published', number_format($publishedTotal)) |
| 51 | + ->description('All-time approved') |
| 52 | + ->color('success') |
| 53 | + ->icon('heroicon-o-check-circle'), |
| 54 | + |
| 55 | + Stat::make("Published ({$window}d)", number_format($publishedWindow)) |
| 56 | + ->description("Approved in last {$window} days") |
| 57 | + ->color('info') |
| 58 | + ->icon('heroicon-o-check-badge'), |
| 59 | + |
| 60 | + Stat::make('Awaiting Approval', number_format($awaiting)) |
| 61 | + ->description('Submitted but not approved') |
| 62 | + ->color('warning') |
| 63 | + ->icon('heroicon-o-clock'), |
| 64 | + ]; |
| 65 | + } |
| 66 | +} |
0 commit comments