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
76 lines (66 loc) · 3.69 KB
/
StatsOverviewWidget.php
File metadata and controls
76 lines (66 loc) · 3.69 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
74
75
76
<?php
namespace App\Filament\Widgets;
use App\Enums\ResultStatus;
use App\Helpers\Number;
use App\Models\Result;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;
class StatsOverviewWidget extends BaseWidget
{
public ?Result $result = null;
protected static ?string $pollingInterval = '60s';
protected function getCards(): array
{
$this->result = Result::query()
->select(['id', 'ping', 'download', 'upload', 'status', 'created_at'])
->where('status', '=', ResultStatus::Completed)
->latest()
->first();
if (blank($this->result)) {
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()
->select(['id', 'ping', 'download', 'upload', 'status', 'created_at'])
->where('id', '<', $this->result->id)
->where('status', '=', ResultStatus::Completed)
->latest()
->first();
if (! $previous) {
return [
Stat::make('Latest download', fn (): string => ! blank($this->result) ? Number::toBitRate(bits: $this->result->download_bits, precision: 2) : 'n/a')
->icon('heroicon-o-arrow-down-tray'),
Stat::make('Latest upload', fn (): string => ! blank($this->result) ? Number::toBitRate(bits: $this->result->upload_bits, precision: 2) : '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) ? Number::toBitRate(bits: $this->result->download_bits, precision: 2) : '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) ? Number::toBitRate(bits: $this->result->upload_bits, precision: 2) : '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'),
];
}
}