Passed
Push — main ( 3b4587...cb10fc )
by Usama
05:39
created

NotificationTimeSeriesChart::getOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Umun\Notifier\Filament\Widgets;
4
5
use Filament\Facades\Filament;
6
use Filament\Widgets\ChartWidget;
7
use Umun\Notifier\Models\Notification;
8
9
class NotificationTimeSeriesChart extends ChartWidget
10
{
11
    protected ?string $heading = 'Notifications Over Time';
12
    protected static ?int $sort = 3;
13
    protected ?string $pollingInterval = '30s';
14
15
16
    public static function canView(): bool
17
    {
18
        return false;
19
    }
20
21
    protected function getData(): array
22
    {
23
        $days = 30;
24
        $labels = [];
25
        $sentData = [];
26
        $openedData = [];
27
        $clickedData = [];
28
29
        for ($i = $days - 1; $i >= 0; $i--) {
30
            $date = now()->subDays($i);
31
            $labels[] = $date->format('M d');
32
33
            $sentData[] = Notification::whereDate('created_at', $date)->count();
34
            $openedData[] = Notification::whereDate('opened_at', $date)->count();
35
            $clickedData[] = Notification::whereDate('clicked_at', $date)->count();
36
        }
37
38
        return [
39
            'datasets' => [
40
                [
41
                    'label' => 'Sent',
42
                    'data' => $sentData,
43
                    'backgroundColor' => 'rgba(59, 130, 246, 0.5)',
44
                    'borderColor' => 'rgb(59, 130, 246)',
45
                    'fill' => false,
46
                ],
47
                [
48
                    'label' => 'Opened',
49
                    'data' => $openedData,
50
                    'backgroundColor' => 'rgba(16, 185, 129, 0.5)',
51
                    'borderColor' => 'rgb(16, 185, 129)',
52
                    'fill' => false,
53
                ],
54
                [
55
                    'label' => 'Clicked',
56
                    'data' => $clickedData,
57
                    'backgroundColor' => 'rgba(245, 158, 11, 0.5)',
58
                    'borderColor' => 'rgb(245, 158, 11)',
59
                    'fill' => false,
60
                ],
61
            ],
62
            'labels' => $labels,
63
        ];
64
    }
65
66
    protected function getType(): string
67
    {
68
        return 'line';
69
    }
70
71
    protected function getOptions(): array
72
    {
73
        return [
74
            'scales' => [
75
                'y' => [
76
                    'beginAtZero' => true,
77
                ],
78
            ],
79
            'plugins' => [
80
                'legend' => [
81
                    'display' => true,
82
                    'position' => 'top',
83
                ],
84
            ],
85
        ];
86
    }
87
}
88
89