Skip to content

Commit 5e90f96

Browse files
authored
Merge pull request #3 from svenvg93/revert-2-add-failed-and-thresholds
Revert "Add failed and thresholds"
2 parents b2ff25b + 68389f1 commit 5e90f96

File tree

9 files changed

+192
-264
lines changed

9 files changed

+192
-264
lines changed

app/Filament/Pages/Dashboard.php

Lines changed: 4 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,17 @@
1414
use Cron\CronExpression;
1515
use Filament\Actions\Action;
1616
use Filament\Actions\ActionGroup;
17-
use Filament\Forms;
18-
use Filament\Forms\Components\DatePicker;
19-
use Filament\Forms\Components\Select;
20-
use Filament\Forms\Form;
2117
use Filament\Notifications\Notification;
22-
use Filament\Pages\Dashboard as BaseDashboard;
23-
use Filament\Pages\Dashboard\Concerns\HasFiltersForm;
18+
use Filament\Pages\Dashboard as BasePage;
2419
use Filament\Support\Enums\IconPosition;
2520
use Illuminate\Support\Arr;
2621

27-
class Dashboard extends BaseDashboard
22+
class Dashboard extends BasePage
2823
{
29-
use HasFiltersForm;
30-
3124
protected static ?string $navigationIcon = 'heroicon-o-chart-bar';
3225

26+
protected static string $view = 'filament.pages.dashboard';
27+
3328
public function getSubheading(): ?string
3429
{
3530
if (blank(config('speedtest.schedule'))) {
@@ -43,67 +38,6 @@ public function getSubheading(): ?string
4338
return 'Next speedtest at: '.$nextRunDate;
4439
}
4540

46-
public function filtersForm(Form $form): Form
47-
{
48-
// Retrieve the default number of days from the configuration
49-
$defaultRangeDays = config('app.chart_default_date_range');
50-
51-
// Calculate the start and end dates based on the configuration value
52-
$defaultEndDate = now(); // Today
53-
$defaultStartDate = now()->subDays($defaultRangeDays); // Start date for the range
54-
55-
return $form
56-
->schema([
57-
Forms\Components\Section::make()
58-
->schema([
59-
Select::make('predefinedRange')
60-
->label('Time Range')
61-
->options([
62-
'24_hours' => 'Last 24 Hours',
63-
'1_week' => 'Last 1 Week',
64-
'1_month' => 'Last 1 Month',
65-
'custom' => 'Custom Range',
66-
])
67-
->reactive()
68-
->afterStateUpdated(function ($state, callable $set) {
69-
switch ($state) {
70-
case '24_hours':
71-
$set('startDate', now()->subDay()->toDateString());
72-
$set('endDate', now()->toDateString());
73-
break;
74-
case '1_week':
75-
$set('startDate', now()->subWeek()->toDateString());
76-
$set('endDate', now()->toDateString());
77-
break;
78-
case '1_month':
79-
$set('startDate', now()->subMonth()->toDateString());
80-
$set('endDate', now()->toDateString());
81-
break;
82-
case 'custom':
83-
break;
84-
}
85-
})
86-
->default('custom'),
87-
88-
DatePicker::make('startDate')
89-
->label('Start Date')
90-
->default($defaultStartDate->toDateString())
91-
->reactive()
92-
->hidden(fn ($get) => $get('predefinedRange') !== 'custom'),
93-
94-
DatePicker::make('endDate')
95-
->label('End Date')
96-
->default($defaultEndDate->toDateString())
97-
->reactive()
98-
->hidden(fn ($get) => $get('predefinedRange') !== 'custom'),
99-
])
100-
->columns([
101-
'default' => 1,
102-
'sm' => 3,
103-
]),
104-
]);
105-
}
106-
10741
protected function getHeaderActions(): array
10842
{
10943
return [
@@ -149,12 +83,6 @@ protected function getHeaderWidgets(): array
14983
{
15084
return [
15185
StatsOverviewWidget::make(),
152-
];
153-
}
154-
155-
public function getWidgets(): array
156-
{
157-
return [
15886
RecentDownloadChartWidget::make(),
15987
RecentUploadChartWidget::make(),
16088
RecentPingChartWidget::make(),

app/Filament/Widgets/RecentDownloadChartWidget.php

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,64 +6,59 @@
66
use App\Helpers\Number;
77
use App\Models\Result;
88
use Filament\Widgets\ChartWidget;
9-
use Filament\Widgets\Concerns\InteractsWithPageFilters;
10-
use Illuminate\Database\Eloquent\Builder;
119

1210
class RecentDownloadChartWidget extends ChartWidget
1311
{
14-
use InteractsWithPageFilters;
15-
1612
protected static ?string $heading = 'Download (Mbps)';
1713

1814
protected int|string|array $columnSpan = 'full';
1915

2016
protected static ?string $maxHeight = '250px';
2117

18+
public ?string $filter = '24h';
19+
2220
protected function getPollingInterval(): ?string
2321
{
2422
return config('speedtest.dashboard_polling');
2523
}
2624

27-
protected function getData(): array
25+
protected function getFilters(): ?array
2826
{
27+
return [
28+
'24h' => 'Last 24h',
29+
'week' => 'Last week',
30+
'month' => 'Last month',
31+
];
32+
}
2933

30-
$startDate = $this->filters['startDate'] ?? now()->subWeek();
31-
$endDate = $this->filters['endDate'] ?? now();
32-
34+
protected function getData(): array
35+
{
3336
$results = Result::query()
3437
->select(['id', 'download', 'created_at'])
3538
->where('status', '=', ResultStatus::Completed)
36-
->when($startDate, fn (Builder $query) => $query->whereDate('created_at', '>=', $startDate))
37-
->when($endDate, fn (Builder $query) => $query->whereDate('created_at', '<=', $endDate))
39+
->when($this->filter == '24h', function ($query) {
40+
$query->where('created_at', '>=', now()->subDay());
41+
})
42+
->when($this->filter == 'week', function ($query) {
43+
$query->where('created_at', '>=', now()->subWeek());
44+
})
45+
->when($this->filter == 'month', function ($query) {
46+
$query->where('created_at', '>=', now()->subMonth());
47+
})
3848
->orderBy('created_at')
3949
->get();
4050

41-
$downloads = $results->map(fn ($item) => ! blank($item->download) ? Number::bitsToMagnitude(bits: $item->download_bits, precision: 2, magnitude: 'mbit') : 0);
42-
$averageDownload = $downloads->avg();
43-
4451
return [
4552
'datasets' => [
4653
[
4754
'label' => 'Download',
48-
'data' => $downloads,
49-
'borderColor' => 'rgba(14, 165, 233)',
50-
'backgroundColor' => 'rgba(14, 165, 233, 0.1)', // 10% opacity
51-
'pointBackgroundColor' => 'rgba(14, 165, 233)',
52-
'fill' => true,
53-
'cubicInterpolationMode' => 'monotone',
54-
'tension' => 0.4,
55-
'pointRadius' => count($downloads) <= 5 ? 3 : 0,
56-
],
57-
[
58-
'label' => 'Average',
59-
'data' => array_fill(0, count($downloads), $averageDownload),
60-
'borderColor' => 'rgb(255, 165, 0)',
61-
'pointBackgroundColor' => 'rgb(255, 165, 0)',
55+
'data' => $results->map(fn ($item) => ! blank($item->download) ? Number::bitsToMagnitude(bits: $item->download_bits, precision: 2, magnitude: 'mbit') : 0),
56+
'borderColor' => '#0ea5e9',
57+
'backgroundColor' => '#0ea5e9',
58+
'pointBackgroundColor' => '#0ea5e9',
6259
'fill' => false,
6360
'cubicInterpolationMode' => 'monotone',
6461
'tension' => 0.4,
65-
'borderDash' => [5, 5],
66-
'pointRadius' => 0,
6762
],
6863
],
6964
'labels' => $results->map(fn ($item) => $item->created_at->timezone(config('app.display_timezone'))->format(config('app.chart_datetime_format'))),
@@ -75,12 +70,12 @@ protected function getOptions(): array
7570
return [
7671
'plugins' => [
7772
'legend' => [
78-
'display' => true,
73+
'display' => false,
7974
],
8075
],
8176
'scales' => [
8277
'y' => [
83-
'beginAtZero' => config('app.chart_begin_at_zero'),
78+
'beginAtZero' => true,
8479
],
8580
],
8681
];

app/Filament/Widgets/RecentDownloadLatencyChartWidget.php

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,72 +5,79 @@
55
use App\Enums\ResultStatus;
66
use App\Models\Result;
77
use Filament\Widgets\ChartWidget;
8-
use Filament\Widgets\Concerns\InteractsWithPageFilters;
9-
use Illuminate\Database\Eloquent\Builder;
108

119
class RecentDownloadLatencyChartWidget extends ChartWidget
1210
{
13-
use InteractsWithPageFilters;
14-
1511
protected static ?string $heading = 'Download Latency';
1612

1713
protected int|string|array $columnSpan = 'full';
1814

1915
protected static ?string $maxHeight = '250px';
2016

17+
public ?string $filter = '24h';
18+
2119
protected function getPollingInterval(): ?string
2220
{
2321
return config('speedtest.dashboard_polling');
2422
}
2523

26-
protected function getData(): array
24+
protected function getFilters(): ?array
2725
{
26+
return [
27+
'24h' => 'Last 24h',
28+
'week' => 'Last week',
29+
'month' => 'Last month',
30+
];
31+
}
2832

29-
$startDate = $this->filters['startDate'] ?? now()->subWeek();
30-
$endDate = $this->filters['endDate'] ?? now();
31-
33+
protected function getData(): array
34+
{
3235
$results = Result::query()
3336
->select(['id', 'data', 'created_at'])
3437
->where('status', '=', ResultStatus::Completed)
35-
->when($startDate, fn (Builder $query) => $query->whereDate('created_at', '>=', $startDate))
36-
->when($endDate, fn (Builder $query) => $query->whereDate('created_at', '<=', $endDate))
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+
})
3747
->orderBy('created_at')
3848
->get();
3949

4050
return [
4151
'datasets' => [
4252
[
4353
'label' => 'Average (ms)',
44-
'data' => $averageData = $results->map(fn ($item) => $item->download_latency_iqm ? number_format($item->download_latency_iqm, 2) : 0),
45-
'borderColor' => 'rgba(16, 185, 129)',
46-
'backgroundColor' => 'rgba(16, 185, 129, 0.1)',
47-
'pointBackgroundColor' => 'rgba(16, 185, 129)',
48-
'fill' => true,
54+
'data' => $results->map(fn ($item) => $item->download_latency_iqm ? number_format($item->download_latency_iqm, 2) : 0),
55+
'borderColor' => '#10b981',
56+
'backgroundColor' => '#10b981',
57+
'pointBackgroundColor' => '#10b981',
58+
'fill' => false,
4959
'cubicInterpolationMode' => 'monotone',
5060
'tension' => 0.4,
51-
'pointRadius' => $averageData->count() <= 5 ? 3 : 0,
5261
],
5362
[
5463
'label' => 'High (ms)',
55-
'data' => $highData = $results->map(fn ($item) => $item->download_latency_high ? number_format($item->download_latency_high, 2) : 0),
56-
'borderColor' => 'rgba(14, 165, 233)',
57-
'backgroundColor' => 'rgba(14, 165, 233, 0.1)',
58-
'pointBackgroundColor' => 'rgba(14, 165, 233)',
59-
'fill' => true,
64+
'data' => $results->map(fn ($item) => $item->download_latency_high ? number_format($item->download_latency_high, 2) : 0),
65+
'borderColor' => '#0ea5e9',
66+
'backgroundColor' => '#0ea5e9',
67+
'pointBackgroundColor' => '#0ea5e9',
68+
'fill' => false,
6069
'cubicInterpolationMode' => 'monotone',
6170
'tension' => 0.4,
62-
'pointRadius' => $highData->count() <= 5 ? 3 : 0,
6371
],
6472
[
6573
'label' => 'Low (ms)',
66-
'data' => $lowData = $results->map(fn ($item) => $item->download_latency_low ? number_format($item->download_latency_low, 2) : 0),
67-
'borderColor' => 'rgba(139, 92, 246)',
68-
'backgroundColor' => 'rgba(139, 92, 246, 0.1)',
69-
'pointBackgroundColor' => 'rgba(139, 92, 246)',
70-
'fill' => true,
74+
'data' => $results->map(fn ($item) => $item->download_latency_low ? number_format($item->download_latency_low, 2) : 0),
75+
'borderColor' => '#8b5cf6',
76+
'backgroundColor' => '#8b5cf6',
77+
'pointBackgroundColor' => '#8b5cf6',
78+
'fill' => false,
7179
'cubicInterpolationMode' => 'monotone',
7280
'tension' => 0.4,
73-
'pointRadius' => $lowData->count() <= 5 ? 3 : 0,
7481
],
7582
],
7683
'labels' => $results->map(fn ($item) => $item->created_at->timezone(config('app.display_timezone'))->format(config('app.chart_datetime_format'))),
@@ -82,7 +89,7 @@ protected function getOptions(): array
8289
return [
8390
'scales' => [
8491
'y' => [
85-
'beginAtZero' => config('app.chart_begin_at_zero'),
92+
'beginAtZero' => true,
8693
],
8794
],
8895
];

0 commit comments

Comments
 (0)