forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatsOverviewWidget.php
More file actions
73 lines (63 loc) · 3.44 KB
/
StatsOverviewWidget.php
File metadata and controls
73 lines (63 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
namespace App\Filament\Widgets;
use App\Models\Result;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;
class StatsOverviewWidget extends BaseWidget
{
public ?Result $result = null;
protected function getPollingInterval(): ?string
{
return config('speedtest.dashboard_polling');
}
protected function getCards(): array
{
$this->result = Result::query()
->latest()
->first();
if (blank($this->result) || ! $this->result->successful) {
return [
Stat::make('Latest download', '-')
->icon('heroicon-o-arrow-down-tray'),
Stat::make('Latest upload', '-')
->icon('heroicon-o-arrow-up-tray'),
Stat::make('Latest ping', '-')
->icon('heroicon-o-clock'),
];
}
$previous = Result::query()
->where('id', '<', $this->result->id)
->latest()
->first();
if (! $previous || ! $previous->successful) {
return [
Stat::make('Latest download', fn (): string => ! blank($this->result) ? toBits(convertSize($this->result->download), 2).' (Mbps)' : 'n/a')
->icon('heroicon-o-arrow-down-tray'),
Stat::make('Latest upload', fn (): string => ! blank($this->result) ? toBits(convertSize($this->result->upload), 2).' (Mbps)' : 'n/a')
->icon('heroicon-o-arrow-up-tray'),
Stat::make('Latest ping', fn (): string => ! blank($this->result) ? number_format($this->result->ping, 2).' (ms)' : 'n/a')
->icon('heroicon-o-clock'),
];
}
$downloadChange = percentChange($this->result->download, $previous->download, 2);
$uploadChange = percentChange($this->result->upload, $previous->upload, 2);
$pingChange = percentChange($this->result->ping, $previous->ping, 2);
return [
Stat::make('Latest download', fn (): string => ! blank($this->result) ? toBits(convertSize($this->result->download), 2).' (Mbps)' : 'n/a')
->icon('heroicon-o-arrow-down-tray')
->description($downloadChange > 0 ? $downloadChange.'% faster' : abs($downloadChange).'% slower')
->descriptionIcon($downloadChange > 0 ? 'heroicon-m-arrow-trending-up' : 'heroicon-m-arrow-trending-down')
->color($downloadChange > 0 ? 'success' : 'danger'),
Stat::make('Latest upload', fn (): string => ! blank($this->result) ? toBits(convertSize($this->result->upload), 2).' (Mbps)' : 'n/a')
->icon('heroicon-o-arrow-up-tray')
->description($uploadChange > 0 ? $uploadChange.'% faster' : abs($uploadChange).'% slower')
->descriptionIcon($uploadChange > 0 ? 'heroicon-m-arrow-trending-up' : 'heroicon-m-arrow-trending-down')
->color($uploadChange > 0 ? 'success' : 'danger'),
Stat::make('Latest ping', fn (): string => ! blank($this->result) ? number_format($this->result->ping, 2).' (ms)' : 'n/a')
->icon('heroicon-o-clock')
->description($pingChange > 0 ? $pingChange.'% slower' : abs($pingChange).'% faster')
->descriptionIcon($pingChange > 0 ? 'heroicon-m-arrow-trending-up' : 'heroicon-m-arrow-trending-down')
->color($pingChange > 0 ? 'danger' : 'success'),
];
}
}