From 68389f1105d6770335eebbde53b40d4c4fb5462f Mon Sep 17 00:00:00 2001 From: Sven van Ginkel Date: Wed, 21 Aug 2024 21:14:18 +0200 Subject: [PATCH] Revert "Add failed and thresholds" --- app/Filament/Pages/Dashboard.php | 80 +------------------ .../Widgets/RecentDownloadChartWidget.php | 57 ++++++------- .../RecentDownloadLatencyChartWidget.php | 65 ++++++++------- .../Widgets/RecentJitterChartWidget.php | 65 ++++++++------- .../Widgets/RecentPingChartWidget.php | 60 ++++++-------- .../Widgets/RecentUploadChartWidget.php | 57 ++++++------- .../RecentUploadLatencyChartWidget.php | 65 ++++++++------- config/app.php | 4 - .../views/filament/pages/dashboard.blade.php | 3 + 9 files changed, 192 insertions(+), 264 deletions(-) create mode 100644 resources/views/filament/pages/dashboard.blade.php diff --git a/app/Filament/Pages/Dashboard.php b/app/Filament/Pages/Dashboard.php index da99d0a43..3737cf9bf 100644 --- a/app/Filament/Pages/Dashboard.php +++ b/app/Filament/Pages/Dashboard.php @@ -14,22 +14,17 @@ use Cron\CronExpression; use Filament\Actions\Action; use Filament\Actions\ActionGroup; -use Filament\Forms; -use Filament\Forms\Components\DatePicker; -use Filament\Forms\Components\Select; -use Filament\Forms\Form; use Filament\Notifications\Notification; -use Filament\Pages\Dashboard as BaseDashboard; -use Filament\Pages\Dashboard\Concerns\HasFiltersForm; +use Filament\Pages\Dashboard as BasePage; use Filament\Support\Enums\IconPosition; use Illuminate\Support\Arr; -class Dashboard extends BaseDashboard +class Dashboard extends BasePage { - use HasFiltersForm; - protected static ?string $navigationIcon = 'heroicon-o-chart-bar'; + protected static string $view = 'filament.pages.dashboard'; + public function getSubheading(): ?string { if (blank(config('speedtest.schedule'))) { @@ -43,67 +38,6 @@ public function getSubheading(): ?string return 'Next speedtest at: '.$nextRunDate; } - public function filtersForm(Form $form): Form - { - // Retrieve the default number of days from the configuration - $defaultRangeDays = config('app.chart_default_date_range'); - - // Calculate the start and end dates based on the configuration value - $defaultEndDate = now(); // Today - $defaultStartDate = now()->subDays($defaultRangeDays); // Start date for the range - - return $form - ->schema([ - Forms\Components\Section::make() - ->schema([ - Select::make('predefinedRange') - ->label('Time Range') - ->options([ - '24_hours' => 'Last 24 Hours', - '1_week' => 'Last 1 Week', - '1_month' => 'Last 1 Month', - 'custom' => 'Custom Range', - ]) - ->reactive() - ->afterStateUpdated(function ($state, callable $set) { - switch ($state) { - case '24_hours': - $set('startDate', now()->subDay()->toDateString()); - $set('endDate', now()->toDateString()); - break; - case '1_week': - $set('startDate', now()->subWeek()->toDateString()); - $set('endDate', now()->toDateString()); - break; - case '1_month': - $set('startDate', now()->subMonth()->toDateString()); - $set('endDate', now()->toDateString()); - break; - case 'custom': - break; - } - }) - ->default('custom'), - - DatePicker::make('startDate') - ->label('Start Date') - ->default($defaultStartDate->toDateString()) - ->reactive() - ->hidden(fn ($get) => $get('predefinedRange') !== 'custom'), - - DatePicker::make('endDate') - ->label('End Date') - ->default($defaultEndDate->toDateString()) - ->reactive() - ->hidden(fn ($get) => $get('predefinedRange') !== 'custom'), - ]) - ->columns([ - 'default' => 1, - 'sm' => 3, - ]), - ]); - } - protected function getHeaderActions(): array { return [ @@ -149,12 +83,6 @@ protected function getHeaderWidgets(): array { return [ StatsOverviewWidget::make(), - ]; - } - - public function getWidgets(): array - { - return [ RecentDownloadChartWidget::make(), RecentUploadChartWidget::make(), RecentPingChartWidget::make(), diff --git a/app/Filament/Widgets/RecentDownloadChartWidget.php b/app/Filament/Widgets/RecentDownloadChartWidget.php index 1a34fecb1..7aca1d1df 100644 --- a/app/Filament/Widgets/RecentDownloadChartWidget.php +++ b/app/Filament/Widgets/RecentDownloadChartWidget.php @@ -6,64 +6,59 @@ use App\Helpers\Number; use App\Models\Result; use Filament\Widgets\ChartWidget; -use Filament\Widgets\Concerns\InteractsWithPageFilters; -use Illuminate\Database\Eloquent\Builder; class RecentDownloadChartWidget extends ChartWidget { - use InteractsWithPageFilters; - protected static ?string $heading = 'Download (Mbps)'; protected int|string|array $columnSpan = 'full'; protected static ?string $maxHeight = '250px'; + public ?string $filter = '24h'; + protected function getPollingInterval(): ?string { return config('speedtest.dashboard_polling'); } - protected function getData(): array + protected function getFilters(): ?array { + return [ + '24h' => 'Last 24h', + 'week' => 'Last week', + 'month' => 'Last month', + ]; + } - $startDate = $this->filters['startDate'] ?? now()->subWeek(); - $endDate = $this->filters['endDate'] ?? now(); - + protected function getData(): array + { $results = Result::query() ->select(['id', 'download', 'created_at']) ->where('status', '=', ResultStatus::Completed) - ->when($startDate, fn (Builder $query) => $query->whereDate('created_at', '>=', $startDate)) - ->when($endDate, fn (Builder $query) => $query->whereDate('created_at', '<=', $endDate)) + ->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(); - $downloads = $results->map(fn ($item) => ! blank($item->download) ? Number::bitsToMagnitude(bits: $item->download_bits, precision: 2, magnitude: 'mbit') : 0); - $averageDownload = $downloads->avg(); - return [ 'datasets' => [ [ 'label' => 'Download', - 'data' => $downloads, - 'borderColor' => 'rgba(14, 165, 233)', - 'backgroundColor' => 'rgba(14, 165, 233, 0.1)', // 10% opacity - 'pointBackgroundColor' => 'rgba(14, 165, 233)', - 'fill' => true, - 'cubicInterpolationMode' => 'monotone', - 'tension' => 0.4, - 'pointRadius' => count($downloads) <= 5 ? 3 : 0, - ], - [ - 'label' => 'Average', - 'data' => array_fill(0, count($downloads), $averageDownload), - 'borderColor' => 'rgb(255, 165, 0)', - 'pointBackgroundColor' => 'rgb(255, 165, 0)', + 'data' => $results->map(fn ($item) => ! blank($item->download) ? Number::bitsToMagnitude(bits: $item->download_bits, precision: 2, magnitude: 'mbit') : 0), + 'borderColor' => '#0ea5e9', + 'backgroundColor' => '#0ea5e9', + 'pointBackgroundColor' => '#0ea5e9', 'fill' => false, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'borderDash' => [5, 5], - 'pointRadius' => 0, ], ], '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 return [ 'plugins' => [ 'legend' => [ - 'display' => true, + 'display' => false, ], ], 'scales' => [ 'y' => [ - 'beginAtZero' => config('app.chart_begin_at_zero'), + 'beginAtZero' => true, ], ], ]; diff --git a/app/Filament/Widgets/RecentDownloadLatencyChartWidget.php b/app/Filament/Widgets/RecentDownloadLatencyChartWidget.php index da69c1199..c7bb0e05a 100644 --- a/app/Filament/Widgets/RecentDownloadLatencyChartWidget.php +++ b/app/Filament/Widgets/RecentDownloadLatencyChartWidget.php @@ -5,35 +5,45 @@ use App\Enums\ResultStatus; use App\Models\Result; use Filament\Widgets\ChartWidget; -use Filament\Widgets\Concerns\InteractsWithPageFilters; -use Illuminate\Database\Eloquent\Builder; class RecentDownloadLatencyChartWidget extends ChartWidget { - use InteractsWithPageFilters; - protected static ?string $heading = 'Download Latency'; protected int|string|array $columnSpan = 'full'; protected static ?string $maxHeight = '250px'; + public ?string $filter = '24h'; + protected function getPollingInterval(): ?string { return config('speedtest.dashboard_polling'); } - protected function getData(): array + protected function getFilters(): ?array { + return [ + '24h' => 'Last 24h', + 'week' => 'Last week', + 'month' => 'Last month', + ]; + } - $startDate = $this->filters['startDate'] ?? now()->subWeek(); - $endDate = $this->filters['endDate'] ?? now(); - + protected function getData(): array + { $results = Result::query() ->select(['id', 'data', 'created_at']) ->where('status', '=', ResultStatus::Completed) - ->when($startDate, fn (Builder $query) => $query->whereDate('created_at', '>=', $startDate)) - ->when($endDate, fn (Builder $query) => $query->whereDate('created_at', '<=', $endDate)) + ->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(); @@ -41,36 +51,33 @@ protected function getData(): array 'datasets' => [ [ 'label' => 'Average (ms)', - 'data' => $averageData = $results->map(fn ($item) => $item->download_latency_iqm ? number_format($item->download_latency_iqm, 2) : 0), - 'borderColor' => 'rgba(16, 185, 129)', - 'backgroundColor' => 'rgba(16, 185, 129, 0.1)', - 'pointBackgroundColor' => 'rgba(16, 185, 129)', - 'fill' => true, + 'data' => $results->map(fn ($item) => $item->download_latency_iqm ? number_format($item->download_latency_iqm, 2) : 0), + 'borderColor' => '#10b981', + 'backgroundColor' => '#10b981', + 'pointBackgroundColor' => '#10b981', + 'fill' => false, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'pointRadius' => $averageData->count() <= 5 ? 3 : 0, ], [ 'label' => 'High (ms)', - 'data' => $highData = $results->map(fn ($item) => $item->download_latency_high ? number_format($item->download_latency_high, 2) : 0), - 'borderColor' => 'rgba(14, 165, 233)', - 'backgroundColor' => 'rgba(14, 165, 233, 0.1)', - 'pointBackgroundColor' => 'rgba(14, 165, 233)', - 'fill' => true, + 'data' => $results->map(fn ($item) => $item->download_latency_high ? number_format($item->download_latency_high, 2) : 0), + 'borderColor' => '#0ea5e9', + 'backgroundColor' => '#0ea5e9', + 'pointBackgroundColor' => '#0ea5e9', + 'fill' => false, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'pointRadius' => $highData->count() <= 5 ? 3 : 0, ], [ 'label' => 'Low (ms)', - 'data' => $lowData = $results->map(fn ($item) => $item->download_latency_low ? number_format($item->download_latency_low, 2) : 0), - 'borderColor' => 'rgba(139, 92, 246)', - 'backgroundColor' => 'rgba(139, 92, 246, 0.1)', - 'pointBackgroundColor' => 'rgba(139, 92, 246)', - 'fill' => true, + 'data' => $results->map(fn ($item) => $item->download_latency_low ? number_format($item->download_latency_low, 2) : 0), + 'borderColor' => '#8b5cf6', + 'backgroundColor' => '#8b5cf6', + 'pointBackgroundColor' => '#8b5cf6', + 'fill' => false, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'pointRadius' => $lowData->count() <= 5 ? 3 : 0, ], ], '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 return [ 'scales' => [ 'y' => [ - 'beginAtZero' => config('app.chart_begin_at_zero'), + 'beginAtZero' => true, ], ], ]; diff --git a/app/Filament/Widgets/RecentJitterChartWidget.php b/app/Filament/Widgets/RecentJitterChartWidget.php index 70ccd03c4..2d2454862 100644 --- a/app/Filament/Widgets/RecentJitterChartWidget.php +++ b/app/Filament/Widgets/RecentJitterChartWidget.php @@ -5,35 +5,45 @@ use App\Enums\ResultStatus; use App\Models\Result; use Filament\Widgets\ChartWidget; -use Filament\Widgets\Concerns\InteractsWithPageFilters; -use Illuminate\Database\Eloquent\Builder; class RecentJitterChartWidget extends ChartWidget { - use InteractsWithPageFilters; - protected static ?string $heading = 'Jitter'; protected int|string|array $columnSpan = 'full'; protected static ?string $maxHeight = '250px'; + public ?string $filter = '24h'; + protected function getPollingInterval(): ?string { return config('speedtest.dashboard_polling'); } - protected function getData(): array + protected function getFilters(): ?array { + return [ + '24h' => 'Last 24h', + 'week' => 'Last week', + 'month' => 'Last month', + ]; + } - $startDate = $this->filters['startDate'] ?? now()->subWeek(); - $endDate = $this->filters['endDate'] ?? now(); - + protected function getData(): array + { $results = Result::query() ->select(['id', 'data', 'created_at']) ->where('status', '=', ResultStatus::Completed) - ->when($startDate, fn (Builder $query) => $query->whereDate('created_at', '>=', $startDate)) - ->when($endDate, fn (Builder $query) => $query->whereDate('created_at', '<=', $endDate)) + ->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(); @@ -41,36 +51,33 @@ protected function getData(): array 'datasets' => [ [ 'label' => 'Download (ms)', - 'data' => $downloadData = $results->map(fn ($item) => $item->download_jitter ? number_format($item->download_jitter, 2) : 0), - 'borderColor' => 'rgba(14, 165, 233)', - 'backgroundColor' => 'rgba(14, 165, 233, 0.1)', - 'pointBackgroundColor' => 'rgba(14, 165, 233)', - 'fill' => true, + 'data' => $results->map(fn ($item) => $item->download_jitter ? number_format($item->download_jitter, 2) : 0), + 'borderColor' => '#0ea5e9', + 'backgroundColor' => '#0ea5e9', + 'pointBackgroundColor' => '#0ea5e9', + 'fill' => false, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'pointRadius' => $downloadData->count() <= 5 ? 3 : 0, ], [ 'label' => 'Upload (ms)', - 'data' => $uploadData = $results->map(fn ($item) => $item->upload_jitter ? number_format($item->upload_jitter, 2) : 0), - 'borderColor' => 'rgba(139, 92, 246)', - 'backgroundColor' => 'rgba(139, 92, 246, 0.1)', - 'pointBackgroundColor' => 'rgba(139, 92, 246)', - 'fill' => true, + 'data' => $results->map(fn ($item) => $item->upload_jitter ? number_format($item->upload_jitter, 2) : 0), + 'borderColor' => '#8b5cf6', + 'backgroundColor' => '#8b5cf6', + 'pointBackgroundColor' => '#8b5cf6', + 'fill' => false, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'pointRadius' => $uploadData->count() <= 5 ? 3 : 0, ], [ 'label' => 'Ping (ms)', - 'data' => $pingData = $results->map(fn ($item) => $item->ping_jitter ? number_format($item->ping_jitter, 2) : 0), - 'borderColor' => 'rgba(16, 185, 129)', - 'backgroundColor' => 'rgba(16, 185, 129, 0.1)', - 'pointBackgroundColor' => 'rgba(16, 185, 129)', - 'fill' => true, + 'data' => $results->map(fn ($item) => $item->ping_jitter ? number_format($item->ping_jitter, 2) : 0), + 'borderColor' => '#10b981', + 'backgroundColor' => '#10b981', + 'pointBackgroundColor' => '#10b981', + 'fill' => false, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'pointRadius' => $pingData->count() <= 5 ? 3 : 0, ], ], '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 return [ 'scales' => [ 'y' => [ - 'beginAtZero' => config('app.chart_begin_at_zero'), + 'beginAtZero' => true, ], ], ]; diff --git a/app/Filament/Widgets/RecentPingChartWidget.php b/app/Filament/Widgets/RecentPingChartWidget.php index 1135c8062..57a10eee5 100644 --- a/app/Filament/Widgets/RecentPingChartWidget.php +++ b/app/Filament/Widgets/RecentPingChartWidget.php @@ -5,64 +5,59 @@ use App\Enums\ResultStatus; use App\Models\Result; use Filament\Widgets\ChartWidget; -use Filament\Widgets\Concerns\InteractsWithPageFilters; -use Illuminate\Database\Eloquent\Builder; class RecentPingChartWidget extends ChartWidget { - use InteractsWithPageFilters; - protected static ?string $heading = 'Ping (ms)'; protected int|string|array $columnSpan = 'full'; protected static ?string $maxHeight = '250px'; + public ?string $filter = '24h'; + protected function getPollingInterval(): ?string { return config('speedtest.dashboard_polling'); } - protected function getData(): array + protected function getFilters(): ?array { + return [ + '24h' => 'Last 24h', + 'week' => 'Last week', + 'month' => 'Last month', + ]; + } - $startDate = $this->filters['startDate'] ?? now()->subWeek(); - $endDate = $this->filters['endDate'] ?? now(); - + protected function getData(): array + { $results = Result::query() ->select(['id', 'ping', 'created_at']) ->where('status', '=', ResultStatus::Completed) - ->when($startDate, fn (Builder $query) => $query->whereDate('created_at', '>=', $startDate)) - ->when($endDate, fn (Builder $query) => $query->whereDate('created_at', '<=', $endDate)) + ->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(); - $ping = $results->map(fn ($item) => ! blank($item->ping) ? number_format($item->ping, 2) : 0); - $averagePing = $ping->avg(); - return [ 'datasets' => [ [ 'label' => 'Ping (ms)', - 'data' => $ping, - '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($ping) <= 5 ? 3 : 0, - ], - [ - 'label' => 'Average', - 'data' => array_fill(0, count($ping), $averagePing), - 'borderColor' => 'rgb(255, 165, 0)', - 'pointBackgroundColor' => 'rgb(255, 165, 0)', + 'data' => $results->map(fn ($item) => ! blank($item->ping) ? number_format($item->ping, 2) : 0), + 'borderColor' => '#10b981', + 'backgroundColor' => '#10b981', + 'pointBackgroundColor' => '#10b981', 'fill' => false, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'borderDash' => [5, 5], - 'pointRadius' => 0, ], ], 'labels' => $results->map(fn ($item) => $item->created_at->timezone(config('app.display_timezone'))->format(config('app.chart_datetime_format'))), @@ -72,14 +67,9 @@ protected function getData(): array protected function getOptions(): array { return [ - 'plugins' => [ - 'legend' => [ - 'display' => true, - ], - ], 'scales' => [ 'y' => [ - 'beginAtZero' => config('app.chart_begin_at_zero'), + 'beginAtZero' => true, ], ], ]; diff --git a/app/Filament/Widgets/RecentUploadChartWidget.php b/app/Filament/Widgets/RecentUploadChartWidget.php index 7cf40e16c..5c9c85baf 100644 --- a/app/Filament/Widgets/RecentUploadChartWidget.php +++ b/app/Filament/Widgets/RecentUploadChartWidget.php @@ -6,64 +6,59 @@ use App\Helpers\Number; use App\Models\Result; use Filament\Widgets\ChartWidget; -use Filament\Widgets\Concerns\InteractsWithPageFilters; -use Illuminate\Database\Eloquent\Builder; class RecentUploadChartWidget extends ChartWidget { - use InteractsWithPageFilters; - protected static ?string $heading = 'Upload (Mbps)'; protected int|string|array $columnSpan = 'full'; protected static ?string $maxHeight = '250px'; + public ?string $filter = '24h'; + protected function getPollingInterval(): ?string { return config('speedtest.dashboard_polling'); } - protected function getData(): array + protected function getFilters(): ?array { + return [ + '24h' => 'Last 24h', + 'week' => 'Last week', + 'month' => 'Last month', + ]; + } - $startDate = $this->filters['startDate'] ?? now()->subWeek(); - $endDate = $this->filters['endDate'] ?? now(); - + protected function getData(): array + { $results = Result::query() ->select(['id', 'upload', 'created_at']) ->where('status', '=', ResultStatus::Completed) - ->when($startDate, fn (Builder $query) => $query->whereDate('created_at', '>=', $startDate)) - ->when($endDate, fn (Builder $query) => $query->whereDate('created_at', '<=', $endDate)) + ->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(); - $upload = $results->map(fn ($item) => ! blank($item->upload) ? Number::bitsToMagnitude(bits: $item->upload_bits, precision: 2, magnitude: 'mbit') : 0); - $averageUpload = $upload->avg(); - return [ 'datasets' => [ [ 'label' => 'Upload', - 'data' => $upload, - '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($upload) <= 5 ? 3 : 0, - ], - [ - 'label' => 'Average', - 'data' => array_fill(0, count($upload), $averageUpload), - 'borderColor' => 'rgb(255, 165, 0)', - 'pointBackgroundColor' => 'rgb(255, 165, 0)', + 'data' => $results->map(fn ($item) => ! blank($item->upload) ? Number::bitsToMagnitude(bits: $item->upload_bits, precision: 2, magnitude: 'mbit') : 0), + 'borderColor' => '#8b5cf6', + 'backgroundColor' => '#8b5cf6', + 'pointBackgroundColor' => '#8b5cf6', 'fill' => false, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'borderDash' => [5, 5], - 'pointRadius' => 0, ], ], '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 return [ 'plugins' => [ 'legend' => [ - 'display' => true, + 'display' => false, ], ], 'scales' => [ 'y' => [ - 'beginAtZero' => config('app.chart_begin_at_zero'), + 'beginAtZero' => true, ], ], ]; diff --git a/app/Filament/Widgets/RecentUploadLatencyChartWidget.php b/app/Filament/Widgets/RecentUploadLatencyChartWidget.php index 9c08663b4..049055adc 100644 --- a/app/Filament/Widgets/RecentUploadLatencyChartWidget.php +++ b/app/Filament/Widgets/RecentUploadLatencyChartWidget.php @@ -5,35 +5,45 @@ use App\Enums\ResultStatus; use App\Models\Result; use Filament\Widgets\ChartWidget; -use Filament\Widgets\Concerns\InteractsWithPageFilters; -use Illuminate\Database\Eloquent\Builder; class RecentUploadLatencyChartWidget extends ChartWidget { - use InteractsWithPageFilters; - protected static ?string $heading = 'Upload Latency'; protected int|string|array $columnSpan = 'full'; protected static ?string $maxHeight = '250px'; + public ?string $filter = '24h'; + protected function getPollingInterval(): ?string { return config('speedtest.dashboard_polling'); } - protected function getData(): array + protected function getFilters(): ?array { + return [ + '24h' => 'Last 24h', + 'week' => 'Last week', + 'month' => 'Last month', + ]; + } - $startDate = $this->filters['startDate'] ?? now()->subWeek(); - $endDate = $this->filters['endDate'] ?? now(); - + protected function getData(): array + { $results = Result::query() ->select(['id', 'data', 'created_at']) ->where('status', '=', ResultStatus::Completed) - ->when($startDate, fn (Builder $query) => $query->whereDate('created_at', '>=', $startDate)) - ->when($endDate, fn (Builder $query) => $query->whereDate('created_at', '<=', $endDate)) + ->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(); @@ -41,36 +51,33 @@ protected function getData(): array 'datasets' => [ [ 'label' => 'Average (ms)', - 'data' => $averageData = $results->map(fn ($item) => $item->upload_latency_iqm ? number_format($item->upload_latency_iqm, 2) : 0), - 'borderColor' => 'rgba(16, 185, 129)', - 'backgroundColor' => 'rgba(16, 185, 129, 0.1)', - 'pointBackgroundColor' => 'rgba(16, 185, 129)', - 'fill' => true, + 'data' => $results->map(fn ($item) => $item->upload_latency_iqm ? number_format($item->upload_latency_iqm, 2) : 0), + 'borderColor' => '#10b981', + 'backgroundColor' => '#10b981', + 'pointBackgroundColor' => '#10b981', + 'fill' => false, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'pointRadius' => $averageData->count() <= 5 ? 3 : 0, ], [ 'label' => 'High (ms)', - 'data' => $highData = $results->map(fn ($item) => $item->upload_latency_high ? number_format($item->upload_latency_high, 2) : 0), - 'borderColor' => 'rgba(14, 165, 233)', - 'backgroundColor' => 'rgba(14, 165, 233, 0.1)', - 'pointBackgroundColor' => 'rgba(14, 165, 233)', - 'fill' => true, + 'data' => $results->map(fn ($item) => $item->upload_latency_high ? number_format($item->upload_latency_high, 2) : 0), + 'borderColor' => '#0ea5e9', + 'backgroundColor' => '#0ea5e9', + 'pointBackgroundColor' => '#0ea5e9', + 'fill' => false, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'pointRadius' => $highData->count() <= 5 ? 3 : 0, ], [ 'label' => 'Low (ms)', - 'data' => $lowData = $results->map(fn ($item) => $item->upload_latency_low ? number_format($item->upload_latency_low, 2) : 0), - 'borderColor' => 'rgba(139, 92, 246)', - 'backgroundColor' => 'rgba(139, 92, 246, 0.1)', - 'pointBackgroundColor' => 'rgba(139, 92, 246)', - 'fill' => true, + 'data' => $results->map(fn ($item) => $item->upload_latency_low ? number_format($item->upload_latency_low, 2) : 0), + 'borderColor' => '#8b5cf6', + 'backgroundColor' => '#8b5cf6', + 'pointBackgroundColor' => '#8b5cf6', + 'fill' => false, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'pointRadius' => $lowData->count() <= 5 ? 3 : 0, ], ], '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 return [ 'scales' => [ 'y' => [ - 'beginAtZero' => config('app.chart_begin_at_zero'), + 'beginAtZero' => true, ], ], ]; diff --git a/config/app.php b/config/app.php index 1d98019cf..60b38f700 100644 --- a/config/app.php +++ b/config/app.php @@ -14,8 +14,4 @@ 'force_https' => env('FORCE_HTTPS', false), - 'chart_default_date_range' => env('CHART_DEFAULT_DATE_RANGE', 7), - - 'chart_begin_at_zero' => env('CHART_BEGIN_AT_ZERO', true), - ]; diff --git a/resources/views/filament/pages/dashboard.blade.php b/resources/views/filament/pages/dashboard.blade.php new file mode 100644 index 000000000..f351a1c97 --- /dev/null +++ b/resources/views/filament/pages/dashboard.blade.php @@ -0,0 +1,3 @@ + + {{-- Silence is golden --}} +