From c1fcf77deae2dd93f5b55724a7ebf17f55f7e50d Mon Sep 17 00:00:00 2001 From: svenvg93 Date: Mon, 5 Aug 2024 23:01:12 +0200 Subject: [PATCH 01/11] first commit --- app/Filament/Widgets/StatsOverviewWidget.php | 64 ++++++++++++++++---- 1 file changed, 53 insertions(+), 11 deletions(-) diff --git a/app/Filament/Widgets/StatsOverviewWidget.php b/app/Filament/Widgets/StatsOverviewWidget.php index 6a7721147..52936e65d 100644 --- a/app/Filament/Widgets/StatsOverviewWidget.php +++ b/app/Filament/Widgets/StatsOverviewWidget.php @@ -5,6 +5,7 @@ use App\Enums\ResultStatus; use App\Helpers\Number; use App\Models\Result; +use Carbon\Carbon; use Filament\Widgets\StatsOverviewWidget as BaseWidget; use Filament\Widgets\StatsOverviewWidget\Stat; @@ -43,21 +44,40 @@ protected function getCards(): array ->latest() ->first(); - if (! $previous) { - return [ - Stat::make('Latest download', fn (): string => ! blank($this->result) ? Number::toBitRate(bits: $this->result->download_bits, precision: 2) : 'n/a') - ->icon('heroicon-o-arrow-down-tray'), - Stat::make('Latest upload', fn (): string => ! blank($this->result) ? Number::toBitRate(bits: $this->result->upload_bits, precision: 2) : 'n/a') - ->icon('heroicon-o-arrow-up-tray'), - Stat::make('Latest ping', fn (): string => ! blank($this->result) ? number_format($this->result->ping, 2).' ms' : 'n/a') - ->icon('heroicon-o-clock'), - ]; - } - $downloadChange = percentChange($this->result->download, $previous->download, 2); $uploadChange = percentChange($this->result->upload, $previous->upload, 2); $pingChange = percentChange($this->result->ping, $previous->ping, 2); + $last24hData = Result::query() + ->select(['download', 'upload', 'ping']) + ->where('status', '=', ResultStatus::Completed) + ->where('created_at', '>=', Carbon::now()->subDay()) + ->get(); + + $averageDownload24h = $last24hData->avg('download'); + $averageUpload24h = $last24hData->avg('upload'); + $averagePing24h = $last24hData->avg('ping'); + + $last7dData = Result::query() + ->select(['download', 'upload', 'ping']) + ->where('status', '=', ResultStatus::Completed) + ->where('created_at', '>=', Carbon::now()->subWeek()) + ->get(); + + $averageDownload7d = $last7dData->avg('download'); + $averageUpload7d = $last7dData->avg('upload'); + $averagePing7d = $last7dData->avg('ping'); + + $last1mData = Result::query() + ->select(['download', 'upload', 'ping']) + ->where('status', '=', ResultStatus::Completed) + ->where('created_at', '>=', Carbon::now()->subMonth()) + ->get(); + + $averageDownload1m = $last1mData->avg('download'); + $averageUpload1m = $last1mData->avg('upload'); + $averagePing1m = $last1mData->avg('ping'); + return [ Stat::make('Latest download', fn (): string => ! blank($this->result) ? Number::toBitRate(bits: $this->result->download_bits, precision: 2) : 'n/a') ->icon('heroicon-o-arrow-down-tray') @@ -74,6 +94,28 @@ protected function getCards(): array ->description($pingChange > 0 ? $pingChange.'% slower' : abs($pingChange).'% faster') ->descriptionIcon($pingChange > 0 ? 'heroicon-m-arrow-trending-up' : 'heroicon-m-arrow-trending-down') ->color($pingChange > 0 ? 'danger' : 'success'), + + Stat::make('Average Download', '') + ->icon('heroicon-o-arrow-down-tray') + ->description( + '24 Hours: '.(blank($averageDownload24h) ? 'n/a' : Number::toBitRate(bits: $averageDownload24h * 8, precision: 2))."\n". + '7 Days: '.(blank($averageDownload7d) ? 'n/a' : Number::toBitRate(bits: $averageDownload7d * 8, precision: 2))."\n". + '1 Month: '.(blank($averageDownload1m) ? 'n/a' : Number::toBitRate(bits: $averageDownload1m * 8, precision: 2)) + ), + Stat::make('Average Upload', '') + ->icon('heroicon-o-arrow-down-tray') + ->description( + '24 Hours: '.(blank($averageUpload24h) ? 'n/a' : Number::toBitRate(bits: $averageUpload24h * 8, precision: 2))."\n". + '7 Days: '.(blank($averageUpload7d) ? 'n/a' : Number::toBitRate(bits: $averageUpload7d * 8, precision: 2))."\n". + '1 Month: '.(blank($averageUpload1m) ? 'n/a' : Number::toBitRate(bits: $averageUpload1m * 8, precision: 2)) + ), + Stat::make('Average Ping', '') + ->icon('heroicon-o-arrow-down-tray') + ->description( + '24 Hours: '.(blank($averagePing24h) ? 'n/a' : number_format($averagePing24h, 2).' ms')."\n". + '7 Days: '.(blank($averagePing7d) ? 'n/a' : number_format($averagePing7d, 2).' ms')."\n". + '1 Month: '.(blank($averagePing1m) ? 'n/a' : number_format($averagePing1m, 2).' ms') + ), ]; } } From 1fb06af461989dd5a1dad52c5c26a12961864781 Mon Sep 17 00:00:00 2001 From: svenvg93 Date: Thu, 8 Aug 2024 21:44:31 +0200 Subject: [PATCH 02/11] Commit it --- .../Widgets/RecentDownloadChartWidget.php | 21 +++++- app/Filament/Widgets/StatsOverviewWidget.php | 66 ++++--------------- 2 files changed, 30 insertions(+), 57 deletions(-) diff --git a/app/Filament/Widgets/RecentDownloadChartWidget.php b/app/Filament/Widgets/RecentDownloadChartWidget.php index 7aca1d1df..0625f7e5d 100644 --- a/app/Filament/Widgets/RecentDownloadChartWidget.php +++ b/app/Filament/Widgets/RecentDownloadChartWidget.php @@ -48,17 +48,32 @@ protected function getData(): array ->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' => $results->map(fn ($item) => ! blank($item->download) ? Number::bitsToMagnitude(bits: $item->download_bits, precision: 2, magnitude: 'mbit') : 0), + 'data' => $downloads, 'borderColor' => '#0ea5e9', 'backgroundColor' => '#0ea5e9', 'pointBackgroundColor' => '#0ea5e9', 'fill' => false, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, + 'pointRadius' => 0, + ], + [ + 'label' => 'Average', + 'data' => array_fill(0, count($downloads), $averageDownload), + 'borderColor' => '#ff0000', + 'pointBackgroundColor' => '#ff0000', + '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'))), @@ -70,12 +85,12 @@ protected function getOptions(): array return [ 'plugins' => [ 'legend' => [ - 'display' => false, + 'display' => true, ], ], 'scales' => [ 'y' => [ - 'beginAtZero' => true, + 'beginAtZero' => false, ], ], ]; diff --git a/app/Filament/Widgets/StatsOverviewWidget.php b/app/Filament/Widgets/StatsOverviewWidget.php index 52936e65d..d6e89f505 100644 --- a/app/Filament/Widgets/StatsOverviewWidget.php +++ b/app/Filament/Widgets/StatsOverviewWidget.php @@ -5,7 +5,6 @@ use App\Enums\ResultStatus; use App\Helpers\Number; use App\Models\Result; -use Carbon\Carbon; use Filament\Widgets\StatsOverviewWidget as BaseWidget; use Filament\Widgets\StatsOverviewWidget\Stat; @@ -44,40 +43,21 @@ protected function getCards(): array ->latest() ->first(); + if (! $previous) { + return [ + Stat::make('Latest download', fn (): string => ! blank($this->result) ? Number::toBitRate(bits: $this->result->download_bits, precision: 2) : 'n/a') + ->icon('heroicon-o-arrow-down-tray'), + Stat::make('Latest upload', fn (): string => ! blank($this->result) ? Number::toBitRate(bits: $this->result->upload_bits, precision: 2) : 'n/a') + ->icon('heroicon-o-arrow-up-tray'), + Stat::make('Latest ping', fn (): string => ! blank($this->result) ? number_format($this->result->ping, 2).' ms' : 'n/a') + ->icon('heroicon-o-clock'), + ]; + } + $downloadChange = percentChange($this->result->download, $previous->download, 2); $uploadChange = percentChange($this->result->upload, $previous->upload, 2); $pingChange = percentChange($this->result->ping, $previous->ping, 2); - $last24hData = Result::query() - ->select(['download', 'upload', 'ping']) - ->where('status', '=', ResultStatus::Completed) - ->where('created_at', '>=', Carbon::now()->subDay()) - ->get(); - - $averageDownload24h = $last24hData->avg('download'); - $averageUpload24h = $last24hData->avg('upload'); - $averagePing24h = $last24hData->avg('ping'); - - $last7dData = Result::query() - ->select(['download', 'upload', 'ping']) - ->where('status', '=', ResultStatus::Completed) - ->where('created_at', '>=', Carbon::now()->subWeek()) - ->get(); - - $averageDownload7d = $last7dData->avg('download'); - $averageUpload7d = $last7dData->avg('upload'); - $averagePing7d = $last7dData->avg('ping'); - - $last1mData = Result::query() - ->select(['download', 'upload', 'ping']) - ->where('status', '=', ResultStatus::Completed) - ->where('created_at', '>=', Carbon::now()->subMonth()) - ->get(); - - $averageDownload1m = $last1mData->avg('download'); - $averageUpload1m = $last1mData->avg('upload'); - $averagePing1m = $last1mData->avg('ping'); - return [ Stat::make('Latest download', fn (): string => ! blank($this->result) ? Number::toBitRate(bits: $this->result->download_bits, precision: 2) : 'n/a') ->icon('heroicon-o-arrow-down-tray') @@ -94,28 +74,6 @@ protected function getCards(): array ->description($pingChange > 0 ? $pingChange.'% slower' : abs($pingChange).'% faster') ->descriptionIcon($pingChange > 0 ? 'heroicon-m-arrow-trending-up' : 'heroicon-m-arrow-trending-down') ->color($pingChange > 0 ? 'danger' : 'success'), - - Stat::make('Average Download', '') - ->icon('heroicon-o-arrow-down-tray') - ->description( - '24 Hours: '.(blank($averageDownload24h) ? 'n/a' : Number::toBitRate(bits: $averageDownload24h * 8, precision: 2))."\n". - '7 Days: '.(blank($averageDownload7d) ? 'n/a' : Number::toBitRate(bits: $averageDownload7d * 8, precision: 2))."\n". - '1 Month: '.(blank($averageDownload1m) ? 'n/a' : Number::toBitRate(bits: $averageDownload1m * 8, precision: 2)) - ), - Stat::make('Average Upload', '') - ->icon('heroicon-o-arrow-down-tray') - ->description( - '24 Hours: '.(blank($averageUpload24h) ? 'n/a' : Number::toBitRate(bits: $averageUpload24h * 8, precision: 2))."\n". - '7 Days: '.(blank($averageUpload7d) ? 'n/a' : Number::toBitRate(bits: $averageUpload7d * 8, precision: 2))."\n". - '1 Month: '.(blank($averageUpload1m) ? 'n/a' : Number::toBitRate(bits: $averageUpload1m * 8, precision: 2)) - ), - Stat::make('Average Ping', '') - ->icon('heroicon-o-arrow-down-tray') - ->description( - '24 Hours: '.(blank($averagePing24h) ? 'n/a' : number_format($averagePing24h, 2).' ms')."\n". - '7 Days: '.(blank($averagePing7d) ? 'n/a' : number_format($averagePing7d, 2).' ms')."\n". - '1 Month: '.(blank($averagePing1m) ? 'n/a' : number_format($averagePing1m, 2).' ms') - ), ]; } -} +} \ No newline at end of file From 78252569233898afba8e7ae9d3ac2ddf027c2795 Mon Sep 17 00:00:00 2001 From: svenvg93 Date: Thu, 8 Aug 2024 21:47:02 +0200 Subject: [PATCH 03/11] lint --- app/Filament/Widgets/RecentDownloadChartWidget.php | 2 +- app/Filament/Widgets/StatsOverviewWidget.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Filament/Widgets/RecentDownloadChartWidget.php b/app/Filament/Widgets/RecentDownloadChartWidget.php index 0625f7e5d..32ae8acb8 100644 --- a/app/Filament/Widgets/RecentDownloadChartWidget.php +++ b/app/Filament/Widgets/RecentDownloadChartWidget.php @@ -48,7 +48,7 @@ protected function getData(): array ->orderBy('created_at') ->get(); - $downloads = $results->map(fn ($item) => !blank($item->download) ? Number::bitsToMagnitude(bits: $item->download_bits, precision: 2, magnitude: 'mbit') : 0); + $downloads = $results->map(fn ($item) => ! blank($item->download) ? Number::bitsToMagnitude(bits: $item->download_bits, precision: 2, magnitude: 'mbit') : 0); $averageDownload = $downloads->avg(); return [ diff --git a/app/Filament/Widgets/StatsOverviewWidget.php b/app/Filament/Widgets/StatsOverviewWidget.php index d6e89f505..6a7721147 100644 --- a/app/Filament/Widgets/StatsOverviewWidget.php +++ b/app/Filament/Widgets/StatsOverviewWidget.php @@ -76,4 +76,4 @@ protected function getCards(): array ->color($pingChange > 0 ? 'danger' : 'success'), ]; } -} \ No newline at end of file +} From 7f1e903237aeac0b46070c50e548f8e0a33022e7 Mon Sep 17 00:00:00 2001 From: svenvg93 Date: Fri, 9 Aug 2024 10:55:58 +0200 Subject: [PATCH 04/11] update-all-charts --- .../RecentDownloadLatencyChartWidget.php | 5 +++- .../Widgets/RecentJitterChartWidget.php | 5 +++- .../Widgets/RecentPingChartWidget.php | 24 +++++++++++++++++-- .../Widgets/RecentUploadChartWidget.php | 21 +++++++++++++--- .../RecentUploadLatencyChartWidget.php | 5 +++- 5 files changed, 52 insertions(+), 8 deletions(-) diff --git a/app/Filament/Widgets/RecentDownloadLatencyChartWidget.php b/app/Filament/Widgets/RecentDownloadLatencyChartWidget.php index c7bb0e05a..fc2bfc05a 100644 --- a/app/Filament/Widgets/RecentDownloadLatencyChartWidget.php +++ b/app/Filament/Widgets/RecentDownloadLatencyChartWidget.php @@ -58,6 +58,7 @@ protected function getData(): array 'fill' => false, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, + 'pointRadius' => 0, ], [ 'label' => 'High (ms)', @@ -68,6 +69,7 @@ protected function getData(): array 'fill' => false, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, + 'pointRadius' => 0, ], [ 'label' => 'Low (ms)', @@ -78,6 +80,7 @@ protected function getData(): array 'fill' => false, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, + 'pointRadius' => 0, ], ], 'labels' => $results->map(fn ($item) => $item->created_at->timezone(config('app.display_timezone'))->format(config('app.chart_datetime_format'))), @@ -89,7 +92,7 @@ protected function getOptions(): array return [ 'scales' => [ 'y' => [ - 'beginAtZero' => true, + 'beginAtZero' => false, ], ], ]; diff --git a/app/Filament/Widgets/RecentJitterChartWidget.php b/app/Filament/Widgets/RecentJitterChartWidget.php index 2d2454862..0b4b2df3f 100644 --- a/app/Filament/Widgets/RecentJitterChartWidget.php +++ b/app/Filament/Widgets/RecentJitterChartWidget.php @@ -58,6 +58,7 @@ protected function getData(): array 'fill' => false, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, + 'pointRadius' => 0, ], [ 'label' => 'Upload (ms)', @@ -68,6 +69,7 @@ protected function getData(): array 'fill' => false, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, + 'pointRadius' => 0, ], [ 'label' => 'Ping (ms)', @@ -78,6 +80,7 @@ protected function getData(): array 'fill' => false, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, + 'pointRadius' => 0, ], ], 'labels' => $results->map(fn ($item) => $item->created_at->timezone(config('app.display_timezone'))->format(config('app.chart_datetime_format'))), @@ -89,7 +92,7 @@ protected function getOptions(): array return [ 'scales' => [ 'y' => [ - 'beginAtZero' => true, + 'beginAtZero' => false, ], ], ]; diff --git a/app/Filament/Widgets/RecentPingChartWidget.php b/app/Filament/Widgets/RecentPingChartWidget.php index 57a10eee5..8dccdd286 100644 --- a/app/Filament/Widgets/RecentPingChartWidget.php +++ b/app/Filament/Widgets/RecentPingChartWidget.php @@ -47,17 +47,32 @@ protected function getData(): array ->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' => $results->map(fn ($item) => ! blank($item->ping) ? number_format($item->ping, 2) : 0), + 'data' => $ping, 'borderColor' => '#10b981', 'backgroundColor' => '#10b981', 'pointBackgroundColor' => '#10b981', 'fill' => false, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, + 'pointRadius' => 0, + ], + [ + 'label' => 'Average', + 'data' => array_fill(0, count($ping), $averagePing), + 'borderColor' => '#ff0000', + 'pointBackgroundColor' => '#ff0000', + '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'))), @@ -67,9 +82,14 @@ protected function getData(): array protected function getOptions(): array { return [ + 'plugins' => [ + 'legend' => [ + 'display' => true, + ], + ], 'scales' => [ 'y' => [ - 'beginAtZero' => true, + 'beginAtZero' => false, ], ], ]; diff --git a/app/Filament/Widgets/RecentUploadChartWidget.php b/app/Filament/Widgets/RecentUploadChartWidget.php index 5c9c85baf..d3086353a 100644 --- a/app/Filament/Widgets/RecentUploadChartWidget.php +++ b/app/Filament/Widgets/RecentUploadChartWidget.php @@ -48,17 +48,32 @@ protected function getData(): array ->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' => $results->map(fn ($item) => ! blank($item->upload) ? Number::bitsToMagnitude(bits: $item->upload_bits, precision: 2, magnitude: 'mbit') : 0), + 'data' => $upload, 'borderColor' => '#8b5cf6', 'backgroundColor' => '#8b5cf6', 'pointBackgroundColor' => '#8b5cf6', 'fill' => false, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, + 'pointRadius' => 0, + ], + [ + 'label' => 'Average', + 'data' => array_fill(0, count($upload), $averageUpload), + 'borderColor' => '#ff0000', + 'pointBackgroundColor' => '#ff0000', + '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'))), @@ -70,12 +85,12 @@ protected function getOptions(): array return [ 'plugins' => [ 'legend' => [ - 'display' => false, + 'display' => true, ], ], 'scales' => [ 'y' => [ - 'beginAtZero' => true, + 'beginAtZero' => false, ], ], ]; diff --git a/app/Filament/Widgets/RecentUploadLatencyChartWidget.php b/app/Filament/Widgets/RecentUploadLatencyChartWidget.php index 049055adc..94ccfd321 100644 --- a/app/Filament/Widgets/RecentUploadLatencyChartWidget.php +++ b/app/Filament/Widgets/RecentUploadLatencyChartWidget.php @@ -58,6 +58,7 @@ protected function getData(): array 'fill' => false, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, + 'pointRadius' => 0, ], [ 'label' => 'High (ms)', @@ -68,6 +69,7 @@ protected function getData(): array 'fill' => false, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, + 'pointRadius' => 0, ], [ 'label' => 'Low (ms)', @@ -78,6 +80,7 @@ protected function getData(): array 'fill' => false, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, + 'pointRadius' => 0, ], ], 'labels' => $results->map(fn ($item) => $item->created_at->timezone(config('app.display_timezone'))->format(config('app.chart_datetime_format'))), @@ -89,7 +92,7 @@ protected function getOptions(): array return [ 'scales' => [ 'y' => [ - 'beginAtZero' => true, + 'beginAtZero' => false, ], ], ]; From 0073e6affe897cea814b8f9a96c7e3495cc20803 Mon Sep 17 00:00:00 2001 From: svenvg93 Date: Mon, 12 Aug 2024 18:21:36 +0200 Subject: [PATCH 05/11] push_local_git --- .../Widgets/RecentDownloadChartWidget.php | 10 +++--- .../RecentDownloadLatencyChartWidget.php | 36 +++++++++---------- .../Widgets/RecentJitterChartWidget.php | 36 +++++++++---------- .../Widgets/RecentPingChartWidget.php | 10 +++--- .../Widgets/RecentUploadChartWidget.php | 10 +++--- .../RecentUploadLatencyChartWidget.php | 36 +++++++++---------- 6 files changed, 69 insertions(+), 69 deletions(-) diff --git a/app/Filament/Widgets/RecentDownloadChartWidget.php b/app/Filament/Widgets/RecentDownloadChartWidget.php index 32ae8acb8..36b72df74 100644 --- a/app/Filament/Widgets/RecentDownloadChartWidget.php +++ b/app/Filament/Widgets/RecentDownloadChartWidget.php @@ -56,13 +56,13 @@ protected function getData(): array [ 'label' => 'Download', 'data' => $downloads, - 'borderColor' => '#0ea5e9', - 'backgroundColor' => '#0ea5e9', - 'pointBackgroundColor' => '#0ea5e9', - 'fill' => false, + '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' => 0, + 'pointRadius' => count($downloads) <= 25 ? 3 : 0, ], [ 'label' => 'Average', diff --git a/app/Filament/Widgets/RecentDownloadLatencyChartWidget.php b/app/Filament/Widgets/RecentDownloadLatencyChartWidget.php index fc2bfc05a..80ce99ac0 100644 --- a/app/Filament/Widgets/RecentDownloadLatencyChartWidget.php +++ b/app/Filament/Widgets/RecentDownloadLatencyChartWidget.php @@ -51,36 +51,36 @@ protected function getData(): array 'datasets' => [ [ 'label' => 'Average (ms)', - '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, + '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, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'pointRadius' => 0, + 'pointRadius' => $averageData->count() <= 25 ? 3 : 0, ], [ 'label' => 'High (ms)', - '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, + '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, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'pointRadius' => 0, + 'pointRadius' => $highData->count() <= 25 ? 3 : 0, ], [ 'label' => 'Low (ms)', - '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, + '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, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'pointRadius' => 0, + 'pointRadius' => $lowData->count() <= 25 ? 3 : 0, ], ], 'labels' => $results->map(fn ($item) => $item->created_at->timezone(config('app.display_timezone'))->format(config('app.chart_datetime_format'))), diff --git a/app/Filament/Widgets/RecentJitterChartWidget.php b/app/Filament/Widgets/RecentJitterChartWidget.php index 0b4b2df3f..e18745bb5 100644 --- a/app/Filament/Widgets/RecentJitterChartWidget.php +++ b/app/Filament/Widgets/RecentJitterChartWidget.php @@ -51,36 +51,36 @@ protected function getData(): array 'datasets' => [ [ 'label' => 'Download (ms)', - 'data' => $results->map(fn ($item) => $item->download_jitter ? number_format($item->download_jitter, 2) : 0), - 'borderColor' => '#0ea5e9', - 'backgroundColor' => '#0ea5e9', - 'pointBackgroundColor' => '#0ea5e9', - 'fill' => false, + '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, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'pointRadius' => 0, + 'pointRadius' => $downloadData->count() <= 25 ? 3 : 0, ], [ 'label' => 'Upload (ms)', - 'data' => $results->map(fn ($item) => $item->upload_jitter ? number_format($item->upload_jitter, 2) : 0), - 'borderColor' => '#8b5cf6', - 'backgroundColor' => '#8b5cf6', - 'pointBackgroundColor' => '#8b5cf6', - 'fill' => false, + '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, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'pointRadius' => 0, + 'pointRadius' => $uploadData->count() <= 25 ? 3 : 0, ], [ 'label' => 'Ping (ms)', - 'data' => $results->map(fn ($item) => $item->ping_jitter ? number_format($item->ping_jitter, 2) : 0), - 'borderColor' => '#10b981', - 'backgroundColor' => '#10b981', - 'pointBackgroundColor' => '#10b981', - 'fill' => false, + '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, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'pointRadius' => 0, + 'pointRadius' => $pingData->count() <= 25 ? 3 : 0, ], ], 'labels' => $results->map(fn ($item) => $item->created_at->timezone(config('app.display_timezone'))->format(config('app.chart_datetime_format'))), diff --git a/app/Filament/Widgets/RecentPingChartWidget.php b/app/Filament/Widgets/RecentPingChartWidget.php index 8dccdd286..f8da5a180 100644 --- a/app/Filament/Widgets/RecentPingChartWidget.php +++ b/app/Filament/Widgets/RecentPingChartWidget.php @@ -55,13 +55,13 @@ protected function getData(): array [ 'label' => 'Ping (ms)', 'data' => $ping, - 'borderColor' => '#10b981', - 'backgroundColor' => '#10b981', - 'pointBackgroundColor' => '#10b981', - 'fill' => false, + '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' => 0, + 'pointRadius' => count($ping) <= 25 ? 3 : 0, ], [ 'label' => 'Average', diff --git a/app/Filament/Widgets/RecentUploadChartWidget.php b/app/Filament/Widgets/RecentUploadChartWidget.php index d3086353a..f96d1bb09 100644 --- a/app/Filament/Widgets/RecentUploadChartWidget.php +++ b/app/Filament/Widgets/RecentUploadChartWidget.php @@ -56,13 +56,13 @@ protected function getData(): array [ 'label' => 'Upload', 'data' => $upload, - 'borderColor' => '#8b5cf6', - 'backgroundColor' => '#8b5cf6', - 'pointBackgroundColor' => '#8b5cf6', - 'fill' => false, + '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' => 0, + 'pointRadius' => count($upload) <= 25 ? 3 : 0, ], [ 'label' => 'Average', diff --git a/app/Filament/Widgets/RecentUploadLatencyChartWidget.php b/app/Filament/Widgets/RecentUploadLatencyChartWidget.php index 94ccfd321..0a2f676ef 100644 --- a/app/Filament/Widgets/RecentUploadLatencyChartWidget.php +++ b/app/Filament/Widgets/RecentUploadLatencyChartWidget.php @@ -51,36 +51,36 @@ protected function getData(): array 'datasets' => [ [ 'label' => 'Average (ms)', - '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, + '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, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'pointRadius' => 0, + 'pointRadius' => $averageData->count() <= 25 ? 3 : 0, ], [ 'label' => 'High (ms)', - '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, + '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, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'pointRadius' => 0, + 'pointRadius' => $highData->count() <= 25 ? 3 : 0, ], [ 'label' => 'Low (ms)', - '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, + '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, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'pointRadius' => 0, + 'pointRadius' => $lowData->count() <= 25 ? 3 : 0, ], ], 'labels' => $results->map(fn ($item) => $item->created_at->timezone(config('app.display_timezone'))->format(config('app.chart_datetime_format'))), From 21221422a2ea931a726705dd6cdc5c0ddab1ea0e Mon Sep 17 00:00:00 2001 From: svenvg93 Date: Mon, 12 Aug 2024 22:29:16 +0200 Subject: [PATCH 06/11] add-timepicker --- app/Filament/Pages/Dashboard.php | 43 +++++++++++++++++-- .../Widgets/RecentDownloadChartWidget.php | 32 +++++--------- .../RecentDownloadLatencyChartWidget.php | 36 ++++++---------- .../Widgets/RecentJitterChartWidget.php | 36 ++++++---------- .../Widgets/RecentPingChartWidget.php | 32 +++++--------- .../Widgets/RecentUploadChartWidget.php | 32 +++++--------- .../RecentUploadLatencyChartWidget.php | 36 ++++++---------- config/speedtest.php | 4 ++ .../views/filament/pages/dashboard.blade.php | 3 -- 9 files changed, 115 insertions(+), 139 deletions(-) delete mode 100644 resources/views/filament/pages/dashboard.blade.php diff --git a/app/Filament/Pages/Dashboard.php b/app/Filament/Pages/Dashboard.php index 3737cf9bf..64fd6afdd 100644 --- a/app/Filament/Pages/Dashboard.php +++ b/app/Filament/Pages/Dashboard.php @@ -14,16 +14,20 @@ use Cron\CronExpression; use Filament\Actions\Action; use Filament\Actions\ActionGroup; +use Filament\Forms; +use Filament\Forms\Components\DatePicker; +use Filament\Forms\Form; use Filament\Notifications\Notification; -use Filament\Pages\Dashboard as BasePage; +use Filament\Pages\Dashboard as BaseDashboard; +use Filament\Pages\Dashboard\Concerns\HasFiltersForm; use Filament\Support\Enums\IconPosition; use Illuminate\Support\Arr; -class Dashboard extends BasePage +class Dashboard extends BaseDashboard { - protected static ?string $navigationIcon = 'heroicon-o-chart-bar'; + use HasFiltersForm; - protected static string $view = 'filament.pages.dashboard'; + protected static ?string $navigationIcon = 'heroicon-o-chart-bar'; public function getSubheading(): ?string { @@ -38,6 +42,31 @@ 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('speedtest.chart_time_range'); + + // Calculate the start and end dates based on the configuration value + $endDate = now(); // Today + $startDate = now()->subDays($defaultRangeDays); // Start date for the range + + return $form + ->schema([ + Forms\Components\Section::make() + ->schema([ + DatePicker::make('startDate') + ->default($startDate), + DatePicker::make('endDate') + ->default($endDate), + ]) + ->columns([ + 'default' => 1, + 'sm' => 2, + ]), + ]); + } + protected function getHeaderActions(): array { return [ @@ -83,6 +112,12 @@ 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 36b72df74..e6a9d7b23 100644 --- a/app/Filament/Widgets/RecentDownloadChartWidget.php +++ b/app/Filament/Widgets/RecentDownloadChartWidget.php @@ -6,45 +6,35 @@ 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 getFilters(): ?array - { - return [ - '24h' => 'Last 24h', - 'week' => 'Last week', - 'month' => 'Last month', - ]; - } - protected function getData(): array { + + $startDate = $this->filters['startDate'] ?? now()->subWeek(); + $endDate = $this->filters['endDate'] ?? now(); + $results = Result::query() ->select(['id', 'download', 'created_at']) ->where('status', '=', ResultStatus::Completed) - ->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()); - }) + ->when($startDate, fn (Builder $query) => $query->whereDate('created_at', '>=', $startDate)) + ->when($endDate, fn (Builder $query) => $query->whereDate('created_at', '<=', $endDate)) ->orderBy('created_at') ->get(); @@ -62,7 +52,7 @@ protected function getData(): array 'fill' => true, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'pointRadius' => count($downloads) <= 25 ? 3 : 0, + 'pointRadius' => count($downloads) <= 5 ? 3 : 0, ], [ 'label' => 'Average', diff --git a/app/Filament/Widgets/RecentDownloadLatencyChartWidget.php b/app/Filament/Widgets/RecentDownloadLatencyChartWidget.php index 80ce99ac0..4ac6f416d 100644 --- a/app/Filament/Widgets/RecentDownloadLatencyChartWidget.php +++ b/app/Filament/Widgets/RecentDownloadLatencyChartWidget.php @@ -5,45 +5,35 @@ 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 getFilters(): ?array - { - return [ - '24h' => 'Last 24h', - 'week' => 'Last week', - 'month' => 'Last month', - ]; - } - protected function getData(): array { + + $startDate = $this->filters['startDate'] ?? now()->subWeek(); + $endDate = $this->filters['endDate'] ?? now(); + $results = Result::query() ->select(['id', 'data', 'created_at']) ->where('status', '=', ResultStatus::Completed) - ->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()); - }) + ->when($startDate, fn (Builder $query) => $query->whereDate('created_at', '>=', $startDate)) + ->when($endDate, fn (Builder $query) => $query->whereDate('created_at', '<=', $endDate)) ->orderBy('created_at') ->get(); @@ -58,7 +48,7 @@ protected function getData(): array 'fill' => true, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'pointRadius' => $averageData->count() <= 25 ? 3 : 0, + 'pointRadius' => $averageData->count() <= 5 ? 3 : 0, ], [ 'label' => 'High (ms)', @@ -69,7 +59,7 @@ protected function getData(): array 'fill' => true, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'pointRadius' => $highData->count() <= 25 ? 3 : 0, + 'pointRadius' => $highData->count() <= 5 ? 3 : 0, ], [ 'label' => 'Low (ms)', @@ -80,7 +70,7 @@ protected function getData(): array 'fill' => true, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'pointRadius' => $lowData->count() <= 25 ? 3 : 0, + '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'))), diff --git a/app/Filament/Widgets/RecentJitterChartWidget.php b/app/Filament/Widgets/RecentJitterChartWidget.php index e18745bb5..e1dae4dea 100644 --- a/app/Filament/Widgets/RecentJitterChartWidget.php +++ b/app/Filament/Widgets/RecentJitterChartWidget.php @@ -5,45 +5,35 @@ 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 getFilters(): ?array - { - return [ - '24h' => 'Last 24h', - 'week' => 'Last week', - 'month' => 'Last month', - ]; - } - protected function getData(): array { + + $startDate = $this->filters['startDate'] ?? now()->subWeek(); + $endDate = $this->filters['endDate'] ?? now(); + $results = Result::query() ->select(['id', 'data', 'created_at']) ->where('status', '=', ResultStatus::Completed) - ->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()); - }) + ->when($startDate, fn (Builder $query) => $query->whereDate('created_at', '>=', $startDate)) + ->when($endDate, fn (Builder $query) => $query->whereDate('created_at', '<=', $endDate)) ->orderBy('created_at') ->get(); @@ -58,7 +48,7 @@ protected function getData(): array 'fill' => true, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'pointRadius' => $downloadData->count() <= 25 ? 3 : 0, + 'pointRadius' => $downloadData->count() <= 5 ? 3 : 0, ], [ 'label' => 'Upload (ms)', @@ -69,7 +59,7 @@ protected function getData(): array 'fill' => true, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'pointRadius' => $uploadData->count() <= 25 ? 3 : 0, + 'pointRadius' => $uploadData->count() <= 5 ? 3 : 0, ], [ 'label' => 'Ping (ms)', @@ -80,7 +70,7 @@ protected function getData(): array 'fill' => true, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'pointRadius' => $pingData->count() <= 25 ? 3 : 0, + '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'))), diff --git a/app/Filament/Widgets/RecentPingChartWidget.php b/app/Filament/Widgets/RecentPingChartWidget.php index f8da5a180..7c142f59a 100644 --- a/app/Filament/Widgets/RecentPingChartWidget.php +++ b/app/Filament/Widgets/RecentPingChartWidget.php @@ -5,45 +5,35 @@ 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 getFilters(): ?array - { - return [ - '24h' => 'Last 24h', - 'week' => 'Last week', - 'month' => 'Last month', - ]; - } - protected function getData(): array { + + $startDate = $this->filters['startDate'] ?? now()->subWeek(); + $endDate = $this->filters['endDate'] ?? now(); + $results = Result::query() ->select(['id', 'ping', 'created_at']) ->where('status', '=', ResultStatus::Completed) - ->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()); - }) + ->when($startDate, fn (Builder $query) => $query->whereDate('created_at', '>=', $startDate)) + ->when($endDate, fn (Builder $query) => $query->whereDate('created_at', '<=', $endDate)) ->orderBy('created_at') ->get(); @@ -61,7 +51,7 @@ protected function getData(): array 'fill' => true, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'pointRadius' => count($ping) <= 25 ? 3 : 0, + 'pointRadius' => count($ping) <= 5 ? 3 : 0, ], [ 'label' => 'Average', diff --git a/app/Filament/Widgets/RecentUploadChartWidget.php b/app/Filament/Widgets/RecentUploadChartWidget.php index f96d1bb09..ef24b76e1 100644 --- a/app/Filament/Widgets/RecentUploadChartWidget.php +++ b/app/Filament/Widgets/RecentUploadChartWidget.php @@ -6,45 +6,35 @@ 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 getFilters(): ?array - { - return [ - '24h' => 'Last 24h', - 'week' => 'Last week', - 'month' => 'Last month', - ]; - } - protected function getData(): array { + + $startDate = $this->filters['startDate'] ?? now()->subWeek(); + $endDate = $this->filters['endDate'] ?? now(); + $results = Result::query() ->select(['id', 'upload', 'created_at']) ->where('status', '=', ResultStatus::Completed) - ->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()); - }) + ->when($startDate, fn (Builder $query) => $query->whereDate('created_at', '>=', $startDate)) + ->when($endDate, fn (Builder $query) => $query->whereDate('created_at', '<=', $endDate)) ->orderBy('created_at') ->get(); @@ -62,7 +52,7 @@ protected function getData(): array 'fill' => true, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'pointRadius' => count($upload) <= 25 ? 3 : 0, + 'pointRadius' => count($upload) <= 5 ? 3 : 0, ], [ 'label' => 'Average', diff --git a/app/Filament/Widgets/RecentUploadLatencyChartWidget.php b/app/Filament/Widgets/RecentUploadLatencyChartWidget.php index 0a2f676ef..5dcc830e9 100644 --- a/app/Filament/Widgets/RecentUploadLatencyChartWidget.php +++ b/app/Filament/Widgets/RecentUploadLatencyChartWidget.php @@ -5,45 +5,35 @@ 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 getFilters(): ?array - { - return [ - '24h' => 'Last 24h', - 'week' => 'Last week', - 'month' => 'Last month', - ]; - } - protected function getData(): array { + + $startDate = $this->filters['startDate'] ?? now()->subWeek(); + $endDate = $this->filters['endDate'] ?? now(); + $results = Result::query() ->select(['id', 'data', 'created_at']) ->where('status', '=', ResultStatus::Completed) - ->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()); - }) + ->when($startDate, fn (Builder $query) => $query->whereDate('created_at', '>=', $startDate)) + ->when($endDate, fn (Builder $query) => $query->whereDate('created_at', '<=', $endDate)) ->orderBy('created_at') ->get(); @@ -58,7 +48,7 @@ protected function getData(): array 'fill' => true, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'pointRadius' => $averageData->count() <= 25 ? 3 : 0, + 'pointRadius' => $averageData->count() <= 5 ? 3 : 0, ], [ 'label' => 'High (ms)', @@ -69,7 +59,7 @@ protected function getData(): array 'fill' => true, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'pointRadius' => $highData->count() <= 25 ? 3 : 0, + 'pointRadius' => $highData->count() <= 5 ? 3 : 0, ], [ 'label' => 'Low (ms)', @@ -80,7 +70,7 @@ protected function getData(): array 'fill' => true, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, - 'pointRadius' => $lowData->count() <= 25 ? 3 : 0, + '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'))), diff --git a/config/speedtest.php b/config/speedtest.php index 3d532b5c8..0f88af691 100644 --- a/config/speedtest.php +++ b/config/speedtest.php @@ -17,6 +17,8 @@ 'public_dashboard' => env('PUBLIC_DASHBOARD', false), + 'chart_time_range' => env('CHART_TIME_RANGE', 7), + /** * Polling settings. */ @@ -35,4 +37,6 @@ 'servers' => env('SPEEDTEST_SERVERS', ''), + + ]; diff --git a/resources/views/filament/pages/dashboard.blade.php b/resources/views/filament/pages/dashboard.blade.php deleted file mode 100644 index f351a1c97..000000000 --- a/resources/views/filament/pages/dashboard.blade.php +++ /dev/null @@ -1,3 +0,0 @@ - - {{-- Silence is golden --}} - From 05b0091ee8c52bdf35123844c05306bbc650fbc8 Mon Sep 17 00:00:00 2001 From: svenvg93 Date: Tue, 13 Aug 2024 17:23:16 +0200 Subject: [PATCH 07/11] add-some-predefined-ranges --- app/Filament/Pages/Dashboard.php | 74 +++++++++++++++++++++++++++++--- config/app.php | 2 + config/speedtest.php | 2 - 3 files changed, 70 insertions(+), 8 deletions(-) diff --git a/app/Filament/Pages/Dashboard.php b/app/Filament/Pages/Dashboard.php index 64fd6afdd..1102b2896 100644 --- a/app/Filament/Pages/Dashboard.php +++ b/app/Filament/Pages/Dashboard.php @@ -16,6 +16,7 @@ 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; @@ -45,24 +46,85 @@ public function getSubheading(): ?string public function filtersForm(Form $form): Form { // Retrieve the default number of days from the configuration - $defaultRangeDays = config('speedtest.chart_time_range'); + $defaultRangeDays = config('app.chart_time_range'); // Calculate the start and end dates based on the configuration value - $endDate = now(); // Today - $startDate = now()->subDays($defaultRangeDays); // Start date for the range + $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([ + '3_hours' => 'Last 3 Hours', + '6_hours' => 'Last 6 Hours', + '12_hours' => 'Last 12 Hours', + '24_hours' => 'Last 24 Hours', + '1_week' => 'Last 1 Week', + '1_month' => 'Last 1 Month', + '3_months' => 'Last 3 Months', + '6_months' => 'Last 6 Months', + 'custom' => 'Custom Range', + ]) + ->reactive() + ->afterStateUpdated(function ($state, callable $set) { + switch ($state) { + case '3_hours': + $set('startDate', now()->subHours(3)->toDateString()); + $set('endDate', now()->toDateString()); + break; + case '6_hours': + $set('startDate', now()->subHours(6)->toDateString()); + $set('endDate', now()->toDateString()); + break; + case '12_hours': + $set('startDate', now()->subHours(12)->toDateString()); + $set('endDate', now()->toDateString()); + break; + 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 '3_months': + $set('startDate', now()->subMonths(3)->toDateString()); + $set('endDate', now()->toDateString()); + break; + case '6_months': + $set('startDate', now()->subMonths(6)->toDateString()); + $set('endDate', now()->toDateString()); + break; + case 'custom': + break; + } + }) + ->default('custom'), + DatePicker::make('startDate') - ->default($startDate), + ->label('Start Date') + ->default($defaultStartDate->toDateString()) + ->reactive() + ->hidden(fn ($get) => $get('predefinedRange') !== 'custom'), + DatePicker::make('endDate') - ->default($endDate), + ->label('End Date') + ->default($defaultEndDate->toDateString()) + ->reactive() + ->hidden(fn ($get) => $get('predefinedRange') !== 'custom'), ]) ->columns([ 'default' => 1, - 'sm' => 2, + 'sm' => 3, ]), ]); } diff --git a/config/app.php b/config/app.php index 60b38f700..2f757b84e 100644 --- a/config/app.php +++ b/config/app.php @@ -14,4 +14,6 @@ 'force_https' => env('FORCE_HTTPS', false), + 'chart_time_range' => env('CHART_TIME_RANGE', 7), + ]; diff --git a/config/speedtest.php b/config/speedtest.php index b43c785b5..6e3654552 100644 --- a/config/speedtest.php +++ b/config/speedtest.php @@ -17,8 +17,6 @@ 'public_dashboard' => env('PUBLIC_DASHBOARD', false), - 'chart_time_range' => env('CHART_TIME_RANGE', 7), - /** * Polling settings. */ From afcf6b3888b0ff239996701e896ed0a8c9340347 Mon Sep 17 00:00:00 2001 From: svenvg93 Date: Tue, 13 Aug 2024 17:30:42 +0200 Subject: [PATCH 08/11] remove whiteline --- config/speedtest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/config/speedtest.php b/config/speedtest.php index 6e3654552..8a55110c7 100644 --- a/config/speedtest.php +++ b/config/speedtest.php @@ -35,6 +35,4 @@ 'servers' => env('SPEEDTEST_SERVERS', ''), - - ]; From 756b324085fa32272e65393ca6aef746c274743c Mon Sep 17 00:00:00 2001 From: svenvg93 Date: Thu, 15 Aug 2024 15:56:54 +0200 Subject: [PATCH 09/11] Add_env_for_chart_start --- app/Filament/Widgets/RecentDownloadChartWidget.php | 4 ++-- app/Filament/Widgets/RecentDownloadLatencyChartWidget.php | 2 +- app/Filament/Widgets/RecentJitterChartWidget.php | 2 +- app/Filament/Widgets/RecentPingChartWidget.php | 2 +- app/Filament/Widgets/RecentUploadChartWidget.php | 2 +- app/Filament/Widgets/RecentUploadLatencyChartWidget.php | 2 +- config/app.php | 2 ++ 7 files changed, 9 insertions(+), 7 deletions(-) diff --git a/app/Filament/Widgets/RecentDownloadChartWidget.php b/app/Filament/Widgets/RecentDownloadChartWidget.php index e6a9d7b23..e50ada7ff 100644 --- a/app/Filament/Widgets/RecentDownloadChartWidget.php +++ b/app/Filament/Widgets/RecentDownloadChartWidget.php @@ -49,7 +49,7 @@ protected function getData(): array 'borderColor' => 'rgba(14, 165, 233)', 'backgroundColor' => 'rgba(14, 165, 233, 0.1)', // 10% opacity 'pointBackgroundColor' => 'rgba(14, 165, 233)', - 'fill' => true, + 'fill' => config('app.chart_fill_background'), 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, 'pointRadius' => count($downloads) <= 5 ? 3 : 0, @@ -80,7 +80,7 @@ protected function getOptions(): array ], 'scales' => [ 'y' => [ - 'beginAtZero' => false, + 'beginAtZero' => config('app.chart_begin_at_zero'), ], ], ]; diff --git a/app/Filament/Widgets/RecentDownloadLatencyChartWidget.php b/app/Filament/Widgets/RecentDownloadLatencyChartWidget.php index 4ac6f416d..da69c1199 100644 --- a/app/Filament/Widgets/RecentDownloadLatencyChartWidget.php +++ b/app/Filament/Widgets/RecentDownloadLatencyChartWidget.php @@ -82,7 +82,7 @@ protected function getOptions(): array return [ 'scales' => [ 'y' => [ - 'beginAtZero' => false, + 'beginAtZero' => config('app.chart_begin_at_zero'), ], ], ]; diff --git a/app/Filament/Widgets/RecentJitterChartWidget.php b/app/Filament/Widgets/RecentJitterChartWidget.php index e1dae4dea..70ccd03c4 100644 --- a/app/Filament/Widgets/RecentJitterChartWidget.php +++ b/app/Filament/Widgets/RecentJitterChartWidget.php @@ -82,7 +82,7 @@ protected function getOptions(): array return [ 'scales' => [ 'y' => [ - 'beginAtZero' => false, + 'beginAtZero' => config('app.chart_begin_at_zero'), ], ], ]; diff --git a/app/Filament/Widgets/RecentPingChartWidget.php b/app/Filament/Widgets/RecentPingChartWidget.php index 7c142f59a..5233c3abc 100644 --- a/app/Filament/Widgets/RecentPingChartWidget.php +++ b/app/Filament/Widgets/RecentPingChartWidget.php @@ -79,7 +79,7 @@ protected function getOptions(): array ], 'scales' => [ 'y' => [ - 'beginAtZero' => false, + 'beginAtZero' => config('app.chart_begin_at_zero'), ], ], ]; diff --git a/app/Filament/Widgets/RecentUploadChartWidget.php b/app/Filament/Widgets/RecentUploadChartWidget.php index ef24b76e1..3bf0d5ba0 100644 --- a/app/Filament/Widgets/RecentUploadChartWidget.php +++ b/app/Filament/Widgets/RecentUploadChartWidget.php @@ -80,7 +80,7 @@ protected function getOptions(): array ], 'scales' => [ 'y' => [ - 'beginAtZero' => false, + 'beginAtZero' => config('app.chart_begin_at_zero'), ], ], ]; diff --git a/app/Filament/Widgets/RecentUploadLatencyChartWidget.php b/app/Filament/Widgets/RecentUploadLatencyChartWidget.php index 5dcc830e9..9c08663b4 100644 --- a/app/Filament/Widgets/RecentUploadLatencyChartWidget.php +++ b/app/Filament/Widgets/RecentUploadLatencyChartWidget.php @@ -82,7 +82,7 @@ protected function getOptions(): array return [ 'scales' => [ 'y' => [ - 'beginAtZero' => false, + 'beginAtZero' => config('app.chart_begin_at_zero'), ], ], ]; diff --git a/config/app.php b/config/app.php index 2f757b84e..025fa8f90 100644 --- a/config/app.php +++ b/config/app.php @@ -16,4 +16,6 @@ 'chart_time_range' => env('CHART_TIME_RANGE', 7), + 'chart_begin_at_zero' => env('CHART_BEGIN_AT_ZERO', true), + ]; From 1c4a92c6fd64f218988eae0e0efac97d125d6661 Mon Sep 17 00:00:00 2001 From: svenvg93 Date: Thu, 15 Aug 2024 16:11:58 +0200 Subject: [PATCH 10/11] change-env-and_time-ranges --- app/Filament/Pages/Dashboard.php | 27 +------------------ .../Widgets/RecentDownloadChartWidget.php | 2 +- config/app.php | 2 +- 3 files changed, 3 insertions(+), 28 deletions(-) diff --git a/app/Filament/Pages/Dashboard.php b/app/Filament/Pages/Dashboard.php index 1102b2896..da99d0a43 100644 --- a/app/Filament/Pages/Dashboard.php +++ b/app/Filament/Pages/Dashboard.php @@ -46,7 +46,7 @@ public function getSubheading(): ?string public function filtersForm(Form $form): Form { // Retrieve the default number of days from the configuration - $defaultRangeDays = config('app.chart_time_range'); + $defaultRangeDays = config('app.chart_default_date_range'); // Calculate the start and end dates based on the configuration value $defaultEndDate = now(); // Today @@ -59,31 +59,14 @@ public function filtersForm(Form $form): Form Select::make('predefinedRange') ->label('Time Range') ->options([ - '3_hours' => 'Last 3 Hours', - '6_hours' => 'Last 6 Hours', - '12_hours' => 'Last 12 Hours', '24_hours' => 'Last 24 Hours', '1_week' => 'Last 1 Week', '1_month' => 'Last 1 Month', - '3_months' => 'Last 3 Months', - '6_months' => 'Last 6 Months', 'custom' => 'Custom Range', ]) ->reactive() ->afterStateUpdated(function ($state, callable $set) { switch ($state) { - case '3_hours': - $set('startDate', now()->subHours(3)->toDateString()); - $set('endDate', now()->toDateString()); - break; - case '6_hours': - $set('startDate', now()->subHours(6)->toDateString()); - $set('endDate', now()->toDateString()); - break; - case '12_hours': - $set('startDate', now()->subHours(12)->toDateString()); - $set('endDate', now()->toDateString()); - break; case '24_hours': $set('startDate', now()->subDay()->toDateString()); $set('endDate', now()->toDateString()); @@ -96,14 +79,6 @@ public function filtersForm(Form $form): Form $set('startDate', now()->subMonth()->toDateString()); $set('endDate', now()->toDateString()); break; - case '3_months': - $set('startDate', now()->subMonths(3)->toDateString()); - $set('endDate', now()->toDateString()); - break; - case '6_months': - $set('startDate', now()->subMonths(6)->toDateString()); - $set('endDate', now()->toDateString()); - break; case 'custom': break; } diff --git a/app/Filament/Widgets/RecentDownloadChartWidget.php b/app/Filament/Widgets/RecentDownloadChartWidget.php index e50ada7ff..14ed3356b 100644 --- a/app/Filament/Widgets/RecentDownloadChartWidget.php +++ b/app/Filament/Widgets/RecentDownloadChartWidget.php @@ -49,7 +49,7 @@ protected function getData(): array 'borderColor' => 'rgba(14, 165, 233)', 'backgroundColor' => 'rgba(14, 165, 233, 0.1)', // 10% opacity 'pointBackgroundColor' => 'rgba(14, 165, 233)', - 'fill' => config('app.chart_fill_background'), + 'fill' => true, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, 'pointRadius' => count($downloads) <= 5 ? 3 : 0, diff --git a/config/app.php b/config/app.php index 025fa8f90..1d98019cf 100644 --- a/config/app.php +++ b/config/app.php @@ -14,7 +14,7 @@ 'force_https' => env('FORCE_HTTPS', false), - 'chart_time_range' => env('CHART_TIME_RANGE', 7), + 'chart_default_date_range' => env('CHART_DEFAULT_DATE_RANGE', 7), 'chart_begin_at_zero' => env('CHART_BEGIN_AT_ZERO', true), From 249340e3198254feafe8bba242790d1f6c6c197b Mon Sep 17 00:00:00 2001 From: svenvg93 Date: Wed, 21 Aug 2024 20:28:21 +0200 Subject: [PATCH 11/11] change_average_to_orange --- app/Filament/Widgets/RecentDownloadChartWidget.php | 4 ++-- app/Filament/Widgets/RecentPingChartWidget.php | 4 ++-- app/Filament/Widgets/RecentUploadChartWidget.php | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/Filament/Widgets/RecentDownloadChartWidget.php b/app/Filament/Widgets/RecentDownloadChartWidget.php index 14ed3356b..1a34fecb1 100644 --- a/app/Filament/Widgets/RecentDownloadChartWidget.php +++ b/app/Filament/Widgets/RecentDownloadChartWidget.php @@ -57,8 +57,8 @@ protected function getData(): array [ 'label' => 'Average', 'data' => array_fill(0, count($downloads), $averageDownload), - 'borderColor' => '#ff0000', - 'pointBackgroundColor' => '#ff0000', + 'borderColor' => 'rgb(255, 165, 0)', + 'pointBackgroundColor' => 'rgb(255, 165, 0)', 'fill' => false, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, diff --git a/app/Filament/Widgets/RecentPingChartWidget.php b/app/Filament/Widgets/RecentPingChartWidget.php index 5233c3abc..1135c8062 100644 --- a/app/Filament/Widgets/RecentPingChartWidget.php +++ b/app/Filament/Widgets/RecentPingChartWidget.php @@ -56,8 +56,8 @@ protected function getData(): array [ 'label' => 'Average', 'data' => array_fill(0, count($ping), $averagePing), - 'borderColor' => '#ff0000', - 'pointBackgroundColor' => '#ff0000', + 'borderColor' => 'rgb(255, 165, 0)', + 'pointBackgroundColor' => 'rgb(255, 165, 0)', 'fill' => false, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4, diff --git a/app/Filament/Widgets/RecentUploadChartWidget.php b/app/Filament/Widgets/RecentUploadChartWidget.php index 3bf0d5ba0..7cf40e16c 100644 --- a/app/Filament/Widgets/RecentUploadChartWidget.php +++ b/app/Filament/Widgets/RecentUploadChartWidget.php @@ -57,8 +57,8 @@ protected function getData(): array [ 'label' => 'Average', 'data' => array_fill(0, count($upload), $averageUpload), - 'borderColor' => '#ff0000', - 'pointBackgroundColor' => '#ff0000', + 'borderColor' => 'rgb(255, 165, 0)', + 'pointBackgroundColor' => 'rgb(255, 165, 0)', 'fill' => false, 'cubicInterpolationMode' => 'monotone', 'tension' => 0.4,