forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecentSpeedChart.php
More file actions
72 lines (62 loc) · 2.31 KB
/
RecentSpeedChart.php
File metadata and controls
72 lines (62 loc) · 2.31 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
<?php
namespace App\Filament\Widgets;
use App\Models\Result;
use App\Settings\GeneralSettings;
use Filament\Widgets\LineChartWidget;
class RecentSpeedChart extends LineChartWidget
{
protected int|string|array $columnSpan = 'full';
protected static ?string $maxHeight = '300px';
public ?string $filter = '24h';
protected function getHeading(): string
{
return 'Download / Upload';
}
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 (Mbps)',
'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 (Mbps)',
'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')),
];
}
}