forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecentJitterChartWidget.php
More file actions
113 lines (101 loc) · 3.8 KB
/
RecentJitterChartWidget.php
File metadata and controls
113 lines (101 loc) · 3.8 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
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
namespace App\Filament\Widgets;
use App\Enums\ResultStatus;
use App\Models\Result;
use Filament\Widgets\ChartWidget;
class RecentJitterChartWidget extends ChartWidget
{
protected static ?string $heading = 'Jitter';
protected int|string|array $columnSpan = 'full';
protected static ?string $maxHeight = '250px';
protected static ?string $pollingInterval = '60s';
public ?string $filter = '24h';
protected function getFilters(): ?array
{
return [
'24h' => 'Last 24h',
'week' => 'Last week',
'month' => 'Last month',
];
}
protected function getData(): array
{
$results = Result::query()
->select(['id', 'data', 'created_at'])
->where('status', '=', ResultStatus::Completed)
->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());
})
->orderBy('created_at')
->get();
return [
'datasets' => [
[
'label' => 'Download (ms)',
'data' => $results->map(fn ($item) => $item->download_jitter),
'borderColor' => 'rgba(14, 165, 233)',
'backgroundColor' => 'rgba(14, 165, 233, 0.1)',
'pointBackgroundColor' => 'rgba(14, 165, 233)',
'fill' => true,
'cubicInterpolationMode' => 'monotone',
'tension' => 0.4,
'pointRadius' => count($results) <= 24 ? 3 : 0,
],
[
'label' => 'Upload (ms)',
'data' => $results->map(fn ($item) => $item->upload_jitter),
'borderColor' => 'rgba(139, 92, 246)',
'backgroundColor' => 'rgba(139, 92, 246, 0.1)',
'pointBackgroundColor' => 'rgba(139, 92, 246)',
'fill' => true,
'cubicInterpolationMode' => 'monotone',
'tension' => 0.4,
'pointRadius' => count($results) <= 24 ? 3 : 0,
],
[
'label' => 'Ping (ms)',
'data' => $results->map(fn ($item) => $item->ping_jitter),
'borderColor' => 'rgba(16, 185, 129)',
'backgroundColor' => 'rgba(16, 185, 129, 0.1)',
'pointBackgroundColor' => 'rgba(16, 185, 129)',
'fill' => true,
'cubicInterpolationMode' => 'monotone',
'tension' => 0.4,
'pointRadius' => count($results) <= 24 ? 3 : 0,
],
],
'labels' => $results->map(fn ($item) => $item->created_at->timezone(config('app.display_timezone'))->format(config('app.chart_datetime_format'))),
];
}
protected function getOptions(): array
{
return [
'plugins' => [
'legend' => [
'display' => true,
],
'tooltip' => [
'enabled' => true,
'mode' => 'index',
'intersect' => false,
'position' => 'nearest',
],
],
'scales' => [
'y' => [
'beginAtZero' => config('app.chart_begin_at_zero'),
],
],
];
}
protected function getType(): string
{
return 'line';
}
}