forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecentSpeedChartWidget.php
More file actions
100 lines (87 loc) · 2.98 KB
/
RecentSpeedChartWidget.php
File metadata and controls
100 lines (87 loc) · 2.98 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace App\Filament\Widgets;
use App\Models\Result;
use App\Settings\GeneralSettings;
use Filament\Widgets\ChartWidget;
class RecentSpeedChartWidget extends ChartWidget
{
protected static ?string $heading = 'Download / Upload';
protected int|string|array $columnSpan = 'full';
protected static ?string $maxHeight = '300px';
public ?string $filter = '24h';
protected function getPollingInterval(): ?string
{
return config('speedtest.dashboard_polling');
}
protected function getFilters(): ?array
{
return [
'24h' => 'Last 24h',
'week' => 'Last week',
'month' => 'Last month',
];
}
protected function getData(): array
{
$settings = new GeneralSettings();
$results = Result::query()
->select(['id', 'download', 'upload', 'created_at'])
->when($this->filter == '24h', function ($query) {
$query->where('created_at', '>=', now()->subDay());
})
->when($this->filter == 'week', function ($query) {
$query->where('created_at', '>=', now()->subWeek());
})
->when($this->filter == 'month', function ($query) {
$query->where('created_at', '>=', now()->subMonth());
})
->get();
return [
'datasets' => [
[
'label' => 'Download',
'data' => $results->map(fn ($item) => ! blank($item->download) ? toBits(convertSize($item->download), 2) : 0),
'borderColor' => '#0ea5e9',
'backgroundColor' => '#0ea5e9',
'fill' => false,
'cubicInterpolationMode' => 'monotone',
'tension' => 0.4,
],
[
'label' => 'Upload',
'data' => $results->map(fn ($item) => ! blank($item->upload) ? toBits(convertSize($item->upload), 2) : 0),
'borderColor' => '#8b5cf6',
'backgroundColor' => '#8b5cf6',
'fill' => false,
'cubicInterpolationMode' => 'monotone',
'tension' => 0.4,
],
],
'labels' => $results->map(fn ($item) => $item->created_at->timezone($settings->timezone)->format('M d - G:i')),
];
}
protected function getOptions(): array
{
return [
'scales' => [
'x' => [
'display' => true,
'title' => [
'display' => true,
],
],
'y' => [
'display' => true,
'title' => [
'display' => true,
'text' => 'Mbps',
],
],
],
];
}
protected function getType(): string
{
return 'line';
}
}