From 8222beee9af86fa70b3abc5fb78b2145832fb449 Mon Sep 17 00:00:00 2001 From: xenov Date: Wed, 24 Sep 2025 15:34:08 +0100 Subject: [PATCH] PacketLoss --- app/Filament/Pages/Dashboard.php | 3 +- .../Widgets/RecentPacketLossChartWidget.php | 106 ++++++++++++++++++ app/Jobs/Ookla/RunSpeedtestJob.php | 1 + ...9_23_192342_add_packet_loss_to_results.php | 28 +++++ resources/views/dashboard.blade.php | 4 + 5 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 app/Filament/Widgets/RecentPacketLossChartWidget.php create mode 100644 database/migrations/2025_09_23_192342_add_packet_loss_to_results.php diff --git a/app/Filament/Pages/Dashboard.php b/app/Filament/Pages/Dashboard.php index d1387e324..0c26456d5 100644 --- a/app/Filament/Pages/Dashboard.php +++ b/app/Filament/Pages/Dashboard.php @@ -1,7 +1,7 @@ 'Last 24h', + 'week' => 'Last week', + 'month' => 'Last month', + ]; + } + + protected function getData(): array + { + $results = Result::query() + ->select(['id', 'packetLoss', '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()); + }) + ->orderBy('created_at') + ->get(); + + return [ + 'datasets' => [ + [ + 'label' => 'packetLoss', + 'data' => $results->map(fn ($item) => is_numeric($item->getRawOriginal('packetLoss')) ? round($item->getRawOriginal('packetLoss'), 2) : null)->values()->all(), + 'backgroundColor' => 'rgba(14, 165, 233, 0.1)', + 'pointBackgroundColor' => 'rgba(14, 165, 233)', + 'fill' => true, + 'cubicInterpolationMode' => 'monotone', + 'tension' => 0.4, + 'pointRadius' => count($results) <= 24 ? 3 : 0, + ]//, + // [ + // 'label' => 'Average', + // 'data' => array_fill(0, count($results), Average::averageDownload($results)), + // 'borderColor' => 'rgb(243, 7, 6, 1)', + // 'pointBackgroundColor' => 'rgb(243, 7, 6, 1)', + // 'fill' => false, + // 'cubicInterpolationMode' => 'monotone', + // 'tension' => 0.4, + // 'pointRadius' => 0, + // ], + ], + // labels as plain array as well + 'labels' => $results->map(fn ($item) => $item->created_at->timezone(config('app.display_timezone'))->format(config('app.chart_datetime_format'))), + ]; + + } + + protected function getOptions(): array + { + return [ + 'plugins' => [ + 'legend' => [ + 'display' => true, + + ], + 'tooltip' => [ + 'enabled' => true, + 'mode' => 'index', + 'intersect' => false, + 'position' => 'nearest', + ], + ], + 'scales' => [ + 'y' => [ + 'beginAtZero' => config('app.chart_begin_at_zero'), + 'grace' => 2, + ], + ], + ]; + } + + protected function getType(): string + { + return 'line'; + } +} diff --git a/app/Jobs/Ookla/RunSpeedtestJob.php b/app/Jobs/Ookla/RunSpeedtestJob.php index 486faab6f..f70aa8562 100644 --- a/app/Jobs/Ookla/RunSpeedtestJob.php +++ b/app/Jobs/Ookla/RunSpeedtestJob.php @@ -92,6 +92,7 @@ public function handle(): void 'upload' => Arr::get($output, 'upload.bandwidth'), 'download_bytes' => Arr::get($output, 'download.bytes'), 'upload_bytes' => Arr::get($output, 'upload.bytes'), + 'packetLoss' => Arr::get($output, 'packetLoss'), 'data' => $output, ]); } diff --git a/database/migrations/2025_09_23_192342_add_packet_loss_to_results.php b/database/migrations/2025_09_23_192342_add_packet_loss_to_results.php new file mode 100644 index 000000000..0eeb48644 --- /dev/null +++ b/database/migrations/2025_09_23_192342_add_packet_loss_to_results.php @@ -0,0 +1,28 @@ +float('packetLoss')->nullable(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('results', function (Blueprint $table) { + $table->dropColumn('packetLoss'); + }); + } +}; diff --git a/resources/views/dashboard.blade.php b/resources/views/dashboard.blade.php index 8a03ce90c..dafd00fcd 100644 --- a/resources/views/dashboard.blade.php +++ b/resources/views/dashboard.blade.php @@ -10,6 +10,7 @@ @endisset +
@livewire(\App\Filament\Widgets\RecentDownloadChartWidget::class)
@@ -33,6 +34,9 @@
@livewire(\App\Filament\Widgets\RecentUploadLatencyChartWidget::class)
+
+ @livewire(\App\Filament\Widgets\RecentPacketLossChartWidget::class) +