RateLimitingStatusWidget::getStats()   B
last analyzed

Complexity

Conditions 8
Paths 9

Size

Total Lines 43
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 31
c 1
b 0
f 1
dl 0
loc 43
rs 8.1795
cc 8
nc 9
nop 0
1
<?php
2
3
namespace Usamamuneerchaudhary\Notifier\Filament\Widgets;
4
5
use Filament\Facades\Filament;
6
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
7
use Filament\Widgets\StatsOverviewWidget\Stat;
8
use Usamamuneerchaudhary\Notifier\Models\NotificationSetting;
9
use Usamamuneerchaudhary\Notifier\Services\RateLimitingService;
10
11
class RateLimitingStatusWidget extends BaseWidget
12
{
13
    protected ?string $heading = 'Rate Limiting Status';
14
    protected static ?int $sort = 6;
15
    protected ?string $pollingInterval = '10s';
16
17
    public static function canView(): bool
18
    {
19
        return false;
20
    }
21
22
    protected function getStats(): array
23
    {
24
        $rateLimiting = NotificationSetting::getRateLimiting();
25
26
        if (!($rateLimiting['enabled'] ?? config('notifier.settings.rate_limiting.enabled', true))) {
27
            return [
28
                Stat::make('Rate Limiting', 'Disabled')
29
                    ->description('Rate limiting is currently disabled')
30
                    ->color('gray'),
31
            ];
32
        }
33
34
        $rateLimitingService = app(RateLimitingService::class);
35
        $status = $rateLimitingService->getStatus();
36
37
        $minuteUsage = $status['limits']['minute']['current'];
38
        $minuteMax = $status['limits']['minute']['max'];
39
        $minutePercent = $minuteMax > 0 ? round(($minuteUsage / $minuteMax) * 100, 1) : 0;
40
41
        $hourUsage = $status['limits']['hour']['current'];
42
        $hourMax = $status['limits']['hour']['max'];
43
        $hourPercent = $hourMax > 0 ? round(($hourUsage / $hourMax) * 100, 1) : 0;
44
45
        $dayUsage = $status['limits']['day']['current'];
46
        $dayMax = $status['limits']['day']['max'];
47
        $dayPercent = $dayMax > 0 ? round(($dayUsage / $dayMax) * 100, 1) : 0;
48
49
        return [
50
            Stat::make('Per Minute', $minuteUsage . ' / ' . $minuteMax)
51
                ->description($minutePercent . '% used')
52
                ->descriptionIcon($minutePercent > 80 ? 'heroicon-m-exclamation-triangle' : 'heroicon-m-check-circle')
53
                ->color($this->getColorForPercent($minutePercent))
54
                ->chart([$minuteUsage, $minuteMax]),
55
56
            Stat::make('Per Hour', $hourUsage . ' / ' . $hourMax)
57
                ->description($hourPercent . '% used')
58
                ->descriptionIcon($hourPercent > 80 ? 'heroicon-m-exclamation-triangle' : 'heroicon-m-check-circle')
59
                ->color($this->getColorForPercent($hourPercent)),
60
61
            Stat::make('Per Day', $dayUsage . ' / ' . $dayMax)
62
                ->description($dayPercent . '% used')
63
                ->descriptionIcon($dayPercent > 80 ? 'heroicon-m-exclamation-triangle' : 'heroicon-m-check-circle')
64
                ->color($this->getColorForPercent($dayPercent)),
65
        ];
66
    }
67
68
    private function getColorForPercent(float $percent): string
69
    {
70
        if ($percent >= 90) {
71
            return 'danger';
72
        } elseif ($percent >= 75) {
73
            return 'warning';
74
        } else {
75
            return 'success';
76
        }
77
    }
78
}
79
80