NotificationChannelPerformance   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
eloc 52
c 1
b 0
f 1
dl 0
loc 84
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getData() 0 53 2
A canView() 0 3 1
A getOptions() 0 12 1
A getType() 0 3 1
1
<?php
2
3
namespace Usamamuneerchaudhary\Notifier\Filament\Widgets;
4
5
use Filament\Facades\Filament;
6
use Filament\Widgets\ChartWidget;
7
use Usamamuneerchaudhary\Notifier\Models\Notification;
8
use Usamamuneerchaudhary\Notifier\Models\NotificationChannel;
9
10
class NotificationChannelPerformance extends ChartWidget
11
{
12
    protected ?string $heading = 'Channel Performance';
13
    protected static ?int $sort = 5;
14
    protected ?string $pollingInterval = '30s';
15
16
    public static function canView(): bool
17
    {
18
        return false;
19
    }
20
21
    protected function getData(): array
22
    {
23
        $channels = NotificationChannel::where('is_active', true)->get();
24
        $labels = [];
25
        $sentData = [];
26
        $openedData = [];
27
        $clickedData = [];
28
        $colors = [
29
            'rgba(59, 130, 246, 0.8)',
30
            'rgba(16, 185, 129, 0.8)',
31
            'rgba(245, 158, 11, 0.8)',
32
            'rgba(239, 68, 68, 0.8)',
33
            'rgba(139, 92, 246, 0.8)',
34
            'rgba(236, 72, 153, 0.8)',
35
        ];
36
37
        foreach ($channels as $index => $channel) {
38
            $labels[] = $channel->title;
39
40
            $sent = Notification::where('channel', $channel->type)
41
                ->where('status', 'sent')
42
                ->count();
43
            $opened = Notification::where('channel', $channel->type)
44
                ->whereNotNull('opened_at')
45
                ->count();
46
            $clicked = Notification::where('channel', $channel->type)
47
                ->whereNotNull('clicked_at')
48
                ->count();
49
50
            $sentData[] = $sent;
51
            $openedData[] = $opened;
52
            $clickedData[] = $clicked;
53
        }
54
55
        return [
56
            'datasets' => [
57
                [
58
                    'label' => 'Sent',
59
                    'data' => $sentData,
60
                    'backgroundColor' => $colors[0],
61
                ],
62
                [
63
                    'label' => 'Opened',
64
                    'data' => $openedData,
65
                    'backgroundColor' => $colors[1],
66
                ],
67
                [
68
                    'label' => 'Clicked',
69
                    'data' => $clickedData,
70
                    'backgroundColor' => $colors[2],
71
                ],
72
            ],
73
            'labels' => $labels,
74
        ];
75
    }
76
77
    protected function getType(): string
78
    {
79
        return 'bar';
80
    }
81
82
    protected function getOptions(): array
83
    {
84
        return [
85
            'scales' => [
86
                'y' => [
87
                    'beginAtZero' => true,
88
                ],
89
            ],
90
            'plugins' => [
91
                'legend' => [
92
                    'display' => true,
93
                    'position' => 'top',
94
                ],
95
            ],
96
        ];
97
    }
98
}
99
100