Skip to content

Commit ed1106c

Browse files
authored
[Feature] Separate charts for download upload or mixed axis chart (alexjustesen#1225)
1 parent 0716bc3 commit ed1106c

File tree

6 files changed

+138
-32
lines changed

6 files changed

+138
-32
lines changed

app/Filament/Pages/Dashboard.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
namespace App\Filament\Pages;
44

55
use App\Console\Commands\RunOoklaSpeedtest;
6+
use App\Filament\Widgets\RecentDownloadChartWidget;
67
use App\Filament\Widgets\RecentJitterChartWidget;
78
use App\Filament\Widgets\RecentPingChartWidget;
8-
use App\Filament\Widgets\RecentSpeedChartWidget;
9+
use App\Filament\Widgets\RecentUploadChartWidget;
910
use App\Filament\Widgets\StatsOverviewWidget;
1011
use App\Settings\GeneralSettings;
1112
use Filament\Actions\Action;
@@ -52,7 +53,8 @@ protected function getHeaderWidgets(): array
5253
{
5354
return [
5455
StatsOverviewWidget::make(),
55-
RecentSpeedChartWidget::make(),
56+
RecentDownloadChartWidget::make(),
57+
RecentUploadChartWidget::make(),
5658
RecentPingChartWidget::make(),
5759
RecentJitterChartWidget::make(),
5860
];

app/Filament/Widgets/RecentSpeedChartWidget.php renamed to app/Filament/Widgets/RecentDownloadChartWidget.php

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
33
namespace App\Filament\Widgets;
44

55
use App\Enums\ResultStatus;
6+
use App\Helpers\Number;
67
use App\Helpers\TimeZoneHelper;
78
use App\Models\Result;
89
use App\Settings\GeneralSettings;
910
use Filament\Widgets\ChartWidget;
1011

11-
class RecentSpeedChartWidget extends ChartWidget
12+
class RecentDownloadChartWidget extends ChartWidget
1213
{
13-
protected static ?string $heading = 'Download / Upload';
14+
protected static ?string $heading = 'Download (Mbps)';
1415

1516
protected int|string|array $columnSpan = 'full';
1617

17-
protected static ?string $maxHeight = '300px';
18+
protected static ?string $maxHeight = '250px';
1819

1920
public ?string $filter = '24h';
2021

@@ -37,7 +38,7 @@ protected function getData(): array
3738
$settings = new GeneralSettings();
3839

3940
$results = Result::query()
40-
->select(['id', 'download', 'upload', 'created_at'])
41+
->select(['id', 'download', 'created_at'])
4142
->where('status', '=', ResultStatus::Completed)
4243
->when($this->filter == '24h', function ($query) {
4344
$query->where('created_at', '>=', now()->subDay());
@@ -55,22 +56,13 @@ protected function getData(): array
5556
'datasets' => [
5657
[
5758
'label' => 'Download',
58-
'data' => $results->map(fn ($item) => ! blank($item->download) ? toBits(convertSize($item->download), 2) : 0),
59+
'data' => $results->map(fn ($item) => ! blank($item->download) ? Number::bitsToMagnitude(bits: $item->download_bits, precision: 2, magnitude: 'mbit') : 0),
5960
'borderColor' => '#0ea5e9',
6061
'backgroundColor' => '#0ea5e9',
6162
'fill' => false,
6263
'cubicInterpolationMode' => 'monotone',
6364
'tension' => 0.4,
6465
],
65-
[
66-
'label' => 'Upload',
67-
'data' => $results->map(fn ($item) => ! blank($item->upload) ? toBits(convertSize($item->upload), 2) : 0),
68-
'borderColor' => '#8b5cf6',
69-
'backgroundColor' => '#8b5cf6',
70-
'fill' => false,
71-
'cubicInterpolationMode' => 'monotone',
72-
'tension' => 0.4,
73-
],
7466
],
7567
'labels' => $results->map(fn ($item) => $item->created_at->timezone(TimeZoneHelper::displayTimeZone($settings))->format('M d - G:i')),
7668
];
@@ -79,19 +71,14 @@ protected function getData(): array
7971
protected function getOptions(): array
8072
{
8173
return [
82-
'scales' => [
83-
'x' => [
84-
'display' => true,
85-
'title' => [
86-
'display' => true,
87-
],
74+
'plugins' => [
75+
'legend' => [
76+
'display' => false,
8877
],
78+
],
79+
'scales' => [
8980
'y' => [
90-
'display' => true,
91-
'title' => [
92-
'display' => true,
93-
'text' => 'Mbps',
94-
],
81+
'beginAtZero' => true,
9582
],
9683
],
9784
];

app/Filament/Widgets/RecentJitterChartWidget.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class RecentJitterChartWidget extends ChartWidget
1414

1515
protected int|string|array $columnSpan = 'full';
1616

17-
protected static ?string $maxHeight = '300px';
17+
protected static ?string $maxHeight = '250px';
1818

1919
public ?string $filter = '24h';
2020

@@ -37,7 +37,7 @@ protected function getData(): array
3737
$settings = new GeneralSettings();
3838

3939
$results = Result::query()
40-
->select(['data', 'created_at'])
40+
->select(['id', 'data', 'created_at'])
4141
->where('status', '=', ResultStatus::Completed)
4242
->when($this->filter == '24h', function ($query) {
4343
$query->where('created_at', '>=', now()->subDay());
@@ -85,6 +85,17 @@ protected function getData(): array
8585
];
8686
}
8787

88+
protected function getOptions(): array
89+
{
90+
return [
91+
'scales' => [
92+
'y' => [
93+
'beginAtZero' => true,
94+
],
95+
],
96+
];
97+
}
98+
8899
protected function getType(): string
89100
{
90101
return 'line';

app/Filament/Widgets/RecentPingChartWidget.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class RecentPingChartWidget extends ChartWidget
1414

1515
protected int|string|array $columnSpan = 'full';
1616

17-
protected static ?string $maxHeight = '300px';
17+
protected static ?string $maxHeight = '250px';
1818

1919
public ?string $filter = '24h';
2020

@@ -37,7 +37,7 @@ protected function getData(): array
3737
$settings = new GeneralSettings();
3838

3939
$results = Result::query()
40-
->select(['ping', 'created_at'])
40+
->select(['id', 'ping', 'created_at'])
4141
->where('status', '=', ResultStatus::Completed)
4242
->when($this->filter == '24h', function ($query) {
4343
$query->where('created_at', '>=', now()->subDay());
@@ -67,6 +67,17 @@ protected function getData(): array
6767
];
6868
}
6969

70+
protected function getOptions(): array
71+
{
72+
return [
73+
'scales' => [
74+
'y' => [
75+
'beginAtZero' => true,
76+
],
77+
],
78+
];
79+
}
80+
7081
protected function getType(): string
7182
{
7283
return 'line';
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace App\Filament\Widgets;
4+
5+
use App\Enums\ResultStatus;
6+
use App\Helpers\Number;
7+
use App\Helpers\TimeZoneHelper;
8+
use App\Models\Result;
9+
use App\Settings\GeneralSettings;
10+
use Filament\Widgets\ChartWidget;
11+
12+
class RecentUploadChartWidget extends ChartWidget
13+
{
14+
protected static ?string $heading = 'Upload (Mbps)';
15+
16+
protected int|string|array $columnSpan = 'full';
17+
18+
protected static ?string $maxHeight = '250px';
19+
20+
public ?string $filter = '24h';
21+
22+
protected function getPollingInterval(): ?string
23+
{
24+
return config('speedtest.dashboard_polling');
25+
}
26+
27+
protected function getFilters(): ?array
28+
{
29+
return [
30+
'24h' => 'Last 24h',
31+
'week' => 'Last week',
32+
'month' => 'Last month',
33+
];
34+
}
35+
36+
protected function getData(): array
37+
{
38+
$settings = new GeneralSettings();
39+
40+
$results = Result::query()
41+
->select(['id', 'upload', 'created_at'])
42+
->where('status', '=', ResultStatus::Completed)
43+
->when($this->filter == '24h', function ($query) {
44+
$query->where('created_at', '>=', now()->subDay());
45+
})
46+
->when($this->filter == 'week', function ($query) {
47+
$query->where('created_at', '>=', now()->subWeek());
48+
})
49+
->when($this->filter == 'month', function ($query) {
50+
$query->where('created_at', '>=', now()->subMonth());
51+
})
52+
->orderBy('created_at')
53+
->get();
54+
55+
return [
56+
'datasets' => [
57+
[
58+
'label' => 'Upload',
59+
'data' => $results->map(fn ($item) => ! blank($item->upload) ? Number::bitsToMagnitude(bits: $item->upload_bits, precision: 2, magnitude: 'mbit') : 0),
60+
'borderColor' => '#8b5cf6',
61+
'backgroundColor' => '#8b5cf6',
62+
'fill' => false,
63+
'cubicInterpolationMode' => 'monotone',
64+
'tension' => 0.4,
65+
],
66+
],
67+
'labels' => $results->map(fn ($item) => $item->created_at->timezone(TimeZoneHelper::displayTimeZone($settings))->format('M d - G:i')),
68+
];
69+
}
70+
71+
protected function getOptions(): array
72+
{
73+
return [
74+
'plugins' => [
75+
'legend' => [
76+
'display' => false,
77+
],
78+
],
79+
'scales' => [
80+
'y' => [
81+
'beginAtZero' => true,
82+
],
83+
],
84+
];
85+
}
86+
87+
protected function getType(): string
88+
{
89+
return 'line';
90+
}
91+
}

resources/views/dashboard.blade.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
@endisset
1212

1313
<div class="col-span-full">
14-
@livewire(\App\Filament\Widgets\RecentSpeedChartWidget::class)
14+
@livewire(\App\Filament\Widgets\RecentDownloadChartWidget::class)
15+
</div>
16+
17+
<div class="col-span-full">
18+
@livewire(\App\Filament\Widgets\RecentUploadChartWidget::class)
1519
</div>
1620

1721
<div class="col-span-full">

0 commit comments

Comments
 (0)