Skip to content

Commit e223632

Browse files
authored
Pretty charts (alexjustesen#213)
1 parent 016c713 commit e223632

File tree

7 files changed

+629
-228
lines changed

7 files changed

+629
-228
lines changed

_ide_helper.php

Lines changed: 206 additions & 86 deletions
Large diffs are not rendered by default.

app/Filament/Pages/Dashboard.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace App\Filament\Pages;
44

5+
use App\Filament\Widgets\RecentJitterChart;
6+
use App\Filament\Widgets\RecentPingChart;
7+
use App\Filament\Widgets\RecentSpeedChart;
58
use App\Filament\Widgets\StatsOverview;
69
use App\Jobs\ExecSpeedtest;
710
use App\Settings\GeneralSettings;
@@ -26,6 +29,9 @@ public function getHeaderWidgets(): array
2629
{
2730
return [
2831
StatsOverview::class,
32+
RecentSpeedChart::class,
33+
RecentPingChart::class,
34+
RecentJitterChart::class,
2935
];
3036
}
3137

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
namespace App\Filament\Widgets;
4+
5+
use App\Models\Result;
6+
use App\Settings\GeneralSettings;
7+
use Filament\Widgets\LineChartWidget;
8+
9+
class RecentJitterChart extends LineChartWidget
10+
{
11+
protected int|string|array $columnSpan = 'full';
12+
13+
protected static ?string $maxHeight = '300px';
14+
15+
protected function getHeading(): string
16+
{
17+
return 'Jitter (ms)';
18+
}
19+
20+
protected function getFilters(): ?array
21+
{
22+
return [
23+
'today' => 'Today',
24+
'week' => 'Last week',
25+
'month' => 'Last month',
26+
];
27+
}
28+
29+
protected function getData(): array
30+
{
31+
$range = [];
32+
33+
$settings = new GeneralSettings();
34+
35+
switch ($this->filter) {
36+
case 'today':
37+
$range = [
38+
['created_at', '>=', now()->startOfDay()],
39+
['created_at', '<=', now()],
40+
];
41+
break;
42+
43+
case 'week':
44+
$range = [
45+
['created_at', '>=', now()->subWeek()],
46+
['created_at', '<=', now()],
47+
];
48+
break;
49+
50+
case 'month':
51+
$range = [
52+
['created_at', '>=', now()->subMonth()],
53+
['created_at', '<=', now()],
54+
];
55+
break;
56+
}
57+
58+
$results = Result::query()
59+
->select(['data', 'created_at'])
60+
->where($range)
61+
->get();
62+
63+
return [
64+
'datasets' => [
65+
[
66+
'label' => 'Download',
67+
'data' => $results->map(fn ($item) => json_decode($item->data)->download->latency->jitter),
68+
'borderColor' => '#0ea5e9',
69+
'backgroundColor' => '#0ea5e9',
70+
],
71+
[
72+
'label' => 'Upload',
73+
'data' => $results->map(fn ($item) => json_decode($item->data)->upload->latency->jitter),
74+
'borderColor' => '#8b5cf6',
75+
'backgroundColor' => '#8b5cf6',
76+
],
77+
[
78+
'label' => 'Ping',
79+
'data' => $results->map(fn ($item) => json_decode($item->data)->ping->jitter),
80+
'borderColor' => '#10b981',
81+
'backgroundColor' => '#10b981',
82+
],
83+
],
84+
'labels' => $results->map(fn ($item) => $item->created_at->timezone($settings->timezone)->format('M d - G:i')),
85+
];
86+
}
87+
88+
protected static ?array $options = [
89+
'plugins' => [
90+
//
91+
],
92+
'scales' => [
93+
'y' => [
94+
'suggestedMin' => 0,
95+
],
96+
],
97+
];
98+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace App\Filament\Widgets;
4+
5+
use App\Models\Result;
6+
use App\Settings\GeneralSettings;
7+
use Filament\Widgets\LineChartWidget;
8+
9+
class RecentPingChart extends LineChartWidget
10+
{
11+
protected int|string|array $columnSpan = 'full';
12+
13+
protected static ?string $maxHeight = '300px';
14+
15+
protected function getHeading(): string
16+
{
17+
return 'Ping (ms)';
18+
}
19+
20+
protected function getFilters(): ?array
21+
{
22+
return [
23+
'today' => 'Today',
24+
'week' => 'Last week',
25+
'month' => 'Last month',
26+
];
27+
}
28+
29+
protected function getData(): array
30+
{
31+
$range = [];
32+
33+
$settings = new GeneralSettings();
34+
35+
switch ($this->filter) {
36+
case 'today':
37+
$range = [
38+
['created_at', '>=', now()->startOfDay()],
39+
['created_at', '<=', now()],
40+
];
41+
break;
42+
43+
case 'week':
44+
$range = [
45+
['created_at', '>=', now()->subWeek()],
46+
['created_at', '<=', now()],
47+
];
48+
break;
49+
50+
case 'month':
51+
$range = [
52+
['created_at', '>=', now()->subMonth()],
53+
['created_at', '<=', now()],
54+
];
55+
break;
56+
}
57+
58+
$results = Result::query()
59+
->select(['ping', 'created_at'])
60+
->where($range)
61+
->get();
62+
63+
return [
64+
'datasets' => [
65+
[
66+
'label' => 'Ping',
67+
'data' => $results->map(fn ($item) => $item->ping),
68+
'borderColor' => '#10b981',
69+
'backgroundColor' => '#10b981',
70+
],
71+
],
72+
'labels' => $results->map(fn ($item) => $item->created_at->timezone($settings->timezone)->format('M d - G:i')),
73+
];
74+
}
75+
76+
protected static ?array $options = [
77+
'plugins' => [
78+
//
79+
],
80+
'scales' => [
81+
'y' => [
82+
'min' => 0,
83+
],
84+
],
85+
];
86+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
namespace App\Filament\Widgets;
4+
5+
use App\Models\Result;
6+
use App\Settings\GeneralSettings;
7+
use Filament\Widgets\LineChartWidget;
8+
9+
class RecentSpeedChart extends LineChartWidget
10+
{
11+
protected int|string|array $columnSpan = 'full';
12+
13+
protected static ?string $maxHeight = '300px';
14+
15+
protected function getHeading(): string
16+
{
17+
return 'Download / Upload (mbps)';
18+
}
19+
20+
protected function getFilters(): ?array
21+
{
22+
return [
23+
'today' => 'Today',
24+
'week' => 'Last week',
25+
'month' => 'Last month',
26+
];
27+
}
28+
29+
protected function getData(): array
30+
{
31+
$range = [];
32+
33+
$settings = new GeneralSettings();
34+
35+
switch ($this->filter) {
36+
case 'today':
37+
$range = [
38+
['created_at', '>=', now()->startOfDay()],
39+
['created_at', '<=', now()],
40+
];
41+
break;
42+
43+
case 'week':
44+
$range = [
45+
['created_at', '>=', now()->subWeek()],
46+
['created_at', '<=', now()],
47+
];
48+
break;
49+
50+
case 'month':
51+
$range = [
52+
['created_at', '>=', now()->subMonth()],
53+
['created_at', '<=', now()],
54+
];
55+
break;
56+
}
57+
58+
$results = Result::query()
59+
->select(['download', 'upload', 'created_at'])
60+
->where($range)
61+
->get();
62+
63+
return [
64+
'datasets' => [
65+
[
66+
'label' => 'Download',
67+
'data' => $results->map(fn ($item) => formatBits(formatBytesToBits($item->download), 2, false)),
68+
'borderColor' => '#0ea5e9',
69+
'backgroundColor' => '#0ea5e9',
70+
],
71+
[
72+
'label' => 'Upload',
73+
'data' => $results->map(fn ($item) => formatBits(formatBytesToBits($item->upload), 2, false)),
74+
'borderColor' => '#8b5cf6',
75+
'backgroundColor' => '#8b5cf6',
76+
],
77+
],
78+
'labels' => $results->map(fn ($item) => $item->created_at->timezone($settings->timezone)->format('M d - G:i')),
79+
];
80+
}
81+
82+
protected static ?array $options = [
83+
'plugins' => [
84+
//
85+
],
86+
'scales' => [
87+
'y' => [
88+
'min' => 0,
89+
],
90+
],
91+
];
92+
}

0 commit comments

Comments
 (0)