NotificationStatsOverview::getStats()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 40
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 29
c 1
b 0
f 1
dl 0
loc 40
rs 9.456
cc 2
nc 2
nop 0
1
<?php
2
namespace Usamamuneerchaudhary\Notifier\Filament\Widgets;
3
4
use Filament\Facades\Filament;
5
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
6
use Filament\Widgets\StatsOverviewWidget\Stat;
7
use Usamamuneerchaudhary\Notifier\Models\Notification;
8
use Usamamuneerchaudhary\Notifier\Models\NotificationChannel;
9
10
class NotificationStatsOverview extends BaseWidget
11
{
12
    protected ?string $pollingInterval = '10s';
13
14
    public static function canView(): bool
15
    {
16
        return false;
17
    }
18
19
    protected function getStats(): array
20
    {
21
        $totalNotifications = Notification::count();
22
        $sentNotifications = Notification::where('status', 'sent')->count();
23
        $pendingNotifications = Notification::where('status', 'pending')->count();
24
        $failedNotifications = Notification::where('status', 'failed')->count();
25
        $activeChannels = NotificationChannel::where('is_active', true)->count();
26
27
        // Calculate success rate
28
        $successRate = $totalNotifications > 0 ? round(($sentNotifications / $totalNotifications) * 100, 1) : 0;
29
30
        // Get recent notifications for chart data
31
        $recentNotifications = $this->getRecentNotificationsData();
32
33
        return [
34
            Stat::make('Total Notifications', $totalNotifications)
35
                ->description('All time notifications')
36
                ->descriptionIcon('heroicon-m-envelope')
37
                ->chart($recentNotifications)
38
                ->color('success'),
39
40
            Stat::make('Success Rate', $successRate . '%')
41
                ->description($sentNotifications . ' of ' . $totalNotifications . ' sent successfully')
0 ignored issues
show
Bug introduced by
Are you sure $totalNotifications of type Illuminate\Database\Eloq...roughRelationship|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
                ->description($sentNotifications . ' of ' . /** @scrutinizer ignore-type */ $totalNotifications . ' sent successfully')
Loading history...
42
                ->descriptionIcon('heroicon-m-check-circle')
43
                ->color('success'),
44
45
            Stat::make('Pending Notifications', $pendingNotifications)
46
                ->description('Awaiting delivery')
47
                ->descriptionIcon('heroicon-m-clock')
48
                ->color('warning'),
49
50
            Stat::make('Failed Notifications', $failedNotifications)
51
                ->description('Failed to deliver')
52
                ->descriptionIcon('heroicon-m-x-circle')
53
                ->color('danger'),
54
55
            Stat::make('Active Channels', $activeChannels)
56
                ->description('Enabled notification channels')
57
                ->descriptionIcon('heroicon-m-bolt')
58
                ->color('info'),
59
        ];
60
    }
61
62
    private function getRecentNotificationsData(): array
63
    {
64
        $data = [];
65
        for ($i = 6; $i >= 0; $i--) {
66
            $date = now()->subDays($i);
67
            $count = Notification::whereDate('created_at', $date)->count();
68
            $data[] = $count;
69
        }
70
        return $data;
71
    }
72
73
}
74