Skip to content

Commit 5ff5489

Browse files
committed
Moved chart widget logic into separate abstract class
1 parent 29bc59d commit 5ff5489

File tree

7 files changed

+87
-399
lines changed

7 files changed

+87
-399
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace App\Filament\Widgets;
4+
5+
use App\Enums\ResultStatus;
6+
use App\Models\Result;
7+
use Filament\Widgets\ChartWidget;
8+
9+
abstract class RecentChartWidget extends ChartWidget
10+
{
11+
protected int|string|array $columnSpan = 'full';
12+
13+
protected static ?string $maxHeight = '250px';
14+
15+
public ?string $filter = '24h';
16+
17+
protected function getPollingInterval(): ?string
18+
{
19+
return config('speedtest.dashboard_polling');
20+
}
21+
22+
protected function getDateFilters()
23+
{
24+
return [
25+
'24h' => ['label' => 'Last 24h', 'date' => now()->subDay()],
26+
'week' => ['label' => 'Last week', 'date' => now()->subWeek()],
27+
'month' => ['label' => 'Last month', 'date' => now()->subMonth()],
28+
];
29+
}
30+
31+
protected function getFilters(): ?array
32+
{
33+
return array_map(fn ($filter) => $filter['label'], $this->getDateFilters());
34+
}
35+
36+
protected function getResults(string $column)
37+
{
38+
$filtersDates = array_map(fn ($filter) => $filter['date'], $this->getDateFilters());
39+
40+
return Result::query()
41+
->select(['id', $column, 'created_at'])
42+
->where('status', '=', ResultStatus::Completed)
43+
->where('created_at', '>=', $filtersDates[$this->filter])
44+
->orderBy('created_at')
45+
->get();
46+
}
47+
48+
protected function getOptions(): array
49+
{
50+
return [
51+
'plugins' => [
52+
'legend' => [
53+
'display' => true,
54+
],
55+
'tooltip' => [
56+
'enabled' => true,
57+
'mode' => 'index',
58+
'intersect' => false,
59+
'position' => 'nearest',
60+
],
61+
],
62+
'scales' => [
63+
'y' => [
64+
'beginAtZero' => config('app.chart_begin_at_zero'),
65+
'grace' => 2,
66+
],
67+
],
68+
];
69+
}
70+
71+
protected function getType(): string
72+
{
73+
return 'line';
74+
}
75+
}

app/Filament/Widgets/RecentDownloadChartWidget.php

Lines changed: 2 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,16 @@
22

33
namespace App\Filament\Widgets;
44

5-
use App\Enums\ResultStatus;
65
use App\Helpers\Average;
76
use App\Helpers\Number;
8-
use App\Models\Result;
9-
use Filament\Widgets\ChartWidget;
107

11-
class RecentDownloadChartWidget extends ChartWidget
8+
class RecentDownloadChartWidget extends RecentChartWidget
129
{
1310
protected static ?string $heading = 'Download (Mbps)';
1411

15-
protected int|string|array $columnSpan = 'full';
16-
17-
protected static ?string $maxHeight = '250px';
18-
19-
public ?string $filter = '24h';
20-
21-
protected function getPollingInterval(): ?string
22-
{
23-
return config('speedtest.dashboard_polling');
24-
}
25-
26-
protected function getFilters(): ?array
27-
{
28-
return [
29-
'24h' => 'Last 24h',
30-
'week' => 'Last week',
31-
'month' => 'Last month',
32-
];
33-
}
34-
3512
protected function getData(): array
3613
{
37-
$results = Result::query()
38-
->select(['id', 'download', 'created_at'])
39-
->where('status', '=', ResultStatus::Completed)
40-
->when($this->filter == '24h', function ($query) {
41-
$query->where('created_at', '>=', now()->subDay());
42-
})
43-
->when($this->filter == 'week', function ($query) {
44-
$query->where('created_at', '>=', now()->subWeek());
45-
})
46-
->when($this->filter == 'month', function ($query) {
47-
$query->where('created_at', '>=', now()->subMonth());
48-
})
49-
->orderBy('created_at')
50-
->get();
14+
$results = $this->getResults('download');
5115

5216
return [
5317
'datasets' => [
@@ -76,33 +40,4 @@ protected function getData(): array
7640
'labels' => $results->map(fn ($item) => $item->created_at->timezone(config('app.display_timezone'))->format(config('app.chart_datetime_format'))),
7741
];
7842
}
79-
80-
protected function getOptions(): array
81-
{
82-
return [
83-
'plugins' => [
84-
'legend' => [
85-
'display' => true,
86-
87-
],
88-
'tooltip' => [
89-
'enabled' => true,
90-
'mode' => 'index',
91-
'intersect' => false,
92-
'position' => 'nearest',
93-
],
94-
],
95-
'scales' => [
96-
'y' => [
97-
'beginAtZero' => config('app.chart_begin_at_zero'),
98-
'grace' => 2,
99-
],
100-
],
101-
];
102-
}
103-
104-
protected function getType(): string
105-
{
106-
return 'line';
107-
}
10843
}

app/Filament/Widgets/RecentDownloadLatencyChartWidget.php

Lines changed: 2 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,13 @@
22

33
namespace App\Filament\Widgets;
44

5-
use App\Enums\ResultStatus;
6-
use App\Models\Result;
7-
use Filament\Widgets\ChartWidget;
8-
9-
class RecentDownloadLatencyChartWidget extends ChartWidget
5+
class RecentDownloadLatencyChartWidget extends RecentChartWidget
106
{
117
protected static ?string $heading = 'Download Latency';
128

13-
protected int|string|array $columnSpan = 'full';
14-
15-
protected static ?string $maxHeight = '250px';
16-
17-
public ?string $filter = '24h';
18-
19-
protected function getPollingInterval(): ?string
20-
{
21-
return config('speedtest.dashboard_polling');
22-
}
23-
24-
protected function getFilters(): ?array
25-
{
26-
return [
27-
'24h' => 'Last 24h',
28-
'week' => 'Last week',
29-
'month' => 'Last month',
30-
];
31-
}
32-
339
protected function getData(): array
3410
{
35-
$results = Result::query()
36-
->select(['id', 'data', 'created_at'])
37-
->where('status', '=', ResultStatus::Completed)
38-
->when($this->filter == '24h', function ($query) {
39-
$query->where('created_at', '>=', now()->subDay());
40-
})
41-
->when($this->filter == 'week', function ($query) {
42-
$query->where('created_at', '>=', now()->subWeek());
43-
})
44-
->when($this->filter == 'month', function ($query) {
45-
$query->where('created_at', '>=', now()->subMonth());
46-
})
47-
->orderBy('created_at')
48-
->get();
11+
$results = $this->getResults('data');
4912

5013
return [
5114
'datasets' => [
@@ -86,32 +49,4 @@ protected function getData(): array
8649
'labels' => $results->map(fn ($item) => $item->created_at->timezone(config('app.display_timezone'))->format(config('app.chart_datetime_format'))),
8750
];
8851
}
89-
90-
protected function getOptions(): array
91-
{
92-
return [
93-
'plugins' => [
94-
'legend' => [
95-
'display' => true,
96-
],
97-
'tooltip' => [
98-
'enabled' => true,
99-
'mode' => 'index',
100-
'intersect' => false,
101-
'position' => 'nearest',
102-
],
103-
],
104-
'scales' => [
105-
'y' => [
106-
'beginAtZero' => config('app.chart_begin_at_zero'),
107-
'grace' => 2,
108-
],
109-
],
110-
];
111-
}
112-
113-
protected function getType(): string
114-
{
115-
return 'line';
116-
}
11752
}

app/Filament/Widgets/RecentJitterChartWidget.php

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,13 @@
22

33
namespace App\Filament\Widgets;
44

5-
use App\Enums\ResultStatus;
6-
use App\Models\Result;
7-
use Filament\Widgets\ChartWidget;
8-
9-
class RecentJitterChartWidget extends ChartWidget
5+
class RecentJitterChartWidget extends RecentChartWidget
106
{
117
protected static ?string $heading = 'Jitter';
128

13-
protected int|string|array $columnSpan = 'full';
14-
15-
protected static ?string $maxHeight = '250px';
16-
17-
public ?string $filter = '24h';
18-
19-
protected function getPollingInterval(): ?string
20-
{
21-
return config('speedtest.dashboard_polling');
22-
}
23-
24-
protected function getFilters(): ?array
25-
{
26-
return [
27-
'24h' => 'Last 24h',
28-
'week' => 'Last week',
29-
'month' => 'Last month',
30-
];
31-
}
32-
339
protected function getData(): array
3410
{
35-
$results = Result::query()
36-
->select(['id', 'data', 'created_at'])
37-
->where('status', '=', ResultStatus::Completed)
38-
->when($this->filter == '24h', function ($query) {
39-
$query->where('created_at', '>=', now()->subDay());
40-
})
41-
->when($this->filter == 'week', function ($query) {
42-
$query->where('created_at', '>=', now()->subWeek());
43-
})
44-
->when($this->filter == 'month', function ($query) {
45-
$query->where('created_at', '>=', now()->subMonth());
46-
})
47-
->orderBy('created_at')
48-
->get();
11+
$results = $this->getResults('data');
4912

5013
return [
5114
'datasets' => [
@@ -86,31 +49,4 @@ protected function getData(): array
8649
'labels' => $results->map(fn ($item) => $item->created_at->timezone(config('app.display_timezone'))->format(config('app.chart_datetime_format'))),
8750
];
8851
}
89-
90-
protected function getOptions(): array
91-
{
92-
return [
93-
'plugins' => [
94-
'legend' => [
95-
'display' => true,
96-
],
97-
'tooltip' => [
98-
'enabled' => true,
99-
'mode' => 'index',
100-
'intersect' => false,
101-
'position' => 'nearest',
102-
],
103-
],
104-
'scales' => [
105-
'y' => [
106-
'beginAtZero' => config('app.chart_begin_at_zero'),
107-
],
108-
],
109-
];
110-
}
111-
112-
protected function getType(): string
113-
{
114-
return 'line';
115-
}
11652
}

0 commit comments

Comments
 (0)